最基本的事件——响应退出操作
while True:
#循环获取event中的事件
for event in pygame.event.get():
#如果获取到的事件类型为quit
if event.type==QUIT:
#析构pygame
pygame.quit()
#退出控制台
sys.exit()
Event队列和Event对象
流程如下:
注:
- 事件队列有容量上限。SDL1.2中最多只能容纳128个event
- 事件队列中所存储的实际是一个event对象,类型为EventType,其有两类属性type和dict(dict)
下列实例说明:
# 导入相关库
import pygame,sys
from pygame.locals import *
# 初始化
pygame.init()
# 设置窗口大小
screen=pygame.display.set_mode(size=(400,300))
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type==MOUSEBUTTONDOWN:
#打印事件
print(event)
#打印事件类型
print(event.type)
#打印除type外的其余属性
print(event.__dict__)
# 刷新
pygame.display.update()
运行效果如下(鼠标点击后):
注:
- type为数字即1025,该数字在pygame.locals中有对应的常量。例如QUIT即对应退出事件
- 可以通过对象.方法的格式调用对象.__dict__中的属性,部分特殊请注意。
- pos表示光标所处位置
- button表示鼠标哪个按键被按下
读取事件对象
#1.poll
pygame.event.poll()
#2.wait
pygame.event.wait()
#3.get
pygame.event.get()
1与2一次只返回一个对象,3返回所有对象列表
1、2、3均会删除把事件从事件队列中删除
自定义事件
MY_EVENT=pygame.USEREVENT+1
event=pygame.event.Event(MY_EVENT,
attri1="attribute1",
attri2="attribute2")
pygame.event.post(event)
- 第一行定义自定义事件的类型取值为(USEREVENT,NUMEVENTS]
- 第二行使用Event函数创建对象type和dict等
- 第五行使用post函数把该事件对象发送到事件队列中
具体实例
# 导入相关库
import pygame,sys
from pygame.locals import *
# 定义自定义事件的类型取值
MY_EVENT1=USEREVENT+1
MY_EVENT2=USEREVENT+2
# 初始化
pygame.init()
# 设置窗口大小
screen=pygame.display.set_mode(size=(600,300))
# 设置窗口标题
pygame.display.set_caption("事件示例")
# 字体设置
type_font=pygame.font.SysFont(None,48)
dict_font=pygame.font.SysFont(None,26)
additional_info=""
buttons=["left","middle","right","wheel up","wheel down"]
times=0
# 解决按住不放的情况第一个参数到来的时间延迟,第二个参数为第一个参数与第二个参数之间的时间间隔,单位均为毫秒
pygame.key.set_repeat(1,50)
while True:
for event in pygame.event.get():
# 退出事件响应
if event.type==QUIT:
pygame.quit()
sys.exit()
# 响应键盘按下操作
elif event.type==KEYDOWN:
times+=1
additional_info="".join([pygame.key.name(event.key),"key is pressed",str(times)])
# 按下q
if event.key==K_q:
sys.exit()
# 按下空格
elif event.key==K_SPACE:
MY_EVENT=pygame.event.Event(MY_EVENT1,attril="attribute1",attri2="my attribute2")
pygame.event.post(MY_EVENT)
# 按下e键
elif event.key==K_e:
pygame.time.set_timer(MY_EVENT2,2000)
#按下d键
elif event.key==K_d:
pygame.time.set_timer(MY_EVENT2,0)
# 响应键盘按键松开
elif event.type==KEYUP:
times=0
additional_info="".join([pygame.key.name(event.key),"key is released"])
# 响应鼠标操作
elif event.type==MOUSEMOTION:
additional_info=""
# 响应鼠标按下操作
elif event.type==MOUSEBUTTONDOWN:
additional_info=buttons[event.button-1].join(["mouse","button is released"])
# 响应鼠标松开操作
elif event.type==MOUSEBUTTONUP:
additional_info=buttons[event.button-1].join(["mouse","button is releaswd"])
elif event.type==MY_EVENT1:
additional_info="my event1 is received"
elif event.type==MY_EVENT2:
additional_info="my event2 is received"
# 将事件的type的数据渲染成surface对象
type_text=pygame.event.event_name(event.type)
type=type_font.render(type_text,True,(255,0,0))
# 将事件的dict的数据渲染成surface对象
dict_text=str(event.__dict__)
attributes=dict_font.render(dict_text,True,(0,0,255))
# 将additional_info渲染成surface对象
additional=dict_font.render(additional_info,True,(0,0,0))
# 填充背景色为白色
screen.fill((255,255,255))
# 将type对象贴到display screen上
screen.blit(type,(20,50))
# 将attributes对象贴到display screen上
screen.blit(attributes,(20,100))
# 将additional对象贴到display screen上
screen.blit(additional,(20,200))
# 刷新
pygame.display.update()
运行效果如下图所示: