-
Notifications
You must be signed in to change notification settings - Fork 8
/
Game.py
178 lines (146 loc) · 5.35 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
import pygame
from Camera import Camera
from Player import Player
from Platform import Platform
from PlatformController import PlatformController
pygame.init()
from Constants import *
from Utils import *
game_display = pygame.display.set_mode(res)
pygame.display.set_caption(GAME_CAPTION)
black = (0,0,0)
blue = (0,0, 255)
white = (255,255,255)
def reinit():
global player
global platform_controller
global floor
global camera
player = Player()
platform_controller = PlatformController()
floor = Platform(0, SCREEN_HEIGHT-36, SCREEN_WIDTH, 36)
camera = Camera(player)
player = Player()
platform_controller = PlatformController()
floor = Platform(0, SCREEN_HEIGHT-36, SCREEN_WIDTH, 36)
arrow_image = load_image("arrow.png")
selected_option = 0.30
background = load_image('background.jpg')
camera = Camera(player)
game_state = 'Menu'
game_loop = True
clock = pygame.time.Clock()
fps = 60
while game_loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_loop = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
if game_state == 'Playing' or game_state == 'Game Over' or game_state == 'About':
game_state = 'Menu'
elif game_state == 'Game Over':
if event.key == pygame.K_SPACE:
reinit()
game_state = 'Playing'
elif game_state == "Menu": #----------------Menu Events-------------
if event.key == pygame.K_DOWN:
if selected_option<0.45:
selected_option+=0.10
else:
selected_option=0.30
elif event.key == pygame.K_UP:
if selected_option>0.35:
selected_option-=0.10
else:
selected_option=0.50
elif event.key == pygame.K_RETURN:
if selected_option < 0.35:
reinit()
game_state = 'Playing'
elif selected_option == 0.40:
game_state = 'About'
elif selected_option == 0.50:
game_loop = False
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_LEFT]:
player.vel_x -= player.acceleration
if player.vel_x < -player.max_vel_x:
player.vel_x = -player.max_vel_x
player.sprite_index_y = 2
elif keys_pressed[pygame.K_RIGHT]:
player.vel_x += player.acceleration
if player.vel_x > player.max_vel_x:
player.vel_x = player.max_vel_x
player.sprite_index_y = 1
else:
if player.vel_x < 0:
player.vel_x += player.acceleration
player.vel_x -= ICE_RESISTANCE
if player.vel_x > 0:
player.vel_x = 0
else:
player.vel_x -= player.acceleration
player.vel_x += ICE_RESISTANCE
if player.vel_x < 0:
player.vel_x = 0
if player.vel_y >= JUMP_VELOCITY/2:
player.sprite_index_y = 0
#---------------------------MENU----------------------------
if game_state == "Menu":
game_display.blit(background,(0,0))
game_display.blit(arrow_image, (MENU_START_X + ARROW_HALF_WIDTH, MENU_START_Y + SCREEN_HEIGHT*selected_option - ARROW_HALF_HEIGHT))
if pygame.font:
# transparent black rectangle
#s = pygame.Surface((SCREEN_WIDTH/2, round(SCREEN_HEIGHT/1.45)), pygame.SRCALPHA)
#s.fill((0,0,0,128))
#game_display.blit(s, (MENU_START_X, MENU_START_Y))
# menu title
message_display(game_display, "Icy Tower(clone)", 0, MENU_START_Y+round(SCREEN_HEIGHT*0.15), 60, white, True)
# menu items
message_display(game_display, "Play", 0, MENU_START_Y+round(SCREEN_HEIGHT*0.30), 50, white, True)
message_display(game_display, "About", 0, MENU_START_Y+round(SCREEN_HEIGHT*0.40), 50, white, True)
message_display(game_display, "Quit", 0, MENU_START_Y+round(SCREEN_HEIGHT*0.50), 50, white, True)
#-------------------------PLAYING---------------------------
elif game_state == 'Playing':
if keys_pressed[pygame.K_SPACE]:
if player.on_any_platform(platform_controller, floor):
player.sprite_index_y = 3
if player.vel_y >= JUMP_VELOCITY/2:
player.vel_y = -JUMP_VELOCITY
player.update()
player.combo()
player.collide_platform(floor,0)
platform_controller.collide_set(player)
platform_controller.score = player.score
camera.update(player.score)
platform_controller.generate_new_platforms(camera)
if player.fallen_off_screen(camera):
game_state = 'Game Over'
# game_display.fill(black)
# image = pygame.transform.scale(image, (800,desired_height))
game_display.blit(background,(0,0))
floor.draw(game_display, camera)
platform_controller.draw(game_display, camera)
player.draw(game_display, camera)
message_display(game_display, str(player.score), 25, 30, 36, white)
#------------------------GAME OVER--------------------------
elif game_state == 'Game Over':
game_display.blit(background,(0,0))
if pygame.font:
message_display(game_display, "GAME OVER", 0, 200, 70, white, True)
message_display(game_display, "Score: %d" % player.score, 0, 300, 50, white, True)
message_display(game_display, "Press SPACE to play again!", 0, 400, 50, white, True)
message_display(game_display, "Press ESC to return to menu!", 0, 500, 40, white, True)
#--------------------------ABOUT----------------------------
elif game_state == 'About':
game_display.blit(background,(0,0))
if pygame.font:
for line in ABOUT_MESSAGE:
message_display(game_display, line, 0, MENU_START_Y+ABOUT_MESSAGE.index(line)*35, 30, white, True)
message_display(game_display, "Press ESC to return to menu!", 0, 500, 40, white, True)
#-----------------------------------------------------------
pygame.display.update()
clock.tick(fps)
pygame.quit()
quit()