简体   繁体   English

我的pygame精灵不会移动

[英]my pygame sprite wont move

im trying to get a sprite to move around the screen with a walk animation and all it will do is stay stationary but it still goes through the animation. 我试图通过步行动画让精灵在屏幕上移动,它会做的只是静止不动,但仍会遍历动画。 here's the code: 这是代码:

from pygame.locals import *
import pygame._view

pygame.init()
clock = pygame.time.Clock()

height = 500
width = 500
screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('placeholder text')

photo = 'grassbackground.png'
background = pygame.image.load(photo).convert()

photo1 = 1

user = pygame.sprite.Sprite()

change = False

x, y = (0, 0)

up = False
down = False
left = False
right = False

speed = 3

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_UP:
                up = True
                y += 1
                change = True
            if event.key == K_DOWN:
                down = True
                y -= 1
                change = True
            if event.key == K_LEFT:
                left = True
                x -= 1
                change = True
            if event.type == K_RIGHT:
                right = True
                x += 1
                change = True

        if event.type == KEYUP:
            if event.key == K_UP:
                up = False
                change = False
            if event.key == K_DOWN:
                down = False
                change = False
            if event.key == K_LEFT:
                left = False
                change = False
            if event.key == K_RIGHT:
                right = False
                change = False

if down and user.rect.bottom < height:
    user.rect.top += speed
if up and user.rect.top > 0:
    user.rect.top -= speed
if left and user.rect.left > 0:
    user.rect.left -= speed
if right and user.rect.right < width:
    user.rect.right += speed

if change == True:
    pygame.time.wait(110)
    photo1 += 1

if change == False:
    photo1 = 1

if photo1 == 1:
    user.image = pygame.image.load("1.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

if photo1 == 2:
    user.image = pygame.image.load("2.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

if photo1 == 3:
    user.image = pygame.image.load("3.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

if photo1 >= 4:
    photo1 = 1

thesprites = pygame.sprite.RenderPlain((user))
thesprites.update()

screen.blit(background, (0, 0))
thesprites.draw(screen)

pygame.display.update()
clock.tick(60)

(ps im not sure why i have the x and y coordinates in there, i guess i just put that there in case i decided to use it) im not using any defined class and would prefer not to. (ps我不确定为什么我在那里有x和y坐标,我想我只是把那放在那里,以防万一我决定使用它)im没有使用任何定义的类,并且宁愿不这样做。 thanks in advance 提前致谢

I can't tell for sure, because you haven't included the image files necessary to run your app. 我不确定,因为您没有包括运行应用程序所需的图像文件。

I can't comment on posts because I haven't been active enough, but it looks like you're recreating the rect here: 由于活动不够,我无法对帖子发表评论,但是您似乎在这里重新创建了矩形:

if photo1 == 1:
    user.image = pygame.image.load("1.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

if photo1 == 2:
    user.image = pygame.image.load("2.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

if photo1 == 3:
    user.image = pygame.image.load("3.png").convert()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

even though you adjust it a few lines above: 即使您在上面几行进行了调整:

if down and user.rect.bottom < height:
    user.rect.top += speed
if up and user.rect.top > 0:
    user.rect.top -= speed
if left and user.rect.left > 0:
    user.rect.left -= speed
if right and user.rect.right < width:
    user.rect.right += speed

You have to store the rect that is used for the player's position, and then re-apply it to the rect after you create the new image. 您必须存储用于播放器位置的rect,然后在创建新图像后将其重新应用于rect。

Beyond that, look into creating and using functions, the code here could benefit greatly from having them. 除此之外,研究创建和使用函数,这里的代码将从拥有它们中受益匪浅。 Take a look here for more information on that: http://www.penzilla.net/tutorials/python/functions/ 请在此处查看有关此内容的更多信息: http : //www.penzilla.net/tutorials/python/functions/

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

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