1

I have been learning pygame from a youtuber called dafluffypotatoe and I have written my own code for player movement I was wondeirng if it would be possible to fix the diagonal movement? Because it goes twice as fast when I go diagonal.

also I am new to stack over flow.

Here is my paste bin for the code I used

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)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

You must move the player with a normalized directional vector and a constant speed. The simplest method is to use 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

    # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174