Commit 141bf5c9 authored by gem2578's avatar gem2578

Update to play multiple games

parent ceabb068
import pygame
from sprite.player import *
class Team(pygame.sprite.Group):
class Team(object):
def __init__(self, side):
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.')
self.brains.append({"class": BrainClass, "pos": pos})
def get_group(self):
return TeamGroup(self.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():
......
......@@ -15,3 +15,7 @@ class TeamItem(pygame.sprite.Group):
return self.left
else:
return self.right
def reset(self):
for sprite in self.sprites():
sprite.reset()
......@@ -13,21 +13,36 @@ background = pygame.image.load("resource/field.png")
screen = pygame.display.set_mode(background.get_size())
background = background.convert()
screen.blit(background, (0, 0))
class Match(object):
done = False
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)
def __init__(self, team_left, team_right):
def reset(self):
self.ball.reset()
self.score_board.reset()
self.done = False
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.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)
clock = pygame.time.Clock()
self.setup(team_left, team_right)
while not self.done:
clock.tick(30)
......@@ -37,19 +52,6 @@ class Match(object):
self.draw()
pygame.display.flip()
def setup(self, team_left, team_right):
self.team_left = team_left
self.team_right = team_right
self.goals = TeamGoal()
self.ball = SingleBall(screen.get_rect().center)
self.ball.setup(self.team_left, self.team_right)
self.team_left.setup(self.ball,self.goals,self.team_right)
self.team_right.setup(self.ball,self.goals,self.team_left)
self.score_board = TeamItem(Score)
self.goals.setup(self.ball, self.score_board)
def check_events(self):
for event in pygame.event.get():
......@@ -90,7 +92,11 @@ def main():
team_right = Team(Side.RIGHT)
team_right.put(FollowBall, (700, 250))
match = Match(team_left, team_right)
match = Match()
match.play(team_left, team_right)
print(match.get_score())
match.play(team_left, team_right)
print(match.get_score())
if __name__ == "__main__":
......
......@@ -4,7 +4,6 @@ import sprite.round_sprite as rs
class Ball(rs.Round):
max_speed = 5
players = []
def __init__(self, pos):
super().__init__(20, (100, 100, 100))
......@@ -12,6 +11,7 @@ class Ball(rs.Round):
self.reset()
def setup(self, *teams):
self.players = []
for team in teams:
for player in team.sprites():
self.players.append(player)
......
......@@ -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