-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui_example.py
executable file
·61 lines (47 loc) · 1.39 KB
/
ui_example.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
import pygame as pg
from ui_defs import *
# SILLY UI EXAMPLE: CLICK ON THE BUTTON AND IT JUMPS
def logic(ui):
#Basic function for relaying pygame events to UI
#Returns "alive" for the main loop
events=pg.event.get()
for e in events:
if e.type==pg.QUIT:
return False
if e.type==pg.MOUSEMOTION:
#If the mouse moves, look which UI elements are under it
ui.test_hover(e.pos)
if e.type == pg.MOUSEBUTTONDOWN:
#Relay click to UI elements
ui.mouse_press(e.pos)
if e.type == pg.MOUSEBUTTONUP:
#Relay release to UI elements
ui.mouse_release(e.pos)
return True
def draw(ui):
#Basic function for displaying UI
ui.screen.fill((0,0,0))
ui.draw()
pg.display.flip()
def response(button,**kwargs):
#When the button is pressed, it jumps to another location
print "Pressed button!"
button.rect.topleft=(np.random.randint(0,500),np.random.randint(0,300) )
def uinit(screen):
#Create UI with one button
ui=UI(screen)
button=ui.add('button',pos=(10,10))
button.bind(response,button)
return ui
def main():
pg.init()
screen = pg.display.set_mode((600, 400), pg.DOUBLEBUF)
ui=uinit(screen)
alive=True
while alive:
draw(ui)
alive=logic(ui)
pg.display.quit()
pg.quit()
if __name__ in '__main__':
main()