Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9249197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:07:57+00:00 2026-06-18T10:07:57+00:00

So basically what I’ve tried to code here is a game of Pong using

  • 0

So basically what I’ve tried to code here is a game of Pong using the Python programming language and the Pygame module.
My problems are as follows:

  1. The game sometimes gets stuck in a loop in which the AI paddle sits there while over and over again it loses
  2. The ball jumps up and down sometimes? I have no clue as to why…
  3. The ball just goes back and forth? I can’t figure out why it isn’t following along a random line like I want…
  4. And my biggest problem, it seems that when the ball hits the top or bottom, it just disappears and the game just continues as if it was still bouncing around.

My Code

# Pong
import random, pygame, sys
from pygame.locals import *

FPS = 300
WINWID = 640
WINHEI = 480

BROWN = (102, 51, 0)
LIGHTBLUE = (0, 204, 204)
GREEN = (0, 153, 0)
PADDLECOLOR = (255, 255, 255)
BALLCOLOR = PADDLECOLOR
BACKGROUNDCOLOR = (0, 0, 0)

def main():
    global FPSCLOCK, DISPLAYSURF
    SPEED = 1
    DIFF = 1.0 # AI paddle speed handicap
    ball_speed = 1
    AISIGHT = WINWID/6
    FONT = "Times New Roman"
    pygame.init()
    DEFAULTFONT = pygame.font.SysFont(FONT, 16)
    BIGGERFONT = pygame.font.SysFont(FONT, 32)
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WINWID, WINHEI))
    DISPLAYSURF.fill(BACKGROUNDCOLOR)
    BACKGROUND = pygame.Rect(0, 0, WINWID, WINHEI)
    pygame.display.set_caption('Pong')
    paddleUP, paddleDOWN = False, False    
    ballpos = (WINWID/2, WINHEI/2)
    AIpoint, PLAYERpoint = 0, 0
    score = "Player:%2f || Computer:%2f" % (AIpoint, PLAYERpoint)
    paddle = WINHEI/2
    AIpaddle = paddle
    pygame.draw.rect(DISPLAYSURF, BACKGROUNDCOLOR, BACKGROUND)
    pygame.draw.line(DISPLAYSURF, PADDLECOLOR, (9, paddle-20), (9, paddle+20), 3)
    pygame.draw.line(DISPLAYSURF, PADDLECOLOR, (629, AIpaddle-20), (629, AIpaddle+20), 3)
    pygame.draw.circle(DISPLAYSURF, BALLCOLOR, ballpos, 5)
    slope, y_int = ballpath(None, ballpos[0], ballpos[1])
    while True:
        for event in pygame.event.get():
            direction = None
            if event.type == QUIT:
                terminate()
            elif event.type == KEYDOWN:
                if (event.key == K_UP or event.key == K_w):
                    paddleUP = True
                elif (event.key == K_DOWN or event.key == K_s):
                    paddleDOWN = True
            elif event.type == KEYUP:
                if (event.key == K_UP or event.key == K_w):
                    paddleUP = False
                elif (event.key == K_DOWN or event.key == K_s):
                    paddleDOWN = False
        # move player paddle
        if paddleUP and paddle >= 20:
            paddle-=SPEED
        elif paddleDOWN and paddle <= 460:
            paddle+=SPEED
        # move AI paddle
        if ballpos[0] > (AISIGHT):
            if AIpaddle-20 < ballpos[1] and AIpaddle <= 460:
                AIpaddle+=SPEED/DIFF
            elif AIpaddle+20 > ballpos[1] and AIpaddle >= 20:
                AIpaddle-=SPEED/DIFF
        # move ball
        x = ballpos[0] + ball_speed
        y = (slope*x) + y_int
        ballpos = (x, y)
        # if it hits a paddle
        if 9 < ballpos[0] < 11:
            if paddle-20 < ballpos[1] < paddle+20:
                slope, y_int = ballpath(None, ballpos[0], ballpos[1])
                ball_speed*=-1
        elif 629 < ballpos[0] < 631:
            if AIpaddle-20 < ballpos[1] < AIpaddle+20:
                slope, y_int = ballpath(None, ballpos[0], ballpos[1])
                ball_speed*=-1
        # if it hits the top or bottom
        if WINHEI-5 > ballpos[1] or ballpos[1] < 5:
            slope, y_int = ballpath(slope, ballpos[0], ballpos[1])
        # if paddle misses ball
        if ballpos[0] < 5:
            AIpoint+=1
            score = "Player:%2f || Computer:%2f" % (PLAYERpoint, AIpoint)
            ballpos = (WINWID/2, WINHEI/2)
            pygame.draw.rect(DISPLAYSURF, (0, 0, 0, 100), pygame.Rect(0, 0, 640, 480))
            DISPLAYSURF.blit(BIGGERFONT.render("Point: Computer", 1, PADDLECOLOR), (WINWID/3, WINHEI/3))
            paddle, AIpaddle = WINWID/2, WINWID/2
            for i in range(5):
                FPSCLOCK.tick(FPS)
        elif ballpos[0] > 635 and AIpaddle-20 < ballpos[1] < AIpaddle+20:
            PLAYERpoint+=1
            score = "Player:%2f || Computer:%2f" % (PLAYERpoint, AIpoint)
            ballpos = (WINWID/2, WINHEI/2)
            pygame.draw.rect(DISPLAYSURF, (0, 0, 0, 100), pygame.Rect(0, 0, 640, 480))
            DISPLAYSURF.blit(BIGGERFONT.render("Point: Player", 1, PADDLECOLOR), (WINWID/3, WINHEI/3))
            for i in range(5):
                FPSCLOCK.tick(FPS)


        pygame.draw.rect(DISPLAYSURF, BACKGROUNDCOLOR, BACKGROUND)
        pygame.draw.line(DISPLAYSURF, PADDLECOLOR, (9, paddle-20), (9, paddle+20), 3)
        pygame.draw.line(DISPLAYSURF, PADDLECOLOR, (629, AIpaddle-20), (629, AIpaddle+20), 3)
        pygame.draw.circle(DISPLAYSURF, BALLCOLOR, ballpos, 5)
        DISPLAYSURF.blit(DEFAULTFONT.render(score, 1, PADDLECOLOR), (WINWID/3, 20))
        pygame.display.update()
        FPSCLOCK.tick(FPS)


