Я создаю игру с использованием pygame и хочу ограничить пользователя только возможностью перемещаться влево, вправо, вверх, вниз, но не по диагонали. Код, который я предоставил ниже, позволяет игроку перемещаться в пределах экрана. Как мне остановить движение по диагонали?
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 = 1 # how far/fast you move with each key press
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: # if player tries to go too far left
pass
else:
self.x -= self.vel
if keys[pygame.K_RIGHT]: # move right: add to x position value
if self.x == 785: # if player tries to go too far right
pass
else:
self.x += self.vel
if keys[pygame.K_UP]: # move up: minus from y position value
if self.y <= 105: # if player tries to go too far up
pass
else:
self.y -= self.vel
if keys[pygame.K_DOWN]: # move down from
if self.y >= 785: # if player tries to go too far down
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