2013-10-16 89 views
0

即時嘗試將我的代碼拆分爲函數,我想輸入我自己的值爲x和y。所以角色可以從輸入的值移動。當我嘗試從函數調用中完成時,沒有任何反應。函數調用python(pygame)

有什麼建議嗎?

import pygame, sys 
from pygame.locals import * 

pygame.init() 

width,height=(842,595) 
screen = pygame.display.set_mode((width,height),0,32) 
pygame.display.set_caption("game") 
man = pygame.image.load("man.png") 
target = pygame.image.load("star.png") 

#setting a background image 
bgimage= pygame.image.load("background.jpg") 
##image for the jupiter object 
another_target = pygame.image.load("jupiter.gif") 


x = 100 
y = height-300 

another_targetx = 400 
another_targety = 500 

#allows movement whilst holding down key. 

clock= pygame.time.Clock() 

def move(x,y): 
    movingX =0 
    movingY =0 

    speedX =0 
    speedY=0 
    while True: 
     pygame.display.update() 
     for event in pygame.event.get(): 
      if event.type==QUIT: 
       pygame.quit() 
       sys.exit() 
      elif event.type==KEYDOWN: 
       if event.key ==K_LEFT: 
        x-=5 
       elif event.key==K_RIGHT: 
        x+=5 
       elif event.key==K_UP: 
        y-=5 
       elif event.key==K_DOWN: 
        y+=5 

     time_Passed=clock.tick(25) 
     time_elapsed_seconds=time_Passed/1000.0 

     distanceX = time_elapsed_seconds*speedX 
     movingX+=distanceX 

     distanceY=time_elapsed_seconds*speedY 
     movingY+=distanceY 

     x+=movingX 
     y+=movingY 
     clock.tick(50) 
    pygame.display.update() 

move(x,y) 
screen.blit(bgimage,(0,0)) 
screen.blit(man, (x,y)) 
screen.blit(another_target,(another_targetx, another_targety)) 
screen.blit(target,(200,400)) 
pygame.display.update()     
+0

你真的應該在你的問題標題上工作。 *調用函數python(pygame)*,*函數調用python(pygames)*和* python(pygame)中的函數調用*並不真正有用:-) – sloth

回答

1

在命令move(x, y)中有一個無限循環。這個循環外面的東西永遠不會到達屏幕,所以沒有任何事情會發生。嘗試將命令定義後的所有內容放入命令中的while True循環中。