-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
169 lines (124 loc) · 6 KB
/
test.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
import pygame
from sys import exit
from random import randint
import importlib
import time
#global start_time
pygame.init()
def game_main():
def display_score():
#current_time = int (pygame.time.get_ticks()/1000) - start_time
score_surface = test_font.render(('Score: '+ str(int(score/60))), False, (64,64,64))
score_rect = score_surface.get_rect (center = (400,50))
screen.blit(score_surface, score_rect)
return score
def obstacle_movement(obstacle_list):
if obstacle_list: #if there is something in the list
for i in obstacle_list:
if i.bottom==300:
screen.blit (snail_surface, i)
else:
screen.blit (fly_surface, i)
i.x -=5
obstacle_list = [obstacle for obstacle in obstacle_list if obstacle.x > -100]
return obstacle_list
else:
return []
def collisions (player, obstacles):
if obstacles:
for i in obstacles:
if player.colliderect(i):
return False
return True
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption ("Runner")
clock = pygame.time.Clock()
test_font = pygame.font.Font('font/Pixeltype.ttf',50)
game_active = True
score = 0
sky_surface = pygame.image.load ('graphics/Sky.png').convert()
ground_surface = pygame.image.load ('graphics/ground.png').convert()
#Obstacles
snail_surface = pygame.image.load ('graphics/snail/snail1.png').convert_alpha()
fly_surface = pygame.image.load('graphics/fly/fly1.png').convert_alpha()
obstacle_rect_list = []
player_surface = pygame.image.load ('graphics/player/player_walk_1.png').convert_alpha()
player_rect = player_surface.get_rect(midbottom= (80,300))
player_gravity = 0
#Game Over screen
player_stand = pygame.image.load ('graphics/player/player_stand.png').convert_alpha()
player_stand= pygame.transform.rotozoom(player_stand,0,2) #width and height of surface
player_stand_rect = player_stand.get_rect(center = (400,200))
game_name = test_font.render ('Pixel Runner', False, (111,196,169))
game_name_rect = player_stand.get_rect(center = (380,150))
game_message = test_font.render ('Press space to run', False, (111,196,169))
game_message_rect = game_message.get_rect(center = (400,320))
menu_message = test_font.render ('Return to Menu', False, (111,196,169))
menu_message_rect = menu_message.get_rect(center = (400, 370))
#Timer
obstacle_timer = pygame.USEREVENT + 1 # to avoid clash with existing in-built events
pygame.time.set_timer (obstacle_timer, 1500)
while True:
#score+=1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if game_active == True:
## if event.type == pygame.MOUSEMOTION:
## if player_rect.collidepoint (event.pos) :
## player_gravity = -20
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and player_rect.bottom>=300: #spacebar
player_gravity = -20
#score+=1
else:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
game_active = True
score = 0
else:
mouse_pos = pygame.mouse.get_pos()
#if event.type == pygame.MOUSEMOTION:
if menu_message_rect.collidepoint(mouse_pos) and event.type == pygame.MOUSEBUTTONDOWN:
## from menu import main_menu
## menu.main_menu()
## menu.close()
menu=importlib.import_module('menu')
#current_time-=current_time
# start_time = int(pygame.time.get_ticks()/1000)
menu.main_menu()
if event.type == obstacle_timer and game_active:
if randint(0,2):
obstacle_rect_list.append(snail_surface.get_rect(bottomright = (randint(900,1100),300)))
else:
obstacle_rect_list.append(fly_surface.get_rect(bottomright = (randint(900,1100),210)))
if game_active == True:
score += 1
screen.blit(sky_surface, (0,0))
screen.blit(ground_surface, (0,300))
score = display_score()
## if snail_rect.right<=0:
## snail_rect.left = 800
## screen.blit(snail_surface, snail_rect) --> not needed as imporved enemy spawn logic used
player_gravity+=0.8
player_rect.y += player_gravity
if player_rect.bottom>=300:
player_rect.bottom=300
screen.blit(player_surface, player_rect)
#obstacle movement
obstacle_rect_list = obstacle_movement(obstacle_rect_list)
game_active = collisions(player_rect,obstacle_rect_list)
else:
screen.fill((94,129,162))
screen.blit(player_stand, player_stand_rect)
obstacle_rect_list.clear()
player_rect.midbottom = (80,300) #in case we crash into a fly, reset player to ground
player_gravity = 0
score_message = test_font.render(('Score: '+ str(int(score/60))), False, (111,196,169))
score_rect = score_message.get_rect (center = (400,50))
screen.blit(score_message, score_rect)
screen.blit(game_message,game_message_rect)
screen.blit(game_name, game_name_rect)
screen.blit(menu_message, menu_message_rect)
pygame.display.update()
clock.tick(60)