Wireframe - #35 - 2020

(Joyce) #1
wfmag.cc \ 37

Substitute Soccer

Toolbox


 Player sprites for both
teams, for every animation
frame and direction.

self.second_leadenabled = second
lead_enabled
self.speed_boost = speed_boost
self.holdoff_timer = holdoff_timer


DIFFICULTY = [Difficulty(False, False, 0 ,
120 ), Difficulty(False, True, 0.1, 90 ),
Difficulty(True, True, 0.2, 60 )]


def sin(x):
return math.sin(x*math.pi/4)


def cos(x):
return sin(x+2)


def vec_to_angle(vec):
return int( 4 * math.
atan2(vec.x, -vec.y) / math.pi + 8.5) % 8


def angle_to_vec(angle):
return Vector2(sin(angle), -cos(angle))


def dist_key(pos):
return lambda p: (p.vpos - pos).length()


def safe_normalise(vec):
length = vec.length()
if length == 0 :
return Vector2( 0 , 0 ), 0
else:
return vec.normalize(), length


class MyActor(Actor):
def init
(self, img, x=0, y=0, anchor=None):
super().init
(img, ( 0 , 0 ), anchor=anchor)
self.vpos = Vector2(x, y)


def draw(self, offset_x, offset_y):
self.pos = (self.vpos.x - offset_x, self.
vpos.y - offset_y)
super().draw()


KICK_STRENGTH = 11.5
DRAG = 0.98


def ball_physics(pos, vel, bounds):
pos += vel


if pos < bounds[ 0 ] or pos > bounds[ 1 ]:
pos, vel = pos - vel, - vel

return pos, vel * DRAG

def steps(distance):
steps, vel = 0 , KICK_STRENGTH

while distance > 0 and vel > 0.25:
distance, steps, vel = distance - vel,
steps + 1 , vel * DRAG

return steps

class Goal(MyActor):
def __init__(self, team):
x = HALF_LEVEL_W
y = 0 if team == 0 else LEVEL_H
super().__init__
(“goal” + str(team), x, y)

self.team = team

def active(self):
return abs(game.ball.vpos.y - self.
vpos.y) < 500

def targetable(target, source):
v0, d0 = safe_normalise(target.
vpos - source.vpos)

if not game.teams[source.team].human():

for p in game.players:
v1, d1 = safe_normalise(p.
vpos - source.vpos)

if p.team != target.
team and d1 > 0 and d1 < d0 and v0*v1 > 0.8:
return False

return target.team == source.
team and d0 > 0 and d0 < 300 and \

GET THE FULL CODE
The full Substitute Soccer listing can be found
on Github at wfmag.cc/CTC1-soccer
Free download pdf