Commit ceabb068 authored by gem2578's avatar gem2578

Changed run to __init__

parent ce2902c8
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
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__()
......
import pygame
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")
......@@ -13,55 +15,83 @@ screen = pygame.display.set_mode(background.get_size())
background = background.convert()
screen.blit(background, (0, 0))
def run(team_left, team_right):
class Match(object):
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')
done = False
clock = pygame.time.Clock()
def __init__(self, team_left, team_right):
goals = TeamsItem(Goal)
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')
ball = Ball(screen.get_rect().center)
ball.setup(team_left, team_right)
clock = pygame.time.Clock()
self.setup(team_left, team_right)
team_left.setup(ball,goals,team_right)
team_right.setup(ball,goals,team_left)
while not self.done:
clock.tick(30)
self.check_events()
self.clean_screen()
self.update()
self.draw()
pygame.display.flip()
score_board = TeamsItem(Score)
def setup(self, team_left, team_right):
self.team_left = team_left
self.team_right = team_right
self.goals = TeamGoal()
for goal in goals.sprites():
goal.setup(ball)
self.ball = SingleBall(screen.get_rect().center)
self.ball.setup(self.team_left, self.team_right)
while 1:
clock.tick(30)
#Handle Input Events
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():
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
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():
team_left = Team(Side.LEFT)
team_right = Team(Side.RIGHT)
team_right.put(FollowBall, (700, 250))
match = Match(team_left, team_right)
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
......@@ -10,9 +10,9 @@ def main():
team_left.put(FollowBall, (100, 250))
team_right = Team(Side.RIGHT)
# team_right.put(FollowBall, (700, 250))
pythocup.run(team_left, team_right)
match = Match(team_left, team_right)
print(match.get_score())
if __name__ == "__main__":
main()
import pygame
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))
self.start = pos
self.rect.center = self.start
self.reset()
def setup(self, *teams):
for team in teams:
......@@ -48,6 +27,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
......
......@@ -14,8 +14,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()
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