-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
136 lines (107 loc) · 3.77 KB
/
Copy pathgame.py
File metadata and controls
136 lines (107 loc) · 3.77 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pygame
from pygame.locals import *
from sys import exit
FOUR_NEIGH = {"left": (0, -1), "right": (0, 1), "up": (-1, 0), "down": (1, 0)}
EIGHT_NEIGH = list(FOUR_NEIGH.values()) + [(1, 1), (1, -1), (-1, 1), (-1, -1)]
DIRECTION = {pygame.K_UP: "up", pygame.K_LEFT: "left", pygame.K_RIGHT: "right", pygame.K_DOWN: "down"}
def hex2rgb(color):
b = color % 256
color = color >> 8
g = color % 256
color = color >> 8
r = color % 256
return (r, g, b)
class Game(object):
def __init__(self, title, size, fps=30):
self.size = size
pygame.init()
self.screen = pygame.display.set_mode(size, 0, 32)
pygame.display.set_caption(title)
self.keys = {}
self.keys_up = {}
self.clicks = {}
self.timer = pygame.time.Clock()
self.fps = fps
self.score = 0
self.end = False
self.fullscreen = False
self.last_time = pygame.time.get_ticks()
self.is_pause = False
self.is_draw = True
self.score_font = pygame.font.SysFont("Calibri", 130, True)
def bind_key(self, key, action):
if isinstance(key, list):
for k in key:
self.keys[k] = action
elif isinstance(key, int):
self.keys[key] = action
def bind_key_up(self, key, action):
if isinstance(key, list):
for k in key:
self.keys_up[k] = action
elif isinstance(key, int):
self.keys_up[key] = action
def bind_click(self, button, action):
self.clicks[button] = action
def pause(self, key):
self.is_pause = not self.is_pause
def set_fps(self, fps):
self.fps = fps
def handle_input(self, event):
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key in self.keys.keys():
self.keys[event.key](event.key)
if event.key == pygame.K_F11: # F11全屏
self.fullscreen = not self.fullscreen
if self.fullscreen:
self.screen = pygame.display.set_mode(self.size, pygame.FULLSCREEN, 32)
else:
self.screen = pygame.display.set_mode(self.size, 0, 32)
if event.type == pygame.KEYUP:
if event.key in self.keys_up.keys():
self.keys_up[event.key](event.key)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button in self.clicks.keys():
self.clicks[event.button](*event.pos)
def run(self):
while True:
for event in pygame.event.get():
self.handle_input(event)
self.timer.tick(self.fps)
self.update(pygame.time.get_ticks())
self.draw(pygame.time.get_ticks())
def draw_score(self, color, rect=None):
score = self.score_font.render(str(self.score), True, color)
if rect is None:
r = self.screen.get_rect()
rect = score.get_rect(center=r.center)
self.screen.blit(score, rect)
def is_end(self):
return self.end
def update(self, current_time):
pass
def draw(self, current_time):
pass
class Test(Game):
def __init__(self, title, size, fps=30):
super(Test, self).__init__(title, size, fps)
self.bind_key(pygame.K_RETURN, self.press_enter)
def press_enter(self):
print("press enter")
def draw(self, current_time):
pass
def press_space(key):
print("press space.")
def click(x, y):
print(x, y)
def main():
print(hex2rgb(0xfcf040))
game = Test("game", (640, 480))
game.bind_key(pygame.K_SPACE, press_space)
game.bind_click(1, click)
game.run()
if __name__ == '__main__':
main()