使用Pygame检测在窗口中输入设备的动作


运行环境:Pygame 1.9.3Python 3.6.2 32bit


简介

用于检测在窗口中输入设备的动作,包括键盘键值,鼠标点击移动等动作。是使用Pygame完成游戏的基础。

代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/9/9 22:47
# @Author  : Wulei
# @Site    : 
# @File    : Pygame.py
# @Software: PyCharm
# 用于检测在窗口中输入设备的动作
# 包括键盘键值,鼠标点击移动等动作

import pygame
import sys

# 初始化pygame
pygame.init()

size = width, height = 600, 400  # 设置窗口大小,size实际为元组
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame Demo")  # 窗口标题
bg = (0, 0, 0)  # 背景填充颜色

font = pygame.font.Font(None, 20)  # 字体None使用系统默认字体,大小为20
line_height = font.get_linesize()  # 获取字体文本的行高
position = 0

screen.fill(bg)  # 填充背景色

while True:
    for event in pygame.event.get():  # 从队列中获取事件
        if event.type == pygame.QUIT:  # 判断是否点击退出动作
            sys.exit()

        screen.blit(font.render(str(event), True, (0, 255, 0)), (0, position))  # 在面板上绘制事件文本,绿色
        position += line_height  # 切换到下一行显示

        if position > height:  # 下一页显示
            position = 0
            screen.fill(bg)

    pygame.display.flip()  # 更新整个面板显示在屏幕上

效果

1

  • KeyDown为按下按键,KeyUp为是放按键
  • MouseMotion为鼠标动作

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值