-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
284 lines (223 loc) · 9.11 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
import pygame
import pymunk
import math
import time
import random
import engine
from engine.render import Renderer
from engine.texture import Texture
from src.camera import Camera2D
from src.snowball import Snowball
from src.player import Player
from src.jackolantern import JackOLantern
from src.icecube import IceCube
from src.hole import Hole
from src.snow import SnowGrid
from src.wall import Wall
def reset_level():
global snow_grid, space, snowball, player, jackolanterns, icecubes, holes, objects
ARENA_SIZE = (3840, 2160)
snow_grid = SnowGrid(ARENA_SIZE)
space = pymunk.Space()
space.collision_bias = (1.0 - 0.5) ** 60
snowball = Snowball(space, (1920, 1080), 5, 0.05)
player = Player(space, (1920, 1080 - 50))
jackolanterns = [
JackOLantern(space, (600, 500)),
JackOLantern(space, (2600, 450)),
JackOLantern(space, (3300, 700)),
JackOLantern(space, (2000, 2000))
]
icecubes = []
for x in range(15):
pos = (random.randint(0, 3840), random.randint(0, 2160))
icecubes.append(IceCube(space, pos))
holes = [
Hole(space, (400, 1200)),
Hole(space, (3400, 1500)),
]
objects = [snowball, player] + jackolanterns + icecubes
for x in range(6):
pos = (128 * (9 + x) - 64, 2160 - 128 * 3 - 64)
objects.append(Wall(space, pos))
for x in range(6):
pos = (128 * (9 + x) - 64, 128 * 2 - 64)
objects.append(Wall(space, pos))
for x in range(4):
pos = (128 * (12 + x) - 64, 2160 - 128 * 6 - 64)
objects.append(Wall(space, pos))
for x in range(4):
pos = (128 * (17 + x) - 64, 2160 - 128 * 6 - 64)
objects.append(Wall(space, pos))
for x in range(4):
pos = (128 * (12 + x) - 64, 128 * 5 - 64)
objects.append(Wall(space, pos))
for x in range(7):
pos = (128 * (24 + x) - 64, 128 * 10 - 64)
objects.append(Wall(space, pos))
for x in range(6):
pos = (128 * (25 + x) - 64, 128 * 14 - 64)
objects.append(Wall(space, pos))
for y in range(3):
pos = (128 * 9 - 64, 2160 - 128 * (y + 4) - 64)
objects.append(Wall(space, pos))
for y in range(3):
pos = (128 * 9 - 64, 128 * (y + 3) - 64)
objects.append(Wall(space, pos))
objects.append(Wall(space, (128 * 12 - 64, 128 * 10 - 64)))
objects.append(Wall(space, (128 * 12 - 64, 128 * 6 - 64)))
for i in range(3840 // 128 + 2):
pos = (i * 128 - 64, -64)
objects.append(Wall(space, pos))
for i in range(3840 // 128 + 2):
pos = (i * 128 - 64, 2160 + 64)
objects.append(Wall(space, pos))
for j in range(2160 // 128 + 2):
pos = (-64, j * 128 - 64)
objects.append(Wall(space, pos))
for j in range(2160 // 128 + 2):
pos = (3840 + 64, j * 128 - 64)
objects.append(Wall(space, pos))
def main():
# initialize -------------------------------------------------- #
pygame.init()
# specify opengl version
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MAJOR_VERSION, 4)
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MINOR_VERSION, 1)
pygame.display.gl_set_attribute(
pygame.GL_CONTEXT_PROFILE_MASK, pygame.GL_CONTEXT_PROFILE_CORE)
# create window
WIN_SIZE = (1280, 720)
pygame.display.set_mode((WIN_SIZE), pygame.DOUBLEBUF | pygame.OPENGL, vsync=1)
renderer = Renderer()
renderer.set_clear_color((200, 200, 200, 255))
renderer.set_size(*WIN_SIZE)
camera = Camera2D(*WIN_SIZE)
camera.zoom = 1
# 0 = start screen, 1 = game, 2 = end screen
mode = 0
time = 0
# start screen init ---------------------------------------- #
title_gui_texture = Texture.from_path('src/imgs/TitleGui.png')
title_nogui_texture = Texture.from_path('src/imgs/TitleNoGui.png')
# end screen init ------------------------------------------ #
end_texture = Texture.from_path('src/imgs/End.png')
snowball_texture = Texture.from_path('src/imgs/Snowball/SnowBall0000.png')
snowball_texture.resize(snowball_texture.width * 2, snowball_texture.height * 2)
# game objects init ---------------------------------------- #
timer_window_texture = Texture.from_path('src/imgs/TimerWindow.png')
timer_window_texture.resize(timer_window_texture.width * 0.5, timer_window_texture.height * 0.5)
reset_level()
# grass_size = 64
# GRASS_DIM = (int(ARENA_SIZE[0] / grass_size), int(ARENA_SIZE[1] / grass_size))
# grass_texture = Texture.from_path('src/imgs/Grass.png')
ground_texture = Texture.from_path('src/imgs/Ground.png')
pygame.mixer.music.load('src/music/LevelMusic.wav')
end_sound = pygame.mixer.Sound('src/music/endsound.wav')
font = pygame.font.SysFont('Comic Sans MS', 30)
font_big = pygame.font.SysFont('Comic Sans MS', 50)
# game loop -------------------------------------------------- #
clock = engine.timer.Timer()
score = 0
pygame.display.set_caption("Snowball Shovler")
pygame.display.set_icon(pygame.image.load('src/imgs/Icon.png'))
running = True
while running:
# timing -------------------------------------------------- #
dt = clock.tick()
# framerate = clock.get_fps()
# check events -------------------------------------------------- #
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_RETURN:
if mode == 0:
mode = 1
time = 0
pygame.mixer.music.play(loops=-1)
elif mode == 2:
mode = 0
reset_level()
if mode == 0:
renderer.begin()
renderer.clear()
renderer.draw_texture(title_nogui_texture, (0, 0))
renderer.draw_texture(title_gui_texture, (0, 0))
renderer.end()
elif mode == 1:
time += dt
if time > 60 * 3 or player.is_dead or snowball.is_dead:
mode = 2
time = 0
pygame.mixer.music.stop()
score = snowball.volume / 1000
end_sound.play()
snowball.position = (640, 280 + snowball.shape.radius)
snowball.velocity = (0, 0)
# update -------------------------------------------------- #
keys = pygame.key.get_pressed()
move_vec = pymunk.Vec2d.zero()
if keys[pygame.K_UP]:
move_vec += 0, 1
if keys[pygame.K_DOWN]:
move_vec -= 0, 1
if keys[pygame.K_RIGHT]:
move_vec += 1, 0
if keys[pygame.K_LEFT]:
move_vec -= 1, 0
punt = keys[pygame.K_SPACE]
player.input(move_vec, punt)
# print(player.position)
camera.look_at(player.position)
for obj in objects:
if hasattr(obj, 'is_dead'):
if obj.is_dead:
objects.remove(obj)
for obj in objects:
obj.update(dt)
snow_grid.update_snow(snowball)
iterations = 2
for _ in range(iterations):
space.step(dt / iterations)
# blit -------------------------------------------------- #
renderer.begin(camera.get_transform())
renderer.clear()
# render grass
# for x in range(GRASS_DIM[0]):
# for y in range(GRASS_DIM[1]):
# pos = (x * grass_size, y * grass_size)
# renderer.draw_texture(grass_texture, pos)
renderer.draw_texture(ground_texture, (0, 0))
snow_grid.draw(renderer, camera)
for hole in holes:
hole.draw(renderer)
y_sorted = sorted(objects, key=lambda obj: obj.position.y, reverse=True)
for obj in y_sorted:
obj.draw(renderer)
renderer.end()
# draw gui -------------------------------------------------- #
renderer.begin()
renderer.draw_texture(timer_window_texture, (15, 620))
surf = font.render(f'Time: {(60 * 3 - time):.0f}', True, (0, 0, 0))
font_texture = Texture.from_pygame_surface(surf)
renderer.draw_texture(font_texture, (50, 640))
renderer.end()
elif mode == 2:
renderer.begin()
renderer.clear()
surf = font_big.render(f'Score: {score:.0f}', True, (0, 0, 0))
font_texture = Texture.from_pygame_surface(surf)
renderer.draw_texture(end_texture, (0, 0))
snowball.draw(renderer)
renderer.draw_texture(font_texture, (530, 130))
renderer.end()
# flip screen
pygame.display.flip()
if __name__ == "__main__":
import os
os.chdir(os.path.abspath(os.path.dirname(__file__)))
main()
pygame.quit()