Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
Latest commit
104 lines (74 loc) · 2.76 KB
/
Copy pathmain.py
File metadata and controls
104 lines (74 loc) · 2.76 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
importpygameaspg
importpygame_gui
fromcommonimportCOLORS, HEIGHT, WIDTH, FPS
classRunGame:
def__init__(self):
self.clock=pg.time.Clock()
self.screen_size= (WIDTH, HEIGHT)
self.buttons=list()
self.init_ui()
self.game_loop()
definit_ui(self):
pg.init()
pg.display.set_caption('Quick Start')
self.window_surface=pg.display.set_mode(self.screen_size)
self.background=pg.Surface(self.screen_size)
self.background.fill(COLORS["black"])
self.manager=pygame_gui.UIManager(self.screen_size)
self.set_buttons()
defset_buttons(self):
defoffset_button(top_left):
padding=80
top_left[1] +=padding
returntop_left
btn_width, btn_height=100, 50
top_left= [WIDTH//2-btn_width//2, btn_height]
dimensions= (btn_width, btn_height)
self.button=pg.Rect(top_left, dimensions)
self.btn_names= ['START', 'SETTINGS', 'OTHER']
forbtn_nameinself.btn_names:
btn=pygame_gui.elements.UIButton(relative_rect=self.button,
text=btn_name,
manager=self.manager)
self.buttons.append(btn)
self.button=pg.Rect(offset_button(top_left), dimensions)
defget_events(self):
"""
Returns necessary events for application. Packed in a dictionary.
"""
events=pg.event.get()
mouse_press=pg.mouse.get_pressed()
keys=pg.key.get_pressed()
mouse_pos=pg.mouse.get_pos()
raw_dt=self.clock.get_time()
dt=raw_dt*FPS
return {
"events": events,
"mouse press": mouse_press,
"keys": keys,
"mouse pos": mouse_pos,
"raw dt": raw_dt,
"dt": dt,
}
defgame_loop(self):
is_running=True
whileis_running:
#time_delta = clock.tick(FPS) / 1000.0
self.clock.tick(FPS)
events=self.get_events()
time_delta=events["dt"]
foreventinevents["events"]:
ifevent.type==pg.QUIT:
is_running=False
ifevent.type==pygame_gui.UI_BUTTON_PRESSED:
forbtninself.buttons:
ifevent.ui_element==btn:
print('Hello World!')
self.manager.process_events(event)
self.manager.update(time_delta)
self.window_surface.blit(self.background, (0, 0))
self.manager.draw_ui(self.window_surface)
pg.display.update()
RunGame()
# Close pygame
pg.quit()