Commit d4c39135cb5b8eae9defdb2d441126cda35718f3
1 parent
92c295ad5f
Exists in
master
1ère version
Showing 1 changed file with 125 additions and 0 deletions Inline Diff
snake.v0.py
File was created | 1 | import pygame | |
2 | import time | ||
3 | import random | ||
4 | |||
5 | pygame.init() | ||
6 | |||
7 | # Définition des couleurs en composante (Rouge, Vert, Bleu) | ||
8 | blanc = (255, 255, 255) | ||
9 | jaune = (255, 255, 102) | ||
10 | noir = (0, 0, 0) | ||
11 | rouge = (213, 50, 80) | ||
12 | vert = (0, 255, 0) | ||
13 | bleu = (50, 153, 213) | ||
14 | |||
15 | largeur_plateau = 600 | ||
16 | hauteur_plateau = 400 | ||
17 | |||
18 | plateau = pygame.display.set_mode((largeur_plateau, hauteur_plateau)) | ||
19 | pygame.display.set_caption("Le Serpent Numérique du LIA") | ||
20 | |||
21 | horloge = pygame.time.Clock() | ||
22 | |||
23 | bloc_serpent = 10 | ||
24 | vitesse_serpent = 5 | ||
25 | |||
26 | style_caractères = pygame.font.SysFont("bahnschrift", 25) | ||
27 | style_scores = pygame.font.SysFont("comicsansms", 35) | ||
28 | |||
29 | |||
30 | def votre_score(score): | ||
31 | valeur = style_scores.render("Votre Score: " + str(score), True, jaune) | ||
32 | plateau.blit(valeur, [0, 0]) | ||
33 | |||
34 | def notre_serpent(bloc_serpent, corps_serpent): | ||
35 | for x in corps_serpent: | ||
36 | pygame.draw.rect(plateau, noir, [x[0], x[1], bloc_serpent, bloc_serpent]) | ||
37 | |||
38 | def message(msg, color, décalage=0): | ||
39 | mesg = style_caractères.render(msg, True, color) | ||
40 | plateau.blit(mesg, [largeur_plateau / 6, hauteur_plateau / 3+décalage]) | ||
41 | |||
42 | def boucle_jeu(): | ||
43 | fin_jeu = False | ||
44 | ferme_jeu = False | ||
45 | |||
46 | x1 = largeur_plateau / 2 | ||
47 | y1 = hauteur_plateau / 2 | ||
48 | |||
49 | x1_change = 0 | ||
50 | y1_change = 0 | ||
51 | |||
52 | corps_serpent = [] | ||
53 | longueur_serpent = 1 | ||
54 | |||
55 | repas_x = round(random.randrange(0, largeur_plateau - bloc_serpent) / 10.0) * 10.0 | ||
56 | repas_y = round(random.randrange(0, hauteur_plateau - bloc_serpent) / 10.0) * 10.0 | ||
57 | |||
58 | while not fin_jeu: | ||
59 | |||
60 | while ferme_jeu == True: | ||
61 | plateau.fill(bleu) | ||
62 | message("Zut ! Vous avez perdu !", rouge) | ||
63 | message("Appuyez sur J pour jouer encore", jaune, 20) | ||
64 | message("ou sur Q pour quitter le jeu.", jaune, 40) | ||
65 | votre_score(longueur_serpent - 1) | ||
66 | pygame.display.update() | ||
67 | |||
68 | for évènement in pygame.event.get(): | ||
69 | if évènement.type == pygame.KEYDOWN: | ||
70 | if évènement.key == pygame.K_q: | ||
71 | fin_jeu = True | ||
72 | ferme_jeu = False | ||
73 | if évènement.key == pygame.K_j: | ||
74 | boucle_jeu() | ||
75 | |||
76 | for évènement in pygame.event.get(): | ||
77 | if évènement.type == pygame.QUIT: | ||
78 | fin_jeu = True | ||
79 | if évènement.type == pygame.KEYDOWN: | ||
80 | if évènement.key == pygame.K_LEFT: | ||
81 | x1_change = -bloc_serpent | ||
82 | y1_change = 0 | ||
83 | elif évènement.key == pygame.K_RIGHT: | ||
84 | x1_change = bloc_serpent | ||
85 | y1_change = 0 | ||
86 | elif évènement.key == pygame.K_UP: | ||
87 | y1_change = -bloc_serpent | ||
88 | x1_change = 0 | ||
89 | elif évènement.key == pygame.K_DOWN: | ||
90 | y1_change = bloc_serpent | ||
91 | x1_change = 0 | ||
92 | |||
93 | if x1 >= largeur_plateau or x1 < 0 or y1 >= hauteur_plateau or y1 < 0: | ||
94 | ferme_jeu = True | ||
95 | x1 += x1_change | ||
96 | y1 += y1_change | ||
97 | plateau.fill(bleu) | ||
98 | pygame.draw.rect(plateau, vert, [repas_x, repas_y, bloc_serpent, bloc_serpent]) | ||
99 | tête_serpent = [] | ||
100 | tête_serpent.append(x1) | ||
101 | tête_serpent.append(y1) | ||
102 | corps_serpent.append(tête_serpent) | ||
103 | if len(corps_serpent) > longueur_serpent: | ||
104 | del corps_serpent[0] | ||
105 | |||
106 | for x in corps_serpent[:-1]: | ||
107 | if x == tête_serpent: | ||
108 | ferme_jeu = True | ||
109 | |||
110 | notre_serpent(bloc_serpent, corps_serpent) | ||
111 | votre_score(longueur_serpent - 1) | ||
112 | |||
113 | pygame.display.update() | ||
114 | |||
115 | if x1 == repas_x and y1 == repas_y: | ||
116 | repas_x = round(random.randrange(0, largeur_plateau - bloc_serpent) / 10.0) * 10.0 | ||
117 | repas_y = round(random.randrange(0, hauteur_plateau - bloc_serpent) / 10.0) * 10.0 | ||
118 | longueur_serpent += 1 | ||
119 | |||
120 | horloge.tick(vitesse_serpent) | ||
121 | |||
122 | pygame.quit() | ||
123 | quit() | ||
124 | |||
125 | boucle_jeu() | ||
126 |