-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
201 lines (177 loc) · 7.02 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
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
# game elements
import pygame
import random
class Bird:
def __init__(self, isHuman):
self.xPos = 120
self.yPos = 300
self.yVel = 0
self.gravity = 2.5
self.angle = 0
self.isHuman = isHuman
self.frame = 0
self.anim_delay = 0
self.afterFlapIter = 0
self.holdingJump = False
self.load_bird_images()
self.dead = False
def load_bird_images(self):
bird_colors = ["yellow" if self.isHuman else "blue"]
self.images = [
pygame.transform.scale(pygame.image.load(f"assets/sprites/{bird_colors[0]}bird-{flap}flap.png").convert(), (68, 48))
for flap in ["mid", "down", "mid", "up"]
]
self.finalImage = self.images[self.frame]
self.imageRect = self.finalImage.get_rect()
self.centerX = self.imageRect.centerx
self.centerY = self.imageRect.centery
self.rotatedRect = self.imageRect
def update(self, flap):
if flap:
if not self.holdingJump:
self.yVel = -20
self.angle = 30
self.anim_delay = 0
self.afterFlapIter = 0
self.holdingJump = True
else:
self.holdingJump = False
self.yVel += self.gravity
if self.yVel > 30:
self.yVel = 30
if self.afterFlapIter > 15:
self.angle -= 1
if self.angle < -90:
self.angle = -90
self.afterFlapIter += 1
self.anim_delay += 1
if self.anim_delay > 45:
self.frame = (self.frame + 1) % len(self.images)
self.anim_delay = 0
self.finalImage = pygame.transform.rotate(self.images[self.frame], self.angle).convert()
if self.yPos < -60:
self.yPos = -60
self.rotatedRect = self.finalImage.get_rect()
self.rotatedRect.center = (self.centerX + self.xPos, self.centerY + self.yPos)
self.yPos += self.yVel
return pygame.Rect(self.xPos + 8, self.yPos + 8, 52, 36)
def render(self, screen):
if not self.dead:
screen.blit(self.finalImage, self.rotatedRect)
class Pipe:
def __init__(self, yPos, isTopPipe, ySpace = 200):
self.xPos = 480
if not isTopPipe:
self.yPos = yPos
self.image = pygame.image.load("assets/sprites/pipe-green.png").convert()
else:
self.yPos = yPos - 320 * 2 - ySpace
self.image = pygame.image.load("assets/sprites/pipe-green-top.png").convert()
self.image = pygame.transform.scale(self.image, (104, 640))
def update(self, xSpeed, ySpeed):
self.xPos -= xSpeed
self.yPos += ySpeed
return pygame.Rect(self.xPos, self.yPos, 104, 640)
def render(self, screen):
screen.blit(self.image, (self.xPos, self.yPos))
class PipeManager:
def __init__(self, ySpace, easy):
pipeHeight = random.randint(280, 510)
if easy:
pipeHeight = 350
self.easy = easy
self.pipes = []
self.ySpace = ySpace
self.pipes.append(Pipe(pipeHeight, True, ySpace))
self.pipes.append(Pipe(pipeHeight, False))
self.placedPipe = False
self.alternate = 0
def update(self):
spawnedPipe = False
if self.pipes[0].xPos < 120:
if not self.placedPipe:
self.placedPipe = True
self.alternate += 1
pipeHeight = random.randint(280, 510)
if self.easy:
if self.alternate % 2 == 0:
pipeHeight = 260
else:
pipeHeight = 500
self.pipes.append(Pipe(pipeHeight, True, self.ySpace))
self.pipes.append(Pipe(pipeHeight, False))
spawnedPipe = True
pipeReturns = []
for pipe in self.pipes:
pipeReturns.append(pipe.update(10, 0))
if self.pipes[0].xPos < -104:
self.pipes.pop(0)
self.pipes.pop(0)
self.placedPipe = False
return [pipeReturns, spawnedPipe]
def render(self, screen):
for pipe in self.pipes:
pipe.render(screen)
class Background:
def __init__(self):
self.image = pygame.image.load("assets/sprites/background-day.png").convert()
self.image = pygame.transform.scale(self.image, (480, 853))
self.backgrounds = [0, 480]
def update(self, xSpeed):
for i in range(len(self.backgrounds)):
self.backgrounds[i] -= xSpeed
if self.backgrounds[0] <= -480:
self.backgrounds.pop(0)
self.backgrounds.append(480)
def render(self, screen):
for background in self.backgrounds:
screen.blit(self.image, (background, -100))
class Ground:
def __init__(self):
self.image = pygame.image.load("assets/sprites/base.png").convert()
self.image = pygame.transform.scale(self.image, (481, 160))
self.grounds = [0, 480]
def update(self, xSpeed):
for i in range(len(self.grounds)):
self.grounds[i] -= xSpeed
if self.grounds[0] <= -480:
self.grounds.pop(0)
self.grounds.append(480)
def render(self, screen):
for ground in self.grounds:
screen.blit(self.image, (ground, 640 - 80))
class Points:
def __init__(self):
self.points = 0
self.font = pygame.font.Font(None, 84)
self.text = str(self.points)
def update(self, pts):
self.points += pts
self.text = str(self.points)
def render(self, screen):
rendered_text = self.font.render(self.text, True, (0, 0, 0))
screen.blit(rendered_text, (480 // 2 - rendered_text.get_width() // 2, 60))
class Button:
def __init__(self, text, xPos, yPos):
self.font = pygame.font.Font(None, 36)
self.xPos = xPos
self.yPos = yPos
self.text = text
self.hitbox = pygame.Rect(0, 0, 0, 0)
self.rendered_text = self.font.render(self.text, True, (255, 255, 255))
def isHovered(self):
a = self.xPos < pygame.mouse.get_pos()[0] < self.xPos + self.hitbox.w + 8
b = self.yPos < pygame.mouse.get_pos()[1] < self.yPos + self.hitbox.h + 8
if a and b:
return True
else:
return False
def render(self, screen):
self.hitbox = pygame.Rect(self.xPos, self.yPos, self.rendered_text.get_width() + 2, self.rendered_text.get_height() + 2)
if not self.isHovered():
self.rendered_text = self.font.render(self.text, True, (255, 255, 255))
pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(self.xPos, self.yPos, self.rendered_text.get_width() + 8, 33), 2)
else:
self.rendered_text = self.font.render(self.text, True, (0, 0, 0))
pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(self.xPos, self.yPos, self.rendered_text.get_width() + 8, 33), 0)
screen.blit(self.rendered_text, (self.xPos + 4, self.yPos + 4))