pong module

class pong.Ball(game, x_pos=0.5, y_pos=0.5, velocity=0.025, direction=[-0.5, 0.5], radius=0.02)[source]

Bases: pong.BallOrPuck

Class representing the ball.

Parameters:radius (float) – Radius of ball in unit length.

For other args, see BallOrPuck.

class pong.BallOrPuck(game, x_pos=0.5, y_pos=0.5, velocity=0.2, direction=0)[source]

Class that represents either a ball or a puck.

Parameters:
  • game (GameOfPong) – Pong game.
  • x_pos (float) – Initial x position in unit length.
  • y_pos (float) – Initial y position in unit length.
  • velocity (float) – Change in position per iteration.
  • direction (list, float) – Heading.
get_cell()[source]
update_cell()[source]

Update cell based on position.

class pong.GameOfPong(x_grid=32, y_grid=32, debug=False, norm='L1')[source]

Bases: object

Class representing a game of Pong. Playing field: 1 by 1 discretized into cells.

Parameters:
  • x_grid (int) – Number of cells to discretize x-axis into.
  • y_grid (int) – Number of cells to discretize y-axis into.
  • debug (bool) – Print debugging messages.
  • norm (string) – “L1” or “L2”, defines norm to be used for velocity vector.
dont_move_right_paddle()[source]
get_ball_cell()[source]
get_right_paddle_cell()[source]
get_right_paddle_length()[source]
get_right_paddle_position()[source]
move_right_paddle_down()[source]
move_right_paddle_up()[source]
propagate_ball_and_paddles()[source]

Update ball and paddle coordinates based on direction and velocity. Also update their cells.

step()[source]

Perform one game step.

update_ball_direction()[source]

In case of a collision, update the direction of ball velocity. Also determine if the ball is in either player’s net.

Returns:Either NO_WIN or RIGHT_WIN.
class pong.Puck(game, length=0.07, y_pos=0.5, velocity=0.05, direction=0.0)[source]

Bases: pong.BallOrPuck

Class representing the puck.

Parameters:direction (float, int) – +1 for up, -1 for down, 0 for no movement.

For other args, see BallOrPuck.