如何修复对角线移动?
How do I fix diagonal movement?
我一直在向一个名叫 dafluffypotatoe 的 youtuber 学习 pygame,并且我已经编写了自己的玩家移动代码 我想知道是否可以修复对角线移动?因为当我走对角线时它的速度是原来的两倍。
我也是stack over flow的新手。
这是我使用的代码的粘贴箱
import pygame, sys
from pygame.locals import *
# init pygame
pygame.init()
# clock
clock = pygame.time.Clock()
WINDOW_SIZE = 1024, 512
# screen and display
screen = pygame.display.set_mode(WINDOW_SIZE)
display = pygame.Surface((512, 256))
FPS = 60
moving_left = False
moving_right = False
moving_up = False
moving_down = False
player_x_loc = 50
player_y_loc = 50
def load_map(path):
f = open(path + '.txt', 'r')
data = f.read()
f.close()
data = data.split('\n')
game_map = []
for row in data:
game_map.append(list(row))
return game_map
game_map = load_map('map')
# player
player_img = pygame.image.load('player.png')
# blocks
block_basic = pygame.image.load('blocck.png')
# game loop
while True:
# colour screen
display.fill((33, 15, 15))
# load tiles
tile_rects = []
y = 0
for row in game_map:
x = 0
for tile in row:
if tile == '1':
display.blit(block_basic, (x * 32, y * 32))
if tile == '0':
tile_rects.append(pygame.Rect(x * 32, y * 32, 32, 32))
x += 1
y += 1
# basic speed x
if moving_left == True:
player_x_loc -= 4
if moving_right == True:
player_x_loc += 4
else:
player_x_loc == 0
# basic speed y
if moving_up == True:
player_y_loc -= 4
if moving_down == True:
player_y_loc += 4
else:
player_y_loc == 0
display.blit(player_img, (player_x_loc, player_y_loc))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# keydown
if event.type == KEYDOWN:
if event.key == K_a:
moving_left = True
if event.key == K_d:
moving_right = True
if event.key == K_w:
moving_up = True
if event.key == K_s:
moving_down = True
# keyup
if event.type == KEYUP:
if event.key == K_a:
moving_left = False
if event.key == K_d:
moving_right = False
if event.key == K_w:
moving_up = False
if event.key == K_s:
moving_down = False
screen.blit(pygame.transform.scale(display, WINDOW_SIZE), (0, 0))
pygame.display.update()
clock.tick(FPS)
您必须使用归一化方向矢量和恒定速度移动玩家。最简单的方法是使用 pygame.math.Vector2.scale_to_length
:
while True:
# [...]
move_vec = pygame.math.Vector2(
moving_right - moving_left,
moving_down - moving_up)
if move_vec.x != 0 or move_vec.y != 0:
move_vec.scale_to_length(4)
player_x_loc += move_vec.x
player_y_loc += move_vec.y
# [...]
我一直在向一个名叫 dafluffypotatoe 的 youtuber 学习 pygame,并且我已经编写了自己的玩家移动代码 我想知道是否可以修复对角线移动?因为当我走对角线时它的速度是原来的两倍。
我也是stack over flow的新手。
这是我使用的代码的粘贴箱
import pygame, sys
from pygame.locals import *
# init pygame
pygame.init()
# clock
clock = pygame.time.Clock()
WINDOW_SIZE = 1024, 512
# screen and display
screen = pygame.display.set_mode(WINDOW_SIZE)
display = pygame.Surface((512, 256))
FPS = 60
moving_left = False
moving_right = False
moving_up = False
moving_down = False
player_x_loc = 50
player_y_loc = 50
def load_map(path):
f = open(path + '.txt', 'r')
data = f.read()
f.close()
data = data.split('\n')
game_map = []
for row in data:
game_map.append(list(row))
return game_map
game_map = load_map('map')
# player
player_img = pygame.image.load('player.png')
# blocks
block_basic = pygame.image.load('blocck.png')
# game loop
while True:
# colour screen
display.fill((33, 15, 15))
# load tiles
tile_rects = []
y = 0
for row in game_map:
x = 0
for tile in row:
if tile == '1':
display.blit(block_basic, (x * 32, y * 32))
if tile == '0':
tile_rects.append(pygame.Rect(x * 32, y * 32, 32, 32))
x += 1
y += 1
# basic speed x
if moving_left == True:
player_x_loc -= 4
if moving_right == True:
player_x_loc += 4
else:
player_x_loc == 0
# basic speed y
if moving_up == True:
player_y_loc -= 4
if moving_down == True:
player_y_loc += 4
else:
player_y_loc == 0
display.blit(player_img, (player_x_loc, player_y_loc))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# keydown
if event.type == KEYDOWN:
if event.key == K_a:
moving_left = True
if event.key == K_d:
moving_right = True
if event.key == K_w:
moving_up = True
if event.key == K_s:
moving_down = True
# keyup
if event.type == KEYUP:
if event.key == K_a:
moving_left = False
if event.key == K_d:
moving_right = False
if event.key == K_w:
moving_up = False
if event.key == K_s:
moving_down = False
screen.blit(pygame.transform.scale(display, WINDOW_SIZE), (0, 0))
pygame.display.update()
clock.tick(FPS)
您必须使用归一化方向矢量和恒定速度移动玩家。最简单的方法是使用 pygame.math.Vector2.scale_to_length
:
while True:
# [...]
move_vec = pygame.math.Vector2(
moving_right - moving_left,
moving_down - moving_up)
if move_vec.x != 0 or move_vec.y != 0:
move_vec.scale_to_length(4)
player_x_loc += move_vec.x
player_y_loc += move_vec.y
# [...]