Ping Pong Game

259 views
Skip to first unread message

DJ 3L3CTRO

unread,
Mar 9, 2016, 12:50:25 AM3/9/16
to VPython-users
Hello, 
I have been working on trying to create the game of Ping Pong on vPython and I have been running into many issues but one issue in particular is getting myself a tad frustrated. To begin, I have all the (invisible) walls set - so the ball cant go beyond the playing area - then I have both paddles on opposite side (one user is in control of the left paddle and the other paddle I want to be the computer in control) So what I am currently working on and having troubles with is how do I write the code if the ping pong ball does not land on the paddle and like it hits the wall so the opponent gets the score? 

Another issue I am currently dealing with is, how do i make the ball like "stop" on the paddle? So if the ball hits my paddle, i want it to bounce back. 

Thanks for your time and effort,
Nick

Bruce Sherwood

unread,
Mar 9, 2016, 1:21:13 AM3/9/16
to VPython-users
It's not clear what kind of help you need. All I can say is that you need to detect when the ball has reached the plane of the paddle and check whether any part of the paddle is at the same place as the ball, and you need to detect when the ball has reached the wall and give the score to the opponent. If the ball is headed to the right, and it has a radius R, and the x coordinate of the left side of the right wall is X, then you would write

if ball.pos.x+R >= X:
    print('The ball has reached the wall')

Aaron Titus

unread,
Mar 9, 2016, 9:40:19 AM3/9/16
to vpytho...@googlegroups.com
What you are wanting to accomplish requires some programming experience. You will need to use:

1. conditional statements
2. collision detection
3. physical properties to determine behavior after the collision
4. keyboard interactions to control the paddle

I created a course  called Physics For Video Games. The first version of my course “workbook" is in VPython Classic, which I presume you are using. All materials are posted at:


I suggest doing the following:

1. download the pdf at:


2. read every chapter and write all of the programs. The chapters that will teach you items 1-4 above are chapters 3, 7-10, 12, and 14. Chapter 14 is the “pong” game which has all of the features (items 1-4 above) that you need for ping pong.

If you want to see pong with no friction and elastic collisions, download the program at:


It uses mouse position to control the paddle in the game. Note that the ball always traverses the same path because there is no energy loss and no friction upon colliding with the paddle and walls. Thus, this version is admittedly not an interesting game.

Aaron Titus


--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

DJ 3L3CTRO

unread,
Mar 9, 2016, 11:13:03 AM3/9/16
to VPython-users
So here's the code that I have at the moment: 

from visual import *

# > for top and right
# < for bottom and left

# x = side to side
# y = up and down


R1 = 0
L1 = 0


RightScore = text(pos=vector(4,6,0),text=str(R1), align='center', depth=0.1, color=color.green)
LeftScore = text(pos=vector(-4,6,0),text=str(L1), align='center', depth=0.1, color=color.green)

#Ball itself
ball = sphere(pos=(-5,0,0),radius=0.5, color=color.cyan)

#Walls
wallR = box(pos=(6,0,0), size=(0.2,12,12), color=color.green, opacity=0)
wallL = box(pos=(-6,0,0), size=(0.2,12,12), color=color.green, opacity=0)
wallT = box(pos=(0,6,0), size=(12,0.2,12), color=color.yellow, opacity=0)
wallB = box(pos=(0,-6,0), size=(12,0.2,12), color=color.yellow, opacity=0)
wallBack = box(pos=(0,0,-6), size=(12,12,0.2), color=color.cyan, opacity=0)

paddleRight = box(pos=(5.5,0,0), size=(1,4,0), color=color.green)
paddleLeft = box(pos=(-5.5,0,0), size=(1,4,0), color=color.green)

ball.velocity=vector(25,35,0)
deltat=0.005
t=1

while 1 >0:
    rate(55)
    
    if ball.pos.x >= wallR.pos.x:
        ball.velocity.x=-25
        
        
    if ball.pos.x == wallL.pos.x:
        ball.velocity.x=25
        
    if ball.pos.y >= wallT.pos.y:
        ball.velocity.y=-35
       
    if ball.pos.y <= wallB.pos.y:
        ball.velocity.y=35
        
### SCOREBOARD ########################

    if ball.pos.x == wallL.pos.x  :
        R1 = R1 + 1
        RightScore.text = str(R1)
        
    if ball.pos.x == wallR.pos.x  :
        L1 = L1 + 1
        LeftScore.text = str(R1)
       

######################################
 KEY PRESS EVENTS
    if scene.kb.keys: 
         key = scene.kb.getkey() 
         if key == "up":
             paddleLeft.pos.y = paddleLeft.pos.y + 0.5

         elif key == "down":
             paddleLeft.pos.y = paddleLeft.pos.y - 0.5
    
    paddleRight.pos.y = ball.pos.y

    ball.pos = ball.pos + ball.velocity * deltat

Everytime the ball hits either wall it counts as a point anyways, and I do not want that. The ball should only be able to bounce off the paddle and if the ball lands off the paddle then it registers as a point to whom ever scored it. I really hope you guys can understand what I am trying to get at. 

Thank you so much for your time and consideration in mentioning a couple of steps to help me out, it really is much appreciated.

Aaron Titus

unread,
Mar 9, 2016, 11:22:45 AM3/9/16
to vpytho...@googlegroups.com
Without looking at the program, I think it is likely an issue with logic and conditional statements.

Did you get to look at my pong game and other resources I sent to the group? 

Aaron

DJ 3L3CTRO

unread,
Mar 9, 2016, 11:28:00 AM3/9/16
to VPython-users
Yes, I did take a quick look at your pong game. I ran it, played it, went over your code to see if it would help me better understand and I couldn't really piece together things. 

If you could whenever you get time to, would you be able to look at the program, like would you be able to copy and paste my code, run the program and then see where I went wrong. You know what I mean?

Thanks,
Nick

Bruce Sherwood

unread,
Mar 9, 2016, 12:28:18 PM3/9/16
to VPython-users
I notice several problems.

1) if ball.pos.x == wallL.pos.x: should be if ball.pos.x <= wallL.pos.x:

2) A test such as if ball.pos.x == wallL.pos.x: will almost always fail, because it is highly unlikely that these two quantities would ever be exactly equal.

3) I don't see any code that checks whether the ball hits a paddle.

4) I don't understand why you create walls with opacity=0 when all you need is variables specifying where walls would be.

Kevin Karplus

unread,
Mar 9, 2016, 12:48:35 PM3/9/16
to vpytho...@googlegroups.com

On Wed, Mar 9, 2016 at 8:28 AM, DJ 3L3CTRO <djele...@gmail.com> wrote:
If you could whenever you get time to, would you be able to look at the program, like would you be able to copy and paste my code, run the program and then see where I went wrong. You know what I mean?

​It looks to me like djelectro25 is asking for homework help here, or a full course on how to program in Python.​  That seems to be outside the normal scope of this forum.  Aaron Titus's suggestions about appropriate reading material seems to be as much help as we should be giving.


Kevin Karplus   kar...@soe.ucsc.edu    http://www.soe.ucsc.edu/~karplus
Professor of Biomolecular Engineering, University of California, Santa Cruz
Program Chair and Undergraduate Director, Bioengineering
Undergraduate Director, Bioinformatics
Affiliations for identification only.
Reply all
Reply to author
Forward
0 new messages