-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceship.py
49 lines (37 loc) · 1.7 KB
/
spaceship.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import pygame
from pygame.sprite import Sprite
class SpaceShip(Sprite):
"""A class to handle the spaceship."""
def __init__(self, game_instance):
"""Initialize spaceship."""
super().__init__()
# Setup settings and main screen
self.screen = game_instance.screen
self.settings = game_instance.settings
self.screen_rectangle = game_instance.screen.get_rect() # Treats screen as rectangle.
# Spaceship setup.
self.image = pygame.image.load("images/rocket.bmp")
self.rect = self.image.get_rect() # Treats spaceship as rectangle.
# Spaceship positioned on screen.
self.rect.midbottom = self.screen_rectangle.midbottom
# Float value for ship's horizontal position
self.x = float(self.rect.x)
# Continuous movement flags
self.moving_left = False
self.moving_right = False
def blitme(self):
"""Draws image to screen at specified position."""
self.screen.blit(self.image, self.rect)
def move_continuously(self):
"""Updates spaceship position based on movement flag and stays within the main screen"""
# Updates decimal position x
if self.moving_right and self.rect.right < self.screen_rectangle.right:
self.x += self.settings.spaceship_speed
if self.moving_left and self.rect.left > self.screen_rectangle.left:
self.x -= self.settings.spaceship_speed
# Updates actual position relative to decimal position x
self.rect.x = self.x
def center_ship(self):
"""Centers the ship on the screen."""
self.rect.midbottom = self.screen_rectangle.midbottom
self.x = float(self.rect.x)