Pygame:我如何让精灵始终向右移动?

时间:2023-02-08 08:35:10

I want my sprite (a 2d top down fighter plane) to always move to the sprites'a right. I currently have it so the plane rotates to where the mouse pointer is. Now I need the plane to always be moving and so you can rotate it with your mouse while the plane is flying. A short code example would be excellent as I am not very experienced. The sprite is "plane.png". I am using Python 2.7 if that matters. Thanks!

我希望我的精灵(一架2d自上而下的战斗机)始终向精灵方向移动。我现在有它,所以飞机旋转到鼠标指针所在的位置。现在我需要飞机始终在移动,因此您可以在飞机飞行时用鼠标旋转它。一个简短的代码示例将是非常好的,因为我不是很有经验。精灵是“plane.png”。如果重要的话,我正在使用Python 2.7。谢谢!

1 个解决方案

#1


There are actually a few things you may want to clarify before you start implementing anything. First, do you actually want the sprite to move in relation to the window, or do you want the sprite to stay stationary and have the background scroll, giving the player the impression that the sprite is moving across your game's landscape?

在开始实施任何事情之前,您可能想要澄清一些事情。首先,你真的希望精灵相对于窗口移动,或者你是否希望精灵保持静止并拥有背景滚动,让玩家觉得精灵正在你的游戏景观中移动?

Assuming you want the sprite itself to move, you'd want to create a method within the sprite's object that constantly updates it's x position to a slightly higher number

假设你想要精灵本身移动,你想在精灵的对象中创建一个方法,不断地将它的x位置更新为略高的数字

while True:
    xpos += 1
    display_image = pygame.image.load(plane.png)
    display.blit(display_image, [xpos, ypos])

The above snippet while increment the x position of your sprite by 1 every frame. If you haven't already set a limit to the number of frames rendered per second, I'd suggest taking a look at creating a clock in pygame and calling the tick function every time the game loop runs

上面的片段,同时每帧增加精灵的x位置1。如果你还没有设置每秒渲染帧数的限制,我建议你看看在pygame中创建一个时钟并在每次游戏循环运行时调用tick函数

Alternatively, you could do the same as above, but pass in the mouse cursor's current coordinates to have the sprite follow your cursor.

或者,您可以执行与上面相同的操作,但是传入鼠标光标的当前坐标以使精灵跟随您的光标。

If you'd rather have the sprite stay stationary and move the background, you could do the same thing for each element of your background, just in reverse.

如果您希望精灵保持静止并移动背景,您可以对背景中的每个元素执行相同的操作,反之亦然。

while True:
    xpos -= 1
    display_image = pygame.image.load(background.png)
    display.blit(display_image, [xpos, ypos]

Hope this is helpful!

希望这有用!

#1


There are actually a few things you may want to clarify before you start implementing anything. First, do you actually want the sprite to move in relation to the window, or do you want the sprite to stay stationary and have the background scroll, giving the player the impression that the sprite is moving across your game's landscape?

在开始实施任何事情之前,您可能想要澄清一些事情。首先,你真的希望精灵相对于窗口移动,或者你是否希望精灵保持静止并拥有背景滚动,让玩家觉得精灵正在你的游戏景观中移动?

Assuming you want the sprite itself to move, you'd want to create a method within the sprite's object that constantly updates it's x position to a slightly higher number

假设你想要精灵本身移动,你想在精灵的对象中创建一个方法,不断地将它的x位置更新为略高的数字

while True:
    xpos += 1
    display_image = pygame.image.load(plane.png)
    display.blit(display_image, [xpos, ypos])

The above snippet while increment the x position of your sprite by 1 every frame. If you haven't already set a limit to the number of frames rendered per second, I'd suggest taking a look at creating a clock in pygame and calling the tick function every time the game loop runs

上面的片段,同时每帧增加精灵的x位置1。如果你还没有设置每秒渲染帧数的限制,我建议你看看在pygame中创建一个时钟并在每次游戏循环运行时调用tick函数

Alternatively, you could do the same as above, but pass in the mouse cursor's current coordinates to have the sprite follow your cursor.

或者,您可以执行与上面相同的操作,但是传入鼠标光标的当前坐标以使精灵跟随您的光标。

If you'd rather have the sprite stay stationary and move the background, you could do the same thing for each element of your background, just in reverse.

如果您希望精灵保持静止并移动背景,您可以对背景中的每个元素执行相同的操作,反之亦然。

while True:
    xpos -= 1
    display_image = pygame.image.load(background.png)
    display.blit(display_image, [xpos, ypos]

Hope this is helpful!

希望这有用!