-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappy.py
executable file
·159 lines (128 loc) · 5.32 KB
/
flappy.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
import pygame
from pygame.locals import *
from random import randint
class button:
def __init__(self, color, x, y, width, height, text=""):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
def draw(self, win, outline=None):
# Call this method to draw the button on the screen
if outline:
pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)
if self.text != "":
font = pygame.font.SysFont("comicsans", 60)
text = font.render(self.text, 1, (0, 0, 0))
win.blit(
text,
(self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)),
)
def isOver(self, pos):
# Pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
class FlappyBoy:
def __init__(self):
# the name of the game and the icon
pygame.display.set_caption("Flappy Boy")
self.FlappyBoyIMG = pygame.image.load("utils/pers.png")
pygame.display.set_icon(self.FlappyBoyIMG)
# Display game window
size = 700, 500
self.window = pygame.display.set_mode(size, pygame.RESIZABLE)
self.screen = pygame.display.get_surface()
# background
self.fond = pygame.image.load("utils/bg.png")
# personage
self.persIMG = pygame.image.load("utils/pers.png")
# background music
pygame.mixer.music.load("utils/background.mp3")
# define colors
self.black = (0, 0, 0)
self.white = (255, 255, 255)
self.green = (0, 200, 0)
self.red = (200, 0, 0)
self.blue = (65, 111, 194)
self.bright_green = (0, 255, 0)
self.bright_red = (255, 0, 0)
# variables initialisation
self.x = 350
self.y = 250
self.x_speed = 0
self.y_speed = 0
self.ground = 477
self.xloc = 700
self.yloc = 0
self.xsize = 70
self.ysize = randint(0, 350)
self.space = 150
self.obspeed = 2.5
self.score = 0
return
def obstacle(self, xloc, yloc, xsize, ysize):
pygame.draw.rect(self.screen, self.blue, [xloc, yloc, xsize, ysize])
pygame.draw.rect(self.screen, self.blue, [xloc, int(yloc + ysize + self.space), xsize, ysize + 500])
return
def game_score(self, score):
font = pygame.font.SysFont(None, 75)
text = font.render("Score:" + str(score), True, self.black)
self.screen.blit(text, [0, 0])
return
def game_personage(self, x, y):
self.window.blit(self.FlappyBoyIMG, (x, y))
return
def text_objects(self, text, font):
textSurface = font.render(text, True, self.black)
return textSurface, textSurface.get_rect()
def gameover(self):
font = pygame.font.SysFont(None, 75)
text = font.render("GAME OVER", True, self.red)
self.screen.blit(text, [200, 250])
pygame.mixer.music.stop()
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 150 + 120 > mouse[0] > 150 and 380 + 60 > mouse[1] > 380:
pygame.draw.rect(self.screen, self.bright_green, (150, 380, 120, 60))
else:
pygame.draw.rect(self.screen, self.green, (150, 380, 120, 60))
smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf1, textRect1 = self.text_objects("RESTART", smallText)
textRect1.center = ((150 + (120 / 2)), (380 + (60 / 2)))
self.screen.blit(textSurf1, textRect1)
if 460 + 120 > mouse[0] > 460 and 380 + 60 > mouse[1] > 380:
pygame.draw.rect(self.screen, self.bright_red, (460, 380, 120, 60))
if click[0] == 1:
pygame.quit()
else:
pygame.draw.rect(self.screen, self.red, (460, 380, 120, 60))
if click[0] == 1:
print("restart")
textSurf2, textRect2 = self.text_objects("EXIT", smallText)
textRect2.center = ((460 + (120 / 2)), (380 + (60 / 2)))
self.screen.blit(textSurf2, textRect2)
return
def main_game(self):
# start background music
pygame.mixer.music.play()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_speed = -10
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
y_speed = 5
# background fond
self.screen.blit(self.fond, (0, 0))
self.obstacle(self.xloc, self.yloc, self.xsize, self.ysize)
self.game_personage(self.x, self.y)
self.game_score(self.score)
return