Commit 908cfab7 authored by gem2578's avatar gem2578

Remove side from team and made pos mirrored

parent 141bf5c9
from model.object_type import ObjectType
from model.cup_object import CupObject
from pygame.math import Vector2
class Brain():
def __init__(self, side):
self.side = side
def setup(self, goals):
self.self_goal = CupObject(ObjectType.GOAL, goals.get(self.side).rect.center, self.side)
other_side = self.side.other()
self.other_goal = CupObject(ObjectType.GOAL, goals.get(other_side).rect.center, other_side)
def pre_action(self, body):
self.ball = CupObject(ObjectType.BALL, body.ball.rect().center)
self.pos = Vector2(body.rect.center)
kick_range = body.radius + body.ball.radius() + 5
self.can_kick = self.pos.distance_to(self.ball.pos) < kick_range
self.self_team = []
for player in body.self_team:
if(player != body):
self.self_team.append(CupObject(ObjectType.PLAYER, player.rect.center, self.side))
self.other_team = []
for player in body.other_team:
self.other_team.append(CupObject(ObjectType.PLAYER, player.rect.center, self.side.other()))
return self.can_kick
def sorted_object(self):
objects = [self.ball, self.self_goal, self.other_goal] + self.self_team + self.other_team
objects.sort(key = lambda agent: self.pos.distance_to(agent.pos))
return objects
def action(self):
raise Exception('action() have not been setup')
from sprite.player import Brain
from brain.brain import Brain
from pygame.math import Vector2
class FollowBall(Brain):
......
SIZE = (800,500)
BACKGROUND = "resource/field.png"
GOAL = "resource/goal.png"
BALL = (100, 100, 100)
LEFT = (200, 10, 10)
RIGHT = (10, 10, 200)
import pygame
from sprite.player import *
import config
from sprite.player import Player
from brain.brain import Brain
class Team(object):
def __init__(self, side):
def __init__(self):
super().__init__()
self.side = side
self.brains = []
def put(self, BrainClass, pos):
if not issubclass(BrainClass, Brain):
raise Exception('The class passed dose not inherit Brain.')
half = int(config.SIZE[0]/2)
if pos[0] > half or pos[0] < 0:
raise Exception('The x value must be between 0 and '+ str(half))
if pos[1] > config.SIZE[1] or pos[1] < 0:
raise Exception('The y value must be between 0 and '+ str(config.SIZE[1]))
self.brains.append({"class": BrainClass, "pos": pos})
def get_group(self):
return TeamGroup(self.side, self.brains)
def get_group(self, side):
return TeamGroup(side, self.brains)
class TeamGroup(pygame.sprite.Group):
......
import pygame
import config
from group.team import Team
from group.team_item import TeamItem
from group.team_goal import TeamGoal
......@@ -9,9 +10,8 @@ from brain.follow_ball import FollowBall
pygame.init()
pygame.display.set_caption("Python Cup")
background = pygame.image.load("resource/field.png")
screen = pygame.display.set_mode(background.get_size())
background = pygame.image.load(config.BACKGROUND)
screen = pygame.display.set_mode(config.SIZE)
background = background.convert()
class Match(object):
......@@ -30,14 +30,9 @@ class Match(object):
screen.blit(background, (0, 0))
def play(self, team_left, team_right):
if team_left.side != Side.LEFT:
raise Exception('the first must be the left team')
if team_right.side != Side.RIGHT:
raise Exception('the second must be the right team')
self.reset()
self.team_left = team_left.get_group()
self.team_right = team_right.get_group()
self.team_left = team_left.get_group(Side.LEFT)
self.team_right = team_right.get_group(Side.RIGHT)
self.team_left.setup(self.ball,self.goals,self.team_right)
self.team_right.setup(self.ball,self.goals,self.team_left)
self.ball.setup(self.team_left, self.team_right)
......@@ -87,16 +82,16 @@ class Match(object):
def main():
team_left = Team(Side.LEFT)
empty_team = Team()
team_right = Team(Side.RIGHT)
team_right.put(FollowBall, (700, 250))
one_player_team = Team()
one_player_team.put(FollowBall, (100, 300))
match = Match()
match.play(team_left, team_right)
match.play(one_player_team, empty_team)
print(match.get_score())
match.play(team_left, team_right)
match.play(empty_team, one_player_team)
print(match.get_score())
if __name__ == "__main__":
......
......@@ -6,12 +6,14 @@ from brain.follow_ball import FollowBall
def main():
team_left = Team(Side.LEFT)
team_left.put(FollowBall, (100, 250))
team_a = Team()
team_a.put(FollowBall, (100, 250))
team_right = Team(Side.RIGHT)
team_b = Team()
# team_b.put(FollowBall, (100, 250))
match = Match(team_left, team_right)
match = Match()
match.play(team_a, team_b)
print(match.get_score())
if __name__ == "__main__":
......
import pygame
import config
import sprite.round_sprite as rs
class Ball(rs.Round):
......@@ -6,7 +7,7 @@ class Ball(rs.Round):
max_speed = 5
def __init__(self, pos):
super().__init__(20, (100, 100, 100))
super().__init__(20, config.BALL)
self.start = pos
self.reset()
......
import pygame
import config
from model.side import Side
class Goal(pygame.sprite.Sprite):
......@@ -6,7 +7,7 @@ class Goal(pygame.sprite.Sprite):
def __init__(self, side):
super().__init__()
self.side = side
self.image = pygame.image.load("resource/goal.png").convert_alpha()
self.image = pygame.image.load(config.GOAL).convert_alpha()
self.rect = self.image.get_rect()
if side == Side.LEFT:
......
import pygame
import config
import sprite.round_sprite as rs
from model.side import Side
from model.object_type import ObjectType
from model.cup_object import CupObject
class Player(rs.Round):
max_speed = 5
def __init__(self, brain, pos):
pos = pygame.math.Vector2(pos)
self.brain = brain
if self.brain.side == Side.LEFT:
colour = (200, 10, 10)
colour = config.LEFT
else:
colour = (10, 10, 200)
colour = config.RIGHT
pos = pygame.math.Vector2(config.SIZE[0] - pos[0],pos[1])
super().__init__(30, colour)
self.rect.center = pos
......@@ -43,39 +44,3 @@ class Player(rs.Round):
if pygame.sprite.collide_circle(self, player):
return True
return pygame.sprite.collide_circle(self, self.ball.sprite)
class Brain():
def __init__(self, side):
self.side = side
def setup(self, goals):
self.self_goal = CupObject(ObjectType.GOAL, goals.get(self.side).rect.center, self.side)
other_side = self.side.other()
self.other_goal = CupObject(ObjectType.GOAL, goals.get(other_side).rect.center, other_side)
def pre_action(self, body):
self.ball = CupObject(ObjectType.BALL, body.ball.rect().center)
self.pos = pygame.math.Vector2(body.rect.center)
kick_range = body.radius + body.ball.radius() + 5
self.can_kick = self.pos.distance_to(self.ball.pos) < kick_range
self.self_team = []
for player in body.self_team:
if(player != body):
self.self_team.append(CupObject(ObjectType.PLAYER, player.rect.center, self.side))
self.other_team = []
for player in body.other_team:
self.other_team.append(CupObject(ObjectType.PLAYER, player.rect.center, self.side.other()))
return self.can_kick
def sorted_object(self):
objects = [self.ball, self.self_goal, self.other_goal] + self.self_team + self.other_team
objects.sort(key = lambda agent: self.pos.distance_to(agent.pos))
return objects
def action(self):
raise Exception('action() have not been setup')
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