0

我试图将我的代码拆分为函数,我想输入我自己的 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()                
4

1 回答 1

1

您在 command 中有一个无限循环move(x, y)。永远不会到达此循环外部更新屏幕的内容,因此似乎什么也没有发生。尝试将命令定义之后的所有内容放入命令内的while True循环中。

于 2013-10-16T21:09:30.337 回答