-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
426 lines (313 loc) · 13.8 KB
/
main.py
File metadata and controls
426 lines (313 loc) · 13.8 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import sys
from time import sleep
from threading import Thread
import pygame
from math import pi, sqrt, sin, cos
from random import choices, uniform, random, randint
from playsound3 import playsound
from Button import Button
from TranslationModule import TranslatedString as tStr
class ConkerModifier:
def __init__(self, name, mod_type, chance, multiplier):
self.name = name
self.type = mod_type
self.chance = chance
self.multiplier = multiplier
common = ConkerModifier("common", "rarity", 80, 1)
rare = ConkerModifier("rare", "rarity", 15, 1.5)
ultra = ConkerModifier("ultra", "rarity", 5, 2.5)
single = ConkerModifier("single", "amount", 80, 1)
double = ConkerModifier("double", "amount", 15, 2)
triple = ConkerModifier("triple", "amount", 5, 3)
RARITIES = {"common": common.chance, "rare": rare.chance, "ultra": ultra.chance}
MULTIPLIERS = {"single": single.chance, "double": double.chance, "triple": triple.chance}
class Tree:
def __init__(self):
self.growth_time = 5
self.max_tree_conker_amount = 17
self.new_conker_appear_time = 1
self.conkers_on_tree: list[Conker] = []
tree = Tree()
class Conker:
def __init__(self):
self.rarity = choices(
list(RARITIES.keys()),
weights=list(RARITIES.values())
)[0]
self.multiplier = choices(
list(MULTIPLIERS.keys()),
weights=list(MULTIPLIERS.values())
)[0]
angle = uniform(0, 2 * pi)
radius = 0.53 * HEIGHT / 2
r = radius * sqrt(random())
x = r * cos(angle)
y = r * sin(angle)
self.position = (WIDTH/2 + x, HEIGHT*2/5 + y)
self.size = randint(60, 75) # TODO: (later) upgrade: "Make the conkers smaller. Makes more fit on the tree"
self.actual_size = 0
# self.rotation = randint(0, 359)
self.rotation = 0 # TODO: looks weird when rotated, because of the shadow. change this
self.surface = pygame.transform.rotate(pygame.transform.scale(conker_t, (self.size, self.size)), self.rotation)
self.surface_pos = [self.position[0]-self.actual_size/2, self.position[1]-self.actual_size/2]
self.growing = True
self.grow_time = tree.growth_time + randint(0, 2)
self.spawn_time = pygame.time.get_ticks()
def update_surface_pos(self):
self.surface_pos = [self.position[0]-self.actual_size/2, self.position[1]-self.actual_size/2]
def __str__(self):
return f"Conker: rarity={self.rarity}, multiplier={self.multiplier}, growing={self.growing}."
def spawn_conker(v=False):
if not len(tree.conkers_on_tree) < tree.max_tree_conker_amount: # check if there is space on the tree to grow
if v: print("Not enough space on the tree")
return
if v: print("Waiting for conker to grow.")
wiggle_room = 15
new_conker = Conker()
new_conker_rect = pygame.Rect(
new_conker.surface_pos[0]-wiggle_room,
new_conker.surface_pos[1]-wiggle_room,
new_conker.surface.get_width()+wiggle_room,
new_conker.surface.get_height()+wiggle_room) # this sucks, but works
found_spot = False
while not found_spot and not tree.conkers_on_tree == []: # genuinely what the fuck. TODO: fix it when the tree is full
for c in tree.conkers_on_tree:
c_rect = pygame.Rect(c.surface_pos[0],
c.surface_pos[1],
c.surface.get_width(),
c.surface.get_height()) # this sucks, but works
if new_conker_rect.colliderect(c_rect):
new_conker = Conker()
new_conker_rect = pygame.Rect(
new_conker.surface_pos[0]-wiggle_room,
new_conker.surface_pos[1]-wiggle_room,
new_conker.surface.get_width()+wiggle_room,
new_conker.surface.get_height()+wiggle_room) # this sucks, but works
break
else:
found_spot = True
tree.conkers_on_tree.append(new_conker)
sleep(new_conker.grow_time)
playsound("sounds/conker_grow.mp3", block=False)
if v: print("Conker finished growing.")
new_conker.growing = False
if v: print(new_conker)
def update(): # function to update variables and other things.
global RARITIES, MULTIPLIERS
RARITIES = {"common": common.chance, "rare": rare.chance, "ultra": ultra.chance}
MULTIPLIERS = {"single": single.chance, "double": double.chance, "triple": triple.chance}
def rotate_with_pivot(surface: pygame.Surface | pygame.Rect, angle: int, pivot_pos: tuple[int, int]):
if isinstance(surface, pygame.Surface):
rect = surface.get_rect()
else:
rect = surface
rect_points = (rect.topleft, rect.topright, rect.bottomleft, rect.bottomright)
rotated_points = []
for point in rect_points:
rotated_points.append((
pivot_pos[0] + (point[0] - pivot_pos[0]) * cos(angle) - (point[1] - pivot_pos[1]) * sin(angle),
pivot_pos[0] + (point[0] - pivot_pos[0]) * sin(angle) + (point[1] - pivot_pos[1]) * cos(angle),
))
# x_new = px + (x - px) * cos(θ) - (y - py) * sin(θ)
# y_new = py + (x - px) * sin(θ) + (y - py) * cos(θ)
return pygame.Rect(rect_points[0][0], rect_points[0][1], rect_points[1][0]-rect_points[0][0], rect_points[2][1]-rect_points[0][1]) # T_T
pygame.init()
# textures
background_day_t = pygame.image.load("images/background_day.png")
menu_background_t = pygame.image.load("images/menu_background.png")
settings_background_t = pygame.image.load("images/settings_background.png")
sun_t = pygame.image.load("images/sun.png")
tree_ground_t = pygame.image.load("images/tree_ground.png")
shadow_t = pygame.image.load("images/shadow.png")
conker_t = pygame.image.load("images/conker/conker.png")
conker_left_t = pygame.image.load("images/conker/conker_left.png")
conker_right_t = pygame.image.load("images/conker/conker_right.png")
sconker_logo_t = pygame.image.load("images/sconker_logo_no_bg.png")
# resolution
WIDTH = pygame.display.Info().current_w
HEIGHT = pygame.display.Info().current_h
FPS = 120
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.NOFRAME)
transition_screen = pygame.Surface((WIDTH, HEIGHT))
transition_screen.set_alpha(0)
pygame.display.set_caption("Sconker")
with open("files/settings", "r") as f:
language = f.readlines()[0].split("language=")[1]
languages_list = {
"en": "English",
"ru": "Русский",
"de": "Deutsch"
}
clock = pygame.time.Clock()
run = True
fullscreen = True
scene = 0
"""
0 - menu
1 - game
2 - settings
"""
def color_fade_transition(surface, color: tuple[int, int, int] = (0, 0, 0), time: int | float = 1, mid_callable=None):
"""
makes a fade transition.\n
don't put this on your main surface, it will change the opacity
:param surface: surface that the fade animation will happen on
:param color: color of the transition
:param time: time (in seconds)
:param mid_callable: callable that will happen right in the middle of the fade animation
:return:
"""
def fade():
nonlocal color
surface.fill(color)
for i in range(85):
surface.set_alpha(i*3)
sleep(time / 255)
if callable(mid_callable):
mid_callable()
for i in range(85):
surface.set_alpha(255 - i*3)
sleep(time / 255)
fade_thread = Thread(daemon=True, target=fade)
fade_thread.start()
def on_play_click():
global scene
scene = 1
def on_settings_click():
def change_scene():
global scene
scene = 2
color_fade_transition(transition_screen, mid_callable=change_scene, time=1)
def on_exit_click():
def quit_game():
global run
run = False
color_fade_transition(transition_screen, mid_callable=quit_game, time=1)
def on_back_click():
def change_scene():
global scene
scene = 0
color_fade_transition(transition_screen, mid_callable=change_scene, time=1)
def on_language_click():
global language, languages_list, language_button
current_language_index = list(languages_list.keys()).index(language)
try:
language = list(languages_list.keys())[current_language_index+1]
except IndexError:
language = list(languages_list.keys())[0]
language_button.text = languages_list[language]
with open("files/settings", "r") as f:
lines = f.readlines()
lines[0] = f"language={language}"
init_buttons()
with open("files/settings", "w") as f:
f.writelines(lines)
play_button: Button | None = None
settings_button: Button | None = None
exit_button: Button | None = None
back_button: Button | None = None
language_button = Button(
screen, pygame.Rect(0.4*WIDTH, 0.2*HEIGHT, WIDTH*0.2, HEIGHT*0.1), [13, 143, 13], on_language_click,
font_path="files/Oswald-VariableFont_wght.ttf", btn_text=languages_list[language], border_thickness=15,
corner_radius=15, border_color=[9, 97, 9]
)
def init_buttons():
global play_button, settings_button, exit_button, back_button, language_button
play_button = Button(
screen, pygame.Rect(0.4*WIDTH, 0.5*HEIGHT, WIDTH*0.2, HEIGHT*0.1), [13, 143, 13], on_play_click,
font_path="files/Oswald-VariableFont_wght.ttf", btn_text=tStr("$/menu_play/", language).t(), border_thickness=15,
corner_radius=15, border_color=[9, 97, 9]
)
settings_button = Button(
screen, pygame.Rect(0.4 * WIDTH, 0.65 * HEIGHT, WIDTH * 0.2, HEIGHT * 0.1), [117, 117, 117], on_settings_click,
font_path="files/Oswald-VariableFont_wght.ttf", btn_text=tStr("$/menu_settings/", language).t(),
border_thickness=15,
corner_radius=15, border_color=[90, 90, 90]
)
exit_button = Button(
screen, pygame.Rect(0.4 * WIDTH, 0.8 * HEIGHT, WIDTH * 0.2, HEIGHT * 0.1), [173, 24, 27], on_exit_click,
font_path="files/Oswald-VariableFont_wght.ttf", btn_text=tStr("$/menu_exit/", language).t(),
border_thickness=15,
corner_radius=15, border_color=[125, 20, 22]
)
back_button = Button(
screen, pygame.Rect(0.02*WIDTH, 0.02*WIDTH, WIDTH*0.2, HEIGHT*0.1), [173, 24, 27], on_back_click,
font_path="files/Oswald-VariableFont_wght.ttf", btn_text=tStr("$/menu_back/", language).t(), border_thickness=15,
corner_radius=15, border_color=[125, 20, 22]
)
# language button not initialized, because it is done seperately.
#
# language_button = Button(
# screen, pygame.Rect(0.4*WIDTH, 0.2*HEIGHT, WIDTH*0.2, HEIGHT*0.1), [13, 143, 13], on_language_click,
# font_path="files/Oswald-VariableFont_wght.ttf", btn_text=languages_list[language], border_thickness=15,
# corner_radius=15, border_color=[9, 97, 9]
# )
init_buttons()
while run:
if scene == 0:
bg = screen.blit(pygame.transform.scale(
menu_background_t, (WIDTH, HEIGHT)), (0, 0))
title = screen.blit(pygame.transform.scale(
sconker_logo_t, (WIDTH*0.6, WIDTH*0.21531)), (WIDTH*0.2, HEIGHT*0.03))
play_button.handle()
settings_button.handle()
exit_button.handle()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif scene == 1:
spawn_conker_thread = Thread(target=lambda: spawn_conker(), daemon=True)
bg = screen.blit(pygame.transform.scale(
background_day_t, (WIDTH, HEIGHT)), (0, 0)) # TODO: replace with day/night cycle
sun_size = 150
sun_pos = (WIDTH/4*3, HEIGHT/5*1) # TODO: make sun move on an arc, change it to moon when it's nighttime
sun = screen.blit(pygame.transform.scale(
sun_t, (sun_size, sun_size)), sun_pos)
tree_ground = screen.blit(pygame.transform.scale(
tree_ground_t, (WIDTH, HEIGHT)), (0, 0))
for conker in tree.conkers_on_tree:
# shadow for the conker to see better
ticks = pygame.time.get_ticks()
if conker.growing:
grown_percent = (ticks - conker.spawn_time) / (conker.grow_time * 1000)
else:
grown_percent = 1
shadow_size = conker.size*1.7*grown_percent
screen.blit(pygame.transform.scale(
shadow_t, (shadow_size, shadow_size)
), (conker.position[0]-shadow_size/2, conker.position[1]-shadow_size/2))
# the conker
if conker.growing:
conker.actual_size = conker.size*grown_percent
conker.update_surface_pos()
screen.blit(pygame.transform.rotate(
pygame.transform.scale(conker_t, (
conker.actual_size,
conker.actual_size)
), conker.rotation), conker.surface_pos)
else:
screen.blit(conker.surface, conker.surface_pos)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r: # TODO: remove this later
spawn_conker_thread.start()
if event.key == pygame.K_ESCAPE:
scene = 0
elif scene == 2: # settings
bg = screen.blit(pygame.transform.scale(
settings_background_t, (WIDTH, HEIGHT)), (0, 0))
back_button.handle()
language_label = pygame.font.Font("files/Oswald-VariableFont_wght.ttf", int(WIDTH/20)).render(
tStr("$/settings_language/:", language).t(),
True, (255, 255, 255))
screen.blit(language_label, (WIDTH*0.5-language_label.get_width()//2, HEIGHT*0.05))
language_button.handle()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.blit(transition_screen, (0, 0))
pygame.display.update()
clock.tick(FPS)
pygame.quit()