-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaiPlays.py
More file actions
93 lines (72 loc) · 2.58 KB
/
aiPlays.py
File metadata and controls
93 lines (72 loc) · 2.58 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
from birdClass import Bird
from bottomClass import Bottom
from pipeClass import Pipe
from playingWindow import PlayWindow as pw
from collisionScreen import Collision as col
import neat
import pygame
import pickle
floorDimension = 730
windowWidth = 600
windowHeight = 800
window = pygame.display.set_mode((windowWidth, windowHeight))
class PlayWithAI:
def aiInControl():
win = window
neuralNetwork = pickle.load(open('winner.pkl', 'rb'))
bird = Bird(300, 250)
base = Bottom(floorDimension)
pipes = [Pipe(700)]
score = 0
clock = pygame.time.Clock()
run = True
while run:
clock.tick(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
break
pipeIndex = 0
if len(pipes) > 1 and bird.x > pipes[0].x + pipes[0].pipeTop.get_width():
pipeIndex = 1
bird.move()
output = neuralNetwork.activate((bird.y, abs(bird.y - pipes[pipeIndex].height), abs(bird.y - pipes[pipeIndex].bottom)))
if output[0] > 0.5:
bird.jump()
base.move()
rem = []
addPipe = False
for pipe in pipes:
pipe.move()
if pipe.collide(bird, win):
while True:
col.aiCollided(score)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
if pipe.x + pipe.pipeTop.get_width() < 0:
rem.append(pipe)
if not pipe.passed and pipe.x < bird.x:
pipe.passed = True
addPipe = True
if addPipe:
score += 1
pipes.append(Pipe(windowWidth))
for r in rem:
pipes.remove(r)
pw.drawPlayWindow(window, bird, pipes, base, score)
def main():
run = True
go = False
while run:
pw.drawStartWindow()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
go = True
run = False
while go:
PlayWithAI.aiInControl()