-
Notifications
You must be signed in to change notification settings - Fork 5
/
game.py
147 lines (111 loc) · 3.37 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
# referenced: https://github.com/TimoWilken/flappy-bird-pygame
import pygame
from random import randint
from pygame.locals import *
from collections import deque
from ai import Net
pygame.init()
width, height = 500, 600
screen = pygame.display.set_mode((width, height))
gravity = .001
bird = pygame.image.load("bird.png")
class Bird(pygame.sprite.Sprite):
WIDTH = HEIGHT = 80
START_VELOCITY = .1
STARTY = 200
STARTX = 100
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.x, self.y = Bird.STARTX, Bird.STARTY
self.image = pygame.image.load("bird.png")
self.image = pygame.transform.scale(self.image, (Bird.WIDTH, Bird.HEIGHT))
self.mask = pygame.mask.from_surface(self.image)
self.velocity = .1
def update(self):
self.velocity += gravity
self.y += self.velocity
def flap(self):
self.velocity -= Bird.START_VELOCITY * 10
if self.velocity < -Bird.START_VELOCITY * 5:
self.velocity = -Bird.START_VELOCITY * 5
@property
def rect(self):
return Rect(self.x, self.y, Bird.WIDTH, Bird.HEIGHT)
class Pipe(pygame.sprite.Sprite):
PIECE_WIDTH = 80
PIECE_HEIGHT = 30
ADD_RATE = 1750
SCROLL_SPEED = .2
def __init__(self):
self.x = width - 1
self.image = pygame.Surface((Pipe.PIECE_WIDTH, height), SRCALPHA)
self.counted = False
self.image.convert()
self.image.fill((0, 0, 0, 0))
total_pieces = int((height - 3 * Bird.HEIGHT - 3 * Pipe.PIECE_HEIGHT)/Pipe.PIECE_HEIGHT)
self.bot_pieces = randint(1, total_pieces)
self.top_pieces = total_pieces - self.bot_pieces
pipe_img = pygame.image.load("pipe.png")
pipe_img = pygame.transform.scale(pipe_img, (Pipe.PIECE_WIDTH, 80))
for i in range(self.bot_pieces + 1):
piece_pos = (0, height - i * Pipe.PIECE_HEIGHT)
self.image.blit(pipe_img, piece_pos)
for i in range(self.top_pieces + 1):
self.image.blit(pipe_img, (0, i * Pipe.PIECE_HEIGHT))
self.top_pieces += 1
self.bot_pieces += 1
self.bot_height = self.bot_pieces * Pipe.PIECE_HEIGHT
self.mask = pygame.mask.from_surface(self.image)
@property
def rect(self):
return Rect(self.x, 0, Pipe.PIECE_WIDTH, Pipe.PIECE_HEIGHT)
@property
def visible(self):
return -Pipe.PIECE_WIDTH < self.x < width
def update(self):
self.x -= Pipe.SCROLL_SPEED
def collides_with(self, bird):
return pygame.sprite.collide_mask(self, bird)
def play(headless=False, ai=None):
bird = Bird()
pipes = deque()
frame = 0
score = 0
done = False
while True:
# draw bird
bird.update()
if not headless:
screen.fill((0, 255, 255))
screen.blit(bird.image, (bird.x, bird.y))
if frame % Pipe.ADD_RATE == 0:
p = Pipe()
pipes.append(p)
col = any(p.collides_with(bird) for p in pipes)
# check bounds
if bird.y > 600 or bird.y < 0 or col:
break
while pipes and not pipes[0].visible:
pipes.popleft()
for p in pipes:
if p.x + Pipe.PIECE_WIDTH/2 < bird.x and not p.counted:
p.counted = True
score += 1
print score
p.update()
if not headless:
screen.blit(p.image, (p.x, 0))
if not ai:
for event in pygame.event.get():
if event.type == pygame.QUIT:
break
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
bird.flap()
elif ai.forward([bird.x - pipes[0].x, height - bird.y - pipes[0].bot_height]) > 0.5:
bird.flap()
# update screen
if not headless:
pygame.display.flip()
frame += 1
print 'Score: ' + str(score)
return score