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
| import pygame
import time
from random import *
blue = (113,177,227)
white = (255,255,255)
green = (124,252,0)
red = (255,0,0)
black = (0,0,0)
pygame.init()
windowW = 700
windowH = 540
ballonW = 50
ballonH = 66
nuageW = 180
nuageH = 490
espace = ballonH*3.5
nuage_vitesse = 0.8
window = pygame.display.set_mode((windowW,windowH))
pygame.display.set_caption("Ballon Volant")
horloge = pygame.time.Clock()
img_ballon = pygame.image.load('avion.png')
img_nuage01 = pygame.image.load('bin_haut.png')
img_nuage02 = pygame.image.load('bin_bas.png')
def main():
x_ballon = 50
y_ballon = 200
y_mouvement=0
espace = ballonH*3
nuage_vitesse = 10
x_nuage = 1000
y_nuage = randint(-650,-100)
score_actuel = 0
high_score_file = open("high_score.txt", "w")
new_high_score = high_score_file
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_mouvement = -5
if event.type == pygame.KEYUP:
y_mouvement = 5
y_ballon += y_mouvement
window.fill(green)
ballon(x_ballon,y_ballon,img_ballon)
nuages(x_nuage, y_nuage, espace)
score(score_actuel)
x_nuage -= nuage_vitesse
if y_ballon>windowH-10 or y_ballon<-0:
gameOver()
if x_ballon + ballonW > x_nuage + 40:
if y_ballon<y_nuage + nuageH - 50 :
if x_ballon-ballonW < x_nuage + nuageW - 20:
gameOver()
if x_ballon + ballonW > x_nuage + 40:
if y_ballon + ballonH > y_nuage + nuageH + espace + 50:
if x_ballon-ballonW < x_nuage + nuageW - 20:
gameOver()
if x_nuage <(-1*nuageW):
x_nuage = windowW
y_nuage = randint(-300,20)
if x_nuage + nuageW < x_ballon and x_nuage + nuageW + nuage_vitesse >= x_ballon :
score_actuel += 1
if new_high_score >= 0:
police = pygame.font.Font('BradBunR.ttf', 20)
texte = police.render("High Score : " + str(new_high_score), True, black)
window.blit(texte, [10,20])
pygame.display.update()
horloge.tick(60)
def gameOver():
displayGameOver()
displaySmallText()
pygame.display.update()
time.sleep(2)
while rejoueOuQuitte() == None:
horloge.tick()
main()
def rejoueOuQuitte():
for event in pygame.event.get([pygame.KEYDOWN, pygame.KEYUP, pygame.QUIT]):
if event.type == pygame.QUIT :
pygame.quit()
quit()
elif event.key ==pygame.KEYUP:
continue
return event.key
return None
def ballon(x,y,img):
window.blit(img, (x,y))
def displayGameOver() :
police = pygame.font.Font('LemonMilk.otf', 100)
texte = police.render("Boom!", True, red)
window.blit(texte, [170,100])
def displaySmallText() :
police = pygame.font.Font('Roboto-Thin.ttf', 20)
texte = police.render("Appuyer sur une touche pour continuer!", True, black)
window.blit(texte, [170,350])
def nuages(x_nuage, y_nuage, espace):
window.blit(img_nuage01,(x_nuage,y_nuage))
window.blit(img_nuage02,(x_nuage,y_nuage + nuageH + espace))
def score(compte):
police = pygame.font.Font('BradBunR.ttf', 20)
texte = police.render("Score : " + str(compte), True, black)
window.blit(texte, [10,0])
def new_high_score():
new_high_score = 0
if score_actuel > high_score:
save_high_score(score_actuel)
def save_high_score(new_high_score):
try:
high_score_file = open("high_score.txt", "w")
high_score_file.write(str(new_high_score))
high_score_file.close()
except IOError:
print("Unable to save the high score.")
main()
pygame.quit()
quit() |
Partager