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
| import random
import pygame
from pygame.locals import *
posx= 100
posy = 0;
vitesse = 0;
gravite = 0.5;
saut=0
def nuage(x,y):
pygame.draw.circle(fenetre,(255,255,255),(x,y),30)
pygame.draw.circle(fenetre,(255,255,255),(x-20,y),20)
pygame.draw.circle(fenetre,(255,255,255),(x+20,y),20)
pygame.draw.circle(fenetre,(255,255,255),(x,y-15),20)
pygame.draw.circle(fenetre,(255,255,255),(x,y+15),20)
def nuages():
nuage(60,40)
nuage(180,100)
nuage(320,60)
nuage(450,120)
nuage(570,40)
nuage(100,240)
nuage(240,270)
nuage(400,220)
nuage(550,250)
nuage(60,380)
nuage(220,390)
nuage(380,360)
nuage(550,400)
liste_obstacle=[]
liste_rectangle=[]
pygame.init() #lancer pygame
try:
fenetre=pygame.display.set_mode((640,480)) #descripteur de la fenetre
pygame.display.set_caption("Flappy Bird") #titre dans la barre sup
fond = pygame.Surface((640,480)).convert() #cree des pixels dans la surface
fond.fill((114, 177, 228)) #colorie les pixels
#chargement des différentes images -> descripteur
image_rect=pygame.image.load("rectangle.jpg").convert()
image_rect2=pygame.image.load("rectangle2.jpg").convert()
Clock = pygame.time.Clock()
image1=image_rect.get_rect()
image2=image_rect2.get_rect()
rectangle1=image_rect.get_rect() #On crée un rectangle à partir de l'image
rectangle2=image_rect.get_rect()
rectangle1.left=640#abscisse du rectangle du haut
rectangle2.left=640#abscisse du rectangle du haut
timer=0#variable
timer2=0
limite=100#limite du timer
oiseau1=pygame.image.load("oiseau1.png").convert_alpha()
oiseau1 = pygame.transform.scale(oiseau1,(100,100))
oiseau2=pygame.image.load("oiseau2.png").convert_alpha()
oiseau2 = pygame.transform.scale(oiseau2,(100,100))
oiseau3=pygame.image.load("oiseau3.png").convert_alpha()
oiseau3 = pygame.transform.scale(oiseau3,(100,100))
oiseau4=pygame.image.load("oiseau4.png").convert_alpha()
oiseau4 = pygame.transform.scale(oiseau4,(100,100))
oiseau = [oiseau1,oiseau2,oiseau3,oiseau4]
oiseauanimation =1
continuer =-1
while continuer==-1:
#pygame.mixer.music.load("son2.wav")
#pygame.mixer.music.play(-1,0.0)
fenetre.fill((0,0,0))
acc=pygame.image.load("acc.jpg")
acc = pygame.transform.scale(acc,(640,480))
fenetre.blit(acc,(0,0))
police = pygame.font.Font(None ,30)
texte = police.render("Pour jouer, appuyez sur c comme commencer", True, (0,0,0))
fenetre.blit(texte, [80, 230])
for event in pygame.event.get():
if event.type == QUIT:
continuer=0
if event.type == KEYUP:
if event.key == K_c:
continuer=1
for event in pygame.event.get():
if event.type == QUIT:
continuer=0
pygame.display.flip()
Clock.tick(20)
while continuer : #boucle principale
for event in pygame.event.get():
if event.type == QUIT:
continuer=0
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
continuer=0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
saut = -10
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
saut = 12
Clock.tick(60)
posy += saut
fenetre.blit(fond, (0,0))#on blitte le fond
nuages()
timer=timer+1#incrementation
timer2=timer2+1
fenetre.blit(oiseau[oiseauanimation-1],(posx,posy))
rect_bird=oiseau[oiseauanimation-1].get_rect()
if (oiseauanimation == 4):
oiseauanimation=1
else:
oiseauanimation+=1;
if timer2<100:
Clock.tick(60)
for image in liste_rectangle:
for obstacle in liste_obstacle:
fenetre.blit(new_rectangles,obstacle[0])#pose image dans rectangle
fenetre.blit(new_rectangles,obstacle[1])
obstacle[0] = obstacle[0].move(-2,0)#decremente l'abscisse de 1
obstacle[1] = obstacle[1].move(-2,0)#decremente l'abscisse de 1
#si oiseau dans un obstacle, on arrete la boucle
if rect_bird.colliderect(obstacle[0]) or rect_bird.colliderect(obstacle[1]):
print("perdu")
continuer=0
if timer2>=100 and timer2<200:
Clock.tick(65)
if timer2>=200 and timer2<300:
Clock.tick(75)
if timer2>=300 and timer2<400:
Clock.tick(90)
if timer2>=400:
Clock.tick(115)
if timer>=limite:#si le temps limite est atteint
timer=0#timer recommence
limite=150+random.randint(0,150)#limite du timer aléatoire
milieu=150+random.randint(0,100)#milieu de l'espace entre les 2 rectangles
rectangle1.bottom=milieu-72#definit le bas du rectangle du haut
rectangle2.top=milieu+71 #ordonne du rectangle du haut+espace entre les 2 rect
new_obstacles=[rectangle1,rectangle2]#liste contenant les obstacles
liste_obstacle.append(new_obstacles)#ajoute un nouvel obstacle
new_rectangles=[image_rect,image_rect2]#liste contenant les obstacles
liste_rectangle.append(new_rectangles)#ajoute un nouvel obstacle
pygame.display.flip() #rafraichit l'écran
finally:
pygame.quit() |
Partager