diff --git a/PyGame/practice/gameprac/hybrid.py b/PyGame/practice/gameprac/hybrid.py index 8714795..6840030 100644 --- a/PyGame/practice/gameprac/hybrid.py +++ b/PyGame/practice/gameprac/hybrid.py @@ -26,20 +26,43 @@ class Spaceship(pygame.sprite.Sprite): self.rect.center = [x,y] self.health_start = health self.health_remaining = health + self.last_shot = pygame.time.get_ticks() def update(self): speed = 8 + cooldown = 500 # millisconds 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 + + time_now = pygame.time.get_ticks() + + # shoot + if key[pygame.K_w] and time_now - self.last_shot > cooldown: + bullet = Bullets(self.rect.centerx, self.rect.top) + bullet_group.add(bullet) + self.last_shot = time_now 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) ) + +class Bullets(pygame.sprite.Sprite): + def __init__(self,x ,y): + pygame.sprite.Sprite.__init__(self) # review init function + self.image = pygame.image.load("img/bullet.png") + self.rect = self.image.get_rect() + self.rect.center = [x, y] + def update(self): + self.rect.y -= 5 + + + # create sprite groups spaceship_group = pygame.sprite.Group() +bullet_group = pygame.sprite.Group() # create player spaceship = Spaceship(int(WIDTH / 2), HEIGHT - 100, 3) @@ -63,8 +86,7 @@ clock = pygame.time.Clock() running = True while running: clock.tick(FPS) - - #draw background + draw_bg() # event handlers @@ -74,9 +96,11 @@ while running: running = False spaceship.update() + + bullet_group.update() # draw sprite groups on screen spaceship_group.draw(screen) - + bullet_group.draw(screen) pygame.display.update() # Go over it again FLIP vs UPDATE Which to use When diff --git a/PyGame/practice/gameprac/img/bullet.png b/PyGame/practice/gameprac/img/bullet.png new file mode 100644 index 0000000..af0101e Binary files /dev/null and b/PyGame/practice/gameprac/img/bullet.png differ