1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| import pygame
from pygame.locals import * #Importation des bibliothèques
import random
import time
pygame.init()
Lfenetre = 1200
Hfenetre = 600
Lbird = 125
Hbird = 125
Ltuyaux = 70
Htuyaux = 500
fenetre = pygame.display.set_mode((Lfenetre, Hfenetre), RESIZABLE) #Création d'un écran d'une résolution de 1024*768 pixels
fond = pygame.image.load("fond.png").convert() #Mise en place du fond d'écran
fond = pygame.transform.scale(fond,(Lfenetre, Hfenetre)) #Redimensionnage de l'image
bird = pygame.image.load("flappy.png").convert() #Affichage de l'image de l'oiseau
bird = pygame.transform.scale(bird,(Lbird, Hbird)) #Redimensionnage de l'image
bird.set_colorkey((255,255,255)) #avec disparition du fond de cette image (blanc)
tuyau = pygame.image.load("tuyau.png").convert()
tuyau = pygame.transform.scale(tuyau,(Ltuyaux, Htuyaux))
tuyau.set_colorkey((255,255,255))
tuyau_inversé = pygame.image.load("tuyau(envers).png").convert()
tuyau_inversé = pygame.transform.scale(tuyau_inversé,(Ltuyaux, Htuyaux))
tuyau_inversé.set_colorkey((255,255,255))
horloge = pygame.time.Clock()
pygame.display.set_caption("Flappy Bird") #Nom de la fenêtre
musique = pygame.mixer.music.load("killer.mp3")
def message_début():
Police = pygame.font.Font("police.ttf", 70)
Rendu = Police.render("Appuyer sur Espace pour jouer !",1,(0,0,0))
fenetre.blit(Rendu, (Lfentre/3, Hfenetre/4))
def messageGO():
Police = pygame.font.Font("police.ttf", 100) #Choix de la police et de la taille du texte
Rendu = Police.render("Game Over",1,(0,0,0)) #Rendu du texte (nom du message, Code RGB)
fenetre.blit(Rendu, (Lfenetre/3, Hfenetre/4))
def message_rejouer():
Police = pygame.font.Font("police.ttf", 50)
Rendu = Police.render('Appuyer sur "r" pour rejouer',1,(0,0,0))
fenetre.blit(Rendu, (Lfenetre/3, Hfenetre/2+200))
def gameover():
pygame.mixer.music.stop()
messageGO()
message_rejouer()
pygame.display.flip()
while rejouer_ou_quitter() == None: #Si la fonction ne renvoie rien alors
horloge.tick() #l'écran reste figé sur le message du GameOver
pygame.event.clear() #Oubli des évènements passés pour éviter que la touche espace influe sur la fonction principale
principale()
def rejouer_ou_quitter():
for event in pygame.event.get([KEYDOWN, QUIT]): #Pour tous les évènements parmi [...]
if event.type == QUIT: #Si l'évènement est de type quitter alors on arrête le programme
pygame.quit()
quit()
elif event.key == K_r: #Si l'évènement est l'appui sur la touche "r" on renvoie un évènement clé
return event.key
return None #Sinon la fonction ne renvoie rien
def collisions(x_bird, y_bird, Lbird, Hbird, x_tuyau, x_tuyau2, y_tuyau, y_tuyau2, Ltuyaux, Htuyaux, espace):
if x_bird + Lbird > x_tuyau + 20 :
if (y_bird + Hbird - 40 > y_tuyau) or (y_bird + 40 < Htuyaux + y_tuyau - espace) :
if x_bird + 20 < x_tuyau + Ltuyaux :
gameover()
if x_bird + Lbird > x_tuyau2 + 20 :
if (y_bird + Hbird - 40 > y_tuyau2) or (y_bird + 40 < Htuyaux + y_tuyau2 - espace) :
if x_bird + 20 < x_tuyau2 + Ltuyaux :
gameover()
def score(compte):
Police = pygame.font.Font("police.ttf", 40)
Rendu = Police.render("Score : " + str(compte), 1, (0,0,0))
fenetre.blit (Rendu, (10, 0))
def principale():
x_bird = 100 #Position de l'oiseau
y_bird = 100
y_movebird = 0 #Mouvement de l'oiseau
x_tuyau = Lfenetre #Position du poteau
y_tuyau = random.randint(210, 470)
x_tuyau_inversé = x_tuyau #Position du poteau à l'envers
espace = 700 #espace entre la base du premier poteau et la base du poteau inversé
x_tuyau2 = Lfenetre * (3/2)
y_tuyau2 = random.randint(210, 470)
x_tuyau2_inversé = x_tuyau2
vitesse_tuyaux = 3
compte = 0
pygame.mixer.music.play()
continuer = 1
while continuer == 1:
for event in pygame.event.get():
if event.type == QUIT:
continuer = 0
if event.type == KEYDOWN and event.key == K_SPACE:
y_movebird = -1
if event.type == KEYUP and event.key == K_SPACE:
y_movebird = 1
y_bird += y_movebird
x_tuyau -= vitesse_tuyaux
x_tuyau_inversé -= vitesse_tuyaux
x_tuyau2 -= vitesse_tuyaux
x_tuyau2_inversé -= vitesse_tuyaux
fenetre.blit (fond, (0,0))
fenetre.blit (bird, (x_bird, y_bird))
fenetre.blit (tuyau, (x_tuyau, y_tuyau))
fenetre.blit (tuyau_inversé, (x_tuyau_inversé ,y_tuyau - espace))
fenetre.blit (tuyau, (x_tuyau2, y_tuyau2))
fenetre.blit (tuyau_inversé, (x_tuyau2_inversé, y_tuyau2 - espace))
if y_bird > 400:
gameover()
collisions(x_bird, y_bird, Lbird, Hbird, x_tuyau, x_tuyau2, y_tuyau, y_tuyau2, Ltuyaux, Htuyaux, espace)
if x_tuyau < (-1 * Ltuyaux) :
x_tuyau = Lfenetre
y_tuyau = random.randint(210, 470)
x_tuyau_inversé = x_tuyau
if x_tuyau2 < (-1 * Ltuyaux) :
x_tuyau2 = Lfenetre
y_tuyau2 = random.randint(210, 470)
x_tuyau2_inversé = x_tuyau2
if (x_tuyau < x_bird < x_tuyau + vitesse_tuyaux) or (x_tuyau2 < x_bird < x_tuyau2 + vitesse_tuyaux):
compte += 1
score(compte)
pygame.display.flip()
principale()
pygame.quit()
quit() |
Partager