Original von Caitlin
This commit is contained in:
parent
e52f0d716b
commit
260eaa8202
BIN
ananas.png
Normal file
BIN
ananas.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 122 KiB |
125
python2dspiel.py
Normal file
125
python2dspiel.py
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
import pygame
|
||||||
|
from pygame.locals import *
|
||||||
|
from random import randint
|
||||||
|
|
||||||
|
|
||||||
|
LARGEUR = 700
|
||||||
|
HAUTEUR = 700
|
||||||
|
fenetre = pygame.display.set_mode((LARGEUR, HAUTEUR))
|
||||||
|
fond = pygame.sprite.Sprite()
|
||||||
|
pygame.sprite.Sprite.__init__(fond)
|
||||||
|
fond.image = pygame.image.load("terre.jpg").convert()
|
||||||
|
fond.rect = fond.image.get_rect()
|
||||||
|
fond.rect.x = 0
|
||||||
|
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
|
||||||
|
|
||||||
|
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, 30)
|
||||||
|
texte = pygame.sprite.Sprite()
|
||||||
|
pygame.sprite.Sprite.__init__(texte)
|
||||||
|
texte.image = police.render("ALERTE! La planète terre est attaquée par une flotte d’avions spatiaux!", 1, (10, 10, 10),(255, 90, 20))
|
||||||
|
texte.rect = texte.image.get_rect()
|
||||||
|
texte.rect.centerx = fenetre.get_rect().centerx
|
||||||
|
texte.rect.centery = fenetre.get_rect().centery
|
||||||
|
liste_des_sprites.add(texte)
|
||||||
|
|
||||||
|
texte2.image = police.render("Vous vous trouver dans l’engin SU-PER12. Votre mission est d’éliminer le plus d’ennemis possible!", 1, (10, 10, 10),(255, 90, 20))
|
||||||
|
texte2.rect = texte2.image.get_rect()
|
||||||
|
texte2.rect.centerx = 50
|
||||||
|
texte2.rect.centery = 70
|
||||||
|
liste_des_sprites.add(texte2)
|
||||||
|
|
||||||
|
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 = 60
|
||||||
|
texte3.rect.centery = 80
|
||||||
|
liste_des_sprites.add(texte3)
|
||||||
|
|
||||||
|
texte4.image = police.render("Bonne chance!", 1, (10, 10, 10),(255, 90, 20))
|
||||||
|
texte4.rect = texte4.image.get_rect()
|
||||||
|
texte4.rect.centerx = 70
|
||||||
|
texte4.rect.centery = 90
|
||||||
|
liste_des_sprites.add(texte4)
|
||||||
|
|
||||||
|
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 = 80
|
||||||
|
texte5.rect.centery = 100
|
||||||
|
liste_des_sprites.add(texte5)
|
||||||
|
|
||||||
|
gameover = False
|
||||||
|
|
||||||
|
score = 0
|
||||||
|
|
||||||
|
while running:
|
||||||
|
liste_des_sprites.draw(fenetre)
|
||||||
|
liste_sprites_ananas.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]
|
||||||
|
keys = pygame.key.get_pressed()
|
||||||
|
if keys[pygame.K_SPACE]:
|
||||||
|
liste_des_sprites.remove(texte)
|
||||||
|
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 += 5
|
||||||
|
if ananas.rect.colliderect(avion):
|
||||||
|
print("COLLISION")
|
||||||
|
score += 1
|
||||||
|
liste_d_ananas.remove(ananas)
|
||||||
|
ananas.kill()
|
||||||
|
if ananas.rect.y > HAUTEUR:
|
||||||
|
gameover = True
|
||||||
|
police = pygame.font.Font(None, 36)
|
||||||
|
texte = pygame.sprite.Sprite()
|
||||||
|
pygame.sprite.Sprite.__init__(texte)
|
||||||
|
texte.image = police.render("Gameover. L'ennemi est entré dans notre atmosphère! Réessaye de tuer tout les ennemis! Score : "+ str(score), 1, (10, 10, 10),(255, 90, 20))
|
||||||
|
texte.rect = texte.image.get_rect()
|
||||||
|
texte.rect.centerx = fenetre.get_rect().centerx
|
||||||
|
texte.rect.centery = fenetre.get_rect().centery
|
||||||
|
liste_des_sprites.add(texte)
|
||||||
|
pygame.display.flip()
|
||||||
|
fenetre.fill((36,242,232))
|
||||||
|
clock.tick(60)
|
||||||
|
pygame.quit()
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user