Commit 80048113 authored by gem2578's avatar gem2578

Merge branch 'featuer/convert-to-class' into 'master'

Featuer/convert to class

See merge request !6
parents ce2902c8 56512d8d
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):
......
import os
path = os.path.dirname(__file__)
SIZE = (800,500)
BACKGROUND = os.path.join(path, "resource/field.png")
GOAL = os.path.join(path, "resource/goal.png")
BALL = (100, 100, 100)
LEFT = (200, 10, 10)
RIGHT = (10, 10, 200)
from pygame.sprite import GroupSingle
from sprite.ball import Ball
class SingleBall(GroupSingle):
def __init__(self, pos):
super().__init__(Ball(pos))
def setup(self, *teams):
self.sprite.setup(*teams)
def kick(self, vector):
self.sprite.kick(vector)
def reset(self):
self.sprite.reset()
def rect(self):
return self.sprite.rect
def radius(self):
return self.sprite.radius
import pygame
from sprite.player import *
import config
from sprite.player import Player
from brain.brain import Brain
class Team(pygame.sprite.Group):
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 ')
self.add(Player(BrainClass(self.side), pos))
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, side):
return TeamGroup(side, self.brains)
class TeamGroup(pygame.sprite.Group):
def __init__(self, side, brains):
super(TeamGroup, self).__init__()
for brain in brains:
self.add(Player(brain["class"](side), brain["pos"]))
def setup(self, ball, goals, other_team):
for player in self.sprites():
......
from group.team_item import TeamItem
from sprite.goal import Goal
class TeamGoal(TeamItem):
def __init__(self):
super().__init__(Goal)
def setup(self, ball, score_board):
for goal in self.sprites():
goal.setup(ball, score_board.get(goal.side.other()))
import pygame
from model.side import Side
class TeamsItem(pygame.sprite.Group):
class TeamItem(pygame.sprite.Group):
def __init__(self, spritetype):
super().__init__()
......@@ -15,3 +15,7 @@ class TeamsItem(pygame.sprite.Group):
return self.left
else:
return self.right
def reset(self):
for sprite in self.sprites():
sprite.reset()
import pygame
import config
from group.team import Team
from group.team_item import TeamItem
from group.team_goal import TeamGoal
from group.single_ball import SingleBall
from model.side import Side
from sprite.goal import Goal
from sprite.ball import Ball
from sprite.score import Score
from group.teams_item import TeamsItem
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()
screen.blit(background, (0, 0))
def run(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')
class Match(object):
clock = pygame.time.Clock()
def __init__(self):
super().__init__()
self.score_board = TeamItem(Score)
self.ball = SingleBall(screen.get_rect().center)
self.goals = TeamGoal()
self.goals.setup(self.ball, self.score_board)
goals = TeamsItem(Goal)
def reset(self):
self.ball.reset()
self.score_board.reset()
self.done = False
screen.blit(background, (0, 0))
ball = Ball(screen.get_rect().center)
ball.setup(team_left, team_right)
def play(self, team_left, team_right):
self.reset()
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)
team_left.setup(ball,goals,team_right)
team_right.setup(ball,goals,team_left)
clock = pygame.time.Clock()
score_board = TeamsItem(Score)
while not self.done:
clock.tick(30)
self.check_events()
self.clean_screen()
self.update()
self.draw()
pygame.display.flip()
for goal in goals.sprites():
goal.setup(ball)
while 1:
clock.tick(30)
#Handle Input Events
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
#Clean screen
goals.clear(screen, background)
ball.clear(screen, background)
team_left.clear(screen, background)
team_right.clear(screen, background)
score_board.clear(screen, background)
#Update
goals.update()
for goal in goals.sprites():
if goal.goal:
ball.reset()
score_board.get(goal.side.other()).increase()
ball.update()
team_left.update()
team_right.update()
score_board.update()
#Draw
goals.draw(screen)
ball.draw(screen)
team_left.draw(screen)
team_right.draw(screen)
score_board.draw(screen)
#Update screen
pygame.display.flip()
self.done = True
if event.type is pygame.KEYDOWN and event.key == pygame.K_F12:
global screen
if screen.get_flags() & pygame.FULLSCREEN:
screen = pygame.display.set_mode(config.SIZE)
else:
screen = pygame.display.set_mode(config.SIZE, pygame.FULLSCREEN)
screen.blit(background, (0, 0))
def clean_screen(self):
self.goals.clear(screen, background)
self.ball.clear(screen, background)
self.team_left.clear(screen, background)
self.team_right.clear(screen, background)
self.score_board.clear(screen, background)
def update(self):
self.goals.update()
self.ball.update()
self.team_left.update()
self.team_right.update()
self.score_board.update()
def draw(self):
self.goals.draw(screen)
self.ball.draw(screen)
self.team_left.draw(screen)
self.team_right.draw(screen)
self.score_board.draw(screen)
def get_score(self):
data = {}
for score in self.score_board:
data[score.side] = score.score
return data
def main():
empty_team = Team()
one_player_team = Team()
one_player_team.put(FollowBall, (100, 300))
match = Match()
match.play(one_player_team, empty_team)
print(match.get_score())
match.play(empty_team, one_player_team)
print(match.get_score())
if __name__ == "__main__":
main()
import pythocup
from group.team import *
from pythocup import Match
from group.team import Team
from model.side import Side
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_right.put(FollowBall, (700, 250))
team_b = Team()
# team_b.put(FollowBall, (100, 250))
pythocup.run(team_left, team_right)
match = Match()
match.play(team_a, team_b)
print(match.get_score())
if __name__ == "__main__":
main()
import pygame
import config
import sprite.round_sprite as rs
class Ball(pygame.sprite.GroupSingle):
def __init__(self, pos):
super().__init__(Ball_Sprite(pos))
def setup(self, *teams):
self.sprite.setup(*teams)
def kick(self, vector):
self.sprite.kick(vector)
def reset(self):
self.sprite.reset()
def rect(self):
return self.sprite.rect
def radius(self):
return self.sprite.radius
class Ball_Sprite(rs.Round):
class Ball(rs.Round):
max_speed = 5
force = pygame.math.Vector2()
players = []
def __init__(self, pos):
super().__init__(20, (100, 100, 100))
super().__init__(20, config.BALL)
self.start = pos
self.rect.center = self.start
self.reset()
def setup(self, *teams):
self.players = []
for team in teams:
for player in team.sprites():
self.players.append(player)
......@@ -48,6 +28,7 @@ class Ball_Sprite(rs.Round):
def reset(self):
self.rect.center = self.start
self.force = pygame.math.Vector2()
def kick(self, vector):
self.force += vector
......
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:
......@@ -14,8 +15,11 @@ class Goal(pygame.sprite.Sprite):
else:
self.rect.midleft = [760, 250]
def setup(self, ball):
def setup(self, ball, score):
self.ball = ball
self.score = score
def update(self):
self.goal = self.rect.contains(self.ball.rect())
if self.rect.contains(self.ball.rect()):
self.ball.reset()
self.score.increase()
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')
......@@ -10,7 +10,7 @@ class Score(pygame.sprite.Sprite):
def __init__(self, side):
super().__init__()
self.side = side
self.score = 0
self.reset()
self.render()
def update(self):
......@@ -27,3 +27,6 @@ class Score(pygame.sprite.Sprite):
def increase(self):
self.score += 1
def reset(self):
self.score = 0
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