Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So I just recently learned BASIC and figured I would try making a clone of pong on my own in darkBASIC pro, and I made everything else work just fine except for the part that makes the ball bounce off the paddle. And yes I'm aware that the game is not yet finished. The error is on lines 39-51 EVERYTHING IS 2D. The problem i am having is that when the ball hits the paddle it gets like stuck(itll move away like 3 pixels and the back onto the paddle), and will move up or down until it finally gets to the edge of the paddle, or it gets sucked into the inside of the paddle, or it does the same thing where it gets stuck on the paddle, but it does it from behind.

  ///////////////////////////////////////////////////////////
  //
  //     Project: Pong
  //     Created: Friday, August 31, 2012
  //     Code: Brandon Spaulding
  //     Art: Brandon Spaulding
  //     Made in CIS lab at CPAVTS
  //     Pong art and code © Brandon Spaulding 2012-2013      
  //
  //////////////////////////////////////////////////////////
y=150
x=0
ay=150
ax=612
ballx=300
bally=200
ballx_DIR=1
bally_DIR=1
hide mouse
set global collision on
//objectnumber=10
//make object box objectnumber,5,150,0
do
load image "media\paddle1.png",1
load image "media\paddle2.png",2
load image "media\ball.png",3
sprite 1,x,y,1
     sprite 2,ax,ay,2
     sprite 3,ballx,bally,3
     if upkey()=1 then y = y - 4
     if downkey()=1 then y = y + 4

    //num_1 = sprite collision(1,0)
    //num_2 = sprite collision(2,0)
      num_3 = sprite collision(3,0)

for t=1 to 2

//ball&paddle collision
if num_3 > 0
    if bally_DIR=1
        bally_DIR=0
    else
        bally_DIR=1
    endif
    if ballx_DIR=0
        ballx_DIR=1
    else
        ballx_DIR=0
    endif
endif

//if bally > 1 and bally < 500 then bally=bally + 2.5
if bally_DIR=1
    bally=bally-2.5 
        if bally<-2.5
            bally_DIR=0
        endif   
else    
    bally=bally+2.5 
        if bally>452.5
            bally_DIR=1
        endif   

endif
if ballx_DIR=1
    ballx=ballx-2.5 
        if ballx<-2.5
            ballx_DIR=0
        endif   
else    
    ballx=ballx+2.5 
        if ballx>612
            ballx_DIR=1
        endif   

endif
//bally = bally + t
//if bally < 600 or bally > 1 then bally = bally - 2.5
//if ballx < 400 or ballx > 1 then ballx = ballx + 2.5
//move sprite 3,1   

   next t
   if escapekey()=1 then exit
   loop
   end

Thank you in advance for the help.

share|improve this question
3  
You should really describe the problem you are having instead of the line numbers (which we can't see without counting) on which the error occurs. –  Seth Battin Sep 1 '12 at 3:46
add comment

closed as too localized by Jonathan Hobbs, michael.bartnett, Byte56, Trevor Powell, akled Sep 29 '12 at 14:36

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

You're reloading your images on every frame. Don't do this. Also, you'll be doing yourself and others who read your code a favor if you clean it up. Make sure it's indented properly and don't use raw numbers for image or sprite or whatever other type of object ID. Also, don't use extremely short variable names such as x and ax. I was able to work out what these referred to, but I had to put some time and brain power into it, which is less brain power I can give toward helping you with your collision problem.

Improved version of your code, much easier for others to read and help you out with:

paddle1y = 150
paddle1x = 0

paddle2y = 150
paddle2x = 612

ballx = 300
bally = 200
ballx_DIR = 1
bally_DIR = 1

hide mouse
set global collision on

paddle1Img = 1
paddle2Img = 2
ballImg = 3

paddle1Sprite = 1
paddle2Sprite = 2
ballSprite = 3

load image "media\paddle1.png", paddle1Image
load image "media\paddle2.png", paddle2Image
load image "media\ball.png", ballImage

do
    sprite paddle1Image, paddle1x, paddle1y, paddle1Sprite
    sprite paddle2Image, paddle2x, paddle2y, paddle2Sprite
    sprite ballImage, ballx, bally, ballSprite

    if upkey()=1 then paddle1y = paddle1y - 4
    if downkey()=1 then paddle1y = paddle1y + 4

    num_paddle1_collisions = sprite collision(paddle1Sprite,0)
    num_paddle2_collisions = sprite collision(paddle2Sprite,0)
    num_ball_collisions = sprite collision(ballSprite,0)

    // etc.

You also aren't using that for loop in there, just running your set of if statements twice for no real reason. You need to think more about how to solve this problem. I would avoid using DarkBasic's collision. Keep track of the positions and collision rectangles yourself, you'll learn a lot more.

This site does not respond well to posts that are just making us fix your code. The Game Creators' community site has plenty of resources and of plenty good examples on how to implement a pong game. Use it, and do a little more homework instead of just dumping code here.

share|improve this answer
1  
Although you don't really answer the question, +1 for the amazing effort at the answer. Considered editing his answer? –  Jonathan Dickinson Sep 3 '12 at 8:04
    
i spent 4 hours on their website and google, i think i did ALOT of my homework myself -.- also you did nothing more then change my var names, AND DONT REMOVE MY DOCUMENTATION, ITS THERE FOR A REASON –  user1601163 Sep 12 '12 at 20:22
1  
@user1601163 Nice way to respond to the only person willing to answer your question. Writing good code is not just about getting the right answer, but also developing some simple good writing habits that lead you to the right answer more quickly. I showed you examples of these habits, and specifically a couple of concerns specific to DarkBasic such as referencing objectIDs via variables. Why are you so upset about the header being left out? –  michael.bartnett Sep 13 '12 at 5:05
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.