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 / Increase speed

Author
Message
Lifebandit
14
Years of Service
User Offline
Joined: 27th Mar 2010
Location: Texas USA
Posted: 1st Oct 2011 03:26
Hey guys. I haven't been around in a long while and had to put my programming lessons on hold due to a family issue. But that has all been resolved and I'm hitting the books once again.

The tutorial I am working on walked me through creating a "Pong" game. The challenge at the end of the lesson is to add a timer and make the ball increase in speed after so much time has past.
I added the timer and got it to work but I just can't figure out how to increase the speed of the ball.

I tried adding this:




But it did not work. I am stumped.


Entire code:

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 1st Oct 2011 04:25
Hi Lifebandit, well it certainly speeds up on this end but not in the way I think you want it to. First problem is that you're adding 1 to both of the speeds. What if they are negative? They'll decrease in speed so you'll have to check to see if you subtract one (for negavites) or add one (for positives).

Your second problem is that setting time# to 60 in the function has no effect because when you go back to the loop that value gets over written by
time#=timer()/1000-init_time#

Solution: Make init_time# as global and then add this instead of the above line:
init_time# = timer()/1000

So you're function should look like:


Hope that helps

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 1st Oct 2011 04:45 Edited at: 1st Oct 2011 05:01
I just glanced at the code and I see some problems. Where to begin?

[edit] Certain plugins I use must use "min" as a variable.

Even though you have variables in your code named speedx and speedy... they are not 'directly' associated with speed as you may think. Rather they are tied to the direction of the ball as well. It's like a vector that has a force and a direction. So incrementing speedx by adding 1 doesn't necessarily increase the speed of the ball. It will increase the "speed" from left to right. decrementing speed by -1 represents increasing the balls "speed" from right to left. You need to determine which direction the ball is traveling; positive value means it is moving left to right,, negative values means it is moving right to left... and either add or subract 1 depending on the direction.

***ALSO,, the slope of your ball is broken out into speedx AND speedy... basically it's RISE/RUN (RISE over RUN). So the result of only changing the speedx variable will have an adverse effect on the balls trajectory/slope... thus putting a hook on the ball. So you are NOT just changing it's speed. Under your current setup you will also need to adjust speedy proportionately... and do the same checks to determine the direction (UP/DOWN)/(NEGATIVE/POSITIVE) and make the corresponding decrement/increment to speedy.

You have a small issue with the ball being reset to a "speed" of 3 whenever it gets to a paddle. And you are resetting your adjustments to time# back to 60 seconds everytime it becomes less than 0. So, you aren't going to much of an effect on the ball as a result. speedx and speedy are only going to change by 1,, for the very short duration that time < 0 AND the ball hasn't reached a limit at either end. Given the initial "speed" of the ball... it will always reach either end well before 60 seconds goes by.

Your signature has been erased by a mod please reduce it to 600 x 120.
Lifebandit
14
Years of Service
User Offline
Joined: 27th Mar 2010
Location: Texas USA
Posted: 1st Oct 2011 04:54 Edited at: 1st Oct 2011 04:57
Ok, so the time# = 60 is useless. I just threw that in to make sure that the timer reset back to 60 after it reached 0.

Now on to the speed x and y. After going back and studying the code, I noticed that it would be hard to increase the ball's speed by altering these variables as they change from a positive to a negative during play depending on where the ball is at when the timer reaches 0.
I have also tried to make it change sync rates with no cigar. Just from what I know (very little), the code was written without the intent on adding a speed adjustment feature unless I was to rewrite the core functions for animation.

(edit) Lol, Zen beat me to it.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 1st Oct 2011 04:57 Edited at: 1st Oct 2011 05:01
check the ***ALSO part in my above post. I was editing while you responded. There is another issue.

Your signature has been erased by a mod please reduce it to 600 x 120.
Lifebandit
14
Years of Service
User Offline
Joined: 27th Mar 2010
Location: Texas USA
Posted: 1st Oct 2011 05:13
I now understand why it is important to create a game design doc and include all features before attempting to write a game.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 1st Oct 2011 05:14 Edited at: 1st Oct 2011 06:03
btw, It's not super difficult to make the changes necessary to the way your code is set up now. Just awkward. If it were me, and no one should have expected you to foresee this... I would have setup the following:

For this type of game I probably wouldn't use actual physics formulas either; Although it may turn out to be easier and allow more functionality down the road. ie. incorporate other physics forces like friction, spinning.



So, for brute force I might have a User Defined Type with variables for breaking up the components of the vector as well as positional info. I haven't thought it out too much but some possibilities would be.

