game

Contains the central game class

Manages interactions with the players and the ball

Game Objects

class Game()

Class that controls the entire game

__init__

 | __init__(team1, team2, sound=True, difficulty=0.6, cam='default')

Initializes the game

Attributes:

  • team1 Team - Right-facing team
  • team2 Team - Left-facing team
  • sound bool - Enable / Disable in-game sounds
  • difficulty float - Game difficulty (0-1)

check_interruptions

 | check_interruptions()

Check for special keyboard buttons

Sets internal flags to pause, quit the game or run it in debug mode

same_team_collision

 | same_team_collision(team, free)

Check if current player collides with any other players of the same team

diff_team_collision

 | diff_team_collision(team1, team2, free)

Check if current player collides with any other players of the opposite team

collision

 | collision(team1, team2, ball)

Handle collisions between all in-game players.

text_draw

 | text_draw(win, text, rect, align='center')

Utility to draw text

Attributes:

  • win pygame.display - window for rendering text (pygame.font (rendered)): The text object
  • rect tuple - Rectangle specified as (x, y, width, height)
  • align string - text alignment can be one of 'left', 'right', 'center' (defaults to 'center')

goal_draw

 | goal_draw(win)

Display the current score (goals for each side)

field_draw

 | field_draw(win, hints)

Draw the football pitch

Attributes:

  • win pygame.display - window for rendering
  • hints bool - If (movement-based) hints are to be shown

draw

 | draw(win, hints=True)

Draw the entire game

Calls field_draw() along with the draw() methods for each team and the ball

practice_instr_draw

 | practice_instr_draw(win)

Draw the practice game instructions (shows extra hints and keyboard controls)

bar_draw

 | bar_draw(win, dim, w0, h0, w, h, col, val, debug_text, invert=False)

Draw a bar in the pause menu (for statistics)

Attributes:

  • win - Main window used for all drawing
  • dim [int] - extra dimensions for the pause menu
  • w0 int - x coordinate of the bar's top left point
  • h0 int - y coordinate of the bar's top left point
  • w int - width of the bar
  • h int - height of the bar
  • col [int] - color of the bar (RGB tuple)
  • val float - % of the bar to fill (between 0 and 1)
  • debug_text str - Text to display in debug mode
  • invert bool - Flip the bar left to right

bar_label_draw

 | bar_label_draw(win, dim, w0, h0, w, h, text)

Draw the label of a bar in the pause menu (for statistics)

Attributes:

  • win - Main window used for all drawing
  • dim [int] - extra dimensions for the pause menu
  • w0 int - x coordinate of the bar's top left point
  • h0 int - y coordinate of the bar's top left point
  • w int - width of the bar
  • h int - height of the bar
  • text str - Text to display in the label

pause_box_draw

 | pause_box_draw(win, dim)

Draw the skeleton of the pause menu (bg, title, exit button)

Attributes:

  • win - Main window used for all drawing
  • dim [int] - extra dimensions for the pause menu

pause_draw

 | pause_draw(win)

Draw the pause menu

Displays statistics for possession, pass accuracy and shot accuracy

get_state

 | get_state()

Create a state object that summarizes the entire game

state = {
    'team1': {
        'players' # list of the team player's coordinates
        'goal_x' # The x-coordinate of their goal post
    },
    'team2': {
        'players' # list of the team player's coordinates
        'goal_x' # The x-coordinate of their goal post
    },
    'ball' # Position of the ball
}

next

 | next()

Move the game forward by 1 frame

Passes state objects to the teams and pass their actions to move_next()

move_next

 | move_next(a1, a2)

Update the players' and ball's internal state based on the teams' actions

Attributes:

  • a1 list - list of actions (1 for each player) in team 1
  • a2 list - list of actions (1 for each player) in team 2

Each action must be a key in the ACT dictionary found in const.py