fix: game: 2d work

This commit is contained in:
2024-04-10 15:40:54 +00:00
parent 8b9c4d7c1c
commit 0b7e72d8e2
10 changed files with 41 additions and 39 deletions

View File

@ -20,7 +20,6 @@ class Point:
return dist((point.x, point.y), (self.x, self.y))
def copy(self):
return Point(self.x, self.y)
def to_dict(self) -> dict:

View File

@ -47,12 +47,16 @@ class PongPlayer(APlayer):
def update_position(self, data: dict):
new_position: Position = Position()
new_position: Position = Position(None)
new_position.location = data.get("position")
position_dict = data.get("position")
if (position_dict is None):
self.send_error("missing position")
return
if (new_position.position is None):
self.send_error("missing new_position")
new_position.location = position_dict.get("location")
if (new_position.location is None):
self.send_error("missing location")
return
new_position.time = data.get("time")
@ -65,9 +69,9 @@ class PongPlayer(APlayer):
self.game_member.send_error("time error")
return
distance: float = abs(self.position.position - new_position.position)
distance: float = abs(self.position.location - new_position.location)
sign: int = 1 if self.position.position >= new_position.position else -1
sign: int = 1 if self.position.location >= new_position.location else -1
time_difference: float = (new_position.time - self.position.time) / 1000
@ -76,19 +80,19 @@ class PongPlayer(APlayer):
new_position_verified: Position = new_position.copy()
if (distance > max_distance):
new_position_verified.position = self.position.position + max_distance * sign
new_position_verified.location = self.position.location + max_distance * sign
if (not config.PADDLE_POSITION_MIN <= new_position_verified.position <= config.PADDLE_POSITION_MAX):
if (not config.PADDLE_POSITION_MIN <= new_position_verified.location <= config.PADDLE_POSITION_MAX):
new_position_verified.position = max(new_position_verified.position, config.PADDLE_POSITION_MIN)
new_position_verified.position = min(new_position_verified.position, config.PADDLE_POSITION_MAX)
new_position_verified.location = max(new_position_verified.location, config.PADDLE_POSITION_MIN)
new_position_verified.location = min(new_position_verified.location, config.PADDLE_POSITION_MAX)
invalid_pos: bool = new_position.position != new_position_verified.position
invalid_pos: bool = new_position.location != new_position_verified.location
if (new_position != self.position):
if (new_position.location != self.position.location):
self.game.update_player(self)
self.position = new_position
self.position.location = new_position.location
if (invalid_pos):
self.send("update_player", self.to_dict())

View File

@ -8,7 +8,10 @@ class Position:
self.location: float = location
def copy(self):
return Position(self.location, self.time)
try:
return Position(self.location.copy(), self.time)
except:
return Position(self.location, self.time)
def to_dict(self):