Commit 12a82da2 authored by gem2578's avatar gem2578

Fix moment/collision system

parent 925b11b7
......@@ -9,3 +9,6 @@ GOAL = os.path.join(path, "resource/goal.png")
BALL = (100, 100, 100)
LEFT = (200, 10, 10)
RIGHT = (10, 10, 200)
PLAYER_SPEED = 5
BALL_SPEED = 5
......@@ -4,8 +4,6 @@ import sprite.round_sprite as rs
class Ball(rs.Round):
max_speed = 5
def __init__(self, pos):
super().__init__(20, config.BALL)
self.start = pos
......@@ -18,12 +16,7 @@ class Ball(rs.Round):
self.players.append(player)
def update(self):
if self.force.length() > self.max_speed:
self.force.scale_to_length(self.max_speed)
self.rect.move_ip(self.force)
while self.collide():
back = (self.force * -1).normalize()
self.rect.move_ip(self.round_vector2(back))
self.move_with_collision(config.BALL_SPEED, self.force)
self.force = pygame.math.Vector2()
def reset(self):
......
......@@ -5,8 +5,6 @@ from model.side import Side
class Player(rs.Round):
max_speed = 5
def __init__(self, brain, pos):
pos = pygame.math.Vector2(pos)
self.brain = brain
......@@ -29,12 +27,7 @@ class Player(rs.Round):
move, kick = self.brain.action()
if can_kick:
self.ball.kick(kick)
if move.length() > self.max_speed:
move.scale_to_length(self.max_speed)
self.rect.move_ip(move)
while self.collide():
back = (move * -1).normalize()
self.rect.move_ip(self.round_vector2(back))
self.move_with_collision(config.PLAYER_SPEED, move)
def collide(self):
for player in self.self_team:
......
from abc import ABC, abstractmethod
import pygame
class Round(pygame.sprite.Sprite):
class Round(ABC, pygame.sprite.Sprite):
def __init__(self, diameter, colour):
super().__init__()
......@@ -14,3 +15,31 @@ class Round(pygame.sprite.Sprite):
x = round(vect[0])
y = round(vect[1])
return pygame.math.Vector2(x,y)
@abstractmethod
def collide(self):
raise NotImplementedError('collide() has not been setup')
def move_with_collision(self, speed, move_force ):
#Set max_distance
move_force = self.round_vector2(move_force)
if move_force.length() > speed:
max_distance = speed
else:
max_distance = int(move_force.length())
distance_moved = 1
# Get current state
start = self.rect.copy()
end = self.rect.copy()
# Test distance 1 to max
while distance_moved <= max_distance:
self.rect = start.copy()
move_force.scale_to_length(distance_moved)
self.rect.move_ip(self.round_vector2(move_force))
if self.collide():
break
else:
end = self.rect.copy()
distance_moved += 1
# move to end point
self.rect = end.copy()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment