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 Discussion / Sprite Tutorial

Author
Message
Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 7th Jul 2008 23:12
I searched for it, but couldn't find any for DBC. Any help here guys? I just want to know how to make a simple sprite do simple things like move left when you press left on the arrow key. I have pretty basic knowledge of DBC. Here's one of my most recent programs to show you what I can do:
Thanks Ahead of Time.
-Irojo



I have mad skills. I can make a dot follow your mouse in 2d. Don't try and tell me someone has done that before.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 8th Jul 2008 00:30
Somewhere in TDK's tutorials there must be some sprite stuff. I'm not sure where to look based on the headings though. Maybe the 2d space invaders.

Take a look at:
http://forum.thegamecreators.com/?m=forum_view&t=34076&b=4

That should give you a good starting point.

Enjoy your day.
Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 8th Jul 2008 00:55
Ok thanks... I'll look through that tutorial.

I have mad skills. I can make a dot follow your mouse in 2d. Don't try and tell me someone has done that before.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 8th Jul 2008 03:40 Edited at: 8th Jul 2008 03:44
Throughout the forums there is a lot of informatino about sprites. I've responded to many posts on the DBC board alone. I'm not up to writing a complete tutorial but I can give you a few basics that may be helpful.

What is a Sprite?
In relation to DBC, a Sprite is an image that is self redrawing. It can be set to have any truly Black colored areas be transparent. A Sprite can also be set to have collision detection between itself and other Sprites.

A Sprite always appears on the main drawing screen (bitmap 0) and there are several built in DBC functions that can be used to control it's size, position, and to a limited degree, it's orientation.

What do I mean by self redrawing? As opposed to a standard Image object in DBC, a Sprite can redraw itself without the need of a loop or additional code. This means that until it is hidden or moved off screen, it will always appear. Those characteristics (and it's collision detection ability) are really what set it apart from a standard Image. A programmer will find this useful because the tasks of redrawing the Sprite itself and the background are reduced greatly. And since a sprite always redraws itself, it will appear on top of every other graphic on the screen (including 3d) - so it can be useful to hide areas of the screen.

How do I make a Sprite?
A sprite is created using the Sprite command:
SPRITE Sprite Number, X, Y, Image Number

The sprite number is the number you assign to the sprite. It can range from 1 to 65535 so you can make plenty!

Note: Sprites quickly eat up the CPU cycles and reduce performance, so managing them and limiting their numbers properly is important.

X,Y are the x and y pixels positions of the screen. X reads from 0 to screen width -1 from left to right and Y read from 0 to screen height -1 from top to bottom. The sprite will be drawn at the X,Y position starting from the sprites upper left hand corner.

Image Number is the previously created or loaded Image object that will be assigned to the sprite. This is also another area where a Sprite makes things easier for a programmer. You can essentially animate a sprite by cycling through a series of previously defined images.

A little bit more on Sprite Images and Animations:
Sprite Sheets

Sprite Positioning:
Using the X,Y coordinates, one can move the sprite around the screen by changing these values. Here's a quick example that will move a green square Sprite across the screen from left to right:



The Sprite can also be positioned using an input device like a keyboard, mouse, joystick, etc. In the following example, we'll use the arrow keys on the keyboard to change x and y:



Enjoy your day.
Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 8th Jul 2008 03:44 Edited at: 8th Jul 2008 04:04
Thank you so much Latch. That is extremely helful for me. I appreciate your time. I'm going to learn alot from this.

[EDIT]
On the topic...
I've seen in many codes people use 'data' and it usually has to do with sprite images. How do people define that data is for a certain sprite, if they have more then one? And any extra info on data?

Example:

data 0,0,1,0,0
data 0,1,1,1,0
data 0,0,1,0,0

[EDIT 2]
Thanks to your help, I'm really starting to get this. Here's a program that demonstrates Sprite images. Thanks again.



I have mad skills. I can make a dot follow your mouse in 2d. Don't try and tell me someone has done that before.
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 8th Jul 2008 04:08
Never really seen that, but I can explain the data command to you.

Basically, data is just a list of information. It can contain strings, floats, or integers. Each piece of information is separated by a comma. The catch is that it all must be the same type of information (one line of data must be all strings, or all floats, etc etc).

To get the data out of the list of data, you use the READ command to read the next piece of data into a variable. You can use data for anything, and reading information to be applied to sprites can be useful, I can imagine. Having never seen it applied this way, I can't begin to guess what it is used for.

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 8th Jul 2008 04:09
The DATA keyword provides a list of numerical or string data (imagine that! ). It is used in conjunction with the READ command. The values contained in data statments can be used for all kinds of things. The are basically set up to store information to make things easier to organize or enumerate.

Data statements do not apply to Sprites only. They can be used to store information about anything. The data is read back using READ which assigns each data item being read to a variable. The data is read back 1 item at a time per READ statement encountered.

It's up to you how you decide to use the data. You could store the positions of a sprite. You always identify your sprite by the first parameter of the Sprite command: SPRITE <sprite number>

Sometimes you want to start reading data from the top of the stack again after it has been read. Remember, as you invoke READ, the next item of data is automatically pointed to. If you read in all of the data, the pointer will be at the end of the data. To reset the "data counter" use the command RESTORE.

The following examples just shows how to assign and read the different data types to the proper variable types:



Enjoy your day.
Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 8th Jul 2008 05:40 Edited at: 8th Jul 2008 06:12
Thanks guys... I had my example of data in use... and then... Dark Basic crashed on me!

Don't worry... I'll do it again.


[EDIT]
Finished.
Recognize this style now?




[EDIT 2]
So the get image command is basically just taking a picture?

I have mad skills. I can make a dot follow your mouse in 2d. Don't try and tell me someone has done that before.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 8th Jul 2008 06:55
OOOPS! I must've been typing at the same time as BN2! And I was beaten to the post!

Enjoy your day.
Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 8th Jul 2008 17:11 Edited at: 11th Jul 2008 03:33
[REMOVED]

I urge you to watch the film "Who killed the electric car". Support electric cars! Did you know their used to be more electric cars then gassoline cars?

Login to post a reply

Server time is: 2025-06-07 06:53:45
Your offset time is: 2025-06-07 06:53:45