game: multiplayer: work
This commit is contained in:
@ -10,18 +10,14 @@ import math
|
||||
class Ball:
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.size: float
|
||||
self.position: Position
|
||||
self.angle: float
|
||||
self.speed: float
|
||||
|
||||
self.reset()
|
||||
self.speed = 0
|
||||
self.size: float = config.BALL_SIZE
|
||||
self.position: Position = Position(Point(config.BALL_SPAWN_POS_X + self.size / 2, config.BALL_SPAWN_POS_Y + self.size / 2), time.time())
|
||||
self.angle: float = 0
|
||||
self.speed: float = 0
|
||||
|
||||
def reset(self) -> None:
|
||||
self.size = config.BALL_SIZE
|
||||
self.position = Position(Point(config.BALL_SPAWN_POS_X + self.size / 2, config.BALL_SPAWN_POS_Y + self.size / 2), time.time())
|
||||
self.angle = math.pi * 0
|
||||
def reset(self, target: Point) -> None:
|
||||
self.position: Position = Position(Point(config.BALL_SPAWN_POS_X + self.size / 2, config.BALL_SPAWN_POS_Y + self.size / 2), time.time())
|
||||
self.angle = math.atan2(target.y - self.position.location.y, target.x - self.position.location.x)
|
||||
self.speed = config.BALL_SPEED_START
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
|
@ -14,6 +14,7 @@ from django.contrib.auth.models import User
|
||||
|
||||
from ...routine import routine
|
||||
|
||||
import random
|
||||
import threading
|
||||
|
||||
class PongGame(AGame):
|
||||
@ -54,6 +55,7 @@ class PongGame(AGame):
|
||||
else:
|
||||
self.walls.append(Segment(corners[i].copy(), corners[(i + 1) % 4].copy()))
|
||||
|
||||
|
||||
self.ball: Ball = Ball()
|
||||
|
||||
def goal(self, goal_defenser: PongPlayer) -> None:
|
||||
@ -73,7 +75,7 @@ class PongGame(AGame):
|
||||
self.finish(player_list[0])
|
||||
return
|
||||
|
||||
self.ball.reset()
|
||||
self.ball.reset(goal_defenser.rail.center())
|
||||
|
||||
def get_valid_players(self) -> list[PongPlayer]:
|
||||
return [player for player in self.players if player.is_connected and not player.is_eliminated]
|
||||
@ -92,7 +94,9 @@ class PongGame(AGame):
|
||||
|
||||
self.broadcast("start")
|
||||
|
||||
self.ball.reset()
|
||||
players: list[PongPlayer] = self.get_valid_players()
|
||||
|
||||
self.ball.reset(players[random.randint(0, len(players) -1)].rail.center())
|
||||
|
||||
self.broadcast("update_ball", self.ball.to_dict())
|
||||
|
||||
|
@ -28,6 +28,9 @@ class Segment:
|
||||
def copy(self):
|
||||
return Segment(self.start.copy(), self.stop.copy())
|
||||
|
||||
def center(self):
|
||||
return Point((self.start.x + self.stop.x) / 2, (self.start.y + self.stop.y) / 2)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
|
||||
data: dict[str: dict] = {
|
||||
|
@ -16,8 +16,6 @@ import asyncio
|
||||
|
||||
import math
|
||||
|
||||
import time
|
||||
|
||||
from asgiref.sync import SyncToAsync
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user