2012-05-09 18 views
0

私はウォークアニメーションで画面の周りを動かすスプライトを取得しようとしていますが、それはすべて静止していますが、まだアニメーションを通過します。 はここのコードです:私のpygameスプライトが移動しない

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座標を持っている理由、私は念のために私はそれを使用することが決定したことを置くと思います)任意の定義されたクラスを使用して イムありませんそうしないことを好むだろう。事前に感謝

答えて

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に適用する必要があります。

これ以外にも、関数を作成して使用すると、ここのコードはそれらを持つことで大きな利益を得ることができます。その詳細についてはここをクリックしてください:http://www.penzilla.net/tutorials/python/functions/

+1

+1今どこにでもコメントできます:) –

+0

@jb。よくシャック、ありがとう! – TankorSmash

関連する問題