-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
252 lines (206 loc) · 6.56 KB
/
main.py
File metadata and controls
252 lines (206 loc) · 6.56 KB
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
import pygame
import random
import sys
# --------------------
# INITIAL SETUP
# --------------------
pygame.init()
pygame.mixer.init()
# 🎥 Dev size (recorded & upscaled later)
WIDTH, HEIGHT = 540, 960
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Santa Ice Skate - Shorts")
clock = pygame.time.Clock()
FPS = 60
# --------------------
# LOAD ASSETS
# --------------------
def load_image(path, scale=None):
img = pygame.image.load(path).convert_alpha()
if scale:
img = pygame.transform.scale(img, scale)
return img
# --------------------
# IMAGES (⬆ BIGGER)
# --------------------
bg = load_image("assets/ice-track5.png", (WIDTH, HEIGHT))
santa_img = load_image("assets/santa-on-skate.png", (150, 180)) # ⬆ BIG
ice_block = load_image("assets/ice-block2.png", (90, 70)) # ⬆ BIG
snowman = load_image("assets/snowman.png", (95, 120)) # ⬆ BIG
coin = load_image("assets/christmas-coin.png", (55, 55)) # ⬆ BIG
candy = load_image("assets/candy-cane.png", (50, 85)) # ⬆ BIG
slip_fx = load_image("assets/ice-slip-effect.png", (180, 180)) # ⬆ BIG
# --------------------
# SOUNDS
# --------------------
music = pygame.mixer.Sound("assets/music.wav")
jump_snd = pygame.mixer.Sound("assets/jump.wav")
collect_snd = pygame.mixer.Sound("assets/collect.wav")
fail_snd = pygame.mixer.Sound("assets/fail.wav")
music.set_volume(0.35)
jump_snd.set_volume(0.7)
collect_snd.set_volume(0.7)
fail_snd.set_volume(0.9)
music.play(-1)
# --------------------
# BACKGROUND SCROLL
# --------------------
bg_y1 = 0
bg_y2 = -HEIGHT
base_speed = 7.5 # slightly faster
# --------------------
# PLAYER
# --------------------
GROUND_Y = HEIGHT - 260
santa = santa_img.get_rect()
santa.midbottom = (WIDTH // 2, GROUND_Y)
# 🔥 Fair Santa hitbox (still smaller than sprite)
santa_hitbox = santa.inflate(-45, -35)
speed_x = 13
jump_power = -22
gravity = 1.2
vel_y = 0
on_ground = True
# --------------------
# OBJECTS
# --------------------
obstacles = []
coins = []
spawn_timer = 0
score = 0
game_over = False
fx_rect = None
fail_played = False
# --------------------
# FONT (⬆ BIGGER)
# --------------------
font = pygame.font.SysFont("arial", 52, bold=True)
# --------------------
# RESET FUNCTION
# --------------------
def reset_game():
global obstacles, coins, spawn_timer, score, game_over
global vel_y, on_ground, fail_played, fx_rect
global bg_y1, bg_y2
obstacles.clear()
coins.clear()
spawn_timer = 0
score = 0
game_over = False
vel_y = 0
on_ground = True
fail_played = False
fx_rect = None
santa.midbottom = (WIDTH // 2, GROUND_Y)
santa_hitbox.center = santa.center
bg_y1 = 0
bg_y2 = -HEIGHT
music.play(-1)
# --------------------
# GAME LOOP
# --------------------
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if game_over and event.key == pygame.K_r:
reset_game()
keys = pygame.key.get_pressed()
if not game_over:
# --------------------
# SPEED (DYNAMIC)
# --------------------
current_speed = base_speed + score * 0.08
# --------------------
# BACKGROUND MOVE
# --------------------
bg_y1 += current_speed
bg_y2 += current_speed
if bg_y1 >= HEIGHT:
bg_y1 = -HEIGHT
if bg_y2 >= HEIGHT:
bg_y2 = -HEIGHT
# --------------------
# PLAYER MOVEMENT
# --------------------
if keys[pygame.K_LEFT]:
santa.x -= speed_x
if keys[pygame.K_RIGHT]:
santa.x += speed_x
if keys[pygame.K_SPACE] and on_ground:
vel_y = jump_power
on_ground = False
jump_snd.play()
vel_y += gravity
santa.y += vel_y
if santa.bottom >= GROUND_Y:
santa.bottom = GROUND_Y
vel_y = 0
on_ground = True
santa.x = max(30, min(WIDTH - santa.width - 30, santa.x))
santa_hitbox.center = santa.center
# --------------------
# SPAWN OBJECTS
# --------------------
spawn_timer += 1
if spawn_timer > 45:
spawn_timer = 0
lane_x = random.choice([WIDTH//4, WIDTH//2, WIDTH*3//4])
if random.random() < 0.6:
obj = random.choice([ice_block, snowman])
rect = obj.get_rect(midtop=(lane_x, -140))
rect.inflate_ip(-30, -30) # fair hitbox
obstacles.append((obj, rect))
else:
obj = random.choice([coin, candy])
rect = obj.get_rect(midtop=(lane_x, -90))
coins.append((obj, rect))
# --------------------
# MOVE OBJECTS
# --------------------
for obj, rect in obstacles:
rect.y += current_speed
for obj, rect in coins:
rect.y += current_speed
obstacles = [(o, r) for o, r in obstacles if r.y < HEIGHT + 200]
coins = [(o, r) for o, r in coins if r.y < HEIGHT + 150]
# --------------------
# COLLISIONS
# --------------------
for obj, rect in obstacles:
if santa_hitbox.colliderect(rect):
game_over = True
fx_rect = slip_fx.get_rect(center=santa.center)
if not fail_played:
fail_snd.play()
music.stop()
fail_played = True
for obj, rect in coins[:]:
if santa_hitbox.colliderect(rect):
coins.remove((obj, rect))
score += 1
collect_snd.play()
# --------------------
# DRAW
# --------------------
screen.blit(bg, (0, bg_y1))
screen.blit(bg, (0, bg_y2))
for obj, rect in obstacles:
screen.blit(obj, rect)
for obj, rect in coins:
screen.blit(obj, rect)
screen.blit(santa_img, santa)
# SCORE (RED, BIG, CLEAR)
score_text = font.render(str(score), True, (255, 0, 0))
screen.blit(score_text, (WIDTH//2 - score_text.get_width()//2, 20))
if game_over:
screen.blit(slip_fx, fx_rect)
over_text = font.render("GAME OVER", True, (255, 60, 60))
retry_text = font.render("PRESS R", True, (0, 255, 0))
screen.blit(over_text, (WIDTH//2 - over_text.get_width()//2, HEIGHT//2 - 50))
screen.blit(retry_text, (WIDTH//2 - retry_text.get_width()//2, HEIGHT//2 + 10))
pygame.display.update()