-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
104 lines (92 loc) · 3.97 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
import random
import pygame
from pipes import Pipes
import numpy as np
from bird.Bird import Bird
class flappyBird:
def __init__(self):
pygame.init()
self.Backgrounds = [ 'Sky.png' ] #'WinterMountain.png', 'Mountain.png', 'Basic.png'
self.gameWidth = 350
self.indexBackground = random.randint(0, len(self.Backgrounds)-1)
self.gameHeight = 500
self.gameDisplay = pygame.display.set_mode((self.gameWidth, self.gameHeight))
pygame.display.set_caption('FlappyBird')
self.background = pygame.image.load('Resources/Backgrounds/background'+self.Backgrounds[self.indexBackground])
self.fps = pygame.time.Clock()
self.score = 0
self.frameRate = 50
self.bird = Bird(self.gameWidth, self.gameHeight)
self.pipes = [Pipes(self.gameWidth, self.gameHeight,50),Pipes(self.gameWidth, self.gameHeight,300)]
def makeobjMsg(self, msg, fontD,color = (0, 0, 0)):
return fontD.render(msg, True, color), fontD.render(msg, True, color).get_rect()
def message(self, msg, color = (0, 0, 0), fontType = 'freesansbold.ttf', fontSize = 15, xpos = 10, ypos = 10):
fontDefination = pygame.font.Font(fontType, fontSize)
msgSurface, msgRectangle = self.makeobjMsg(msg, fontDefination, color)
msgRectangle = (xpos, ypos)
self.gameDisplay.blit(msgSurface, msgRectangle)
def closestPipe(self, bird):
thePipe = self.pipes[0]
x = self.gameWidth
for pipe in self.pipes:
if bird.x < pipe.x + pipe.pipeBreadth + 0.00001:
if abs(pipe.x + pipe.pipeBreadth - bird.x) < x:
thePipe = pipe
x = abs(pipe.x + pipe.pipeBreadth - bird.x)
return thePipe
def pauseGame(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
return
self.gameDisplay.fill((51, 51, 51))
self.message(msg = 'Use SpaceBar to jump',color = (255, 255, 255), fontSize = 30, xpos = self.gameWidth // 2 - 160, ypos = self.gameHeight // 2 - 35)
self.message(msg = 'Press S to start.!',color = (255, 255, 255), fontSize = 30, xpos = self.gameWidth // 2 - 120, ypos = self.gameHeight // 2)
pygame.display.update()
def play(self):
# move = 0.0
while not self.bird.dead:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: self.bird.jump()
#pygame.draw.line(gameDisplay, (255, 255, 255),(self.x, self.y), (px, py),width = 1 )
self.gameDisplay.blit(self.background, (0, 0))
self.bird.showBird(self.gameDisplay)
self.bird.fall()
self.fps.tick(self.frameRate)
self.message(msg = 'score : ' + str(self.score))
for pipe in self.pipes: pipe.showPipes(self.gameDisplay)
for pipe in self.pipes: pipe.move()
for pipe in self.pipes:
if pipe.offScreen():
pipe.reset()
someOneCrossed = 0
pipe = self.closestPipe(self.bird)
if pipe.isdead(self.bird):
self.bird.dead = True
pygame.draw.line(self.gameDisplay, (255, 0, 0),(self.bird.x, self.bird.y), (pipe.x + pipe.pipeBreadth, pipe.bottom_top_y))
pygame.draw.line(self.gameDisplay, (0, 0, 255),(self.bird.x, self.bird.y), (pipe.x + pipe.pipeBreadth, pipe.top_bottom_y + pipe.pipeLength))
pygame.draw.line(self.gameDisplay, (0, 0, 0),(self.bird.x, self.bird.y), (pipe.x, self.bird.y))
# pygame.draw.line(self.gameDisplay, (100, 10, 1),(bird.x, bird.y), (bird.x, 0),5)
# pygame.draw.line(self.gameDisplay, (1, 10, 100),(bird.x, bird.y), (bird.x, self.gameHeight),5)
# pygame.display.flip()
# print(bird.x , pipe.x + pipe.pipeBreadth,bird.x > pipe.x + pipe.pipeBreadth)
if self.bird.x >= (pipe.x + pipe.pipeBreadth):
someOneCrossed = 1
if someOneCrossed == 1: self.score += 1
pygame.display.update()
pygame.display.flip()
# print('dead')
def init_game():
game = flappyBird()
game.pauseGame()
game.play()
if __name__ == '__main__':
init_game()