(Used in the routine that draws the actual x,y position of the ball)
cur_ball_pos_x
cur_ball_pos_y

(Either INTEGERs or BOOLEANs... basically flags to let know the current direction. for x 1=right 0=left, for y 1=down 0=up)
ball_dir_x
ball_dir_y

(This would be just a speed that I could set. I would use a subroutine to convert this speed into values to adjust ball_speed_x and ball_speed_y. The conversion would take both the X and Y direction checks into account and update the new_ball_pos_x and new_ball_pos_y accordingly. The results would be copied to ball_pos_x and ball_pos_y.
glob_ball_speed

(I would still have keep these individually so that I could adjust the speeds independently to either axis. This wouild allow me to add curve-like effects to the ball. glob_ball_speed would never be used directly to update the drawing position of the ball, but rather glob_ball_speed would always convert to update these variables. Not ideal but sufficient)
ball_speed_x
ball_speed_y

(Not exaclty necessary, but I would use them as a temp holding spot for my calculations before copying the values into current_ball_pos_x and _y. It doesn't take much and I may find a way to utilize it)
new_ball_pos_x
new_ball_pos_y

I'm sure I would have more values. I'd have a
-ball_max_x & ball_min_x and y equivalents
-friction value.
-I might have ball_size_x & ball_size_y to allow changes tothe size of the ball.
-I would also have variables for anything else related to the ball. Including it's starting X,Y coordinates, it's values related to graphical representation, color etc...

I'd have multiple types of timers. Everything from global movement timers to, volley (round) timers, lasting effects timers etc...

======

After just going through all of this, I could almost decide that it is nearing the point of using actual (simple 2D newtonian physics formulas and sticking with true vectors). If I kept it to that I would hard-code it myself. If I decided to go beyond friction to add spin, reflection based on spin and/or part of paddle hit. etc... I might look to see what's out there as far a 2D physics plug-ins

Your signature has been erased by a mod please reduce it to 600 x 120.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 1st Oct 2011 05:44
Quote: "I now understand why it is important to create a game design doc and include all features before attempting to write a game."


I think you got more out of the tutorial than even the author anticipated. That is just as valuable as a lesson as the code you have learned by taken on the challenge of modifying the base-code.

Even my 30 minutes of brainstorming above wouldn't be sufficient to begin coding, what is relatively considered a "simple" game. But sometimes those insights only come from taking a running leap and diving into to the code. Eventually, as programmers tackle more and more complex tasks, we realize we need to change our methods. I like both. I love to tackle code head on. I learn what I need to by coming up short in my design, and it helps me to design it better the next time. Yeah, I waste alot of time, but it works for me. But if I ever want to complete anything of a large scale, I too will have to reevaluate my design habits.

=)

Your signature has been erased by a mod please reduce it to 600 x 120.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 1st Oct 2011 14:20
Quote: "But sometimes those insights only come from taking a running leap and diving into to the code"

Actually, this isn't something that I've seen mentioned on these forums at all, and it is something that is so true.

Basically, writing code knowing that you are going to throw it away later.

Whenever I start writing anything that is going to be substantial, one of the early things I do is to create a subdirectory where I will write standalone pieces of code that I use to work out ways of doing things. Then when I'm ready later, I'll incorporate the idea (generally not the code) into my main code.

Note that this is not the same as throwing away existing code to rewrite it - that's a different thing altogether and should generally be avoided (as Netscape found out to their cost - it lost them the browser wars, although it hasn't turned out too badly for us as it gave us Firefox).

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Oct 2011 11:10 Edited at: 3rd Oct 2011 11:11
Quote: "After going back and studying the code, I noticed that it would be hard to increase the ball's speed by altering these variables as they change from a positive to a negative during play depending on where the ball is at when the timer reaches 0. "

Not really, a couple of if statments will do the trick. Something like this:


Zenassem has provided some knowledge which could be very handy later but for a pong game while you learn the ropes of programming this should suffice. I'm not saying don't use Zenassem's methods but if you find them a little daunting for now, this is a basic alternative.

Quote: "I now understand why it is important to create a game design doc and include all features before attempting to write a game. "

The problems you hit weren't really due to lack of game design docs, they were more or less unforseen problems. The more you program the more likely you'll see these problems and fix them. As Albert Einstein once said "A clever person solves a problem, a wise person avoids it". The trick is to move from being a clever programmer to a wise programmer, like IanM or Zenassem.

Quote: "But sometimes those insights only come from taking a running leap and diving into to the code."

Like IanM I totally agree.

Edit- forgot to close quote

