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 clone issues

Author
Message
xlxvernxlx
11
Years of Service
User Offline
Joined: 30th Sep 2012
Location:
Posted: 1st Oct 2012 00:50
Okay, so I'm 4 days deep in learning some code, and I've been working on a pong clone on what little I know. To my understanding, it's a helpful way to start learning the basics of programming. Here's what I have:



Pay no mind to the two REMs at the end. I was using another variable system and I broke it down to even simpler terms before I posted.

So, if you run that little tidbit, you'll see that it for some reason places either one or both sprites in the upper left hand corner, and won't really move much either, typically.

The most peculiar part is that I moved the variable declarations into the main, and then out of the main, and the program worked for the test run RIGHT after, but not the one after that.

I just don't know what's going on, and I'm quite puzzled. Also, attached is the Paddle bmp.

I'm a US Army soldier- What more could you ask for?

Attachments

Login to view attachments
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 1st Oct 2012 10:43
There's all kinds of things wrong with that code.

First off, subroutines. These are routines that are usually attached to the end of your program and are called from above. A subroutine has a label and a return command, stating where it starts, and where it ends respectively.

An example of good usage of subroutines:



When you call a subroutine, the program will jump to the label, execute whatever is inside the subroutine, and then it will jump back to the line after the call.

In your code you don't have any return commands anywhere. I'm surprised it even compiled.

Another thing I notice is that you are setting up your game inside the main loop? This means you're loading stuff over and over again. That's not good, you're supposed to do setups and loading outside of the main loop, not inside it.



Can't really help more without the rest of the code.

TheComet

"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 1st Oct 2012 10:51
Hi there xlxvernxlx, welcome to the forum .

I'm afraid your code seems a little confused here. I can't actually compile and run the code you posted there because some of it appears to be missing. For example you refer to two labels (Main_Menu and Pong_setup) that don't actually exist, whilst you have the label images which is never used. If you post your full code listings, that would help us debug it for you.

I think I I can give you a few tips to get you going with this though. First off, don't use absolute file addresses like this:



Instead, use relative file addresses like this:



It will make your programs portable. If you don't start with C:\, it is assumed that the address is relative to the exe.

A second point is on gosubs. Many people around here actually prefer the use of functions because they are generally neater and help keep you code more organised. You can use functions like this:



If you want to continue using gosubs, you should use them with return statements. Return statements return execute to the line of code where you made your gosub statement. To translate the above example into using gosubs, you would do this:



So to get back to your code, I've reworked your code a little and it now performs as I think you intended. I've based it as closely on your code as I can and I've commented every change I've made to help you understand what I've done. Here are the new listings:



I hope that helps but if not, just post some more of your code or some further questions and I or someone else around here will be more than happy to help .

EDIT:
Ninja'd by TheComet

xlxvernxlx
11
Years of Service
User Offline
Joined: 30th Sep 2012
Location:
Posted: 1st Oct 2012 11:54
Wow, thanks guys. You actually taught me quite a bit there. Like I said, I'm 4 days deep, and just needed that extra little help to really understand it.

Rather than copy down what you guys had edited, I just used my own understanding and I got the right side to work. Now I just need to do the left. Here's the source.




Thanks again, this soldier appreciates it

I'm a US Army soldier- What more could you ask for?
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 1st Oct 2012 19:01
I remember writing an extensive tutorial on basic things not too long ago because I was bored, below is a copy-paste of that. I think you will find it useful.

Before I do that, let's check out your code.



Putting this in a subroutine is useless if you're not calling it.



The name you've given to that subroutine does not match it's function accurately.

Gosub Pong_Draw_Field would be more accurate.

Why sprite it twice?



Missing comments...



Okay, it's starting to look like something usable now.

Let's discuss screen settings. Here are the commands:



Alright, so what does each command do? Turning sync on will give you control of when to render to the screen. When sync is on, the screen will only be updated when you use the command sync. By default the sync is off, which causes the screen to be refreshed after every single command. This is very rarely needed, and I can tell you right away that your pong game does not need automatic refreshing. You can try this code to see what it does:



Run that. No text, huh? Try turning the sync off. You will see text again. After that, try turning the sync back on and take the "rem" away from the sync command. Text text will be rendered to the screen.

What does sync rate do? This command defines how fast you can update the screen per second. Here's an example:



Try changing sync rate to 30 and see what happens.

Never go over 60 frames per second unless you know what you're doing. It does not make any sense because the monitor physically can't refresh faster than 60 frames per second. You'd be wasting GPU power.

