none
My Pong game so far! I am now struggling with collision stuff! RRS feed

  • Question

  • My Pong game so far!

    DLL764

    I am now struggling with collision stuff! I cannot for the life of me get the collision stuff to work what is the basic theory for collision?

    Any help would be appreciated

    Monday, February 21, 2011 8:02 PM

Answers

  • 1] Best to place a subroutine code outside and away from any other code (by itself with all other subroutines).  Also breaking the code sections up with bold comments helps.

    2] Note the Shapes.Move modified to move the shape centre - the Shapes.Move command moves the shape top left to the coordinates given.  We use Shapes.Move(shapeName,x-rad,y-rad), where x and y are the shape centre and rad is the radius or half the shape width/height (in your case this is 10 pixels) - this gives the shape moving with its centre at x,y and the game is easier to write.

    3] Collisions require us knowing where the ball and paddle are and checking if they overlap or have touched each other.  If they do, then a ball will bounce and reverse its y (vertical) velocity.  So we need variables to know where the ball and paddle are (you have this) and variables for the velocity or direction of the ball movement.

    The collision first checks if the ball bottom has passed the top of the paddle, then if the ball x centre is within the position of the paddle.

    Import DLL764-0

    Monday, February 21, 2011 9:23 PM
    Moderator

All replies

  • 1] Best to place a subroutine code outside and away from any other code (by itself with all other subroutines).  Also breaking the code sections up with bold comments helps.

    2] Note the Shapes.Move modified to move the shape centre - the Shapes.Move command moves the shape top left to the coordinates given.  We use Shapes.Move(shapeName,x-rad,y-rad), where x and y are the shape centre and rad is the radius or half the shape width/height (in your case this is 10 pixels) - this gives the shape moving with its centre at x,y and the game is easier to write.

    3] Collisions require us knowing where the ball and paddle are and checking if they overlap or have touched each other.  If they do, then a ball will bounce and reverse its y (vertical) velocity.  So we need variables to know where the ball and paddle are (you have this) and variables for the velocity or direction of the ball movement.

    The collision first checks if the ball bottom has passed the top of the paddle, then if the ball x centre is within the position of the paddle.

    Import DLL764-0

    Monday, February 21, 2011 9:23 PM
    Moderator
  • Cheers for the help I really appreciate it. I have got the ball bouncing off the walls and the ceiling. I am now working on the score . I have the variable working and updating every hit. I am now finding it difficult to show the score as it seems to print over the previous score do I need to do this on a loop?

    MDS584

     

    Cheers I also took your advice on the comments making life a lot easier

    Ferg

    Wednesday, February 23, 2011 10:30 AM
  • Some possible suggestions/modifications first.

    1] Subroutines should be moved outside the main code section - to the end

    2] The score update can be done as the ball hits the paddle - repeated checks not needed

    3] Some small changes to the bounce arithmatic help bounce the ball cleanly on the walls and paddle

    4] Condider using  KeyDown and KeyUp events to set a flag showing the status of the keys - this makes the movement smoother - eliminates the auto-repeat delay effect

    5] Put the GraphicsWindow.CanResize before setting the window size since changing CanResize alters the window size and affects the right wall bounce arithmetic

    If you want I can send the modified code or you can work on it yourself and use the suggestions or not as you work on it.

    Now the Score - you can either draw a region with background colour before outputting the score, put the score in the title or use Controls command to set a textbox, which should be created and sized initially:

    scoreBox = Controls.AddTextBox(740,10)
    Controls.SetSize(scoreBox,50,20)

    Then the text set in the code as the score changes.

    If (y+10 > 550) Then
      If (x > PaddleX And x < PaddleX+100) Then
        y = 550-10 'Position the ball just touching the paddle
        yvel = -yvel
        score = score + 1 'Do this here to prevent having the repeated code below
        Controls.SetTextBoxText(scoreBox,score)
      EndIf
    EndIf

     

    Wednesday, February 23, 2011 7:11 PM
    Moderator
  • Yeah I would appreciate that code I am getting a bit lost in my own!

    LOL

    Thursday, February 24, 2011 12:34 PM
  • Here it is, Import GZG214.
    Thursday, February 24, 2011 6:49 PM
    Moderator