只有按住按钮并且鼠标移动时,字符才会连续移动
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了只有按住按钮并且鼠标移动时,字符才会连续移动相关的知识,希望对你有一定的参考价值。
我试图让代码接受用户持有密钥,并不断更新。但是,它只执行一次更新,然后执行其余代码,忽略用户的输入。
import pygame, sys
from pygame.locals import *
import math
pygame.init()
black = (0,0,0)
Window = pygame.display.set_mode((800,500),0,32)
pygame.display.set_caption('Game')
FPS = 60
fpsClock = pygame.time.Clock()
background = pygame.Rect(0,0,800,500)
player = pygame.Rect(725,425, 25,25)
playerx = player.x
playery = player.y
jumping = False
onground = True
while True:
onground = True
jumping = False
Window.fill(black)
pygame.draw.rect(Window, pygame.color.Color('Royal Blue'), player, 0)
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_w]:
jumping = True
onground = False
elif keys[K_a]:
player.move_ip(-1,0)
elif keys[K_d]:
player.move_ip(2,0)
if jumping == True and onground == False:
player.move_ip(0,-1)
if keys[K_a]:
player.move_ip(-2,-1)
elif keys[K_d]:
player.move_ip(2,-1)
elif (jumping == False and onground == False) or playery > 412:
player.move_ip(0,1)
jumping = False
print player.copy()
player.clamp_ip(background)
pygame.display.flip()
fpsClock.tick(FPS)
答案
你有
keys = pygame.key.get_pressed()
if keys[K_w]:
jumping = True
onground = False
elif keys[K_a]:
player.move_ip(-1,0)
elif keys[K_d]:
player.move_ip(2,0)
内
for e in pygame.event.get():
因此只有在按下鼠标移动或按键等事件时才会执行。
键盘事件仅在您按/按键时创建 - 而不是在您按下时创建。
因此,只有当您移动鼠标或按键时,此for
循环才能在内部执行任何操作。
我的代码版本:
import pygame, sys
from pygame.locals import *
#import math
# CONSTANTS - uppercase
BLACK = (0,0,0)
FPS = 60
BLUE = pygame.color.Color('Royal Blue')
# GAME
pygame.init()
# window - variables - always lowercase
screen = pygame.display.set_mode((800,500),0,32)
pygame.display.set_caption('Game')
background = pygame.Rect(0,0,800,500)
player = pygame.Rect(725,425, 25,25)
playerx = player.x
playery = player.y
# mainloop
fps_clock = pygame.time.Clock()
jumping = False
onground = True
gaming = True
paused = False
while gaming:
onground = True
jumping = False
# --- (all) events ---
for e in pygame.event.get():
if e.type == QUIT:
gaming = False
elif e.type == KEYDOWN:
if e.key == K_ESCAPE:
gaming = False
elif e.key == K_SPACE:
paused = not paused
# --- (events and) moves ---
if not paused:
# not in `for e in event`
keys = pygame.key.get_pressed()
if keys[K_w]:
jumping = True
onground = False
elif keys[K_a]:
player.move_ip(-1,0)
elif keys[K_d]:
player.move_ip(2,0)
if jumping == True and onground == False:
player.move_ip(0,-1)
if keys[K_a]:
player.move_ip(-2,-1)
elif keys[K_d]:
player.move_ip(2,-1)
elif (jumping == False and onground == False) or playery > 412:
player.move_ip(0,1)
jumping = False
print player #.copy() # why copy ???
#player.clamp_ip(background) # I don't need it
# --- (all) draws ---
screen.fill(BLACK)
pygame.draw.rect(screen, BLUE, player, 0)
pygame.display.flip()
# --- FPS ---
fps_clock.tick(FPS)
# the end
pygame.quit()
sys.exit()
以上是关于只有按住按钮并且鼠标移动时,字符才会连续移动的主要内容,如果未能解决你的问题,请参考以下文章