-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygame_runner.py
84 lines (67 loc) · 1.93 KB
/
pygame_runner.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
import pygame
pygame.init()
# =============================================================================
# Constants
# =============================================================================
# Screen size
DEFAULT_WIDTH = 640
DEFAULT_HEIGHT = 480
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# =============================================================================
# Base class
# =============================================================================
class BasePygameApp:
def __init__(self):
self.running = False
def init_runner(self):
pass
def run(self):
# Init the runner
self.init_runner()
# Setup the app
self.setup()
# Loop on draw while app runs
self.running = True
while self.running:
# Process all pygame events
for event in pygame.event.get():
# Quitting
if event.type == pygame.QUIT:
running = False
# Mouse Events
elif event.type == pygame.MOUSEBUTTONDOWN:
#pos = pygame.mouse.get_pos()j
pos = event.pos
self.mouse_pressed(pos[0], pos[1])
elif event.type == pygame.KEYDOWN:
key = event.key
self.key_pressed(key)
else:
#print event.type
continue
# Draw the app
self.draw()
# Update the screen
pygame.display.flip()
# =============================================================================
# Methods to extend
# =============================================================================
def setup(self):
self.size(DEFAULT_WIDTH, DEFAULT_HEIGHT)
def draw(self):
pass
def mouse_pressed(self, x,y):
pass
def key_pressed(self, key):
pass
# =============================================================================
# Util Methods
# =============================================================================
def size(self, width, height):
self.screen = pygame.display.set_mode((width, height))
def background(self, color):
self.screen.fill(color)
def quit(self):
self.running = False