-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.py
179 lines (145 loc) · 5.66 KB
/
file.py
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
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 4 14:03:42 2024
@author: kenza_hatem
"""
import pygame
from math import cos, sin, pi, tan
from numpy import deg2rad
from random import randint
pygame.init() # Initialise pygame
# Permet de gérer la vitesse du jeu
clock = pygame.time.Clock()
FPS = 60
# pygame.display Permet de nommer la fenetre, voir ses dimensions, parametres d'initialisation
pygame.display.set_caption("Drift")
HEIGHT = 920
WIDTH = 600
VELOCITY = 3
background_img = pygame.image.load("./fond.jpg")
screen = pygame.display.set_mode((WIDTH, HEIGHT))
colors = [
"#8FBCBB",
"#88C0D0",
"#81A1C1",
"#5E81AC",
"#BF616A",
"#D08770",
"#EBCB8B",
"#A3BE8C",
"#B48EAD",
]
taxi_img = pygame.image.load("./Taxi.png")
rock_img = pygame.image.load("./Rock_bery.png")
class Voiture(pygame.sprite.Sprite):
def __init__(self, x=0, y=0):
super().__init__()
self.image = taxi_img
self.origin_image = self.image
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.centerx = self.x
self.rect.centery = self.y
self.angle = 0
def draw(self):
screen.blit(self.image, self.rect)
def rotate(self, sens=1):
self.angle = self.angle + 0.5 * sens
self.image = pygame.transform.rotozoom(self.origin_image, self.angle, 1)
self.rect = self.image.get_rect(center=self.rect.center)
def move(self, direction):
if direction == "right" and self.rect.x<300:
self.rect.x += VELOCITY # Bouge vers la droite
elif direction == "left" and self.rect.x>-250:
self.rect.x -= VELOCITY # Bouge vers la gauche
class Obstacle(pygame.sprite.Sprite):
def __init__(self, x=WIDTH // 3, y=0):
super().__init__()
self.x = x
self.y = y
self.image = rock_img
self.origin_image = self.image
self.rect = self.image.get_rect()
self.rect.centerx = self.x
self.rect.centery = self.y
self.velocity = VELOCITY
self.rect = self.image.get_rect(center=self.rect.center)
def draw(self, angle):
self.rect = self.image.get_rect(center=self.rect.center)
self.rect.x = self.rect.x + tan(deg2rad(angle)) * VELOCITY
self.rect.y = self.rect.y + VELOCITY
# permet de supprimer l'obstacle si l'on sort de l'écran
if not screen.get_rect().contains(self.rect):
self.remove()
self.kill()
screen.blit(self.image, self.rect)
if __name__ == "__main__":
frame_count = 0
running = True
game_over = False # Variable de statut de jeu
score = 0 # Score initial
taxi = Voiture(WIDTH // 2, HEIGHT // 2)
Lobs = []
obstacle_speed = 1 # Vitesse initiale des obstacles
while running:
frame_count += 1
screen.blit(background_img, (0, 0))
taxi.draw()
if not game_over: # Si le jeu n'est pas terminé
# Création des obstacles
if frame_count % 30 == 0:
Lobs.append(Obstacle(x=randint(10, WIDTH - 10), y=0))
score += 1
# Gestion des événements clavier
for event in pygame.event.get():
# Gestion de la fermeture de la fenêtre
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
running = False
elif event.type == pygame.KEYDOWN:
keypressed = event.key
if keypressed == pygame.K_SPACE:
print("space bar")
# proj = Projectile(player.x, player.y, player.angle)
else:
keypressed = 0
if keypressed == pygame.K_RIGHT:
taxi.move("right")
elif keypressed == pygame.K_LEFT:
taxi.move("left")
# Déplacement des obstacles
for obs in Lobs:
obs.draw(taxi.angle)
obs.rect.y += obstacle_speed # Déplacement vertical vers le bas
# Vérification des collisions
if obs.rect.collidepoint(taxi.rect.centerx, taxi.rect.centery):
game_over = True # Le joueur a touché un obstacle, il perd
print("Game Over")
# Augmentation de la vitesse après chaque 10 obstacles passés
if score % 10 == 0 and score != 0:
obstacle_speed += 0.05
# Affichage du score en haut à droite de l'écran
font_score = pygame.font.Font(None, 24)
text_score = font_score.render("Score: " + str(score), True, (0, 0, 0)) # Couleur du texte : blanc
text_score2 = text_score.get_rect(center=(WIDTH // 10, HEIGHT // 14))
screen.blit(text_score, text_score2)
# Affichage du message "Game Win" si le score dépasse 100
if score >= 100:
game_over = True # Le joueur a atteint le score requis, il gagne
print("Game Win")
# Affichage du message "Game Over" si le jeu est terminé
if game_over:
# Affichage du message approprié sur l'écran
font = pygame.font.Font(None, 36)
if score >= 100:
text = font.render("Game Win", True, (0, 255, 0))
else:
text = font.render("Game Over", True, (255, 0, 0))
text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
screen.blit(text, text_rect)
# Quitter le jeu après quelques secondes
running = False # Arrêter le jeu
pygame.display.flip()
clock.tick(FPS)
pygame.time.delay(2000) # Attente de 2 secondes
pygame.quit()