pygame 文字输入交互_如何在pygame中创建交互式对象?
您可以使用pygame的colliderect检查冲突
首先,创建三个矩形,表示三个选项矩形:simulator_rect = pygame.Rect(600, 125, 30, 30)
quiz_rect = pygame.Rect(225, 125, 30, 30)
quit_rect = pygame.Rect(375, 425, 30, 30)
接下来,我们将创建一个表示蓝色选择器rect的rect:
^{pr2}$
现在你得到了只创建一次的矩形,而不是每次都创建的未命名矩形
现在,对于实际的碰撞检测:
^{3}$
最终代码:import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False
x = 30
y = 30
clock = pygame.time.Clock()
# RGB values for red
red = (255, 0 ,0)
# Your three button rects :
simulator_rect = pygame.Rect(225, 125, 30, 30)
quiz_rect = pygame.Rect(600, 125, 30, 30)
quit_rect = pygame.Rect(375, 425, 30, 30)
# These represent your three option rects
option_rects = [simulator_rect, quiz_rect, quit_rect]
# Your blue selector rect
selector_rect = pygame.Rect(50, 50, 60, 60)
# The 50, 50 xy coords are temporary
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP] and y > 0: y -= 5
if pressed[pygame.K_DOWN] and y < 600 - 60: y += 5
if pressed[pygame.K_LEFT] and x > 0: x -= 5
if pressed[pygame.K_RIGHT] and x < 800 - 60: x += 5
# Set the slector rect's coords to x/y
selector_rect.x, selector_rect.y = x, y
screen.fill((0, 0, 0))
color = (0, 128, 255)
pygame.draw.rect(screen, color, selector_rect)
myfont = pygame.font.SysFont("monospace", 15)
label = myfont.render("Start the experiment simulator", 1, (255,255,255))
screen.blit(label, (100, 100))
label2 = myfont.render("Start the quiz", 1, (255,255,255))
screen.blit(label2, (550, 100))
label3 = myfont.render("Quit game", 1, (255,255,255))
screen.blit(label3, (350, 400))
# Use our created rects
pygame.draw.rect(screen, red, simulator_rect)
pygame.draw.rect(screen, red, quiz_rect)
pygame.draw.rect(screen, red, quit_rect)
# Check to see if the user presses the enter key
if pressed[pygame.K_RETURN]:
# Check to see if the selection rect
# collides with any other rect
for rect in option_rects:
# Add rects as needed
if selector_rect.colliderect(rect):
if rect == simulator_rect:
# Do simulations stuff!
print('Simulating!')
elif rect == quiz_rect:
# Do quizzing stuff!
print('Quizzing!')
elif rect == quit_rect:
# Quit!
done = True
pygame.display.flip()
clock.tick(60)
这确实给你的程序增加了一些复杂度,但至少这是一个坚如磐石的方法,你也可以添加一些特性,而且这将保持健壮。在
pygame 文字输入交互_如何在pygame中创建交互式对象?相关推荐
- 如何在mysql中创建过程_如何在MySQL 中创建存储过程?
问题阐述 自MySQL 5.0 开始,MySQL 就支持存储过程.存储过程是一些被用户定义的SQL 语句集合.一个存储程序是可以被存储在服务器中的一套SQL 语句.存储过程可以被程序.触发器或另一个存 ...
- java 运费_如何在Java中创建运费成本计算器
我正在创建计算器来计算运费.代码是这样的:如何在Java中创建运费成本计算器 class ShippingCalc { public static void main(String[] args) { ...
- wordpress 数据库_如何在WordPress中创建视频库(逐步)
wordpress 数据库 Recently, a user asked us how to create a video gallery in WordPress. WordPress makes ...
- wordpress创建_如何在WordPress中创建问卷(简易方式)
wordpress创建 Do you want to create a questionnaire in WordPress to survey your visitors or collect da ...
- html树状图右侧_如何在 Tableau 中创建控制图?
本文作者:Jose Valero 控制图是用于研究流程如何随时间变化,并确定制造或业务流程是否在统计控制状态下的图表. 我相信这对每家公司来说都是一个有益的工具,所以在这篇文章中,我将尝试如何在 Ta ...
- vue 递归创建菜单_如何在Vue中创建类似中等的突出显示菜单
vue 递归创建菜单 by Taha Shashtari 由Taha Shashtari 如何在Vue中创建类似中等的突出显示菜单 (How to Create a Medium-Like Highl ...
- figma下载_如何在Figma中创建逼真的3D对象
figma下载 by Gbolahan Taoheed Fawale 通过Gbolahan Taoheed Fawale 如何在Figma中创建逼真的3D对象 (How to create reali ...
- hive 元数据 自定义_如何在Hive中创建自定义函数UDF及如何直接通过Impala的同步元数据重用UDF的jar文件-阿里云开发者社区...
如何在Hive中创建自定义函数UDF及使用 如何在Impala中使用Hive的自定义函数 UDF函数开发 使用Intellij工具开发Hive的UDF函数,进行编译: 1.使用Intellij工具通过 ...
- wordpress创建_如何在WordPress中创建子页面
wordpress创建 Do you want to create a child page on your site? Pages in WordPress can be standalone or ...
- wordpress模板_如何在WordPress中创建类别模板
wordpress模板 Have you visited a site where each category has different layout? In WordPress theme dev ...
最新文章
- 最近做了一个小小的系统,收获挺大的....我想总结一下
- electron窗口自适应_Go 限流器系列(3)自适应限流
- log4j写入mysql数据库_log4j日志写入数据库
- Fedora 18 正式发布
- 反制爬虫之Burp Suite RCE
- Codeforces Round #709 (Div. 1, based on Technocup 2021 Final Round) A. Basic Diplomacy
- 关于cell中添加子视图 复用重叠问题的解决方法
- sonar不支持mysql_sonar-iOS的实践
- linux Centos下mysql安装
- 从 1 加到 55 的值
- 封装cuda/cudnn写卷积网络前向计算程序
- emacs命令/edwin参考
- →箭头符号大全复制_特殊符号及名称大全,复制即用
- 工商银行网银U盾在win7 64位操作系统无法使用的问题
- 那些看着高大上的名词
- Spring Boot入门系列(六)Spring Boot如何整合Mybatis【附详细步骤】
- linux下安装EDK2开发环境,EDK2开发环境搭建 - osc_y9wmeuxa的个人空间 - OSCHINA - 中文开源技术交流社区...
- matlab接收电视信号,DRM接收及matlab实验 (转载)
- 信息部门人员角色划分及任职资格
- atom开发java_Atom简明用法