-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-window.py
96 lines (75 loc) · 2.76 KB
/
main-window.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
import os
import pygame as pg
import counting_objects as co
import pygame_textinput as ti
#from level import Level
if not pg.font:
print("Warning, fonts disabled")
if not pg.mixer:
print("Warning, sound disabled")
def main():
"""this function is called when the program starts.
it initializes everything it needs, then runs in
a loop until the function returns."""
# Initialize Everything
pg.init()
screen = pg.display.set_mode((1000, 1000), pg.SCALED)
pg.display.set_caption("Counting Simulator")
# Create The Background
background = pg.Surface(screen.get_size())
background = background.convert()
background.fill((102, 204, 10))
# Load level and failure sound
#currentLevel = Level("what is my age", "uh_you_suck.wav", 2, 5)
#failureSound = pg.mixer.Sound(currentLevel.failureSound)
# Put Text On The Background, Centered
if pg.font:
font = pg.font.Font(None, 64)
text = font.render("How many objects are there?", True, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width() / 2, y=background.get_height() - 100)
background.blit(text, textpos)
# Display The Background
screen.blit(background, (0, 0))
pg.display.flip()
# Prepare Game Objects
sheep = co.CountingObject()
allobjects = pg.sprite.Group(sheep)
allobjects.draw(screen)
clock = pg.time.Clock()
# Create TextInput-object with at most 15 characters
manager = ti.TextInputManager(validator=lambda input: len(input) <= 15)
textinput = ti.TextInputVisualizer(manager)
receiveinput = True
# Main Loop
going = True
while going:
clock.tick(60)
events = pg.event.get()
# Handle Input Events
for event in events:
if event.type == pg.QUIT:
going = False
elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
# Close window if user presses ESC
going = False
elif event.type == pg.KEYDOWN and event.key == pg.K_RETURN:
userinput = textinput.value
try:
uiasint = int(userinput)
print(uiasint)
receiveinput = False
except ValueError:
print("That's not even an integer!")
allobjects.update()
# Draw Everything
screen.blit(background, (0, 0))
allobjects.draw(screen)
if receiveinput:
textinput.update(events)
screen.blit(textinput.surface, (background.get_width() / 2 - 150, background.get_height() - 50), (0,0,300,100))
pg.display.flip()
pg.quit()
# Game Over
# this calls the 'main' function when this script is executed
if __name__ == "__main__":
main()