-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
62 lines (46 loc) · 1.65 KB
/
game.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
import pygame
import pygame.locals
import config
class Game:
def __init__(self, name, width, height):
self.width = width
self.height = height
self.on = True
self.screen = pygame.display.set_mode(
# set the size
(width, height),
# use double-buffering for smooth animation
pygame.locals.DOUBLEBUF |
# apply alpha blending
pygame.locals.SRCALPHA)
# set the title of the window
pygame.display.set_caption(name)
def game_logic(self, keys, newkeys):
raise NotImplementedError()
def paint(self, surface):
raise NotImplementedError()
def main_loop(self):
clock = pygame.time.Clock()
keys = set()
while True:
clock.tick(config.FRAMES_PER_SECOND)
newkeys = set()
for e in pygame.event.get():
# did the user try to close the window?
if e.type == pygame.QUIT:
pygame.quit()
return
# did the user just press the escape key?
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
pygame.quit()
return
# track which keys are currently set
if e.type == pygame.KEYDOWN:
keys.add(e.key)
newkeys.add(e.key)
if e.type == pygame.KEYUP:
keys.discard(e.key)
if self.on:
self.game_logic(keys, newkeys)
self.paint(self.screen)
pygame.display.flip()