Pygame事件处理机制

Pygame事件处理机制

事件处理需求

  • • 响应用户键盘、鼠标等外设操作
  • • 响应屏幕尺寸和模式变化
  • • 响应游戏情节的特定触发条件
  • • 产生一些触发条件

1

1

1

1

1

键盘事件及类型的使用

1

1

1

1

1

1

鼠标事件及类型的使用

1

1

1

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Unit PYG04: Pygame Event Print v1
import pygame,sys

pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame事件处理")

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.unicode == "":
print("[KEYDOWN]:", "#", event.key, event.mod)
else:
print("[KEYDOWN]:", event.unicode, event.key, event.mod)
elif event.type == pygame.MOUSEMOTION:
print("[MOUSEMOTION]:", event.pos, event.rel, event.buttons)
elif event.type == pygame.MOUSEBUTTONUP:
print("[MOUSEBUTTONUP]:", event.pos, event.button)
elif event.type == pygame.MOUSEBUTTONDOWN:
print("[MOUSEBUTTONDOWN]:", event.pos, event.button)

pygame.display.update()

壁球小游戏(鼠标型)

  • 鼠标左键按下
    左键按下拖拽小球
  • 鼠标左键释放
    左键释放小球继续运动

  • 局部处理
    增加一种交互方式要考虑
    额外的局部处理
    游戏逻辑的调试

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
# Unit PYG03: Pygame Wall Ball Game version 8  鼠标型
import pygame,sys

pygame.init()
size = width, height = 600, 400
speed = [1,1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE) #窗口大小可调

icon = pygame.image.load("PYG03-flower.png")
pygame.display.set_icon(icon)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
still = False

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
still = True
elif event.type == pygame.MOUSEBUTTONUP:
still = False
if event.button == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
speed[1] = - speed[1]

screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)

Pygame事件处理函数

1

1

1

1

1

1

1

1

1

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
# Unit PYG04: Pygame Event Post
import pygame,sys

pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame事件处理")
fps = 1
fclock = pygame.time.Clock()
num = 1

while True:
uevent = pygame.event.Event(pygame.KEYDOWN, {"unicode": 123, "key":pygame.K_SPACE, "mod":pygame.KMOD_ALT})
pygame.event.post(uevent)
num = num + 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.unicode == "":
print("[KEYDOWN {}]:".format(num), "#", event.key, event.mod)
else:
print("[KEYDOWN {}]:".format(num), event.unicode, event.key, event.mod)

pygame.display.update()
fclock.tick(fps)
Donate? comment?