Commit 049b6bf9 authored by gem2578's avatar gem2578

Cleaned up sprites code

parent 1306c5a2
import pygame
import sprite.round_sprite as rs
BALL = (10, 10, 200)
class Ball(pygame.sprite.GroupSingle):
def __init__(self, pos):
......@@ -26,7 +24,7 @@ class Ball(pygame.sprite.GroupSingle):
class Ball_Sprite(rs.Round):
max_speed = 5
force = pygame.math.Vector2(0,0)
force = pygame.math.Vector2()
players = []
def __init__(self, pos):
......@@ -40,14 +38,13 @@ class Ball_Sprite(rs.Round):
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()
back = (self.force * -1).normalize()
self.rect.move_ip(self.round_vector2(back))
self.force = pygame.math.Vector2(0,0)
self.force = pygame.math.Vector2()
def reset(self):
self.rect.center = self.start
......
......@@ -3,7 +3,6 @@ from model.side import Side
class Goal(pygame.sprite.Sprite):
def __init__(self, side):
super().__init__()
self.side = side
......@@ -11,12 +10,9 @@ class Goal(pygame.sprite.Sprite):
self.rect = self.image.get_rect()
if side == Side.LEFT:
self.rect.midright = [40, 250]
self.rect.midright = [40, 250]
else:
self.rect.midleft = [760, 250]
self.mask = pygame.mask.from_surface(self.image)
self.rect.midleft = [760, 250]
def setup(self, ball):
self.ball = ball
......
......@@ -9,12 +9,12 @@ 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))
colour = (200, 10, 10)
else:
super().__init__(size, (10, 10, 200))
colour = (10, 10, 200)
super().__init__(30, colour)
self.rect.center = pos
def setup(self, ball, goals, self_team, other_team):
......@@ -32,7 +32,7 @@ class Player(rs.Round):
move.scale_to_length(self.max_speed)
self.rect.move_ip(move)
while self.collide():
back = (move * - 1).normalize()
back = (move * -1).normalize()
self.rect.move_ip(self.round_vector2(back))
def collide(self):
......@@ -53,7 +53,7 @@ class Brain():
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))
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)
......
......@@ -8,8 +8,7 @@ class Round(pygame.sprite.Sprite):
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)
pygame.draw.circle(self.image, colour, self.rect.center , self.radius)
def round_vector2(self, vect):
x = round(vect[0])
......
......@@ -3,12 +3,13 @@ from model.side import Side
pygame.font.init()
FONT = pygame.font.Font(None, 40)
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()
......@@ -17,7 +18,7 @@ class Score(pygame.sprite.Sprite):
def render(self):
text = str(self.score).zfill(2)
self.image = self.font.render(text, 0, (0,0,0), (200,200,200))
self.image = FONT.render(text, 0, (0,0,0), (200,200,200))
self.rect = self.image.get_rect()
if self.side == Side.LEFT:
self.rect.topright = (395,25)
......
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