-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.py
304 lines (235 loc) · 8.68 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
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
from itertools import filterfalse
import pygame
import os
import random
import sys
pygame.init()
SCREEN_HEIGHT=700
SCREEN_WIDTH=700
SCREEN=pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
RUNNING=[pygame.image.load(os.path.join("assets/mainchar","sprite_0.png")),
pygame.image.load(os.path.join("assets/mainchar","sprite_1.png")),
pygame.image.load(os.path.join("assets/mainchar","sprite_2.png"))]
FLOWER=[pygame.image.load(os.path.join("assets","flower.png"))]
ROCK=[pygame.image.load(os.path.join("assets","rock.png"))]
BCKGRD=[pygame.image.load(os.path.join("assets/road","sprite_0.png")),
pygame.image.load(os.path.join("assets/road","sprite_1.png")),
pygame.image.load(os.path.join("assets/road","sprite_2.png"))]
for i in range(len(RUNNING)):
RUNNING[i]=pygame.transform.scale(RUNNING[i],(140,140))
for i in range(len(BCKGRD)):
BCKGRD[i]=pygame.transform.scale(BCKGRD[i],(700,700))
ROCK[0]=pygame.transform.scale(ROCK[0],(50,50))
FLOWER[0]=pygame.transform.scale(FLOWER[0],(70,70))
class State:
def __init__(self):
self.main_menu = True
self.flower_game_start = False
self.stone_collision = False
self.flower_collision = False
class Menu:
def __init__(self):
self.bg_color = (255,255,255)
self.text_color = (50,50,50)
self.font_50 = pygame.font.Font("assets/font.otf", 50)
self.font_80 = pygame.font.Font("assets/font.otf", 80)
self.start_image = self.font_50.render("Press Space To Start" , True , self.text_color , self.bg_color)
self.start_image_rect = self.start_image.get_rect()
self.mp_x = SCREEN_WIDTH/2 - self.start_image_rect[2]/2
self.mp_y = 2*SCREEN_HEIGHT/3 - self.start_image_rect[3]/2
self.game_name_image = self.font_80.render("Runn" , True , self.text_color , self.bg_color)
self.game_name_image_rect = self.game_name_image.get_rect()
self.game_name_x = SCREEN_WIDTH/2 - self.game_name_image_rect[2]/2
self.game_name_y = SCREEN_HEIGHT/3 - self.game_name_image_rect[3]/2
def draw_mainmenu(self , SCREEN):
SCREEN.blit(self.game_name_image , [self.game_name_x , self.game_name_y])
SCREEN.blit(self.start_image , [self.mp_x , self.mp_y])
class mainCharacter:
X_POS=280
Y_POS=450
def __init__(self):
self.run_img=RUNNING
self.run=True
self.step_index=0
self.image=self.run_img[0]
self.rect=self.image.get_rect()
self.rect.x=self.X_POS
self.rect.y=self.Y_POS
def update(self,userInput):
if self.run:
self.run_func()
if self.step_index ==3:
self.step_index=0
if userInput[pygame.K_UP]:
self.run=True
self.X_POS=280
elif userInput[pygame.K_LEFT]:
self.run=True
self.X_POS=130
elif userInput[pygame.K_RIGHT]:
self.run=True
self.X_POS=430
def run_func(self):
self.image=self.run_img[self.step_index]
self.rect=self.image.get_rect()
self.rect.x=self.X_POS
self.rect.y=self.Y_POS
self.step_index+=1
self.run=True
def draw(self,SCREEN):
SCREEN.blit(self.image,(self.rect.x,self.rect.y))
class background:
X_POS=0
Y_POS=0
def __init__(self):
self.run_img=BCKGRD
self.run=True
self.step_index=0
self.image=self.run_img[0]
self.rect=self.image.get_rect()
self.rect.x=self.X_POS
self.rect.y=self.Y_POS
def update(self):
if self.run:
self.run_func()
if self.step_index ==3:
self.step_index=0
def run_func(self):
self.image=self.run_img[self.step_index]
self.rect=self.image.get_rect()
self.rect.x=self.X_POS
self.rect.y=self.Y_POS
pygame.time.Clock().tick(10)
self.step_index+=1
self.run=True
def draw(self,SCREEN):
SCREEN.blit(self.image,(self.rect.x,self.rect.y))
def score():
global points, game_speed, collisions, scoregain
global initial
font=pygame.font.Font('freesansbold.ttf',20)
points=points+1
text=font.render("Distance Covered:"+str(points),True,(100,0,50))
textRect = text.get_rect()
textRect.center=(500,20)
SCREEN.blit(text,textRect)
text=font.render("Collisions:"+str(collisions),True,(100,0,50))
textRect = text.get_rect()
textRect.center=(500,40)
SCREEN.blit(text,textRect)
text=font.render("Score:"+str(scoregain),True,(100,0,50))
textRect = text.get_rect()
textRect.center=(500,60)
SCREEN.blit(text,textRect)
text=font.render("Total:"+str((scoregain*5)+int(points/3)-(collisions*10)),True,(100,0,0))
textRect = text.get_rect()
textRect.center=(300,20)
SCREEN.blit(text,textRect)
class Obstacle:
def __init__(self, image):
self.image = image
self.rect = self.image[0].get_rect()
self.rect.y = SCREEN_HEIGHT
def update(self):
game_speed2=random.randint(10,25)
self.rect.y += game_speed
if self.rect.y > 500:
obstacles.pop()
def draw(self, SCREEN):
SCREEN.blit(self.image[0], self.rect)
class NOTObstacle:
def __init__(self, image):
self.image = image
self.rect = self.image[0].get_rect()
self.rect.y = SCREEN_HEIGHT
def update(self):
game_speed2=random.randint(10,50)
self.rect.y += game_speed2
if self.rect.y > 500:
gainers.pop()
def draw(self, SCREEN):
SCREEN.blit(self.image[0], self.rect)
class Stone(Obstacle):
def __init__(self, image):
super().__init__(image)
self.rect.y = 0
self.rect.x = random.choice([210,340,490])
class Flower(NOTObstacle):
def __init__(self, image):
super().__init__(image)
self.rect.y = 0
self.rect.x = random.choice([210,340,490])
def main():
global game_speed, xposbg,yposbg,points,obstacles,collisions,scoregain, gainers
points=0
collisions=0
scoregain=0
obstacles=[]
gainers=[]
game_speed=14
run=True
clock=pygame.time.Clock()
player=mainCharacter()
bg=background()
state = State()
menu = Menu()
pygame.display.set_caption("Runn")
while run:
pygame.event.get()
SCREEN.fill((255,255,255))
userInput=pygame.key.get_pressed()
if state.flower_game_start==True:
bg.draw(SCREEN)
bg.update()
score()
if len(obstacles) == 0:
obstacles.append(Stone(ROCK))
if len(gainers) == 0:
gainers.append(Flower(FLOWER))
for obstacle in obstacles:
obstacle.draw(SCREEN)
obstacle.update()
if player.rect.colliderect(obstacle.rect):
#pygame.draw.rect(SCREEN,(255,0,0),player.rect,2)
if state.stone_collision == False:
collisions+=1
state.stone_collision = True
continue
else:
state.stone_collision = False
#print(collisions)
for fl in gainers:
fl.draw(SCREEN)
fl.update()
if player.rect.colliderect(fl.rect):
#pygame.draw.rect(SCREEN,(0,255,0),player.rect,2)
if state.flower_collision == False:
scoregain+=1
state.flower_collision = True
continue
else:
state.flower_collision = False
player.draw(SCREEN)
player.update(userInput)
elif state.main_menu==True:
menu.draw_mainmenu(SCREEN)
pygame.display.update()
# enable quit functionality in pygame
for event in pygame.event.get():
run = False if event.type == pygame.QUIT else True
if event.type == pygame.KEYDOWN:
if state.main_menu==True:
if event.key == pygame.K_SPACE:
state.main_menu = False
state.flower_game_start = True
if event.key == pygame.K_ESCAPE:
sys.exit()
if state.flower_game_start == True:
if event.key == pygame.K_ESCAPE:
state.main_menu = True
state.flower_game_start = False
points=0
collisions=0
scoregain=0
if __name__ == '__main__':
main()