python2dspiel/main.py
2025-01-03 23:12:17 +01:00

182 lines
6.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pygame
from pygame.locals import *
from random import randint
LARGEUR = 900
HAUTEUR = 700
fenetre = pygame.display.set_mode((LARGEUR, HAUTEUR))
fond = pygame.sprite.Sprite()
fond.image = pygame.image.load("terre.jpg").convert()
fond.rect = fond.image.get_rect()
fond.rect.x = 55
fond.rect.y = 0
def creer_ananas(pos_x, pos_y):
ananas = pygame.sprite.Sprite()
pygame.sprite.Sprite.__init__(ananas)
ananas.image = pygame.image.load("ananas.png").convert_alpha()
ananas.image = pygame.transform.scale(ananas.image,[200, 100])
ananas.rect = ananas.image.get_rect()
ananas.rect.x = pos_x
ananas.rect.y = pos_y
return ananas
def tirer_missiles(pos_x, pos_y):
missiles = pygame.sprite.Sprite()
pygame.sprite.Sprite.__init__(missiles)
missiles.image = pygame.image.load("missiles.png").convert_alpha()
missiles.image = pygame.transform.scale(missiles.image,[10, 100])
missiles.rect = missiles.image.get_rect()
missiles.rect.x = pos_x
missiles.rect.y = pos_y
return missiles
missiles = pygame.sprite.Sprite()
liste_de_missiles = []
liste_sprites_missiles = pygame.sprite.Group()
pygame.sprite.LayeredUpdates()
pygame.init()
clock = pygame.time.Clock()
running = True
avion = pygame.sprite.Sprite()
pygame.sprite.Sprite.__init__(avion)
avion.image = pygame.image.load("avion.png").convert_alpha()
avion.image = pygame.transform.scale(avion.image,[120, 120])
avion.rect = avion.image.get_rect()
avion.rect.x = 200
avion.rect.y = 400
liste_des_sprites = pygame.sprite.LayeredUpdates()
liste_des_sprites.add(fond)
liste_sprites_ananas = pygame.sprite.LayeredUpdates()
liste_des_sprites.add(avion)
liste_d_ananas = []
police = pygame.font.Font(None, 25)
texte = pygame.sprite.Sprite()
pygame.sprite.Sprite.__init__(texte)
texte.image = police.render("ALERTE! La planète Terre est attaquée par une flotte davions spatiaux!", 1, (10, 10, 10),(255, 90, 20))
texte.rect = texte.image.get_rect()
texte.rect.centerx = 360
texte.rect.centery = 30
liste_des_sprites.add(texte)
police = pygame.font.Font(None, 20)
texte2 = pygame.sprite.Sprite()
texte2.image = police.render("Vous vous trouvez dans lengin SU-PER12. Votre mission est déliminer le plus dennemis possible!", 1, (10, 10, 10),(255, 90, 20))
texte2.rect = texte2.image.get_rect()
texte2.rect.centerx = 360
texte2.rect.centery = 60
liste_des_sprites.add(texte2)
police = pygame.font.Font(None, 20)
texte3 = pygame.sprite.Sprite()
texte3.image = police.render("Pour ce faire, appuyez sur « espace » pour tirer et bougez la souris pour manoeuvrer votre SU-PER12!", 1, (10, 10, 10),(255, 90, 20))
texte3.rect = texte3.image.get_rect()
texte3.rect.centerx = 360
texte3.rect.centery = 80
liste_des_sprites.add(texte3)
police = pygame.font.Font(None, 20)
texte4 = pygame.sprite.Sprite()
texte4.image = police.render("Bonne chance!", 1, (10, 10, 10),(255, 90, 20))
texte4.rect = texte4.image.get_rect()
texte4.rect.centerx = 360
texte4.rect.centery = 100
liste_des_sprites.add(texte4)
police = pygame.font.Font(None, 20)
texte5 = pygame.sprite.Sprite()
texte5.image = police.render("ALERTE, PLANETE TERRE EST SOUS ATTAQUE", 1, (10, 10, 10),(255, 90, 20))
texte5.rect = texte5.image.get_rect()
texte5.rect.centerx = 360
texte5.rect.centery = 1500
liste_des_sprites.add(texte5)
gameover = False
while running:
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
liste_des_sprites.remove(texte)
liste_des_sprites.remove(texte1)
liste_des_sprites.remove(texte2)
liste_des_sprites.remove(texte3)
liste_des_sprites.remove(texte4)
liste_des_sprites.remove(texte5)
liste_des_sprites.draw(fenetre)
liste_sprites_ananas.draw(fenetre)
liste_sprites_missiles.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("Vous avez tué l'ennemi! Continuez!")
nouveau_missile = tirer_missiles(avion.rect.centerx, HAUTEUR/2)
liste_de_missiles.append(nouveau_missile) == [nouveau_missile]
liste_sprites_missiles.add(nouveau_missile) == pygame.sprite.Group(nouveau_missile)
if gameover == False:
nombre_aleatoire = randint(0, 100)
if nombre_aleatoire == 100:
position_x_aleatoire = randint(0, LARGEUR - 60)
nouvel_ananas = creer_ananas(position_x_aleatoire, -100)
liste_d_ananas.append(nouvel_ananas)
liste_sprites_ananas.add(nouvel_ananas)
for ananas in liste_d_ananas:
ananas.rect.y += 2
if ananas.rect.colliderect(avion):
print("COLLISION, L'OBJET A ÉTÉ ÉLIMINÉ, MALHEUREUSEMENT, VOUS AUSSI")
liste_d_ananas.remove(ananas)
ananas.kill()
if ananas.rect.y > HAUTEUR:
gameover = True
police = pygame.font.Font(None, 36)
texte1 = pygame.sprite.Sprite()
pygame.sprite.Sprite.__init__(texte1)
texte1.image = police.render("Gameover.", 1, (10,10,10),(255, 90, 20))
texte1.rect = texte1.image.get_rect()
texte1.rect.centerx = 360
texte1.rect.centery = 450
liste_des_sprites.add(texte1)
police = pygame.font.Font(None, 36)
texte22 = pygame.sprite.Sprite()
pygame.sprite.Sprite.__init__(texte22)
texte22.image = police.render("L'ennemi est entré dans notre atmosphère!", 1, (10,10,10),(255, 90, 20))
texte22.rect = texte22.image.get_rect()
texte22.rect.centerx = 360
texte22.rect.centery = 500
liste_des_sprites.add(texte22)
police = pygame.font.Font(None, 36)
texte33 = pygame.sprite.Sprite()
pygame.sprite.Sprite.__init__(texte33)
texte33.image = police.render("Réessaye de tuer tout les ennemis!", 1, (10,10,10),(255, 90, 20))
texte33.rect = texte33.image.get_rect()
texte33.rect.centerx = 360
texte33.rect.centery = 450
liste_des_sprites.add(texte33)
for missiles in liste_de_missiles:
missiles.rect.y -= 1
if missiles.rect.y <= 0:
liste_de_missiles.remove(missiles)
missiles.kill()
else:
# Check for hitting missiles
for ananas in liste_d_ananas:
if ananas.rect.colliderect(missiles):
liste_d_ananas.remove(ananas)
ananas.kill()
liste_de_missiles.remove(missiles)
missiles.kill()
pygame.display.flip()
fenetre.fill((0,0,0))
clock.tick(60)
pygame.quit()