如果我按一个键,然后将另一个键推得太快,我的Pygame Sprite停止移动



我是pygame的新手,所以我的代码有点混乱,但我希望您能为我提供帮助。当我进行精灵移动时,它可以正常工作,但是如果我非常快速切换钥匙,或者按下跳线按钮放开,我的精灵会停止移动。

import shelve
import sys, pygame, pygame.mixer
import time
from pygame.locals import *
import random
pygame.init()
shelfFile = shelve.open('save_game')
#screen
sise = width, hight = 700, 600
red = 30,30,30
screen = pygame.display.set_mode(sise)
pygame.display.set_caption('Thing')
#varuables
background = pygame.image.load('background.png')
player = pygame.image.load('you.png')
enemy1 = pygame.image.load('enemy1.png')
clock = pygame.time.Clock()
px = shelfFile['px']
py = shelfFile['py']
health = shelfFile['health']
x2 = 0
x = 0
y = 0
u = 0
d = 0
t = 0
r = 0
ex = 0
ey = 0

cutseane = shelfFile ['cutseane']
black = 255,255,255
Punch = False
color = 255,0,0
radius = 5

room = shelfFile['room']
ehealth = shelfFile['ehealth']

while True:
    pygame.event.get()
    if jump == 1:
        y = -20
    if py <= 200:
        jump = 2
    if jump == 2:
        y = +20
    if py >= 480:
        py = 480
                #The Wall
    if px <= 0:
        px = 0
    if px >= 630:
        px = 630
    pygame.display.flip()
    screen.blit(background,(1,1))
    screen.blit(player,(px,py))
    #if ehealth >= 1:
        #screen.blit(enemy1,(ex,ey))
    #else:
        #ex = 0
        #ey = 0
                px = px + x 
    py = py + y
    clock.tick(30)
    for event in pygame.event.get():
        keys = pygame.key.get_pressed()

        if event.type == KEYDOWN and event.key == pygame.K_ESCAPE:
            shelfFile['px'] = px
            shelfFile['py'] = py
            shelfFile['health'] = health
            shelfFile['ehealth'] = ehealth
            shelfFile['cutseane'] = cutseane
            shelfFile['room'] = room
            shelfFile.close()
            pygame.quit(); sys.exit();

                    #Player Movement
        elif event.type == pygame.KEYDOWN:
            if keys [pygame.K_0]:
                health -= 1
                text = 1
                shelfFile['text'] = text
            elif event.key == pygame.K_RIGHT:
                x = -15
            elif event.key == pygame.K_LEFT:
                x = +15
            elif event.key == pygame.K_UP:
                y = +15
            elif event.key == pygame.K_DOWN:
                y = -15
        elif event.type == KEYUP:
            if event.key == pygame.K_LEFT and x > 0:
                x = 0
            elif event.key == pygame.K_RIGHT and x < 0:
                x = 0
            elif event.key == pygame.K_UP and x > 0:
                y = 0
            elif event.key == pygame.K_DOWN and x < 0:
                y = 0

我认为问题是在某些地方,您写了y = +20而不是y += 20。如果您希望y增加20个单位,请执行y += 20,如果要将y设置为20,请执行y = 20(与y = +20相同(。我不知道您要做什么,因为我没有您的PNG文件,也无法运行该程序以查看会发生什么。另外,如果这甚至是一个无意的错误,那么您对否定性和代码的其他部分也做了同样的事情。

这并不是真正的错误,如果不纠正,它将起作用,但您也拼写错误(如果是故意的,请忽略此事。(

Sise应该是大小,高度应该是身高,切口应该是过场动画...

还有其他错误。如果所有这些都有效,并且它们不仅是错别字,那么您可能会使用我不知道的某种版本的Python。

您的事件循环真的很混乱,应重组。在事件循环中,不应在循环的外部循环中调用keys = pygame.key.get_pressed()。我有一个最小的例子,可以向您展示一种进行运动和事件处理的方法(仅在事件循环中并且没有key.get_pressed(。

import sys
import pygame

pygame.init()
screen = pygame.display.set_mode((700, 600))
clock = pygame.time.Clock()
player = pygame.Surface((30, 30))
player.fill((120, 240, 90))
px = 100
py = 200
x_speed = 0
y_speed = 0
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            # If player is on the ground, jump (in more advanced
            # games you need collision detection instead >= 480).
            if event.key == pygame.K_UP and py >= 480:
                y_speed = -20
            elif event.key == pygame.K_RIGHT:
                x_speed = 10
            elif event.key == pygame.K_LEFT:
                x_speed = -10
        elif event.type == pygame.KEYUP:
            # If player is moving right, stop him.
            if event.key == pygame.K_RIGHT and x_speed > 0:
                x_speed = 0
            # If player is moving left, stop him.
            elif event.key == pygame.K_LEFT and x_speed < 0:
                x_speed = 0
    # Don't jump too high.
    if py <= 200:
        y_speed = 20
    # Don't go below y = 480.
    if py >= 480:
        py = 480
    # Move the player.
    px += x_speed
    py += y_speed
    # The Wall.
    if px <= 0:
        px = 0
    if px >= 630:
        px = 630
    screen.fill((50, 50, 50))
    screen.blit(player, (px, py))
    pygame.display.flip()
    clock.tick(30)

最新更新