-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjeuClassiqueBonusSecondDegre.py
More file actions
256 lines (193 loc) · 6.92 KB
/
jeuClassiqueBonusSecondDegre.py
File metadata and controls
256 lines (193 loc) · 6.92 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import pygame
import random
import sys
import math
import matplotlib.pyplot as plt
# --- Constantes ---
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
GRAVITY = 0.3
JUMP_STRENGTH = -8
PIPE_WIDTH = 50
PIPE_GAP = 0
PIPE_SPEED = 0
PIPE_GAP_INIT = 200
PIPE_SPEED_INIT = 1
FPS = 60
NUM_PIPES = 3
WIND_STRENGTH = 0 # I removed the moving pipes and wind (as explained in the rapport.pdf)
GRACE_PERIOD = 0
PIPE_MOVE_AMPLITUDE = 0
PIPE_MOVE_SPEED = 0
PIPE_MAX_OFFSET = 0
BONUS_APPEAR_INTERVAL = 100
BONUS_RADIUS = 10
BONUS_SCORE = 5000
class Bird:
def __init__(self):
self.x = 50
self.y = SCREEN_HEIGHT // 2
self.velocity = 0
def update(self):
self.velocity += GRAVITY
self.y += self.velocity
def jump(self):
self.velocity = JUMP_STRENGTH
def get_rect(self):
return pygame.Rect(self.x - 20, self.y - 20, 40, 40)
class Pipe:
def __init__(self, x):
self.base_x = x
self.x = x
self.x_offset_bottom = random.randint(-PIPE_MAX_OFFSET, PIPE_MAX_OFFSET)
self.base_height = random.randint(100, SCREEN_HEIGHT - PIPE_GAP - 100)
self.height = self.base_height
self.osc_y = random.uniform(0, 2 * math.pi)
self.osc_x = random.uniform(0, 2 * math.pi)
def update(self):
self.osc_y += PIPE_MOVE_SPEED
self.osc_x += PIPE_MOVE_SPEED
self.height = self.base_height + int(math.sin(self.osc_y) * PIPE_MOVE_AMPLITUDE)
self.x = self.base_x - PIPE_SPEED + int(math.cos(self.osc_x) * 5)
self.base_x -= PIPE_SPEED
def collides_with(self, bird_rect):
pipe_top = pygame.Rect(self.x, 0, PIPE_WIDTH, self.height)
pipe_bottom = pygame.Rect(self.x + self.x_offset_bottom, self.height + PIPE_GAP, PIPE_WIDTH, SCREEN_HEIGHT)
return bird_rect.colliderect(pipe_top) or bird_rect.colliderect(pipe_bottom)
class Bonus:
def __init__(self):
self.x = SCREEN_WIDTH + random.randint(100, 300)
self.y = random.randint(100, SCREEN_HEIGHT - 100)
self.collected = False
def update(self):
self.x -= PIPE_SPEED
def get_rect(self):
return pygame.Rect(self.x - BONUS_RADIUS, self.y - BONUS_RADIUS, BONUS_RADIUS * 2, BONUS_RADIUS * 2)
def should_jump_complexe(bird, pipes, weights, bonus):
min_dist = float('inf')
next_pipe = None
for pipe in pipes:
pipe_end = max(pipe.x + PIPE_WIDTH + 20, pipe.x + pipe.x_offset_bottom + PIPE_WIDTH + 20)
if pipe_end < bird.x:
continue
dx = pipe.x - bird.x
if dx < min_dist:
min_dist = dx
next_pipe = pipe
if not next_pipe:
return False
pipe_top_height = next_pipe.height
pipe_bottom_height = next_pipe.height + PIPE_GAP
dx = next_pipe.x - bird.x
dy_top = bird.y - pipe_top_height
dy_bottom = bird.y - pipe_bottom_height
v = bird.velocity
altitude = bird.y
dx_bonus = 999
dy_bonus = 999
if bonus:
min_dist = float('inf')
closest_bonus = None
for b in bonus:
if b.collected:
continue
dist = b.x - bird.x
if 0 <= dist < min_dist:
min_dist = dist
closest_bonus = b
if closest_bonus:
dx_bonus = closest_bonus.x - bird.x
dy_bonus = closest_bonus.y - bird.y
if dx_bonus == 0 :
dx_bonus = 1
if dy_bonus == 0 :
dy_bonus = 1
inputs = [dy_top, dy_bottom, dx, v, altitude, dx_bonus, dy_bonus]
inputs_squared = [i**2 for i in inputs]
full_inputs = inputs + inputs_squared
decision = sum(w * i for w, i in zip(weights, full_inputs))
return decision < 0
def run_game_classique_bonus(weights=None, render=False, manual=False):
global PIPE_GAP, PIPE_SPEED, PIPE_GAP_INIT, PIPE_SPEED_INIT, frame
bird = Bird()
pipes = [Pipe(SCREEN_WIDTH + i * 300) for i in range(NUM_PIPES)]
score = 0
frame = 0
alive_distance = 0
pipe_count = 0
wind = 0
bonuses = []
PIPE_SPEED = PIPE_SPEED_INIT
PIPE_GAP = PIPE_GAP_INIT
while True:
if render:
screen.fill((135, 206, 250))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if frame % BONUS_APPEAR_INTERVAL == 0:
bonuses.append(Bonus())
wind = random.uniform(-WIND_STRENGTH, WIND_STRENGTH)
if manual:
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
bird.jump()
else:
if should_jump_complexe(bird, pipes, weights, bonuses):
bird.jump()
bird.velocity += GRAVITY + wind
bird.update()
for pipe in pipes:
pipe.update()
for b in bonuses:
b.update()
for pipe in pipes:
if pipe.x + PIPE_WIDTH < 0:
pipes.remove(pipe)
pipes.append(Pipe(300 * NUM_PIPES - PIPE_WIDTH))
score += 1
pipe_count += 1
if pipe_count % 5 == 0:
if PIPE_GAP > 60:
PIPE_GAP -= 10
if PIPE_SPEED < 10:
PIPE_SPEED += 0.5
bird_rect = bird.get_rect()
collision = bird.y > SCREEN_HEIGHT or bird.y < 0 or any(pipe.collides_with(bird_rect) for pipe in pipes)
bonuses = [b for b in bonuses if b.x + BONUS_RADIUS > 0 and not b.collected]
for b in bonuses:
if bird_rect.colliderect(b.get_rect()):
score += BONUS_SCORE // 1000
b.collected = True
alive_distance += 1
if render:
pygame.draw.circle(screen, (255, 255, 0), (int(bird.x), int(bird.y)), 20)
for pipe in pipes:
pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(pipe.x, 0, PIPE_WIDTH, pipe.height))
pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(pipe.x + pipe.x_offset_bottom, pipe.height + PIPE_GAP, PIPE_WIDTH, SCREEN_HEIGHT))
for b in bonuses:
if not b.collected:
pygame.draw.circle(screen, (255, 0, 255), (int(b.x), int(b.y)), BONUS_RADIUS)
score_text = font.render(f"Score: {score}", True, (0, 0, 0))
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(FPS)
if collision:
break
frame += 1
if frame > 30000:
break
return score * 1000 + alive_distance
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 36)
import json
if len(sys.argv) < 2:
sys.exit(1)
with open(sys.argv[1], 'r') as f:
weights = json.load(f)
score = run_game_classique_bonus(weights=weights, render=True, manual=False)
print("Score final :", score)