Commit 6460a0cd authored by Marcus M. Scheunemann's avatar Marcus M. Scheunemann

Merge branch 'develop' into 'master'

Develop

See merge request !1
parents 5652f47c b2e7dea6
*.aux
*.blg
*.dvi
*.log
*.bbl
auto
*~
core
*.pyc
*.DS_Store
MIT License
Copyright (c) 2018 Bold Hearts
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
# Python Cup
This is based on Daniel Polani's [PythoCup](https://gitlab.com/boldhearts/pythocup-fltk).
PythoCup is a python-based simple simulator to study several aspects of the 2D RoboCup scenario without the hassle of understanding the detailed synchronization and command model of the standard 2D simulator. It simplifies many aspects, does not include turns, currently does not implement noise, outs (field boundaries) and many other features, and has been developed on-the-fly for the purpose of an quick-access to 2D RoboCup scenarios.
## Prerequirements
- Python 3 (installation [described below](#python-version-3))
- Pygame 1.9.3
## Install
You'll need python 3.5.3 or later. You can find out your python version with the command `python --version` or `python3 --version`.
### Clone this code
Create a folder named, e.g., `PythoCup` and clone the code:
git clone git@gitlab.com:boldhearts/pythocup.git
### Create a virtual environment
Python's [virtual environments](https://docs.python.org/3/library/venv.html) come in handy for encapsulating dependencies from global installed packages. The following command uses the `venv` module and creates a folder named `pyenv-pythocup` with the environment:
python3 -m venv pyenv-pythocup
You will need to source the environment in the terminal you're working in:
source pyenv-pycup/bin/activate
If successful, the prompt of the terminal will have a prefix with the given name of the virtual environment (i.e. pyenv-pythocup). You can now install all requirments with the following command:
pip3 install -r requirements.txt
Test if Pygame is working with running:
python3 -m pygame.examples.aliens
## Python version 3
If your system doesn't have Python's version 3, you can install it the following way:
### Ubuntu/Linux
On older systems, you may have to install python version 3 like this:
```
sudo apt-get install python3 python3-pip
pip3 install pygame
```
### Windows
1. Download Python 3.5.3
- https://www.python.org/downloads/
2. Install Python 3.5.3
- On Windows make sure to add Python to PATH
3. Open the `Command Prompt` or `Terminal`
1. Check the python version `python --version`
2. `python -m pip install wheel`
4. Download .whl file
- https://pypi.python.org/pypi/Pygame/1.9.3
- 32 byte = pygame-1.9.3-cp35-cp35m-win32.whl
- 64 byte = pygame-1.9.3-cp35-cp35m-win_amd64.whl
### Mac
1. Download Python 3.5.3
- https://www.python.org/downloads/
2. Install Python 3.5.3
- On Windows make sure to add Python to PATH
3. `Terminal`
1. Check the python version `python3 --version`
2. `python3 -m pip install pygame`
# Further Information
You can read about used [classes](https://gitlab.com/scheunemann/pythocup/wikis/Useful-Classes) in the wiki. There is also a set of [exercises](https://gitlab.com/scheunemann/pythocup/wikis/Exercises) to begin with.
# Contribution
You're welcome to contribute. Please have a look at [Gitlab's workflow](https://docs.gitlab.com/ee/workflow/forking_workflow.html) first. It is helpful if each commit addresses a specific feature before requesting a merge. You can achieve this with, e.g., squashing the commits of your working copy.
\ No newline at end of file
from sprite.player import Brain
from pygame.math import Vector2
class FollowBall(Brain):
def action(self):
move = self.ball.pos - self.pos
if self.can_kick:
return move, self.other_goal.pos - self.ball.pos
else:
return move, Vector2(0,0)
import pygame
from sprite.player import *
class Team(pygame.sprite.Group):
def __init__(self, side):
super().__init__()
self.side = side
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))
def setup(self, ball, goals, other_team):
for player in self.sprites():
player.setup(ball, goals, self, other_team)
import pygame
from model.side import Side
class TeamsItem(pygame.sprite.Group):
def __init__(self, spritetype):
super().__init__()
self.left = spritetype(Side.LEFT)
self.right = spritetype(Side.RIGHT)
self.add(self.left)
self.add(self.right)
def get(self, side):
if side == Side.LEFT:
return self.left
else:
return self.right
from pygame.math import Vector2
from model.side import Side
class CupObject():
def __init__(self, obj_type, pos, side=None):
self.type = obj_type
self.pos = Vector2(pos)
self.side = side
def __repr__(self):
if self.side is not None:
return self.type.value + " " + self.side.value
else:
return self.type.value
from enum import Enum
class ObjectType(Enum):
BALL = "Ball"
GOAL = "Goal"
PLAYER = "Player"
from enum import Enum
class Side(Enum):
LEFT = 'Left'
RIGHT = 'Right'
def other(self):
if self == Side.LEFT:
return Side.RIGHT
else:
return Side.LEFT
import pygame
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
pygame.init()
pygame.display.set_caption("Python Cup")
background = pygame.image.load("resource/field.png")
screen = pygame.display.set_mode(background.get_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')
clock = pygame.time.Clock()
goals = TeamsItem(Goal)
ball = Ball(screen.get_rect().center)
ball.setup(team_left, team_right)
team_left.setup(ball,goals,team_right)
team_right.setup(ball,goals,team_left)
score_board = TeamsItem(Score)
for goal in goals.sprites():
goal.setup(ball)
while 1:
clock.tick(30)
#Handle Input Events
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()
import pythocup
from group.team import *
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_right = Team(Side.RIGHT)
# team_right.put(FollowBall, (700, 250))
pythocup.run(team_left, team_right)
if __name__ == "__main__":
main()
import pygame
import sprite.round_sprite as rs
BALL = (10, 10, 200)
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):
max_speed = 5
force = pygame.math.Vector2(0,0)
players = []
def __init__(self, pos):
super().__init__(20, (100, 100, 100))
self.start = pos
self.rect.center = self.start
def setup(self, *teams):
for team in teams:
for player in team.sprites():
self.players.append(player)
def update(self):
pos = self.rect.copy()
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(back)
self.force = pygame.math.Vector2(0,0)
def reset(self):
self.rect.center = self.start
def kick(self, vector):
self.force += vector
def collide(self):
for player in self.players:
if(pygame.sprite.collide_circle(self, player)):
return True
return False
import pygame
from model.side import Side
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.rect = self.image.get_rect()
if side == Side.LEFT:
self.rect.midright = [40, 250]
else:
self.rect.midleft = [761, 250]
self.mask = pygame.mask.from_surface(self.image)
def setup(self, ball):
self.ball = ball
def update(self):
self.goal = self.rect.contains(self.ball.rect())
import pygame
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):
size = 30
self.brain = brain
if self.brain.side == Side.LEFT:
super().__init__(size, (200, 10, 10))
else:
super().__init__(size, (10, 10, 200))
self.rect.center = pos
def setup(self, ball, goals, self_team, other_team):
self.brain.setup(goals)
self.ball = ball
self.self_team = self_team
self.other_team = other_team
def update(self):
can_kick = self.brain.pre_action(self)
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(back)
def collide(self):
for player in self.self_team:
if player != self and pygame.sprite.collide_circle(self, player):
return True
for player in self.other_team:
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, Side.other(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')
import pygame
class Round(pygame.sprite.Sprite):
def __init__(self, diameter, colour):
super().__init__()
self.image = pygame.Surface((diameter, diameter)).convert_alpha()
self.rect = self.image.get_rect()
self.image.fill((0, 0, 0, 0))
self.radius = int(diameter/2)
pygame.draw.circle(self.image, colour, self.rect.center , self.radius, 0)
self.mask = pygame.mask.from_surface(self.image)
import pygame
from model.side import Side
pygame.font.init()
class Score(pygame.sprite.Sprite):
def __init__(self, side):
super().__init__()
self.side = side
self.font = pygame.font.Font(None, 40)
self.score = 0
self.render()
def update(self):
self.render()
def render(self):
self.image = self.font.render(str(self.score), 0, (0,0,0))
self.rect = self.image.get_rect()
if self.side == Side.LEFT:
self.rect.topright = (399,25)
else:
self.rect.topleft = (405,25)
def increase(self):
self.score += 1
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