added shooting
This commit is contained in:
parent
4c9446c09d
commit
cfd4c1bf86
|
@ -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)
|
||||
|
@ -64,7 +87,6 @@ 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
|
||||
|
||||
|
||||
|
|
BIN
PyGame/practice/gameprac/img/bullet.png
Normal file
BIN
PyGame/practice/gameprac/img/bullet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.6 KiB |
Loading…
Reference in New Issue
Block a user