【问题标题】:How to restrict movement of a Pygame character?如何限制 Pygame 角色的移动?
【发布时间】:2019-10-06 07:14:17
【问题描述】:

我正在 Pygame 中构建一个游戏,其中涉及使用箭头键移动一个红色矩形(玩家)。我已经让玩家使用箭头键移动(箭头键控制速度,回车键确认移动),但我需要能够限制玩家每回合能够移动的量。我需要使新的速度/位置最多只能向上/向下 20 px 和向左/向右 20 px(20 px 表示按两次箭头键)。

目前,玩家按照方向键设置的速度移动,但速度随着方向键无限增加/减少。一旦在任一方向(上/下,左/右)按下箭头键最多 2 次,我需要它停止更改。

这是控制速度的代码:

if event.type == pygame.KEYDOWN:
    if p1_turn:
        if event.key == pygame.K_RIGHT:
            p1_velocity_x += 10
        if event.key == pygame.K_LEFT:
                p1_velocity_x -= 10    

这是确认更改的代码(实际上是移动玩家):

if event.key == pygame.K_RETURN:
    if p1_turn:
        p1.y += p1_velocity_y
        p1.x += p1_velocity_x
        p1_turn = False
        p2_turn = True

如前所述,应该有一些机制来阻止速度从原始 x 速度和原始 y 速度超过 20px 增加/减少。

【问题讨论】:

  • 而不是像大多数答案所暗示的那样使用 if 语句。您可以将位置限制在最小值和最大值之间
  • 检查p1_velocity_xp1_velocity_y的当前值,不要增加它,否则它们会超出范围。

标签: python oop pygame game-development


【解决方案1】:

只需使用if statements:

original_vel_x = 0

if p1_turn:
    if event.key == pygame.K_RIGHT:
        if p1_velocity_x <= original_vel_x + 20:  # I used <= in case it's being increased with floats
            p1_velocity_x += 10
        else: 
            print('Cannot Move')

【讨论】:

  • 没有效果。我将在哪里/如何定义 original_vel_x?
  • 无论你的原始速度是多少,它都是一个占位符
  • 我以前尝试过类似的方法,所以你的想法是对的,但我不知道为什么它没有效果。
  • 好吧,我还没有定义“原始速度”。我与速度相关的唯一变量是 p1_velocity_x。
  • 那么你的原始速度是 0 吗?
【解决方案2】:
import pygame

...


#Change those to whatever you want.
maximux_x = 20
minimum_x = 10

#This holds the players velocity on x.
p1_velocity_x = 0

def ChangeVelocity(change):
    global p1_velocity_x

    #cacl new x.
    new_x = p1_velocity_x + change


    if new_x < minimum_x:
        return

    if new_x > maximux_x:
        return

    #Change is inside the restrictions, so apply the change.
    p1_velocity_x += change

...



if event.type == pygame.KEYDOWN:

    if p1_turn:

        if event.key == pygame.K_RIGHT:
            ChangeVelocity(10)

        if event.key == pygame.K_LEFT:
            ChangeVelocity(-10)

为了更好地控制,您应该使用类创建精灵对象。示例如下:

import pygame


class Vector:

    def __init__(self, x, y):
        self.x = x
        self.y = y


class Player:

    def __init__(self):
        self.pos      = Vector(0,0)
        self.maxX     = 20
        self.maxY     = 10


    def MoveOnX(self, change):
        global p1_velocity_x

        #cacl new x.
        new_x = self.pos.x + change


        if new_x < self.maxX:
            return


        if new_x > self.maxY:
            return

        #Change is inside the restrictions, so apply the change.
        self.pos.x += change


#Create a new player and spawn him at (5,5)
player = Player()
player.pos.x = 5
player.pos.y = 5


if event.type == pygame.KEYDOWN:

    if p1_turn:

        if event.key == pygame.K_RIGHT:
            player.MoveOnX(10)

        if event.key == pygame.K_LEFT:
            player.MoveOnX(-10)

【讨论】:

  • 这在大部分情况下都有效,但是 minimum_x 和 maximum_x 变量需要随着播放器的新 x 位置而改变。每次轮到玩家时,都需要设置新的限制,将其从当前位置每边的移动范围限制为 20 像素。
  • 每次将玩家的位置更改为等于minimux_x = player_x - 20maximum_x = player_x + 20时更新minimux_x和maximum_x
  • 如果您能对我的答案投票并接受它吗?另外,如果您愿意,请在我回答您的其他问题的地方对我的评论进行投票。
  • 我的声望低于 15,所以我的投票没有显示,但我点击了投票按钮。
猜你喜欢
  • 2019-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多