如果我按下一个键而另一个键太快,我的pygame精灵会停止移动

时间:2017-06-04 19:22:51

标签: python pygame

我是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

2 个答案:

答案 0 :(得分:1)

我认为问题在于,在某些地方,你写了y = +20而不是y += 20。如果您希望y增加20个单位,请执行y += 20,如果您希望将y设置为值20,请执行y = 20(与y = +20相同{1}})。我不知道你要做什么,因为我没有你的png文件,我无法运行程序看看会发生什么。此外,如果它甚至是一个无意的错误,你对负面和代码的其他部分做了同样的事情。

这不是一个真正的错误,如果没有纠正它会起作用,但你也拼错了一些东西(如果它是故意的,请忽略它。)

sise应该是大小, 高度应该是高度, cutseane应该是过场动画......

还有其他错误。如果所有这些都有效并且它们不仅仅是拼写错误,那么您可能正在使用我不知道的某些版本的python。

答案 1 :(得分:0)

你的事件循环真的搞砸了,应该重组。不应在事件循环中调用keys = pygame.key.get_pressed(),而应在外部while循环中调用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) )。

Shared/Business