-
Notifications
You must be signed in to change notification settings - Fork 0
/
flappy_mytry.py
782 lines (635 loc) · 27.4 KB
/
flappy_mytry.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
from itertools import cycle
import random
import sys
import numpy as np
import pygame
from pygame.locals import *
import json
i=0;
FPS = 40
SCREENWIDTH = 288
SCREENHEIGHT = 512
# amount by which base can maximum shift to left
PIPEGAPSIZE = 170 # gap between upper and lower part of pipe
BASEY = SCREENHEIGHT * 0.79
eta = 0.7
# image, sound and hitmask dicts
IMAGES, SOUNDS, HITMASKS = {}, {}, {}
X = np.array([[100]])
# list of all possible players (tuple of 3 positions of flap)
max_score=0
PLAYERS_LIST = (
# red bird
(
'assets/sprites/redbird-upflap.png',
'assets/sprites/redbird-midflap.png',
'assets/sprites/redbird-downflap.png',
),
# blue bird
(
# amount by which base can maximum shift to left
'assets/sprites/bluebird-upflap.png',
'assets/sprites/bluebird-midflap.png',
'assets/sprites/bluebird-downflap.png',
),
# yellow bird
(
'assets/sprites/yellowbird-upflap.png',
'assets/sprites/yellowbird-midflap.png',
'assets/sprites/yellowbird-downflap.png',
),
)
# list of backgrounds
BACKGROUNDS_LIST = (
'assets/sprites/background-day.png',
'assets/sprites/background-night.png',
)
# list of pipes
PIPES_LIST = (
'assets/sprites/pipe-green.png',
'assets/sprites/pipe-red.png',
)
try:
xrange
except NameError:
xrange = range
def sigmoid(x):
return 1.0/(1.0 + np.exp(-x))
def sigmoid_prime(x):
return sigmoid(x)*(1.0-sigmoid(x))
def tanh(x):
return np.tanh(x)
def tanh_prime(x):
return 1.0 - x**2
class NeuralNetwork:
def __init__(self, layers, activation='sigmoid'):
if activation == 'sigmoid':
self.activation = sigmoid
self.activation_prime = sigmoid_prime
elif activation == 'tanh':
self.activation = tanh
self.activation_prime = tanh_prime
# Set weights
self.weights = []
# layers = [2,2,1]
# range of weight values (-1,1)
# input and hidden layers - random((2+1, 2+1)) : 3 x 3
# for i in range(1, len(layers) - 1):
# r = 2*np.random.random((layers[i-1] + 1, layers[i] + 1)) -1
# self.weights.append(r)
# # output layer - random((2+1, 1)) : 3 x 1
# r = 2*np.random.random( (layers[i] + 1, layers[i+1])) - 1
# self.weights.append(r)
with open('weights.json', 'r') as datafile:
data_load = json.load(datafile)
theta1 = np.array(data_load['theta1'])
theta2 = np.array(data_load['theta2'])
self.weights.append(theta1)
self.weights.append(theta2)
def fit(self, X, y, learning_rate=0.4, epochs=1):
# Add column of ones to X
# This is to add the bias unit to the input layer
ones = np.atleast_2d(np.ones(X.shape[0]))
X = np.concatenate((ones.T, X), axis=1)
for k in range(epochs):
i = np.random.randint(X.shape[0])
a = [X[i]]
for l in range(len(self.weights)):
dot_value = np.dot(a[l], self.weights[l])
activation = self.activation(dot_value)
a.append(activation)
# output layer
error = y[i] - a[-1]
deltas = [error * self.activation_prime(a[-1])]
# we need to begin at the second to last layer
# (a layer before the output layer)
for l in range(len(a) - 2, 0, -1):
deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_prime(a[l]))
# reverse
# [level3(output)->level2(hidden)] => [level2(hidden)->level3(output)]
deltas.reverse()
# backpropagation
# 1. Multiply its output delta and input activation
# to get the gradient of the weight.
# 2. Subtract a ratio (percentage) of the gradient from the weight.
for i in range(len(self.weights)):
layer = np.atleast_2d(a[i])
delta = np.atleast_2d(deltas[i])
self.weights[i] += learning_rate * layer.T.dot(delta)
# if k % 4 == 0: #print ('epochs:', k)
def predict(self, x):
a = np.concatenate((np.ones(1).T, np.array(x)))
for l in range(0, len(self.weights)):
a = self.activation(np.dot(a, self.weights[l]))
return a
# # Renforcement Learning part with neural network
# class Network(object):
# """docstring for Network"""
# def __init__(self, sizes):
# self.num_layers = len(sizes)
# self.sizes = sizes
# self.biases = [np.random.randn(y,1) for y in sizes[1:]]
# self.weights = [np.random.randn(y, x)
# for x, y in zip(sizes[:-1], sizes[1:])]
# def feedforward(self,a):
# for b,w in zip(self.biases,self.weights):
# a = sigmoid(np.dot(a,w)+b)
# return a
# def SGD(self, training_data, epochs, mini_batch_size, eta,test_data=None):
# """Train the neural network using mini-batch stochastic
# gradient descent. The ``training_data`` is a list of tuples
# ``(x, y)`` representing the training inputs and the desired
# outputs. The other non-optional parameters are
# self-explanatory. If ``test_data`` is provided then the
# network will be evaluated against the test data after each
# epoch, and partial progress printed out. This is useful for
# tracking progress, but slows things down substantially."""
# print(training_data)
# if test_data: n_test = len(test_data)
# n = len(training_data)
# for j in xrange(epochs):
# mini_batches = [
# training_data[k:k+mini_batch_size]
# for k in xrange(0, n, mini_batch_size)]
# for mini_batch in mini_batches:
# self.update_mini_batch(mini_batch, eta)
# # if test_data:
# # print "Epoch {0}: {1} / {2}".format(
# # j, self.evaluate(test_data), n_test)
# # else:
# # print "Epoch {0} complete".format(j)
# def update_mini_batch(self, mini_batch, eta):
# """Update the network's weights and biases by applying
# gradient descent using backpropagation to a single mini batch.
# The ``mini_batch`` is a list of tuples ``(x, y)``, and ``eta``
# is the learning rate."""
# print(mini_batch)
# nabla_b = [np.zeros(b.shape) for b in self.biases]
# nabla_w = [np.zeros(w.shape) for w in self.weights]
# for x, y in mini_batch:
# delta_nabla_b, delta_nabla_w = self.backprop(x, y)
# nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
# nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
# self.weights = [w-(eta/len(mini_batch))*nw
# for w, nw in zip(self.weights, nabla_w)]
# self.biases = [b-(eta/len(mini_batch))*nb
# for b, nb in zip(self.biases, nabla_b)]
# def training(self,x,y,eta):
# nabla_b = [np.zeros(b.shape) for b in self.biases]
# nabla_w = [np.zeros(w.shape) for w in self.weights]
# delta_nabla_b, delta_nabla_w = self.backprop(x, y)
# nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
# nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
# self.weights = [w-(eta/len(mini_batch))*nw
# for w, nw in zip(self.weights, nabla_w)]
# self.biases = [b-(eta/len(mini_batch))*nb
# for b, nb in zip(self.biases, nabla_b)]
# def backprop(self, x, y):
# """Return a tuple ``(nabla_b, nabla_w)`` representing the
# gradient for the cost function C_x. ``nabla_b`` and
# ``nabla_w`` are layer-by-layer lists of numpy arrays, similar
# to ``self.biases`` and ``self.weights``."""
# nabla_b = [np.zeros(b.shape) for b in self.biases]
# nabla_w = [np.zeros(w.shape) for w in self.weights]
# # feedforward
# activation = x
# activations = [x] # list to store all the activations, layer by layer
# zs = [] # list to store all the z vectors, layer by layer
# for b, w in zip(self.biases, self.weights):
# z = np.dot(w, activation)+b
# zs.append(z)
# activation = sigmoid(z)
# activations.append(activation)
# # backward pass
# delta = self.cost_derivative(activations[-1], y) * \
# sigmoid_prime(zs[-1])
# nabla_b[-1] = delta
# nabla_w[-1] = np.dot(delta, activations[-2].transpose())
# # Note that the variable l in the loop below is used a little
# # differently to the notation in Chapter 2 of the book. Here,
# # l = 1 means the last layer of neurons, l = 2 is the
# # second-last layer, and so on. It's a renumbering of the
# # scheme in the book, used here to take advantage of the fact
# # that Python can use negative indices in lists.
# for l in xrange(2, self.num_layers):
# z = zs[-l]
# sp = sigmoid_prime(z)
# delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
# nabla_b[-l] = delta
# activations[-l-1] = np.matrix(activations[-l-1])
# nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
# return (nabla_b, nabla_-20
# def cost_derivative(self, output_activations, y):
# """Return the vector of partial derivatives \partial C_x /
# \partial a for the output activations."""
# return (output_activations-y)
# #### Miscellaneous functions
net = NeuralNetwork([2,6,1])
def main():
global SCREEN, FPSCLOCK
pygame.init()
FPSCLOCK = pygame.time.Clock()
SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption('Flappy Bird')
# numbers sprites for score display
IMAGES['numbers'] = (
pygame.image.load('assets/sprites/0.png').convert_alpha(),
pygame.image.load('assets/sprites/1.png').convert_alpha(),
pygame.image.load('assets/sprites/2.png').convert_alpha(),
pygame.image.load('assets/sprites/3.png').convert_alpha(),
pygame.image.load('assets/sprites/4.png').convert_alpha(),
pygame.image.load('assets/sprites/5.png').convert_alpha(),
pygame.image.load('assets/sprites/6.png').convert_alpha(),
pygame.image.load('assets/sprites/7.png').convert_alpha(),
pygame.image.load('assets/sprites/8.png').convert_alpha(),
pygame.image.load('assets/sprites/9.png').convert_alpha()
)
# game over sprite
IMAGES['gameover'] = pygame.image.load('assets/sprites/gameover.png').convert_alpha()
# message sprite for welcome screen
IMAGES['message'] = pygame.image.load('assets/sprites/message.png').convert_alpha()
# base (ground) sprite
IMAGES['base'] = pygame.image.load('assets/sprites/base.png').convert_alpha()
# sounds
if 'win' in sys.platform:
soundExt = '.wav'
else:
soundExt = '.ogg'
SOUNDS['die'] = pygame.mixer.Sound('assets/audio/die' + soundExt)
SOUNDS['hit'] = pygame.mixer.Sound('assets/audio/hit' + soundExt)
SOUNDS['point'] = pygame.mixer.Sound('assets/audio/point' + soundExt)
SOUNDS['swoosh'] = pygame.mixer.Sound('assets/audio/swoosh' + soundExt)
SOUNDS['wing'] = pygame.mixer.Sound('assets/audio/wing' + soundExt)
while True:
# select random background sprites
randBg = random.randint(0, len(BACKGROUNDS_LIST) - 1)
IMAGES['background'] = pygame.image.load(BACKGROUNDS_LIST[randBg]).convert()
# select random player sprites
randPlayer = random.randint(0, len(PLAYERS_LIST) - 1)
IMAGES['player'] = (
pygame.image.load(PLAYERS_LIST[randPlayer][0]).convert_alpha(),
pygame.image.load(PLAYERS_LIST[randPlayer][1]).convert_alpha(),
pygame.image.load(PLAYERS_LIST[randPlayer][2]).convert_alpha(),
)
# select random pipe sprites
pipeindex = random.randint(0, len(PIPES_LIST) - 1)
IMAGES['pipe'] = (
pygame.transform.rotate(
pygame.image.load(PIPES_LIST[pipeindex]).convert_alpha(), 180),
pygame.image.load(PIPES_LIST[pipeindex]).convert_alpha(),
)
# hismask for pipes
HITMASKS['pipe'] = (
getHitmask(IMAGES['pipe'][0]),
getHitmask(IMAGES['pipe'][1]),
)
# hitmask for player
HITMASKS['player'] = (
getHitmask(IMAGES['player'][0]),
getHitmask(IMAGES['player'][1]),
getHitmask(IMAGES['player'][2]),
)
movementInfo = showWelcomeAnimation()
crashInfo = mainGame(movementInfo)
showGameOverScreen(crashInfo)
def showWelcomeAnimation():
"""Shows welcome screen animation of flappy bird"""
# index of player to blit on screen
playerIndex = 0
playerIndexGen = cycle([0, 1, 2, 1])
# iterator used to change playerIndex after every 5th iteration
loopIter = 0
playerx = int(SCREENWIDTH * 0.2)
playery = int((SCREENHEIGHT - IMAGES['player'][0].get_height()) / 2)
messagex = int((SCREENWIDTH - IMAGES['message'].get_width()) / 2)
messagey = int(SCREENHEIGHT * 0.12)
basex = 0
# amount by which base can maximum shift to left
baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()
# player shm for up-down motion on welcome screen
playerShmVals = {'val': 0, 'dir': 1}
while True:
# for event in pygame.event.get():
# if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
# pygame.quit()
# sys.exit()
# #if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP):
# make first flap sound and return values for mainGame
# SOUNDS['wing'].play()
return {
'playery': playery + playerShmVals['val'],
'basex': basex,
'playerIndexGen': playerIndexGen,
}
# adjust playery, playerIndex, basex
if (loopIter + 1) % 5 == 0:
playerIndex = next(playerIndexGen)
loopIter = (loopIter + 1) % 30
basex = -((-basex + 4) % baseShift)
playerShm(playerShmVals)
# draw sprites
SCREEN.blit(IMAGES['background'], (0,0))
SCREEN.blit(IMAGES['player'][playerIndex],
(playerx, playery + playerShmVals['val']))
SCREEN.blit(IMAGES['message'], (messagex, messagey))
SCREEN.blit(IMAGES['base'], (basex, BASEY))
pygame.display.update()
FPSCLOCK.tick(FPS)
def mainGame(movementInfo):
score = playerIndex = loopIter = 0
playerIndexGen = movementInfo['playerIndexGen']
playerx, playery = int(SCREENWIDTH * 0.2), movementInfo['playery']
basex = movementInfo['basex']
baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()
# get 2 new pipes to add to upperPipes lowerPipes list
newPipe1 = getRandomPipe()
newPipe2 = getRandomPipe()
print("new pipe",newPipe1)
# list of upper pipes
upperPipes = [
{'x': SCREENWIDTH , 'y': newPipe1[0]['y']},
{'x': SCREENWIDTH + (SCREENWIDTH / 2), 'y': newPipe2[0]['y']},
]
# list of lowerpipe
lowerPipes = [
{'x': SCREENWIDTH, 'y': newPipe1[1]['y']},
{'x': SCREENWIDTH + (SCREENWIDTH / 2), 'y': newPipe2[1]['y']},
]
pipeVelX = -4
# player velocity, max velocity, downward accleration, accleration on flap
playerVelY = -9 # player's velocity along Y, default same as playerFlapped
playerMaxVelY = 10 # max vel along Y, max descend speed
playerMinVelY = -8 # min vel along Y, max ascend speed
playerAccY = 1 # players downward accleration
playerRot = 45 # player's rotation
playerVelRot = 3 # angular speed
playerRotThr = 20 # rotation threshold
playerFlapAcc = -9 # players speed on flapping
playerFlapped = False # True when player flaps
# Defining input for feedforward
global X
while True:
# for event in pygame.event.get():
# if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
# pygame.quit()
# sys.exit()
# if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP):
# if playery > -2 * IMAGES['player'][0].get_height():
# # playerVelY = playerFlapAcc
# # playerFlapped = True
# # SOUNDS['wing'].play()
# if(len(lowerPipes)==3):
# a = [lowerPipes[1]['x']+4-playerx,(lowerPipes[1]['y'])-playery]
# X = np.array([[lowerPipes[1]['x']+4-playerx,lowerPipes[1]['y']-20-playery]])
# elif (len(lowerPipes)==2 and lowerPipes[0]['x']<50):
# a = [lowerPipes[1]['x']-playerx,lowerPipes[1]['y']-playery]
# X = np.array([[lowerPipes[1]['x']+4-playerx,lowerPipes[1]['y']-20-playery]])
# else:
# a = [lowerPipes[0]['x']-playerx,lowerPipes[0]['y']-playery]
# X = np.array([[lowerPipes[0]['x']+4-playerx,lowerPipes[0]['y']-20-playery]])
if(lowerPipes[0]['x'] < 40):
x_data = lowerPipes[1]['x']-playerx;
y_data = lowerPipes[1]['y']-playery;
else:
x_data = lowerPipes[0]['x']-playerx;
y_data = lowerPipes[0]['y']-playery;
X = np.array([[x_data, y_data]])
#crashTest = checkCrash({'x': playerx, 'y': playery, 'index': playerIndex},
#upperPipes, lowerPipes)
# print(lowerPipes)
# print(playerx,playery)
for e in X:
output = net.predict(e)
#print(output)
if(output>0.5):
playerVelY = playerFlapAcc
playerFlapped = True
SOUNDS['wing'].play()
crashTest = checkCrash({'x': playerx, 'y': playery, 'index': playerIndex},
upperPipes, lowerPipes)
# print(crashTest[0])
if(playery<0 or crashTest[0]==True):
# print('Hello')
if(output>0.5):
y = np.array([0])
else:
y = np.array([1])
# print("Fitting")
net.fit(X,y)
else:
if(output>0.5):
y = np.array([1])
else:
y = np.array([0])
# net.fit(X,y)
# check for crash here
# if(len(lowerPipes)==3):
# a = [lowerPipes[1]['x']-playerx,lowerPipes[1]['y']-playery]
# X = np.array([[lowerPipes[1]['x']-playerx,lowerPipes[1]['y']-playery]])
# else:
# a = [lowerPipes[0]['x']-playerx,lowerPipes[0]['y']-playery]
# X = np.array([[lowerPipes[0]['x']-playerx,lowerPipes[0]['y']-playery]])
crashTest = checkCrash({'x': playerx, 'y': playery, 'index': playerIndex},
upperPipes, lowerPipes)
if crashTest[0] or playery<0:
print ("----------------------------------------------------------------")
return {
'y': playery,
'groundCrash': crashTest[1],
'basex': basex,
'upperPipes': upperPipes,
'lowerPipes': lowerPipes,
'score': score,
'playerVelY': playerVelY,
'playerRot': playerRot
}
global max_score
# check for score
playerMidPos = playerx + IMAGES['player'][0].get_width() / 2
for pipe in upperPipes:
pipeMidPos = pipe['x'] + IMAGES['pipe'][0].get_width() / 2
if pipeMidPos <= playerMidPos < pipeMidPos + 4:
score += 1
max_score = max(max_score,score)
print(max_score)
#SOUNDS['point'].play()
# playerIndex basex change
if (loopIter + 1) % 3 == 0:
playerIndex = next(playerIndexGen)
loopIter = (loopIter + 1) % 30
basex = -((-basex + 100) % baseShift)
# rotate the player
if playerRot > -90:
playerRot -= playerVelRot
# player's movement
if playerVelY < playerMaxVelY and not playerFlapped:
playerVelY += playerAccY
if playerFlapped:
playerFlapped = False
# more rotation to cover the threshold (calculated in visible rotation)
playerRot = 45
playerHeight = IMAGES['player'][playerIndex].get_height()
playery += min(playerVelY, BASEY - playery - playerHeight)
# move pipes to left
for uPipe, lPipe in zip(upperPipes, lowerPipes):
uPipe['x'] += pipeVelX
lPipe['x'] += pipeVelX
# add new pipe when first pipe is about to touch left of screen
if 0 < upperPipes[0]['x'] < 5:
newPipe = getRandomPipe()
upperPipes.append(newPipe[0])
lowerPipes.append(newPipe[1])
# remove first pipe if its out of the screen
if upperPipes[0]['x'] < -IMAGES['pipe'][0].get_width():
upperPipes.pop(0)
lowerPipes.pop(0)
# draw sprites
SCREEN.blit(IMAGES['background'], (0,0))
for uPipe, lPipe in zip(upperPipes, lowerPipes):
SCREEN.blit(IMAGES['pipe'][0], (uPipe['x'], uPipe['y']))
SCREEN.blit(IMAGES['pipe'][1], (lPipe['x'], lPipe['y']))
SCREEN.blit(IMAGES['base'], (basex, BASEY))
# print score so player overlaps the score
showScore(score)
# Player rotation has a threshold
visibleRot = playerRotThr
if playerRot <= playerRotThr:
visibleRot = playerRot
playerSurface = pygame.transform.rotate(IMAGES['player'][playerIndex], visibleRot)
SCREEN.blit(playerSurface, (playerx, playery))
pygame.display.update()
FPSCLOCK.tick(FPS)
def showGameOverScreen(crashInfo):
"""crashes the player down ans shows gameover image"""
score = crashInfo['score']
playerx = SCREENWIDTH * 0.2
playery = crashInfo['y']
playerHeight = IMAGES['player'][0].get_height()
playerVelY = crashInfo['playerVelY']
playerAccY = 2
playerRot = crashInfo['playerRot']
playerVelRot = 7
basex = crashInfo['basex']
upperPipes, lowerPipes = crashInfo['upperPipes'], crashInfo['lowerPipes']
# play hit and die sounds
if not crashInfo['groundCrash']:
print('hell0')
while True:
# for event in pygame.event.get():
# if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
# pygame.quit()
# sys.exit()
# if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP):
# if playery + playerHeight >= BASEY - 1:
# theta1 = net.weights[0].tolist()
# theta2 = net.weights[1].tolist()
# dict_obj = {"theta1" : theta1, "theta2" : theta2}
# with open('weights.json', 'w') as outfile:
# json.dump(dict_obj, outfile)
# print(net.weights)
return
# player y shift
if playery + playerHeight < BASEY - 1:
playery += min(playerVelY, BASEY - playery - playerHeight)
# player velocity change
if playerVelY < 15:
playerVelY += playerAccY
# rotate only when it's a pipe crash
if not crashInfo['groundCrash']:
if playerRot > -90:
playerRot -= playerVelRot
# draw sprites
SCREEN.blit(IMAGES['background'], (0,0))
for uPipe, lPipe in zip(upperPipes, lowerPipes):
SCREEN.blit(IMAGES['pipe'][0], (uPipe['x'], uPipe['y']))
SCREEN.blit(IMAGES['pipe'][1], (lPipe['x'], lPipe['y']))
SCREEN.blit(IMAGES['base'], (basex, BASEY))
showScore(score)
playerSurface = pygame.transform.rotate(IMAGES['player'][1], playerRot)
SCREEN.blit(playerSurface, (playerx,playery))
FPSCLOCK.tick(FPS)
pygame.display.update()
def playerShm(playerShm):
"""oscillates the value of playerShm['val'] between 8 and -8"""
if abs(playerShm['val']) == 8:
playerShm['dir'] *= -1
if playerShm['dir'] == 1:
playerShm['val'] += 1
else:
playerShm['val'] -= 1
def getRandomPipe():
"""returns a randomly generated pipe"""
# y of gap between upper and lower pipe
gapY = random.randrange(0, int(BASEY * 0.6 - PIPEGAPSIZE))
gapY += int(BASEY * 0.2)
pipeHeight = IMAGES['pipe'][0].get_height()
pipeX = SCREENWIDTH + 10
return [
{'x': pipeX, 'y': gapY - pipeHeight}, # upper pipe
{'x': pipeX, 'y': gapY + PIPEGAPSIZE}, # lower pipe
]
def showScore(score):
"""displays score in center of screen"""
scoreDigits = [int(x) for x in list(str(score))]
totalWidth = 0 # total width of all numbers to be printed
for digit in scoreDigits:
totalWidth += IMAGES['numbers'][digit].get_width()
Xoffset = (SCREENWIDTH - totalWidth) / 2
for digit in scoreDigits:
SCREEN.blit(IMAGES['numbers'][digit], (Xoffset, SCREENHEIGHT * 0.1))
Xoffset += IMAGES['numbers'][digit].get_width()
def checkCrash(player, upperPipes, lowerPipes):
"""returns True if player collders with base or pipes."""
pi = player['index']
player['w'] = IMAGES['player'][0].get_width()
player['h'] = IMAGES['player'][0].get_height()
# if player crashes into ground
if player['y'] + player['h'] >= BASEY - 1:
return [True, True]
else:
playerRect = pygame.Rect(player['x'], player['y'],
player['w'], player['h'])
pipeW = IMAGES['pipe'][0].get_width()
pipeH = IMAGES['pipe'][0].get_height()
for uPipe, lPipe in zip(upperPipes, lowerPipes):
# upper and lower pipe rects
uPipeRect = pygame.Rect(uPipe['x'], uPipe['y'], pipeW, pipeH)
lPipeRect = pygame.Rect(lPipe['x'], lPipe['y'], pipeW, pipeH)
# player and upper/lower pipe hitmasks
pHitMask = HITMASKS['player'][pi]
uHitmask = HITMASKS['pipe'][0]
lHitmask = HITMASKS['pipe'][1]
# if bird collided with upipe or lpipe
uCollide = pixelCollision(playerRect, uPipeRect, pHitMask, uHitmask)
lCollide = pixelCollision(playerRect, lPipeRect, pHitMask, lHitmask)
if uCollide or lCollide:
return [True, False]
return [False, False]
def pixelCollision(rect1, rect2, hitmask1, hitmask2):
"""Checks if two objects collide and not just their rects"""
rect = rect1.clip(rect2)
if rect.width == 0 or rect.height == 0:
return False
x1, y1 = rect.x - rect1.x, rect.y - rect1.y
x2, y2 = rect.x - rect2.x, rect.y - rect2.y
for x in xrange(rect.width):
for y in xrange(rect.height):
if hitmask1[x1+x][y1+y] and hitmask2[x2+x][y2+y]:
return True
return False
def getHitmask(image):
"""returns a hitmask using an image's alpha."""
mask = []
for x in xrange(image.get_width()):
mask.append([])
for y in xrange(image.get_height()):
mask[x].append(bool(image.get_at((x,y))[3]))
return mask
def sigmoid(z):
"""The sigmoid function."""
return 1.0/(1.0+np.exp(-z))
def sigmoid_prime(z):
"""Derivative of the sigmoid function."""
return sigmoid(z)*(1-sigmoid(z))
if __name__ == '__main__':
main()