Skip to content

neelkamal123-code/python-pingpong

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Pong Game (Python Turtle)

image

A polished 2-player Pong game built with Python's built-in turtle graphics module.

What This Project Uses

  • python (tested with Python 3.11 on Windows)
  • Standard library only:
  • turtle for graphics, text, and keyboard input
  • time for frame pacing with sleep

No external packages are required.

Files

  • pong_turtle.py: main game script
  • README.md: project documentation

Features

  • Two-player local gameplay
  • Live top scoreboard (Left and Right)
  • Dashed center line for a cleaner arena
  • Pause and resume (Space)
  • Restart (R)
  • Quit (Q)
  • Win condition: first player to reach 7 points
  • Ball speed increases slightly after paddle hits

Controls

  • W: left paddle up
  • S: left paddle down
  • Up Arrow: right paddle up
  • Down Arrow: right paddle down
  • Space: pause/resume
  • R: restart game
  • Q: quit game

Run

From this folder:

python pong_turtle.py

Rules

  1. Press Space to start.
  2. Ball crossing the right edge gives a point to Left.
  3. Ball crossing the left edge gives a point to Right.
  4. After each point, the ball resets to center.
  5. First player to 7 points wins.
  6. After win: press R to restart or Q to quit.

Code Overview

1) Constants and window setup

pong_turtle.py defines constants for screen size, paddle limits, and win score:

  • WIDTH, HEIGHT
  • PADDLE_LIMIT_TOP, PADDLE_LIMIT_BOTTOM
  • WIN_SCORE

screen.tracer(0) enables manual frame updates for smoother animation.

2) Core game objects

  • make_paddle(x_pos) builds paddles with shared styling
  • left_pad and right_pad are paddle turtles
  • ball uses custom attributes:
  • ball.dx: horizontal velocity
  • ball.dy: vertical velocity
  • ball.base_speed: reset speed after scoring
  • divider draws the dashed middle line

3) Score and info UI

  • score_writer prints current score
  • info_writer prints controls, pause text, and winner text
  • draw_score() refreshes the score line
  • draw_info() refreshes the info line

4) State and helper functions

State variables:

  • left_score, right_score
  • game_paused
  • game_over

Helper functions:

  • reset_ball(direction) resets position and serve direction
  • check_winner() stops active play when a player reaches WIN_SCORE
  • toggle_pause(), restart_game(), quit_game()

5) Keyboard bindings

The script uses screen.onkeypress(...) for controls:

  • Left paddle: w / s
  • Right paddle: Up / Down
  • Game control: space, r, q

6) Main loop behavior

Every frame:

  • update screen
  • wait 0.01s
  • skip movement if paused
  • move ball by dx/dy
  • bounce on top/bottom walls
  • update score on left/right boundary crossing
  • detect paddle collisions and increase speed slightly

Easy Customization

  • Change WIN_SCORE for match length
  • Change colors (bgcolor, paddle/ball colors)
  • Change difficulty by editing:
  • ball.base_speed
  • collision multipliers (1.05, 1.02)
  • Add sound on hit/score (for example, winsound on Windows)
  • Add a simple AI for one paddle

Troubleshooting

  • Check Python install:
python --version
  • If keys do not respond, click inside the game window once to focus it.
  • If game feels too fast or too slow, change time.sleep(0.01).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages