36 / wfmag.cc
Substitute Soccer
Toolbox
import pgzero, pgzrun, pygame
import math, sys, random
from enum import Enum
from pygame.math import Vector2
if sys.version_info < ( 3 , 5 ):
print(“This game requires at least version
3.5 of Python. Please download”
“it from http://www.python.org”)
sys.exit()
pgzero_version = [int(s) if s.
isnumeric() else s for s in pgzero.__
version__.split(‘.’)]
if pgzero_version < [ 1 , 2 ]:
print(“This game requires at least version
1.2 of Pygame Zero. You are”
“using version {pgzero.__
version__}. Please upgrade using the command”
“’pip install --upgrade pgzero’”)
sys.exit()
WIDTH = 800
HEIGHT = 480
TITLE = “Substitute Soccer”
HALF_WINDOW_W = WIDTH / 2
LEVEL_W = 1000
LEVEL_H = 1400
HALF_LEVEL_W = LEVEL_W // 2
HALF_LEVEL_H = LEVEL_H // 2
HALF_PITCH_W = 442
HALF_PITCH_H = 622
GOAL_WIDTH = 186
GOAL_DEPTH = 20
HALF_GOAL_W = GOAL_WIDTH // 2
PITCH_BOUNDS_X = (HALF_LEVEL_W - HALF_
PITCH_W, HALF_LEVEL_W + HALF_PITCH_W)
PITCH_BOUNDS_Y = (HALF_LEVEL_H - HALF_
PITCH_H, HALF_LEVEL_H + HALF_PITCH_H)
GOAL_BOUNDS_X = (HALF_LEVEL_W - HALF_
GOAL_W, HALF_LEVEL_W + HALF_GOAL_W)
GOAL_BOUNDS_Y = (HALF_LEVEL_H - HALF_
PITCH_H - GOAL_DEPTH,
HALF_LEVEL_H + HALF_
PITCH_H + GOAL_DEPTH)
PITCH_RECT = pygame.rect.Rect(PITCH_
BOUNDS_X[ 0 ], PITCH_BOUNDS_Y[ 0 ],
HALF_
PITCH_W * 2 , HALF_PITCH_H * 2 )
GOAL_ 0 _RECT = pygame.rect.Rect(GOAL_
BOUNDS_X[ 0 ], GOAL_BOUNDS_Y[ 0 ],
GOAL_
WIDTH, GOAL_DEPTH)
GOAL_1_RECT = pygame.rect.Rect(GOAL_
BOUNDS_X[ 0 ], GOAL_BOUNDS_Y[ 1 ] - GOAL_DEPTH,
GOAL_
WIDTH, GOAL_DEPTH)
AI_MIN_X = 78
AI_MAX_X = LEVEL_W - 78
AI_MIN_Y = 98
AI_MAX_Y = LEVEL_H - 98
PLAYER_START_
POS = [( 350 , 550 ), ( 650 , 450 ), ( 200 , 850 ),
( 500 , 750 ), ( 800 , 950 ),
( 350 , 1250 ), ( 650 , 1150 )]
LEAD_DISTANCE_1 = 10
LEAD_DISTANCE_2 = 50
DRIBBLE_DIST_X, DRIBBLE_DIST_Y = 18 , 16
PLAYER_DEFAULT_SPEED = 2
CPU_PLAYER_WITH_BALL_BASE_SPEED = 2.6
PLAYER_INTERCEPT_BALL_SPEED = 2.75
LEAD_PLAYER_BASE_SPEED = 2.9
HUMAN_PLAYER_WITH_BALL_SPEED = 3
HUMAN_PLAYER_WITHOUT_BALL_SPEED = 3.3
DEBUG_SHOW_LEADS = False
DEBUG_SHOW_TARGETS = False
DEBUG_SHOW_PEERS = False
DEBUG_SHOW_SHOOT_TARGET = False
DEBUG_SHOW_COSTS = False
class Difficulty:
def __init__(self, goalie_enabled, second_
lead_enabled, speed_boost,
holdoff_timer):
self.goalie_enabled = goalie_enabled
The goalposts for the
bottom and top of
the pitch.
The banner used for the
scores at the top of
the screen.
RUNNING
THE GAME
You’ll find the full code for
Substitute Soccer on Wireframe’s
GitHub – simply head over to
wfmag.cc/CTC1-soccer. To get
the game running on your system,
you’ll need to install Pygame Zero
- you can find full instructions at
wfmag.cc/pgzero.