Alright, so what is the backdrop? The backdrop is pretty much the picture you see on your screen. If we turn the backdrop on, this will cause the backdrop to clear itself before every screen update. The backdrop is off by default, which means that the backdrop isn't cleared before updating, which causes the new data being printed to the screen to accumulate.

Run this to see:



You should only be able to see "awesome". Turn the backdrop off, and you will see everything again. Note that we don't have to call sync because sync is off by default.

Alright! So with that out of the way, your game requires sync to be on and backdrop to be on, so put this at the start of your code:



And add the sync command to your main loop so we refresh the screen once every loop:



Right, the rest you should be able to get working by yourself. Here's that tutorial I promised:

----------------------------------------------------------------------------------

First off, let's talk about indentation. Indentation is when you press "tab" to move sections of code further to the right. There are rules you need to follow in order to do correct indentation. It's very simple: Every command that has a "partner" to it will trigger an indentation. Examples of these commands are:



And an example with correct indentation:



Why does this help? Imagine you forgot to use an "endif" somewhere in your huge amount of code. All the compiler will do is say "GAY! NESTING ERROR AT LINE <insert garbage number here>!!" and jump to a line that has nothing to do with the actual error. If you indented correctly, you will easily be able to see where you have an indentation too much, or too little. That's one of the major reasons why it's useful, the other reason is that it's so much easier to read because it reflects the logic.


Absolute and Relative File Names

This is an absolute file name.



This is a relative file name.



How does this help? The most important thing is that your program, when using absolute file names, will only run if you have placed it in the folder "C:\Program Files (x86)\The Game Creators\Dark Basic Pro FRee\Dark Basic Pro\Projects\Unit 22 Game\". Well that isn't useful if you want to give it to people who want to play it, right? That's where relative file names become useful. If the executable program is in the same directory as the image, you only need to use:



If you decide to make a directory in that folder called "images" and place your image in there, you just change it to:



It's as simple as that.



Comments

As some guy I can't remember the name of once said, "Good comments are half the code". This is both literally and ideologically true, as you do write much more, but if you leave a program for 6 months and return to it, you'll praise the lord and kiss Adam's apple like a bag of potato chips (I have no idea where this metaphor is going) for having written comments. A non-commented program is a programmer's worst nightmare because you easily forget why you did something in a particular way. Comments will help you remember. There are many ways to make comments in DBP, the standard BASIC syntax is the keyword "rem". But you can use these as well:



You don't necesseraly have to write comments for every single line of code, but try to group your code and explain as much as possible. Here's an example of good grouping:



See how there isn't a comment before every command, but there is a comment explaining what each section does.


Arrays

Said to be one of the scariest types of variables in existance, but they are vital if you want to have more than one of the same thing in your program (such as more than 1 asteroid). This is why you must understand the concept of an array. Here's an example of how to define and fill an array:



So let's print that to the screen:



Well that was a waste of coding space... Here's where arrays get cool. You can compress the latter by doing this:



Arrays don't have to be strings, they can be floats, integers, bytes, booleans, words... Anything a normal variable can be.


User Defined Types

These help structure and group together variables that have things in common. If you wanted to create a player UDT (user defined type), this is how you'd do it:



See how all of the elements between the "type...endtype" commands can now be used inside the player variable? This is really cool because it's really easy to add variables just to the player later on, and it keeps all of your variables really structured.

Oh and if you're wondering what the "VT" stands for, it's a habit of mine so I know that it's a type and will be used for a variable, hence "Variable Type". If it were for an array, I'd use "AT" for "Array Type".

TheComet

"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
MrValentine
AGK Backer
13
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 2nd Oct 2012 21:27
hi xlxvernxlx, welcome to the forum, interesting username there

I suppose there is little I can add to this... so just wanted to welcome you as well to the forums...

xlxvernxlx
11
Years of Service
User Offline
Joined: 30th Sep 2012
Location:
Posted: 3rd Oct 2012 21:05
@TheComet: Thanks for the information and helpfulness. I love the camaraderie here on this forum, and you're a shining example of just that.

@MrValentine: Thanks for the welcoming and such, I appreciate it. I did finish the pong clone, and it's as basic as it gets, but I'm a bit proud I managed to pull it off haha.

I'm a US Army soldier- What more could you ask for?

Login to post a reply

Server time is: 2024-04-19 13:16:31
Your offset time is: 2024-04-19 13:16:31