Pygame事件捕捉

如果之前有过编程的基础,那么,一定对于事件(event)有一定的理解,在Pygame中,可以使用对应的函数来获取一定的事件,下面我使用Pygame里面的一个简单的例子来进行展示,在这个例子中,我们通过屏幕来展示从鼠标键盘获取的事件

import pygame
from pygame.locals import *
from sys import exit
# init pygame and define the size of the screen
pygame.init()
SCREEN_SIZE = (800, 600)
# create a display surface
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
# set the format of the text
font = pygame.font.SysFont("arial", 16);
font_height = font.get_linesize()
event_text = []
while True:
    # get the event
    event = pygame.event.wait()
    # add the event_text
    event_text.append(str(event))
    # get the current event text
    event_text = event_text[-SCREEN_SIZE[1]/font_height:]
    if event.type == QUIT:
        exit()
    # set the background color
    screen.fill((255, 255, 255))
    # get the display position
    y = SCREEN_SIZE[1]-font_height
    for text in reversed(event_text):
        # blit the text to the screen
        screen.blit( font.render(text, True, (0, 0, 0)), (0, y) )
        y-=font_height
    # display the text on the screen that have been created in the memory
    pygame.display.update()




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值