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 / Ball movement in pong

Author
Message
Ashir
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Caracas, Venezuela
Posted: 12th Oct 2007 12:45
Hi again guys, I'm still practicing with a pseudo pong game and a few minutes ago I made the ball move (Yeeeeahhhh!!!).

I want to stablish a invisible border on the screen, so if the ball goes up line 5 (that's the Y axis invisible border) the ball rebound and goes down. Problem is, the ball "sees" the invisible border and stop going up, but it doesn't go down, istead the ball just go to the right.

How I make the ball go down and right like in a rebound trajectory?.

Thanks in advance.

Kentaree
21
Years of Service
User Offline
Joined: 5th Oct 2002
Location: Clonmel, Ireland
Posted: 12th Oct 2007 13:52
It's kind of hard to figure out your code as both variables and comments are in spanish (I think, forgive me if I'm wrong), but the logical way to do this would be to have a verticalDirection variable, and set it to something like -1 if the ball is moving up, or 1 if the ball is moving down. Then, when moving the ball, all you have to do is multiple the upspeed by verticalDirection, and it should move in the right direction.
When it reaches the top, you change the value of verticalDirection to 1 so it'll move down, and when it reaches the bottom you change it to -1 so it'll move up, and you can use something similar for horizontal movement aswell.

Ashir
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Caracas, Venezuela
Posted: 12th Oct 2007 23:26
In fact, the vertical direction variable was a trick in Beginner's Guide to DarkBASIC Game Programming bubble rebound, but from my point of view my logic leads me to a similar point, it's just that I can't the freaking ball in my code just go to the right after "touch" the border instead of goind down AND right... It's so frustrating.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 13th Oct 2007 04:03 Edited at: 13th Oct 2007 04:05
Ashir

First of all, why not use sprites to move the ball and paddles instead of clearing and re-drawing the screen every time? This is a lot of unnecessary work that you are coding.
From what I can tell, you need to press 'W' and hold down the space bar in order for the ball to even move. When it does get to the top, it bounces along the top without coming back down as you have said.
The code that you had needed some work. Rather than try to tell you what I would recommend, it was easier to re-write some of it. Here it is:


I have replaced the constant screen redraws with sprites. I have shortened your do-loop considerably. I have made it so that you can used the 'W' key to move the left paddle up and the 'S' key to move it down. I left the coding of the right paddle to you. The ball bounces and moves correctly now, but it basically will move off the right side of the screen. You need to put in the code to check whether or not the players missed the ball. I left the mesa() function there, but it was not necessary to call it in the new code, so you could just delete it if you want.

Hope this helps.

LB
pcRaider
17
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 13th Oct 2007 05:27
I think that this thread is useful for you.
http://forum.thegamecreators.com/?m=forum_view&t=115868&b=1
Ashir
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Caracas, Venezuela
Posted: 13th Oct 2007 06:13
LBFN that was an amazing piece of code. I don't knew that get image and sprite commands could do that.

I know I'm botering too much but, could you explain me:

1. What I was doing wrong in my code for the rebound (for not doin it again)?

2. Why sync on?

3. Why backdrop off?

4. Why the net looks like holed in some spots?

5. Please explain me the get image and sprite mechanic used in your code to understand it better?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 13th Oct 2007 06:59
I'm glad the code helped you. I like the "amazing" comment, but when you have been around a while you will realize it is basic stuff. To answer your questions:
Quote: "1. What I was doing wrong in my code for the rebound (for not doin it again)?
"

You were looking at the y as being out of bounds if you were <5 or >595 at the same time, but your code adjusted if it either was in or out of these bounds. It wasn't like a one-time correction. So, when the ball was <5, it added to the y and when it was (>5 and <595) then it subtracted from the y. What I did was change the direction of the ball only if it exceeded these borders.

Quote: "2. Why sync on?
"

You need to look at your help files. To quote from there, "By default, sync is set to off which allows the system to automatically handle screen refreshing. " By turning the sync on, you are telling the computer you want to handle refreshing the screen. This way, you can complete a signinfcant amount of drawing work before the screen is refreshed, resulting in a program that operates much faster and more efficiently. You had declared a sync rate, but you had not turned it on.

Quote: "3. Why backdrop off?
"

My personal preference. When I am working with 2D stuff as you are, I see the backdrop as unnecessary, so I just turn it off. It will work either way.

Quote: "4. Why the net looks like holed in some spots?"

That's exactly how it looked to me when I ran your code. I took your mesa() function and moved it into the code instead of leaving it in the function. Since you were no longer re-drawing it every loop, you didn't need it in a function. I didn't change how it looked at all.

Quote: "5. Please explain me the get image and sprite mechanic used in your code to understand it better?"

That could take a while. Briefly, you can draw images in MSPaint (or whatever drawing program you use) and load them into DBP. You can also draw them with code and grab the images from the screen (which is what was done). The last number of the sprite is the image it is using for display. Sprites do not have to be redrawn every loop. They can be sized differently. If you notice, the ball, paddles and net are all very different shapes. You can also do collision checks with sprites to see if they are colliding.
You really need to review some of the tutorials on sprites and look at the help files and examples.

You also need to look at the bottom of this page at the 'Thread Subject Search' box. You can enter in a subject there and it will check to see if there are any forum matches. You can then review the threads found to see if other coders have asked the same questions that you have.

Best,

LB
Ashir
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Caracas, Venezuela
Posted: 13th Oct 2007 13:40
Man I didn't knew that draw/image/sprite trick and it's not mentioned on the book Beginner's Guide to DarkBASIC Game Programming, really nice.

I asked about the sync because I read the help file and it says more or less the same you said, so if I want 60 fps and I put sync rate 60 and sync on in theory screen should refresh at 60 but I get 60-67 fps and without sync on I also get 60-67 fps, so I concluded that it wasn't that useful or the manual was wrong. The only difference between no sync on and sync on was the ball speed. Without sync on a ball speed of 2 was slow (like it was supposed to be) but with sync on the same ball speed was equal to a no sync on ball speed of 5.

I rerun your code and the bites in the white squares used to recreate the net dissapear. Hell, I'm not crazy I swear I saw the white squares with tiny holes in the borders, so much like when you bite a cookie...

So oh wise master of DBP, enlightme a bit more. If I understand what you said, if I make a ball on screen and filled with pixels then I can make an image of that pixel filled ball and use it later like a sprite that will not be redraw in every cycle... right?

Btw, I read the help file and manual, but you have to admit that some commands aren't well explain while others are almost cryptic; and yes, I make often thread subject search, is just that my questions are so... unique, but you know what south park state before: there are not stupid questions, just stupid people.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 14th Oct 2007 01:06
Quote: "oh wise master of DBP"
lol

You can make the ball whatever color, size or shape you want and you can fill in the pixels as you please. You can draw it using DBP commands or you can draw it with a paint program and load the image into your game.

Quote: "I saw the white squares with tiny holes in the borders, so much like when you bite a cookie...
"

My guess is too much or too little sleep (just kidding).

You are looking in the right places for help and while the answers may seem obscure at first, keep at it and things will begin to click for you. You could also look at the code snippets board. There are a lot of handy things there and you can learn from others' work.

The sync rate is used to limit how fast the program can go. Your program will fluctuate a little bit from the sync rate that you establish. This is designed to keep gameplay at a steady pace. If you want to see how fast your program will run on your computer, you could enter 'sync rate 0' instead of 60 (I don't use this for my games, but it is interesting sometimes to see how fast it will run).

LB

Login to post a reply

Server time is: 2024-09-27 05:16:28
Your offset time is: 2024-09-27 05:16:28