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 / Asteroid Game Help.

Author
Message
Heliosin
12
Years of Service
User Offline
Joined: 19th Jun 2012
Location:
Posted: 19th Jun 2012 15:20 Edited at: 19th Jun 2012 17:16
Hi all, I'm new to this forum!
I thought I'd start with demanding help!

As you can guess, I'm new to DB, so please excuse any stupid mistakes.
What I'm trying to do is:
Make the summoned lasers go up the screen,
Make the asteroids go down the screen until the bottom where they de-spawn,
and allow for multiples of each on the screen.

At some point I'll need to create a collision code for when the laser hits the asteroids...but one thing at a time.

Any help you provided for the laser/asteroid movement would be nice

Boredom, Fanning the Flames of Stupidity Since Time in Memorial...
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 20th Jun 2012 11:00 Edited at: 1st Oct 2012 18:39
Let's see what help I can provide. *Cracks Knuckles*

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".


Comet, will you shut up already and get to my code?!

Since you asked so nicely, of course!

Let's first get your player moving around at the bottom of the screen. First off, let's get a basic program structure down, declare the player type and define a player variable:



Alright, now we move the player! For this we create a function which controls the player:



Insert that into the program:



Wow, it moves! Alright, what's next? Asteroids that spawn at the top of the screen and despawn at the bottom. This is where we will use the full power of our knowledge and use... ARRAYS!!

The same procedure. Declare the asteroid type, define the asteroid array.





Awesome. Now here is where it get's a bit complicated, but cope with me. We are going to create a spawn and despawn function for the asteroids. Here's the spawn function:



And here we have the despawn function:



Alright, now we just need to control the asteroids. For this, we'll add a control function just like we did with the player.



Awesomeness! Now add it to the main program. I also added one line to spawn asteroids randomly in the main loop just to demonstrate.



Phew, that's a lot of code. BTW, I actually can't guarantee that it will run, because I wrote this off the top of my head. The theory works though, you just might find an odd error here or there.

I'll stop here with this tutorial. I suggest you play around with the code, get to understand it etc. When you feel safe, try adding bullets next. When you make bullets, do exactly the same thing as we did with the asteroids. Create CreateBullet(), DestroyBullet(), ControlBullet() functions. This way of coding is very structured, and you can't go wrong.

Collision is pretty simple then, just check if an asteroid is near a bullet in the ControlBullet() function, if so, destroy the asteroid and the bullet.


I hope this helped!

TheComet

"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 20th Jun 2012 16:56 Edited at: 20th Jun 2012 16:58
RonsWare
14
Years of Service
User Offline
Joined: 5th Sep 2010
Location: Netherlands
Posted: 20th Jun 2012 18:03
TheComet is great

Cheers Ron

Programming is learning
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 21st Jun 2012 10:23
That was a great mini-tutorial, Comet. I clicked on this expecting a typical "give me t3h codez" thread. Well your tut actually caused my to laugh a couple of times! I like your writing style. Thanks man, I needed that - especially since the power got cut sometime before I woke up and I'm runing off batteries and no coffee.

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 21st Jun 2012 11:45
How long did it take you to type that TheComet? :y You truly are one of the forum's finest.

TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 21st Jun 2012 19:07
Aw, shucks guys I try my best to help out the newcomers wherever possible!

I was at work when I wrote it (I'll probably have to try and sneak in a DBP compiler somehow so I can actually test what I write) and it took me ~50 minutes or so.

TheComet

WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 22nd Jun 2012 13:33
And proper code formatting and commenting... 10/10...

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!

Login to post a reply

Server time is: 2024-11-22 00:09:17
Your offset time is: 2024-11-22 00:09:17