Pygame编程(4)event模块

Pygame编程(4)event模块

  • 函数
  • 示例

函数

  • pygame.event.pump
    让 Pygame 内部自动处理事件
  • pygame.event.get
    从队列中获取事件
  • pygame.event.poll
    从队列中获取一个事件
  • pygame.event.wait
    等待并从队列中获取一个事件
  • pygame.event.peek
    检测某类型事件是否在队列中
  • pygame.event.clear
    从队列中删除所有的事件
  • pygame.event.event_name
    通过 id 获得该事件的字符串名字
  • pygame.event.set_blocked
    控制哪些事件禁止进入队列
  • pygame.event.get_blocked
    检测某一类型的事件是否被禁止进入队列
  • pygame.event.set_allowed
    控制哪些事件允许进入队列
  • pygame.event.set_grab
    控制输入设备与其他应用程序的共享
  • pygame.event.get_grab
    检测程序是否共享输入设备
  • pygame.event.set_keyboard_grab
    控制键盘设备与其他应用程序的共享
  • pygame.event.get_keyboard_grab
    检测键盘设备是否与其他应用程序的共享
  • pygame.event.post
    放置一个新的事件到队列中
  • pygame.event.custom_type
    自定义用户事件类型
  • pygame.event.Event
    创建一个新的事件对象

示例

import sys
import pygame
from pygame import *


pygame.init()

pygame.display.set_mode((800, 480), flags=RESIZABLE)

while True:
    for event in pygame.event.get():
        print(event)
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        # 键盘事件
        if event.type == KEYDOWN:
            print('Event:KEYDOWN')
            if event.key == K_LEFT:
                print('K_LEFT DOWN')

        if event.type == KEYUP:
            print('Event:KEYUP')
            if event.key == K_LEFT:
                print('K_LEFT UP')

        if event.type == KEYMAPCHANGED:
            print('Event:KEYMAPCHANGED')

        # 鼠标事件
        if event.type == MOUSEBUTTONDOWN:
            print('Event:MOUSEBUTTONDOWN')
            if event.button == 1:
                print('鼠标左键')
            if event.button == 2:
                print('鼠标滚轮')
            if event.button == 3:
                print('鼠标右键')
        if event.type == MOUSEBUTTONUP:
            print('Event:MOUSEBUTTONUP')
        if event.type == MOUSEWHEEL:
            print('Event:MOSUEWHEEL')
        if event.type == MOUSEMOTION:
            print('Event:MOUSEMOTION')
            #
            mouse_x, mouse_y = event.pos
            rel_x, rel_y = event.rel
            btns = event.buttons
            event.touch
            print(mouse_x, mouse_y)
        
        # 键盘输入事件
        if event.type == TEXTEDITING:
            print('Event:TEXTEDITING')
            #  
            print(event.text)
            print(event.start)
            print(event.length)

        if event.type == TEXTINPUT:
            print('Event:TEXTINPUT')
            # 
            print(event.text)


        # 游戏杆事件
        if event.type == JOYBUTTONDOWN:
            print('Event:JOYBUTTONDOWN')
        if event.type == JOYBUTTONUP:
            print('Event:JOYBUTTONUP')
        if event.type == JOYHATMOTION:
            print('Event:JOYHATMOTION')
        if event.type == JOYAXISMOTION:
            print('Event:JOYAXISMOTION')
        if event.type == JOYBALLMOTION:
            print('Event:JOYBALLMOTION')
        if event.type == JOYDEVICEADDED:
            print('Event:JOYDEVICEADDED')
        if event.type == JOYDEVICEREMOVED:
            print('Event:JOYDEVICEREMOVED')
        
        # 窗口事件
        if event.type == WINDOWENTER:
            print('Event:WINDOWENTER')
        if event.type == WINDOWCLOSE:
            print('Event:WINDOWCLOSE')
        if event.type == WINDOWSHOWN:
            print('Event:WINDOWSHOW')
        if event.type == WINDOWHIDDEN:
            print('Event:WINDOWHIDDEN')
        if event.type == WINDOWMINIMIZED:
            print('Eevent:WINDOWMINNMIZED')
        if event.type == WINDOWMAXIMIZED:
            print('Event:WINDOWMAXIMIZED')
        if event.type == WINDOWMOVED:
            print('Event:WINDOWMOVED')
        if event.type == WINDOWRESIZED:
            print('Event:WINDOWRESIZED')
        if event.type == WINDOWSIZECHANGED:
            print('Event:WINDOWSIZECHANGED')
        if event.type == WINDOWENTER:
            print('Event:鼠标移入窗口')
        if event.type == WINDOWLEAVE:
            print('Event:鼠标移出窗口')
        if event.type == WINDOWFOCUSGAINED:
            print('Evvent:窗口获取焦点')
        if event.type == WINDOWFOCUSLOST:
            print('Event:窗口失去焦点')
        if event.type == WINDOWDISPLAYCHANGED:
            print('Event:WINDOWDISPLAYCHANGED')        

你可能感兴趣的:(PyGame,pygame)