def ballpath(old_slope, ball_x, ball_y):
    if old_slope == None:
        slope = int(random.random() * 2)
    else:
        slope = old_slope * -1
    y_int = ball_y - (slope * ball_x)
    return slope, y_int

def terminate():
    pygame.quit()
    sys.exit()

if __name__ == '__main__':
    main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:07:58+00:00Added an answer on June 18, 2026 at 10:07 am

    I cleaned up your code quite a bit. It should help a bit with how to use the classes. http://pastebin.com/rZF3Gr0D

    Things to note:

    • prints when score event happens
    • paddle movement using keystate vs “on events”. It makes that kind of input easier.
    • Sprite group’s let you ‘draw’ or check collisions in batches.
    • removed global keyword. Game owns most things.
    • Using Rect functions and properties all over the place. Such as ball.rect.center = screen.get_rect().center .
    • random angle for ball spawns via trig.
    • caps framerate. Next you would move onto time-based movement. I left that out for simplicity.
    • Color() lets you do many things, including Color("red"), Color("gray40") , etc…
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I'm using python to print out some information in the terminal and some
Basically here is the main part of the code that contains the error: ...
Basically, from the form that I made I send customer_name and with this code
basically ive followed this tutorial here and have everything up and running and working
Basically the template wont render to a ScrollView using kendo.render(template, response) but WILL work
Basically I want to know how I would do this. Here's an example string:
Basically I want to get a handle of the python interpreter so I can
Basically, I'm using this query to group a bunch of users based on the
Basically I have code that initialises the database in a DatabaseHelper extended class (see
Basically i have built an appointment scheduler webapp using java servlets. It relies heavily

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.