From 928ad097a5aab5b8a790208bea22dd8a6ff0c083 Mon Sep 17 00:00:00 2001 From: GreenXenith <24834740+GreenXenith@users.noreply.github.com> Date: Mon, 24 Feb 2020 17:46:44 -0800 Subject: [PATCH] Make player speed consistent in all directions --- player.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/player.py b/player.py index 0754f2b..874bb9b 100644 --- a/player.py +++ b/player.py @@ -1,5 +1,6 @@ import pygame import controller +import math from vector import * from sprite import Sprite @@ -23,27 +24,26 @@ class Player: self.pos = vec_or_num(vec_or_x, y) def update(self, dtime, map): - vx = 0 - vy = 0 + self.vel = Vector(0, 0) if controller.is_down("left"): self.dir = 3 - vx -= 1 + self.vel.x -= 1 if controller.is_down("right"): self.dir = 1 - vx += 1 + self.vel.x += 1 if controller.is_down("up"): self.dir = 2 - vy -= 1 + self.vel.y -= 1 if controller.is_down("down"): self.dir = 0 - vy += 1 + self.vel.y += 1 - self.vel.x = vx - self.vel.y = vy + if self.vel.x != 0 and self.vel.y != 0: + self.vel = self.vel * math.sqrt(0.5) oldx = self.pos.x self.set_pos(self.pos.x + self.vel.x * self.speed * dtime, self.pos.y)