Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让玩家不断朝一个方向移动_Python_Pygame - Fatal编程技术网

Python 如何让玩家不断朝一个方向移动

Python 如何让玩家不断朝一个方向移动,python,pygame,Python,Pygame,我正在做一个游戏,我需要玩家不断朝一个方向移动,直到玩家决定朝另一个方向移动。在我提供的代码中,玩家必须按住一个键才能朝那个方向移动 以下是我的播放器代码和移动功能: class player: def __init__(self, x, y, width, height, colour): self.width = width # dimensions of player self.height = height # dimensions of pl

我正在做一个游戏,我需要玩家不断朝一个方向移动,直到玩家决定朝另一个方向移动。在我提供的代码中,玩家必须按住一个键才能朝那个方向移动

以下是我的播放器代码和移动功能:

class player:
    def __init__(self, x, y, width, height, colour):
        self.width = width  # dimensions of player
        self.height = height  # dimensions of player
        self.x = x  # position on the screen
        self.y = y  # position on the screen
        self.colour = colour  # players colour
        self.rect = (x, y, width, height)  # all the players properties in one
        self.vel = 2  # how far/fast you move with each key press
        self.path = []

    def draw(self, win):
        pygame.draw.rect(win, self.colour, self.rect)

    def move(self):
        keys = pygame.key.get_pressed()  # dictionary of keys - values of 0/1

        if keys[pygame.K_LEFT]:  # move left: minus from x position value
            if self.x <= 5:
                pass
            else:
                self.x -= self.vel
                self.y = self.y

        elif keys[pygame.K_RIGHT]:  # move right: add to x position value
            if self.x == 785:
                pass
            else:
                self.x += self.vel

        elif keys[pygame.K_UP]:  # move up: minus from y position value
            if self.y <= 105:
                pass
            else:
                self.y -= self.vel

        elif keys[pygame.K_DOWN]:  # move down from
            if self.y >= 785:
                pass
            else:
                self.y += self.vel

        self.update()

    def update(self):
        self.rect = (self.x, self.y, self.width, self.height)  # redefine where the player is

一种方法是只在
player
类中设置移动量。游戏的每一帧,玩家都会以设定的速度移动一点。运动的每个方向分量都保持单独的速度。这使得玩家可以对角移动

class Player():
    def __init__( self ):
        ...
        self.x_vel = 0
        self.y_vel = 0

    def move(self):
        keys = pygame.key.get_pressed()  # dictionary of keys - values of 0/1
        if keys[pygame.K_LEFT]:  # move left: minus from x position value
            self.x_vel = -1
        if keys[pygame.K_RIGHT]:
            self.x_vel = 1     # move right: plus from x position value
        # etc. for up/down

    def update( self ):
        self.x += self.x_vel
        self.y += self.y_vel
在主循环中,只需调用
player.update()
函数

这种方法的缺点是屏幕速度与帧速率有关。在这种情况下,代码可以使用pygame毫秒时钟来使用以像素/秒为单位的实际实时速度。或者根据实际FPS计算速度,并相应地设置
player.x_vel
(等)

class Player():
    def __init__( self ):
        ...
        self.x_vel = 0
        self.y_vel = 0

    def move(self):
        keys = pygame.key.get_pressed()  # dictionary of keys - values of 0/1
        if keys[pygame.K_LEFT]:  # move left: minus from x position value
            self.x_vel = -1
        if keys[pygame.K_RIGHT]:
            self.x_vel = 1     # move right: plus from x position value
        # etc. for up/down

    def update( self ):
        self.x += self.x_vel
        self.y += self.y_vel