2012-05-09 1 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를 저장 한 다음 새 이미지를 만든 후 rect에 다시 적용해야합니다.

그 외에도 기능을 만들고 사용하는 방법을 살펴보면 여기의 코드가 기능을 수행하는 데 큰 도움이 될 수 있습니다. 여기에 대한 자세한 정보는 여기를 참조하십시오. http://www.penzilla.net/tutorials/python/functions/

+1

+1 이제 어디서나 댓글을 달 수 있습니다 :) –

+0

@jb. 잘 shucks, 고마워! – TankorSmash