2013-01-09 115 views
1

好吧,我的代碼在下面,現在我是pygame的新手,並試圖找出如何在屏幕上移動圖像。從我所見過的pygame中,下面的代碼應該將圖像向上移動(現在唯一可能的移動)。但是,當我推動圖像不動。 Python正在認識到我正在向上推,因爲它在系統托盤中打印,但是沒有任何動作,有人可以幫忙嗎?python和pygame圖像移動

import pygame, sys 
from pygame.locals import * 

FPS = 30 


WIN_WIDTH = 800 
WIN_HEIGHT = 600 
HALF_WIDTH = int(WIN_WIDTH/2) 
HALF_HEIGHT = int(WIN_HEIGHT/2) 

COLOR = (255, 0, 255) 
IMAGE = pygame.image.load('squirrel.png') 
STARTSIZE = 25 

LEFT = 'left' 

def main(): 
    pygame.init() 
    FPS_CLOCK = pygame.time.Clock() 
    movement = 1 

    moveUp = False 

    DISPLAY_SURFACE = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) 

    while True: 
     DISPLAY_SURFACE.fill(COLOR) 

     playerObj = {'surface': pygame.transform.scale(IMAGE, (STARTSIZE, STARTSIZE)), 
        'facing': LEFT, 
        'size': STARTSIZE, 
        'x': HALF_WIDTH, 
        'y': HALF_HEIGHT} 

     playerObj['rect'] = pygame.Rect((playerObj['x'], playerObj['y'], playerObj['size'], playerObj['size'])) 
     DISPLAY_SURFACE.blit(playerObj['surface'], playerObj['rect']) 


     for event in pygame.event.get(): 

      if event.type == KEYDOWN: 
       if event.key == K_UP: 
        moveUp = True 

      if event.type == KEYUP: 
       if event.key == K_UP: 
        moveUp = False 

      elif event.type == QUIT: 
       pygame.quit() 
       sys.exit() 

     if moveUp: 
      playerObj['y'] += movement 
      print("moving up") 

     else: 
      print('stopped') 

     pygame.display.update() 
     FPS_CLOCK.tick(FPS) 

if __name__ == '__main__': 
    main() 

回答

1

你正在創建每次while循環遍歷所以你不會看到它移動(因爲它是設置它的「Y」值每次HALF_HEIGHT)時間新playerObj。將playerObj定義移出while循環。

+0

我試過,仍然沒有任何動靜,我想這可能是我的問題早,但無論哪種方式,它不移動圖像 – user1758231

+0

那麼*就是問題的一部分。另一個可能與你的blit命令有關,不幸的是我現在無法正確測試。嘗試將DISPLAY_SURFACE.fill(color)和DISPLAY_SURFACE.blit(..)移至pygame.display.update() – timc

+0

之前哦,我明白了,謝謝您的建議!我實際上需要將playerObj移出,但將playerObj保留在while循環中,現在它的工作雖然謝謝! – user1758231

0

在進入while循環之前,您必須先創建playerObj,然後每次只繪製它到DISPLAY_SURFACE。另外,增加字典中的鍵y也無濟於事,您必須修改rect。微創解決方案將是:

import pygame, sys 
from pygame.locals import * 

FPS = 30 


WIN_WIDTH = 800 
WIN_HEIGHT = 600 
HALF_WIDTH = int(WIN_WIDTH/2) 
HALF_HEIGHT = int(WIN_HEIGHT/2) 

COLOR = (255, 0, 255) 
IMAGE = pygame.image.load('squirrel.png') 
STARTSIZE = 25 

LEFT = 'left' 

def main(): 
    pygame.init() 
    FPS_CLOCK = pygame.time.Clock() 
    movement = 1 

    moveUp = False 

    DISPLAY_SURFACE = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) 

    playerObj = {'surface': pygame.transform.scale(IMAGE, (STARTSIZE, STARTSIZE)), 
        'facing': LEFT, 
        'size': STARTSIZE, 
        'x': HALF_WIDTH, 
        'y': HALF_HEIGHT} 

    playerObj['rect'] = pygame.Rect((playerObj['x'], playerObj['y'], playerObj['size'], playerObj['size'])) 

    while True: 
     DISPLAY_SURFACE.fill(COLOR) 
     DISPLAY_SURFACE.blit(playerObj['surface'], playerObj['rect']) 
     for event in pygame.event.get(): 

      if event.type == KEYDOWN: 
       if event.key == K_UP: 
        moveUp = True 

      if event.type == KEYUP: 
       if event.key == K_UP: 
        moveUp = False 

      elif event.type == QUIT: 
       pygame.quit() 
       sys.exit() 

     if moveUp: 
      playerObj['rect'].y -= movement 
      print("moving up") 

     else: 
      print('stopped') 

     pygame.display.update() 
     FPS_CLOCK.tick(FPS) 

if __name__ == '__main__': 
    main() 

它的工作原理,我自己試了一下。但是你應該重構你的代碼,不需要在你的字典中攜帶最初的x,y等值。

編輯 在所有四個方向的移動,這將是:

import pygame, sys 
from pygame.locals import * 

FPS = 30 


WIN_WIDTH = 800 
WIN_HEIGHT = 600 
HALF_WIDTH = int(WIN_WIDTH/2) 
HALF_HEIGHT = int(WIN_HEIGHT/2) 

COLOR = (255, 0, 255) 
IMAGE = pygame.image.load('squirrel.png') 
STARTSIZE = 25 

LEFT = 'left' 

def move_rect(rect, key, distance): 
    if key == K_UP: 
     rect.y-=distance 
    elif key == K_DOWN: 
     rect.y+=distance 
    elif key == K_LEFT: 
     rect.x-=distance 
    elif key == K_RIGHT: 
     rect.x+=distance 

def main(): 
    pygame.init() 
    FPS_CLOCK = pygame.time.Clock() 
    movement = 1 

    key2mvmt = {K_UP:False, K_DOWN:False, K_LEFT:False, K_RIGHT:False} 

    DISPLAY_SURFACE = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) 

    playerObj = {'surface': pygame.transform.scale(IMAGE, (STARTSIZE, STARTSIZE)), 
        'facing': LEFT, 
        'size': STARTSIZE, 
        'x': HALF_WIDTH, 
        'y': HALF_HEIGHT} 

    playerObj['rect'] = pygame.Rect((playerObj['x'], playerObj['y'], playerObj['size'], playerObj['size'])) 

    while True: 
     DISPLAY_SURFACE.fill(COLOR) 
     DISPLAY_SURFACE.blit(playerObj['surface'], playerObj['rect']) 
     for event in pygame.event.get(): 

      if event.type == KEYDOWN: 
       if key2mvmt.has_key(event.key): 
        key2mvmt[event.key]= True 

      if event.type == KEYUP: 
       if key2mvmt.has_key(event.key): 
        key2mvmt[event.key]= False 

      elif event.type == QUIT: 
       pygame.quit() 
       sys.exit() 

     for k in key2mvmt.keys(): 
      if key2mvmt[k]: 
       move_rect(playerObj['rect'], k, movement) 

     else: 
      print('stopped') 

     pygame.display.update() 
     FPS_CLOCK.tick(FPS) 

if __name__ == '__main__': 
    main()