0

ウォークアニメーションでスプライトを画面上で動かそうとすると、静止したままになりますが、それでもアニメーションは実行されます。コードは次のとおりです。

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は、なぜx座標とy座標がそこにあるのかわかりません。使用することにした場合に備えて、そこに配置したと思います)定義されたクラスを使用していないため、使用したくないと思います。前もって感謝します

4

1 に答える 1

4

アプリの実行に必要な画像ファイルが含まれていないため、はっきりとはわかりません。

十分に活動していないため、投稿にコメントすることはできませんが、ここでrectを再作成しているようです。

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/

于 2012-05-09T01:24:44.533 に答える