函数调用python(pygame)

时间:2013-10-16 21:01:02

标签: python pygame

我试图将我的代码拆分成函数,我想输入我自己的x和y值。所以角色可以从输入的值中移动。当我尝试从函数调用中执行此操作时,没有任何反应。

有什么建议吗?

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()                

1 个答案:

答案 0 :(得分:1)

命令move(x, y)中有一个无限循环。从来没有达到更新屏幕的循环外部的东西,所以似乎没有任何事情发生。尝试将命令定义之后的所有内容放入命令内的while True循环中。