Lifebandit
14
Years of Service
User Offline
Joined: 27th Mar 2010
Location: Texas USA
Posted: 3rd Oct 2011 19:02 Edited at: 3rd Oct 2011 19:36
Ya, Zen's suggestion was a lil daunting but I haven't forgotten it. Your method of chasing the positive and negative variable might be the way to go. I thought about doing something like that but didn't think it would work. Ill sure try it though. Thank you all for the help.

I would also like to say that since my 10 yr old son is into algebra now in school, he has asked me if he could also learn programming!!! /sniff, my lil boy wants to help his ol man.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Oct 2011 23:58
Quote: "I would also like to say that since my 10 yr old son is into algebra now in school, he has asked me if he could also learn programming!!! /sniff, my lil boy wants to help his ol man. "

You must be very proud and your son is doing very well if he's learning algebra at 10 yrs old. I didn't learn until I was 12 and I was young for my year! One of the best ways of learning is to teach others, remember that.

Lifebandit
14
Years of Service
User Offline
Joined: 27th Mar 2010
Location: Texas USA
Posted: 4th Oct 2011 02:00 Edited at: 4th Oct 2011 02:04
Ya he is home schooled. He is about 2 years ahead of where he should be. My daughter is 7 and is in public school. She also wants to learn programming (must be just because I am teaching my son, lol), but I had to tell her she needs to wait until she understands algebra. Then if she is still interested, I would teach her. My wife said she kinda enjoys this being a "guy" thing for right now lol.

I have 0 problems with both kids graduating high school, knowing programming and maybe even having a title or two under their belts. I have told them that it is a challenge to learn and will take many many hours of reading and practicing. I can only hope that my will and desire to learn will rub off.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 4th Oct 2011 03:37
You have some bright kids, if your kids start coding at the age of 10 they'll be exceptionally good when they enter the industry. Education is one thing but experience is another.

Quote: "I had to tell her she needs to wait until she understands algebra."

She might be interested in The 3D Game Maker. It's very basic (and written in DarkBASIC I think) but I think she'd have a blast with it.

I've attached a small project which I wrote a while ago and it moves a sprite according to the direction it faces. It could come in very handy for your future endevours and keep an open mind, it's not just limited to moving a space ship. If you'd like to learn the maths behind it punch "Projectile Motion" into google (don't worry, it's not as complicated as it sounds).

Attachments

Login to view attachments
Lifebandit
14
Years of Service
User Offline
Joined: 27th Mar 2010
Location: Texas USA
Posted: 4th Oct 2011 05:39 Edited at: 4th Oct 2011 05:54
Hodgey, I am gonna sound like a child here but...I DID IT!! Your example worked like a charm, well sort of. Even in your sample code:



The "1" was producing hectic results. I had to change the the speedx and y from an integer to a real number in order to slow it down even further. I also had to add in a real variable that represents the speed of the paddles so I could modify that in the game speed function as well. Game runs as intended with the exception of the timer does not reset back to 60 upon a missed ball. I'm still working on that lol.

Thank you my friend.

Quote: "She might be interested in The 3D Game Maker. It's very basic (and written in DarkBASIC I think) but I think she'd have a blast with it. "


I will def check that out!

Modified full code:

[quote][/quote]
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 4th Oct 2011 06:14
Quote: "I DID IT!! Your example worked like a charm"

Glad to hear it A little tweaking may have been necessary. Pong was the first game I wrote in DBPro and like you once I was finished with the basic engine, I had to make the ball speed up over time except I increased the x/y speeds on every hit of a paddle. I remember having to change a few integers to floats and in the end it was quite fun, and the ball got very fast!

Quote: "Game runs as intended with the exception of the timer does not reset back to 60 upon a missed ball. I'm still working on that lol."

There's actually a very easy solution to that in my first post. Two easy steps:
1. Make init_time# a global so it can be accessed by functions
2. Put this line in whenever you need to reset it back to 60:
init_time# = timer()/1000

With step 2 make sure that it is only called once each time you need it and not every loop otherwise it will be stuck at 60.

Lifebandit
14
Years of Service
User Offline
Joined: 27th Mar 2010
Location: Texas USA
Posted: 4th Oct 2011 07:16 Edited at: 5th Oct 2011 00:19
Nice, ty. I wonder how pong will be with 2 or more balls? /devil
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 5th Oct 2011 01:33
Quote: "I wonder how pong will be with 2 or more balls?"

The word "hectic" comes to mind.

Login to post a reply

Server time is: 2024-11-22 12:01:39
Your offset time is: 2024-11-22 12:01:39