This is a simple game of pong with made using pygame. You will need to make a picture of the pong for it to run properly. Simply make a cirlce in paint, 25x25. Just a note, the use of globals is a bad habit. With a little extra effort you can assign them to function arguments and return statements. Mini-project #4 - 'Pong' In this project, we will build a version of Pong, one of the first arcade video games (1972).While Pong is not particularly exciting compared to today's video games, Pong is relatively simple to build and provides a nice opportunity to work on the skills that you will need to build a game like Asteroids.As usual, we have provided a program template that can be used to.
pong.py
#PONG pygame |
import random |
import pygame, sys |
from pygame.locals import* |
pygame.init() |
fps = pygame.time.Clock() |
#colors |
WHITE= (255,255,255) |
RED= (255,0,0) |
GREEN= (0,255,0) |
BLACK= (0,0,0) |
#globals |
WIDTH=600 |
HEIGHT=400 |
BALL_RADIUS=20 |
PAD_WIDTH=8 |
PAD_HEIGHT=80 |
HALF_PAD_WIDTH=PAD_WIDTH/2 |
HALF_PAD_HEIGHT=PAD_HEIGHT/2 |
ball_pos = [0,0] |
ball_vel = [0,0] |
paddle1_vel =0 |
paddle2_vel =0 |
l_score =0 |
r_score =0 |
#canvas declaration |
window = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) |
pygame.display.set_caption('Hello World') |
# helper function that spawns a ball, returns a position vector and a velocity vector |
# if right is True, spawn to the right, else spawn to the left |
defball_init(right): |
global ball_pos, ball_vel # these are vectors stored as lists |
ball_pos = [WIDTH/2,HEIGHT/2] |
horz = random.randrange(2,4) |
vert = random.randrange(1,3) |
if right False: |
horz =- horz |
ball_vel = [horz,-vert] |
# define event handlers |
definit(): |
global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel,l_score,r_score # these are floats |
global score1, score2 # these are ints |
paddle1_pos = [HALF_PAD_WIDTH-1,HEIGHT/2] |
paddle2_pos = [WIDTH+1-HALF_PAD_WIDTH,HEIGHT/2] |
l_score =0 |
r_score =0 |
if random.randrange(0,2) 0: |
ball_init(True) |
else: |
ball_init(False) |
#draw function of canvas |
defdraw(canvas): |
global paddle1_pos, paddle2_pos, ball_pos, ball_vel, l_score, r_score |
canvas.fill(BLACK) |
pygame.draw.line(canvas, WHITE, [WIDTH/2, 0],[WIDTH/2, HEIGHT], 1) |
pygame.draw.line(canvas, WHITE, [PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1) |
pygame.draw.line(canvas, WHITE, [WIDTH-PAD_WIDTH, 0],[WIDTH-PAD_WIDTH, HEIGHT], 1) |
pygame.draw.circle(canvas, WHITE, [WIDTH//2, HEIGHT//2], 70, 1) |
# update paddle's vertical position, keep paddle on the screen |
if paddle1_pos[1] >HALF_PAD_HEIGHTand paddle1_pos[1] <HEIGHT-HALF_PAD_HEIGHT: |
paddle1_pos[1] += paddle1_vel |
elif paddle1_pos[1] HALF_PAD_HEIGHTand paddle1_vel >0: |
paddle1_pos[1] += paddle1_vel |
elif paddle1_pos[1] HEIGHT-HALF_PAD_HEIGHTand paddle1_vel <0: |
paddle1_pos[1] += paddle1_vel |
if paddle2_pos[1] >HALF_PAD_HEIGHTand paddle2_pos[1] <HEIGHT-HALF_PAD_HEIGHT: |
paddle2_pos[1] += paddle2_vel |
elif paddle2_pos[1] HALF_PAD_HEIGHTand paddle2_vel >0: |
paddle2_pos[1] += paddle2_vel |
elif paddle2_pos[1] HEIGHT-HALF_PAD_HEIGHTand paddle2_vel <0: |
paddle2_pos[1] += paddle2_vel |
#update ball |
ball_pos[0] +=int(ball_vel[0]) |
ball_pos[1] +=int(ball_vel[1]) |
#draw paddles and ball |
pygame.draw.circle(canvas, RED, ball_pos, 20, 0) |
pygame.draw.polygon(canvas, GREEN, [[paddle1_pos[0] -HALF_PAD_WIDTH, paddle1_pos[1] -HALF_PAD_HEIGHT], [paddle1_pos[0] -HALF_PAD_WIDTH, paddle1_pos[1] +HALF_PAD_HEIGHT], [paddle1_pos[0] +HALF_PAD_WIDTH, paddle1_pos[1] +HALF_PAD_HEIGHT], [paddle1_pos[0] +HALF_PAD_WIDTH, paddle1_pos[1] -HALF_PAD_HEIGHT]], 0) |
pygame.draw.polygon(canvas, GREEN, [[paddle2_pos[0] -HALF_PAD_WIDTH, paddle2_pos[1] -HALF_PAD_HEIGHT], [paddle2_pos[0] -HALF_PAD_WIDTH, paddle2_pos[1] +HALF_PAD_HEIGHT], [paddle2_pos[0] +HALF_PAD_WIDTH, paddle2_pos[1] +HALF_PAD_HEIGHT], [paddle2_pos[0] +HALF_PAD_WIDTH, paddle2_pos[1] -HALF_PAD_HEIGHT]], 0) |
#ball collision check on top and bottom walls |
ifint(ball_pos[1]) <=BALL_RADIUS: |
ball_vel[1] =- ball_vel[1] |
ifint(ball_pos[1]) >=HEIGHT+1-BALL_RADIUS: |
ball_vel[1] =-ball_vel[1] |
#ball collison check on gutters or paddles |
ifint(ball_pos[0]) <=BALL_RADIUS+PAD_WIDTHandint(ball_pos[1]) inrange(paddle1_pos[1] -HALF_PAD_HEIGHT,paddle1_pos[1] +HALF_PAD_HEIGHT,1): |
ball_vel[0] =-ball_vel[0] |
ball_vel[0] *=1.1 |
ball_vel[1] *=1.1 |
elifint(ball_pos[0]) <=BALL_RADIUS+PAD_WIDTH: |
r_score +=1 |
ball_init(True) |
ifint(ball_pos[0]) >=WIDTH+1-BALL_RADIUS-PAD_WIDTHandint(ball_pos[1]) inrange(paddle2_pos[1] -HALF_PAD_HEIGHT,paddle2_pos[1] +HALF_PAD_HEIGHT,1): |
ball_vel[0] =-ball_vel[0] |
ball_vel[0] *=1.1 |
ball_vel[1] *=1.1 |
elifint(ball_pos[0]) >=WIDTH+1-BALL_RADIUS-PAD_WIDTH: |
l_score +=1 |
ball_init(False) |
#update scores |
myfont1 = pygame.font.SysFont('Comic Sans MS', 20) |
label1 = myfont1.render('Score '+str(l_score), 1, (255,255,0)) |
canvas.blit(label1, (50,20)) |
myfont2 = pygame.font.SysFont('Comic Sans MS', 20) |
label2 = myfont2.render('Score '+str(r_score), 1, (255,255,0)) |
canvas.blit(label2, (470, 20)) |
#keydown handler |
defkeydown(event): |
global paddle1_vel, paddle2_vel |
if event.key K_UP: |
paddle2_vel =-8 |
elif event.key K_DOWN: |
paddle2_vel =8 |
elif event.key K_w: |
paddle1_vel =-8 |
elif event.key K_s: |
paddle1_vel =8 |
#keyup handler |
defkeyup(event): |
global paddle1_vel, paddle2_vel |
if event.key in (K_w, K_s): |
paddle1_vel =0 |
elif event.key in (K_UP, K_DOWN): |
paddle2_vel =0 |
init() |
#game loop |
whileTrue: |
draw(window) |
for event in pygame.event.get(): |
if event.type KEYDOWN: |
keydown(event) |
elif event.type KEYUP: |
keyup(event) |
elif event.type QUIT: |
pygame.quit() |
sys.exit() |
pygame.display.update() |
fps.tick(60) |
commented Mar 7, 2017 • edited
edited
Nice and Simple code. Just one feedback. Please update all the lines having / to // as python 3 does not give back Integer with normal division symbol if the variables being used are not integer. hence creating a Type Error. for example: ball_pos = [WIDTH/2,HEIGHT/2] to ball_pos = [WIDTH//2,HEIGHT//2] Otherwise users will get TypeError: integer argument expected, got float Rest is perfect. |
commented Aug 12, 2017
yeah can someone please help I have the same issue as above and idk how to fix this of. 'Traceback (most recent call last): File 'pong.py', line 157, in draw(window) File 'pong.py', line 111, in draw if int(ball_pos[0]) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH and int(ball_pos[1]) in range(paddle2_pos[1] - HALF_PAD_HEIGHT,paddle2_pos[1] + HALF_PAD_HEIGHT,1): TypeError: 'float' object cannot be interpreted as an integer' |
commented Sep 1, 2017
@Actonwarrior, @BrandedAlok, see the first comment about replacing '/' lines with '//' throughout the code. https://treevoip909.weebly.com/ac-dc-for-mac-download-free.html. I had to change those on about about 6 or 8 lines to make the game run. |
commented Dec 3, 2017
I forked a version and modify it to Python 3 https://github.com/hamdyaea/Daylight-Pong-python3 |
commented Dec 22, 2017
mine is fine but how do you control the other person's one |
commented Jan 25, 2018
Starbound enraged koala version for mac download. how do you make the ball go faster? |
commented Mar 10, 2018
How to make pong such that the 2nd player can control it from another network. |
commented Mar 24, 2018
https://ameblo.jp/merlritthigrio1975/entry-12639681494.html. KhushThePatel you use z and s |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
![Python Pong Game Code Python Pong Game Code](/uploads/1/3/3/8/133821457/336175484.png)
Active3 months ago
So I've been working on a few games in Python (battleships, tic-tac-toe etc.) and this week's project is Snake. I've got a basic set-up going; the snake can move and eats the food but I haven't programmed in collision detection or going off the edge yet. The problem is response time. If you run the code below, you'll see that the snake responds to key presses, but not for a couple of - I'll call them frames - after the press. I don't quite understand how the listen() method works; am I using it properly? If not, how should I use it, and if so, how can I fix the delay? I know about Pygame, but a) I can't find an easy to install 64 bit version for python 3.4 (this one http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame is not easy to install, what the heck is a whl file?) and b) I want to challenge myself anyway.Any help would be appreciated.
Isaac MiddlemissIsaac Middlemiss
3 Answers
Whenever you use
while True:
(sans break
) in turtle code, you're defeating the event hander. You should instead use an ontimer()
event to run your code compatibly with the event handler. Below is my rewrite of your code to do this along with some other functional and style tweaks:Does this provide the kind of response for which you're looking?
cdlanecdlane23k44 gold badges1212 silver badges4949 bronze badges
It probably is a challenge to make a speedy game this way (but that's not all bad). I think that
listen()
should go after the onkey()
s.You should also look at getting rid of all the duplicated code. It might seem easy short term to just copy/paste then alter. But if you have to make major changes (like after asking on a forum) it will be tedious and error prone to alter.
PS (EDIT) also your Snake.moveOneStep() method makes a new instance of Square just to check for self collision, this seems extravagant for the sake of elegance. Better to just keep a list of locations that python (ho, ho) can check through. (Quite apart from this probably not working. Try
print(Square(1,2) in [Square(1,2)])
)paddygpaddyg
Pong Game For Sale
Tomáš JelínekTomáš Jelínek
protected by eyllanescMar 27 '18 at 4:46
Original Pong Game
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?