Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Pong: Ball Movement

Author
Message
Foretore
14
Years of Service
User Offline
Joined: 5th Nov 2009
Location: MN
Posted: 25th Aug 2010 09:50
I'm having trouble wrapping my head around how to move the ball on it's own, but I'm pretty sure how to do all the collisions.

Here's what I've got so far.



e8400@3.8GHz
GTS 250 750-1836-1100
Working on a bad Pong remake
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 25th Aug 2010 19:06
I would suggest you create two variables, one to control the X speed of the ball and one to control the Y speed.

If the ball reaches the top or the bottom, you simply multiply the Y speed by -1. If it hits a paddle, multiply the X speed by -1. This way, you can always add the x/y speeds to the x/y positions of the ball.

Other notes:
The paddles go off the screen so you need to check for the y position and not allow them to go too far.

Have you thought about using sprites at all?

You have not turned on manual syncing by using SYNC ON. It should precede the SYNC RATE 60 command and at the end of your main loop, you need to include a SYNC command.

So many games to code.......so little time.
Foretore
14
Years of Service
User Offline
Joined: 5th Nov 2009
Location: MN
Posted: 26th Aug 2010 06:43 Edited at: 27th Aug 2010 01:13
alright I'm going to need a little more help than I thought. I don't really know what sort of commands I should be using, and I don't really know how to stop the paddles. I have thought about using Sprites and looking at the commands it looks a lot easier, but I would like to use my method, so I can just finish it and know how to work with it if I have to something like this later.

Edit 1: I've added a few things and my attempt at collision has failed....hard, but the scoring and ball reset does work.



e8400@3.8GHz
GTS 250 750-1836-1100
Working on a bad Pong remake
Foretore
14
Years of Service
User Offline
Joined: 5th Nov 2009
Location: MN
Posted: 27th Aug 2010 07:39
hmm, maybe i should describe the exact problems that I am having instead of just going "IT'S NOT WORKING". I need to figure out how to check between 2 points and stopping an action.

e8400@3.8GHz
GTS 250 750-1836-1100
Working on a bad Pong remake
Foretore
14
Years of Service
User Offline
Joined: 5th Nov 2009
Location: MN
Posted: 27th Aug 2010 12:48
I think I have something with the POINT command, but It seems the way that I move the ball is not letting me actually use it.



e8400@3.8GHz
GTS 250 750-1836-1100
Working on a bad Pong remake
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 27th Aug 2010 16:57 Edited at: 27th Aug 2010 18:23
Using POINT is not advisable, as it is much too slow. Using what is referred to as 'box' collision is usually good enough for most games I have found. I'll take a look at it and get back to you soon.

EDIT: okay, I have made some changes that I hope will help you understand things. First, here is the revised code:



This introduces you to a few new things; randomizing, arrays, subroutines, select/case and grabbing a screen image.

You will notice that the CLS is no longer needed. The reason for this is that the entire screen is grabbed as an image after the court is drawn. This image is pasted every loop onto the screen, in effect wiping out whatever was there previously. You could use this same technique to grab images of the ball and paddles. Instead of drawing them every game loop, you would simply paste the image at that location.

The scores are now held in an array. Arrays are handy for organizing things. You could store the player's X/Y coordinates in an array, the ball coordinates can be held in an array, etc.

Subroutines are used for code that gets repeated many times. By placing specific tasks into a subroutine, it makes your code easier to read, follow and debug (fix errors that come up). I did not put everything into a subroutine that I normally would have, as I left that for you. Some suggestions: UserInput, MoveBall and CheckPaddles. Normally, your main DO - LOOP consists of jumps to subroutines (or functions) and is normally very compact. Again, structuring your code like this will help you keep organized.

So many games to code.......so little time.
Foretore
14
Years of Service
User Offline
Joined: 5th Nov 2009
Location: MN
Posted: 28th Aug 2010 00:13 Edited at: 28th Aug 2010 04:41
oh thank you. Once I get a chance to mull this over I'll be back with a revised version.

EDIT: I've gotten the code to accept a new speed for the ball, but I have the problem of it not keeping the speed variable when the Restart subroutine is initiated. Now, I know that local variables are destroyed inside a subroutine when it's returned, so do I have the Input change a Global variable if that's even possible?



The SelectBallSpeed GoSub is right below gosub DisplayScores

e8400@3.8GHz
GTS 250 750-1836-1100
Working on a bad Pong remake
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 29th Aug 2010 01:37
Quote: "Now, I know that local variables are destroyed inside a subroutine when it's returned, so do I have the Input change a Global variable if that's even possible?"


I'm not 100% sure what you are trying to do here.

Variables within a FUNCTION are local, but are not local when a subroutine is used. If you change a variable within a subroutine, it will hold that value when it returns from it.

Within the subroutine you've written, it will only process the commands when NewSpeed = 1. If you want to allow the user to change the ball speed, ask for the input first in the subroutine. You could check to make sure the input falls within a certain range and default to a set speed if it falls outside of the acceptable range.

Personally, I would recommend not allowing the user to set that, but would start slow and gradually increase the ball speed in the code; say every six hits of the paddle, increase the speed by 1. You could do this by assigning a variable that is increased every time one of the players hits the ball back in play. When it reaches 6, increment the ball speed variable and reset the hit-the-paddle-counter. Bear in mind that your collision checks will have to take into account the increased ball speed in order to be accurate. I'm not at my comp right now to check it.

So many games to code.......so little time.

Login to post a reply

Server time is: 2024-09-28 22:26:45
Your offset time is: 2024-09-28 22:26:45