-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
279 lines (242 loc) · 10.7 KB
/
game.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import pygame
import os
import random
import numpy as np
from pynput.keyboard import Controller
from dinosaur import Dinosaur
from obstacle import SmallCactus, LargeCactus, Bird
from neuralNetwork import NeuralNetwork
pygame.init()
SCREEN_HEIGHT = 525
SCREEN_WIDTH = 1200
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
Y_POS = SCREEN_HEIGHT-200
first_run = True
if (os.path.exists("high_score.txt") and not os.stat("high_score.txt").st_size == 0):
myfile = open("high_score.txt", "r")
max_score = int(myfile.readline())
myfile.close()
else:
max_score = 0
start_speed = 20
kb = Controller()
state = np.zeros(2)
training_inputs = []
training_outputs = []
# [jump,run]
RUNNING = [pygame.image.load(os.path.join("Assets/Dino", "DinoRun1.png")),
pygame.image.load(os.path.join("Assets/Dino", "DinoRun2.png"))]
JUMPING = pygame.image.load(os.path.join("Assets/Dino", "DinoJump.png"))
DUCKING = [pygame.image.load(os.path.join("Assets/Dino", "DinoDuck1.png")),
pygame.image.load(os.path.join("Assets/Dino", "DinoDuck2.png"))]
SMALL_CACTUS = [pygame.image.load(os.path.join("Assets/Cactus", "SmallCactus1.png")),
pygame.image.load(os.path.join("Assets/Cactus", "SmallCactus2.png")),
pygame.image.load(os.path.join("Assets/Cactus", "SmallCactus3.png"))]
LARGE_CACTUS = [pygame.image.load(os.path.join("Assets/Cactus", "LargeCactus1.png")),
pygame.image.load(os.path.join("Assets/Cactus", "LargeCactus2.png")),
pygame.image.load(os.path.join("Assets/Cactus", "LargeCactus3.png"))]
BIRD = [pygame.image.load(os.path.join("Assets/Bird", "Bird1.png")),
pygame.image.load(os.path.join("Assets/Bird", "Bird2.png"))]
BG = pygame.image.load(os.path.join("Assets/Other", "Track.png"))
# functions for showing score and speed, background, data, getting input, checking for collisions, and getting obstacles
def main(ai, generation_size, run_AI, generation):
global game_speed, x_pos_bg, y_pos_bg, points, obstacles, first_run
run = True
clock = pygame.time.Clock()
players = []
if run_AI and ai=='genetic':
for _ in range(generation_size):
players.append(Dinosaur(Y_POS, DUCKING, RUNNING, JUMPING))
else:
players.append(Dinosaur(Y_POS, DUCKING, RUNNING, JUMPING))
if first_run:
game_speed = start_speed
first_run = False
x_pos_bg = 0
y_pos_bg = Y_POS+70
points = 0
font = pygame.font.Font('freesansbold.ttf', 30)
obstacles = []
def score_and_speed():
global points, game_speed, max_score
points += 0.3
if int(points) % 50 == 0 and int(points) != 0:
game_speed += .5
text = font.render(str(int(points)).zfill(5), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (SCREEN_WIDTH-75, 35)
SCREEN.blit(text, textRect)
if int(points) > max_score:
max_score = int(points)
file = open("high_score.txt", "w")
file.write(str(max_score) + "\n" + str(game_speed))
file.close()
text = font.render("HI " + str(max_score).zfill(5), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (SCREEN_WIDTH-225, 35)
SCREEN.blit(text, textRect)
text = font.render("Game Speed: " + str(game_speed).zfill(5), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (SCREEN_WIDTH-500, 35)
SCREEN.blit(text, textRect)
def background():
global x_pos_bg, y_pos_bg
image_width = BG.get_width()
SCREEN.blit(BG, (x_pos_bg, y_pos_bg))
SCREEN.blit(BG, (image_width + x_pos_bg, y_pos_bg))
if x_pos_bg <= -image_width:
SCREEN.blit(BG, (image_width + x_pos_bg, y_pos_bg))
x_pos_bg = 0
x_pos_bg -= game_speed
def data(generation):
text = font.render("Gen " + str(generation), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (600, SCREEN_HEIGHT-70)
SCREEN.blit(text, textRect)
if ai=='genetic':
text = font.render(str(len(players)) + ' players left', True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (600, SCREEN_HEIGHT-30)
SCREEN.blit(text, textRect)
# also adds to training set when dino jumps successfully
def getInput():
global state, prev
for player, nn in zip(players, NN):
if run_AI:
if len(obstacles)!=0 and player.getY() == Y_POS:
prev = state
dist = nn.getDist(obstacles, SCREEN_WIDTH)
height = nn.getHeight(y_pos_bg, obstacles)
state = np.array([dist, round(game_speed/70, 1)])
userInput = nn.predict(state)
if (dist>prev[0] and not NN[0].check_state(prev, training_inputs)):
training_inputs.append(prev)
training_outputs.append(np.array([1,0]))
else:
userInput = 1
else:
userInput = pygame.key.get_pressed()
if userInput[pygame.K_UP]:
userInput = 0
elif userInput[pygame.K_DOWN]:
userInput = 2
else:
userInput = 1
player.draw(SCREEN)
player.update(userInput)
# also adds to training set when dino dies
def checkCollision():
global game_speed
for obstacle in obstacles:
obstacle.draw(SCREEN)
obstacle.update()
for i, player in enumerate(players):
if player.dino_rect.colliderect(obstacle.rect):
if ai=='nn' and run_AI:
v = round(player.getV(),3)
if not NN[0].check_state(prev_run_state, training_inputs):
# die while going up: jump earlier
if player.getV()<7.25 and player.getV()>0:
training_inputs.append(prev_run_state)
training_outputs.append(np.array([1,0]))
# die while going down: jump later
elif player.getV()<0:
training_inputs.append(state)
training_outputs.append(np.array([0,1]))
# die while running: jump earlier
else:
training_inputs.append(prev_run_state)
training_outputs.append(np.array([1,0]))
NN[0].train(training_inputs, training_outputs, epochs=1500)
game_speed = start_speed
scores[i] = player.getScore()
if ai=='genetic' and run_AI:
pass
players.remove(player)
if len(players)==0:
pygame.time.delay(100)
menu(ai, generation_size, generation+1, run_AI, 1)
# also updates previous states
def getObstacle():
global prev_jump_state, prev_run_state
if len(obstacles) == 0:
if run_AI:
num = random.randint(0,1)
else:
num = random.randint(0,1)
if num == 0:
obstacles.append(SmallCactus(SMALL_CACTUS, SCREEN_WIDTH, Y_POS, game_speed, obstacles))
elif num == 1:
obstacles.append(LargeCactus(LARGE_CACTUS, SCREEN_WIDTH, Y_POS, game_speed, obstacles))
elif num == 2:
obstacles.append(Bird(BIRD, SCREEN_WIDTH, Y_POS, game_speed, obstacles))
elif run_AI:
if players[0].getY() < Y_POS:
prev_jump_state = prev
else:
prev_run_state = prev
# calls all the functions to make the game work
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
SCREEN.fill((255, 255, 255))
getInput()
checkCollision()
getObstacle()
background()
score_and_speed()
if run_AI: data(generation+1)
clock.tick(40)
pygame.display.update()
# main menu before game starts
def menu(ai, generation_size, generation, run_AI, death_count):
global points
run = True
while run:
SCREEN.fill((255, 255, 255))
if run_AI:
kb.press('a')
SCREEN.blit(RUNNING[0], (SCREEN_WIDTH // 2 - 20, SCREEN_HEIGHT // 2 - 140))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type != pygame.QUIT:
main(ai, generation_size, run_AI, generation)
else:
font = pygame.font.Font('freesansbold.ttf', 30)
if death_count == 0:
text = font.render("Press any Key to Start", True, (0, 0, 0))
elif death_count > 0:
text = font.render("Press any Key to Restart", True, (0, 0, 0))
score = font.render("Your Score: " + str(int(points)), True, (0, 0, 0))
scoreRect = score.get_rect()
scoreRect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50)
SCREEN.blit(score, scoreRect)
textRect = text.get_rect()
textRect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
SCREEN.blit(text, textRect)
SCREEN.blit(RUNNING[0], (SCREEN_WIDTH // 2 - 20, SCREEN_HEIGHT // 2 - 140))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
main(ai, generation_size, run_AI, generation)
def start(generation_size=2, run_AI=True, ai='nn', learningRate=1e-1, dimensions=[2,4,2]):
global NN
NN = []
if run_AI:
print('\nRunning AI')
global scores
if ai=='nn':
scores = np.empty(1)
NN.append(NeuralNetwork(learningRate, dimensions))
elif ai=='genetic':
scores = np.empty(generation_size)
for _ in range(generation_size):
NN.append(NeuralNetwork(dimensions))
else:
NN.append(0)
menu(ai, generation_size, 0, run_AI, 0)