简体   繁体   English

为什么我的运动代码不起作用?

[英]Why won't my Movement code work?

I am trying to make a character move around. 我试图使角色四处走动。 My problem is that when I run the program it immediately stops responding so I don't even know what the problem is. 我的问题是,当我运行该程序时,它立即停止响应,所以我什至不知道问题出在哪里。 Here is my code. 这是我的代码。

import pygame, sys
from pygame.locals import*

pygame.init()
DISPLAYSURF = pygame.display.set_mode((780, 500), 0, 32)

FPS = 30
fpsClock = pygame.time.Clock()

sprite = pygame.image.load('CharacterFront.png')
spritex = 50
spritey = 50
charLeft = False
charRight = False
charUp = False
charDown = False

while True:

DISPLAYSURF.blit(sprite,(spritex,spritey))

for event in pygame.event.get():
    if event.type==QUIT:
        pygame.quit()
        sys.exit()

    if event.type == KEYDOWN:
        if (event.key == K_LEFT):
            charLeft = True
        elif (event.key == K_d):
            charRight = True
        elif (event.key == K_w):
            charUp = True
        elif (event.key == K_s):
            charDown = True
    if event.type == KEYUP:
        if (event.key == K_LEFT):
            charLeft = False
        elif (event.key == K_d):
            charRight = False
        elif (event.key == K_w):
            charUp = False
        elif (event.key == K_s):
            charDown = False

while charLeft == True:
    spritex -= 10
    sprite=pygame.image.load('CharacterLeft.png')
while charRight == True:
    spritex += 10
    sprite=pygame.image.load('CharacterRight.png')
while charUp == True:
    spritey -= 10
    sprite=pygame.image.load('CharacterBack.png')
while charDown == True:
    spritey += 10
    sprite=pygame.image.load('CharacterFront.png')


pygame.display.update()
fpsClock.tick(FPS)

I have already tried many different ways to do this but the closest I got caused the character to get pasted over and over and I had to spam the directions to actually move more than 10 pixels. 我已经尝试了许多不同的方法来执行此操作,但是最接近的方法导致角色被一遍又一遍地粘贴,并且我不得不向垃圾邮件发送方向以实际移动超过10个像素。

Your while char.. loops never end. 您的while char..循环永远不会结束。 You are already looping ( while True: at the top). 您已经在循环播放( while True:在顶部)。 Just make one move (eg spritey -= 10 ) and allow the outer loop to keep running. 只需动一动(例如spritey -= 10 )并允许外循环继续运行。

For ideas on how to keep your character moving while a key is held, see this question . 有关如何在按住键的同时保持角色移动的想法,请参阅此问题

Apart from what jonrsharpe said, you should not load the sprite every time a keypress is done. 除了jonrsharpe所说的之外,您不应在每次按键时加载精灵。 Instead load all your images before, and just blit them when necessary. 而是先加载所有图像,然后在必要时将其变灰。

So your code will look like this: 因此,您的代码将如下所示:

sprite_back = pygame.image.load('CharacterBack.png')
sprite_front = pygame.image.load('CharacterFront.png')
sprite_right = pygame.image.load('CharacterRight.png')
sprite_left = pygame.image.load('CharacterLeft.png')

sprite = sprite_front

while True:

    DISPLAYSURF.blit(sprite,(spritex,spritey))

    if charLeft == True:
        spritex -= 10
    elif charRight == True:
        spritex += 10
    elif charUp == True:
        spritey -= 10
    elif charDown == True:
        spritey += 10

    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if (event.key == K_LEFT):
                charLeft = True
                sprite=sprite_left
            elif (event.key == K_d):
                charRight = True
                sprite=sprite_right
            elif (event.key == K_w):
                charUp = True
                sprite=sprite_back
            elif (event.key == K_s):
                charDown = True
                sprite=sprite_front
        if event.type == KEYUP:
            if (event.key == K_LEFT):
                charLeft = False
            elif (event.key == K_d):
                charRight = False
            elif (event.key == K_w):
                charUp = False
            elif (event.key == K_s):
                charDown = False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM