python - 如何在 pygame 库中测试持有的密钥
问题描述
我试图在 pygame 中制作一个简单的平台游戏。但是,当我执行移动并按住移动key(d)
时,它一次只会将玩家移动到右侧。每当我按住移动键时,它只会移动一次,直到我再次按下它时它会再移动一次。
这是我的代码:
import pygame
import sys
# General setup
pygame.init()
clock = pygame.time.Clock()
# Setting up the main window
screen_width = 1280
screen_height = 920
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('PyPlat')
# Game rects
accelerationY = .164
speedY = 7
speedX = 3
player = pygame.Rect(1, 1, 20, 60)
ground = pygame.Rect(0, screen_height-50, screen_width, 60)
bg_color = pygame.Color('gray12')
light_blue = (62, 175, 250)
grounded = player.colliderect(ground)
while True:
# Handling inputs
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
player.x += speedX
# Handling collisions
if player.colliderect(ground) == False:
player.y += speedY
speedY += accelerationY
# Visuals
screen.fill(bg_color)
pygame.draw.rect(screen, light_blue, player)
pygame.draw.rect(screen, light_blue, ground)
# Updating the window
pygame.display.flip()
clock.tick(60)
解决方案
推荐阅读
- android - Android chrome“简化视图”又名阅读模式中的css是什么?
- javascript - 将 JavaScript 字符串解析为对象
- database - 关于 2 个数据库 oracle 的 DBLink 问题
- mql5 - MQL5 多变量数组结构
- reactjs - 在远距离组件之间传递函数的正确方法
- c# - 当您的代码依赖于(本地)DLL 时如何配置构建过程
- javascript - 链接创建和画布下载图像只能在 Chrome 中使用一次
- python - Theano 给出 TypeError: 'Add' object is not iterable
- android - Android App Link 声明网站关联 SHA-256 指纹错误
- python-2.7 - 我没有得到 BlockReference 的位置?