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.

DarkBASIC Professional Discussion / Dark BAsic Pong game

Author
Message
nick L
11
Years of Service
User Offline
Joined: 22nd Nov 2013
Location:
Posted: 22nd Nov 2013 16:54
I have been following the book to make the pong game. The display is great, but the controls dont work and the score keeps going up pas 10 for only player one. none of the parts move. here is my code.



and my code for ball movement




please help me out here. I dont know what im doing wrong.

gamecreator NIck
Rudolpho
19
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 22nd Nov 2013 19:32
Are your variables globals?
Otherwise they are local only to the function in which they appear and will thus not affect any other part of your program.


"Why do programmers get Halloween and Christmas mixed up?"
nick L
11
Years of Service
User Offline
Joined: 22nd Nov 2013
Location:
Posted: 23rd Nov 2013 21:48
Yes all my variables are global, but the problem still persists.

gamecreator NIck
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Nov 2013 02:10
Eugh! Why do books still print commands in uppercase?

Rather than correct a variable when it goes out of bounds it's better to make it impossible for it to go out of bounds in the first place.

I changed the variable names for three reasons:
1. Player one was controlling paddle two and player two controlling paddle one, that's just daft!
2. For ease of reading it's best to avoid using numbers in variable names if you can, especially at the end of the name. Here we have "one" and "two", which are the same length and only an extra two characters to write them out.
3. These variables refer to a property of an entity, the y-value of paddle one or two, so the name of the entity should come first, i.e. "paddleOneY" rather than "paddleYOne", paddles one and two are separate entities so they should be distinguished from each other before we bring in the y-value. "paddleY1" and "paddleY2" could easily be misinterpreted as the top and bottom y-values of the same paddle.

I can't answer your question of why nothing is moving because you haven't posted the full code. Functionally the code you've posted so far seems to be fine.


Formerly OBese87.
Vidiot
19
Years of Service
User Offline
Joined: 15th May 2006
Location: TN
Posted: 27th Nov 2013 06:35
@Libervurto, I'm trying to learn a little more about programing best practices and I was stunned by the simplicity of what you suggested there.

Quote: "Rather than correct a variable when it goes out of bounds it's better to make it impossible for it to go out of bounds in the first place."


That sounds logical and more elegant but it flies in the face of something I've always assumed about program construction; namely, that you never put an unnecessary test in your loop. In your example, the computer has to ask both question every time: Is the shift key pressed AND is the position >= 3. Since the position is irrelevant until the shift key is pressed, I would assume testing both with an "and" would be wasting processor cycles. Of course, I really have no idea what is best practices for code optimization in dbpro. Its just an assumption I've held since computers were much slower. Clearly from a logic and code readability stand point, your way is better.

Am I picking nits here?

The answer to Life, the Universe, and Everything? "Tea for Two". Deep Thought was Dyslexic.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 27th Nov 2013 11:55
Quote: "In your example, the computer has to ask both question every time: Is the shift key pressed AND is the position >= 3. Since the position is irrelevant until the shift key is pressed, I would assume testing both with an "and" would be wasting processor cycles."

I'm not actually sure whether DBP always tests both conditions or gives up if the first is false.
I used AND for readability, but if speed is an issue (and if DBP always tests both conditions) you could simply give it a slight restructuring:



Formerly OBese87.
MonoCoder
19
Years of Service
User Offline
Joined: 4th Dec 2005
Location: england
Posted: 27th Nov 2013 14:13 Edited at: 27th Nov 2013 14:20
DBP does "eager evaluation" (as opposed to "lazy evaluation"), that is, it checks all conditions:




Although it's quibbling, I think it'd be faster to (first storing the shift/controlkey() values in variables, ofc) check keys in the outer if and the y-coord condition in the inner if, as it seems to me key=1 would be true a little less often than y>=3/y<=402.



edit: that's the general thinking anyway (putting the least-likely to-be-true condition first, assuming the checking of the condition itself isn't a task more expensive then checking the others), though the reversed nesting of if's is apparent in this case for a different reason - that (as I think mentioned somewhere above) there's no movement without a keypress, and so no need for a position check until there has been a keypress.

Login to post a reply

Server time is: 2025-05-17 22:00:48
Your offset time is: 2025-05-17 22:00:48