角色仅在同时按住两个按钮并移动鼠标时持续移动

0 投票
1 回答
539 浏览
提问于 2025-04-18 13:08

我正在尝试让代码能够识别用户按住了某个键,并且持续更新。但是它只更新一次,然后就继续执行后面的代码,完全忽略了用户的输入。

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)

1 个回答

0

你有

    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()

撰写回答