diff --git a/PyGame/practice/gameprac/hybrid.py b/PyGame/practice/gameprac/hybrid.py index 9490901..8714795 100644 --- a/PyGame/practice/gameprac/hybrid.py +++ b/PyGame/practice/gameprac/hybrid.py @@ -19,18 +19,30 @@ def draw_bg(): # create spaceship class class Spaceship(pygame.sprite.Sprite): - def __init__(self,x ,y): + def __init__(self,x ,y, health): pygame.sprite.Sprite.__init__(self) # review init function self.image = pygame.image.load("img/spaceship.png") self.rect = self.image.get_rect() self.rect.center = [x,y] + self.health_start = health + self.health_remaining = health + def update(self): + speed = 8 + key = pygame.key.get_pressed() + if key[pygame.K_a] and self.rect.left > 0: # Why zero not width tried width would not move + self.rect.x -= speed # why speed not pixal + or - like others?? + if key[pygame.K_d] and self.rect.right < WIDTH: + self.rect.x += speed + pygame.draw.rect(screen, RED, (self.rect.x, (self.rect.bottom +10), self.rect.width, 15)) + if self.health_remaining > 0: + pygame.draw.rect(screen, GREEN, (self.rect.x, (self.rect.bottom +10), int(self.rect.width * (self.health_remaining / self.health_start)), 15) ) # create sprite groups spaceship_group = pygame.sprite.Group() # create player -spaceship = Spaceship(int(WIDTH / 2), HEIGHT - 100) +spaceship = Spaceship(int(WIDTH / 2), HEIGHT - 100, 3) spaceship_group.add(spaceship) @@ -60,8 +72,8 @@ while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False - - + + spaceship.update() # draw sprite groups on screen spaceship_group.draw(screen)