簡體   English   中英

我的pygame精靈不會移動

[英]my pygame sprite wont move

我試圖通過步行動畫讓精靈在屏幕上移動,它會做的只是靜止不動,但仍會遍歷動畫。 這是代碼:

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我不確定為什么我在那里有x和y坐標,我想我只是把那放在那里,以防萬一我決定使用它)im沒有使用任何定義的類,並且寧願不這樣做。 提前致謝

我不確定,因為您沒有包括運行應用程序所需的圖像文件。

由於活動不夠,我無法對帖子發表評論,但是您似乎在這里重新創建了矩形:

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

您必須存儲用於播放器位置的rect,然后在創建新圖像后將其重新應用於rect。

除此之外,研究創建和使用函數,這里的代碼將從擁有它們中受益匪淺。 請在此處查看有關此內容的更多信息: http : //www.penzilla.net/tutorials/python/functions/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM