main: bullets

This commit is contained in:
Stefan Liebl 2024-12-28 20:16:47 +01:00
parent 26a339710f
commit 85ad7d08ef

33
main.py
View File

@ -9,6 +9,20 @@ fond.image = pygame.image.load("terre.jpg").convert()
fond.rect = fond.image.get_rect()
fond.rect.x = 55
fond.rect.y = 0
# Bullets
def creer_bullet(pos_x, pos_y):
bullet = pygame.sprite.Sprite()
pygame.sprite.Sprite.__init__(bullet)
bullet.image = pygame.image.load("avion.png").convert_alpha()
bullet.image = pygame.transform.scale(bullet.image,[20, 20])
bullet.rect = bullet.image.get_rect()
bullet.rect.x = pos_x
bullet.rect.y = pos_y
return bullet
liste_d_bullets = []
liste_sprites_bullets = pygame.sprite.LayeredUpdates()
def creer_ananas(pos_x, pos_y):
ananas = pygame.sprite.Sprite()
pygame.sprite.Sprite.__init__(ananas)
@ -79,13 +93,16 @@ score = 0
while running:
liste_des_sprites.draw(fenetre)
liste_sprites_ananas.draw(fenetre)
liste_sprites_bullets.draw(fenetre)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == MOUSEMOTION:
avion.rect.x = event.pos[0]
if event.type == MOUSEBUTTONDOWN:
print("Peng")
nouvel_bullet = creer_bullet(avion.rect.centerx, avion.rect.y)
liste_d_bullets.append(nouvel_bullet)
liste_sprites_bullets.add(nouvel_bullet)
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
liste_des_sprites.remove(texte)
@ -113,6 +130,20 @@ while running:
texte.rect.centerx = 360
texte.rect.centery = 450
liste_des_sprites.add(texte)
# Check for hitting bullets
for bullet in liste_d_bullets:
if ananas.rect.colliderect(bullet):
score += 1
liste_d_ananas.remove(ananas)
ananas.kill()
liste_d_bullets.remove(bullet)
bullet.kill()
# Move all bullets
for bullet in liste_d_bullets:
bullet.rect.y -= 10
if bullet.rect.y <= 0:
liste_d_bullets.remove(bullet)
bullet.kill()
pygame.display.flip()
fenetre.fill((0,0,0))
clock.tick(60)