-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
120 lines (94 loc) · 4.11 KB
/
main.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
import pygame
from pygame.locals import *
import sys
import Window
import Player
import Pipe
pygame.init()
class Main:
def __init__(self):
self.wd = Window.Window()
self.FPS = 30
self.FramesPerSecond = pygame.time.Clock()
self.themeSong = pygame.mixer.Sound("./assets/Theme.wav")
self.startSound = pygame.mixer.Sound("./assets/start.wav")
self.dieSound = pygame.mixer.Sound("./assets/die.wav")
self.dingSound = pygame.mixer.Sound("./assets/ding.wav")
self.flappSound = pygame.mixer.Sound("./assets/flapp.wav")
self.p = Player.Player(self.wd, self)
self.pipes = []
self.breakLoop = False
def start(self, first):
if first:
pygame.mixer.Sound.play(self.themeSong)
self.wd.window.fill(self.wd.WHITE)
self.wd.draw_bg()
self.wd.display_text(100, "Flappy Goin", self.wd.BLACK, self.wd.width/2, 100)
self.wd.display_text(50, "Press Space to start", self.wd.BLACK, self.wd.width/2, 400)
self.p.draw()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
quit()
if event.type == KEYDOWN:
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
pygame.quit()
quit()
elif keys[K_SPACE]:
pygame.mixer.stop()
pygame.mixer.Sound.play(self.startSound)
self.main()
self.wd.window.fill(self.wd.WHITE)
self.wd.update_bg()
self.wd.display_text(100, "Flappy Goin", self.wd.BLACK, self.wd.width/2, 100)
self.wd.display_text(50, "Press Space to start", self.wd.BLACK, self.wd.width/2, 400)
self.p.draw()
pygame.display.update()
self.FramesPerSecond.tick(self.FPS)
def main(self):
self.pipes.append(Pipe.Pipe(self.wd))
while not self.breakLoop:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
quit()
if event.type == KEYDOWN:
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
pygame.quit()
quit()
elif keys[K_SPACE] and self.p.dead:
pygame.mixer.Sound.play(self.startSound)
pygame.mixer.Sound.play(self.startSound)
main = Main()
main.start(False)
if not self.p.dead:
self.wd.window.fill(self.wd.WHITE)
self.wd.update_bg()
if self.pipes[-1].downPipeX <= self.wd.width - 400:
self.pipes.append(Pipe.Pipe(self.wd))
i = 0
for pipe in self.pipes:
if pipe.isOutOfScreen():
self.pipes.pop(i)
else:
pipe.update()
i += 1
self.p.update(self)
self.wd.display_text(100, f"{self.p.score}", self.wd.BLACK, self.wd.width/2, 100)
if self.p.y + self.p.heigth + self.p.width > self.wd.heigth:
self.p.die()
for closePipe in self.pipes:
if not closePipe.upRect.bottom < 2:
if pygame.Rect.colliderect(self.p.rect, closePipe.upRect): self.p.die(); continue
if pygame.Rect.colliderect(self.p.rect, closePipe.downRect): self.p.die(); continue
if self.p.x+self.p.heigth == closePipe.downPipeX + (closePipe.width/2):
self.p.score += 1
pygame.mixer.Sound.play(self.dingSound)
pygame.display.update()
self.FramesPerSecond.tick(self.FPS)
if __name__ == "__main__":
Game = Main()
Game.start(True)