-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
67 lines (56 loc) · 2.27 KB
/
app.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
import random
import pygame
from classes import mainmenu
icon_file_names = ['sand', 'water', 'stone', 'acid', 'plastic', 'fire', 'oil', 'iron', 'gold', 'copper', 'hydrogen', 'chlorine']
class App:
def __init__(self, width, height, fullscreen, vsync):
# Save the data passed into the function to variables
self.clock = pygame.time.Clock()
self.fps = 60
self.width = width
self.height = height
self.is_FS_enabled = fullscreen
self.is_vsync_enabled = vsync
self.scale = 1
self.hotbar_height = 100 * self.scale
# Initialize pygame
pygame.init()
# Window setup
if fullscreen:
self.screen = pygame.display.set_mode((width, height + self.hotbar_height), pygame.FULLSCREEN, vsync=int(vsync))
else:
self.screen = pygame.display.set_mode((width, height + self.hotbar_height), vsync=int(vsync))
self.active_simulation = None
self.ui = mainmenu.MainMenu(self)
self.run = True # Variable to determine if the app is running
pygame.display.set_caption("Sandbox")
icon_name = random.choice(icon_file_names)
icon_image = pygame.image.load(f"img/{icon_name}.png")
icon_surface = pygame.transform.scale(icon_image, (32, 32))
pygame.display.set_icon(icon_surface)
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
self.run = False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
click_pos = pygame.mouse.get_pos()
for button in self.ui.buttons:
if button.rect.collidepoint(click_pos[0], click_pos[1]):
button.click()
# if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
# print(pygame.mouse.get_pos())
def background(self):
self.screen.fill((0, 0, 0))
def update(self):
pygame.display.update()
def mainloop(self):
self.clock.tick(self.fps)
#print(self.clock.get_fps())
if self.run is False:
pygame.quit()
self.background()
self.ui.render()
self.update()
self.ui.events()
self.events()