如何限制 Pygame 角色的移动?
How to restrict movement of a Pygame character?
我正在 Pygame 中构建一个游戏,其中涉及使用箭头键移动红色矩形(玩家)。我已经让玩家使用箭头键移动(箭头键控制速度,回车键确认移动),但我需要能够限制玩家每回合能够移动的数量。我需要做的是让新的velocity/position最大只能是20pxup/down和20pxleft/right(20px就是按方向键两次)
目前,玩家根据箭头键设置的速度移动,但速度increases/decreases 与箭头键一起无限移动。一旦在任一方向最多按下箭头键 2 次(up/down、left/right),我需要它停止更改。
这里是控制速度的代码:
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 increase/decrease。
只需使用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')
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)
我正在 Pygame 中构建一个游戏,其中涉及使用箭头键移动红色矩形(玩家)。我已经让玩家使用箭头键移动(箭头键控制速度,回车键确认移动),但我需要能够限制玩家每回合能够移动的数量。我需要做的是让新的velocity/position最大只能是20pxup/down和20pxleft/right(20px就是按方向键两次)
目前,玩家根据箭头键设置的速度移动,但速度increases/decreases 与箭头键一起无限移动。一旦在任一方向最多按下箭头键 2 次(up/down、left/right),我需要它停止更改。
这里是控制速度的代码:
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 increase/decrease。
只需使用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')
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)