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 / Need Help Sprite Problem.

Author
Message
summerfed
15
Years of Service
User Offline
Joined: 14th Jun 2011
Location:
Posted: 24th Jun 2011 04:17
How to put enemy sprites to a 2d game, wherein all the enemy sprites is scattered accross the screen let's say 100 enemies, and assign attributes to each like health, attack and etc. I manage to put 1 enemy, but I'm having difficulties adding another enemy sprite. Thank you in advance.
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 24th Jun 2011 10:25 Edited at: 24th Jun 2011 10:32
You could use arrays in combination with user defined types. Something like this perhaps:



A clever person solves a problem, a wise person avoids it - Albert Einstein
summerfed
15
Years of Service
User Offline
Joined: 14th Jun 2011
Location:
Posted: 24th Jun 2011 12:24
@Hodgey: Thank you for the reply,


I manage to make this code, How can I transform it to LOOP type?
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 24th Jun 2011 13:09
Quote: "How can I transform it to LOOP type? "

You have the do-loop in there but I assume you mean for-loops?

Anyway, you currently have this:

which creates 3 sprites 1, 2 and 3. Now the long way to handle each of these sprites is to make variables for each one such as enemy1x, enemy2x etc. What you could do if you plan to use the same concept of variables (e.g have an 'x' and a 'y' for each sprite) for each sprite is to make a user defined type as seen in my first post.

If you wish to collectively handle sprites, you can create an array of your user defined type. Same code as before:

Ok the first bit is the type endtype declaration. This will create a "type" or DBPro's version of a class (not as powerful as the ones seen in c++, java etc).
The next line creates an array of that class, enemy. So now we have 100 (101 if you count enemies(0), you don't have to) enemies. Each of those 100 enemies now has each of those variables listed in our type enemy. The for-loop I use in the do-loop is just a small example of how you can handle all of those aliens collectively and you don't have to do it individually.

To access each element of the type you follow the main variable (enemies) with a dot (.) and then the element. E.g if I wanted access to the first alien's x value I would access it like this enemies(1).x .

Note, you cannot have arrays in UDTs


Hopefully I have explained well enough and provided enough information for you to have a go at restructuring your code. If you have any questions just ask

A clever person solves a problem, a wise person avoids it - Albert Einstein
summerfed
15
Years of Service
User Offline
Joined: 14th Jun 2011
Location:
Posted: 24th Jun 2011 13:26
I appreciate your help for me. Thank you very much. Here is my produced code so far. If you are going to run this software this only display 1 monster. Up to now I am having difficulties in displaying many enemies. I tried everything in my mind. but still it produce error.

type Monster_Stats
Health as integer
Attack as integer
xmonster# as float

endtype

Dim Monster(100) as Monster_Stats

for n = 10 to 100

Monster(n).Health=1000
Monster(n).Attack=10
Monster(n).xmonster#=rnd(1000)

next n

--> here is my user defined codes, my question now is how and where I must put Sprite i , Monster(n).xmonster#,ymonster#, monster. Meaning where I must put the show sprite code. I must finish this program 2 months from now as my Thesis Requirement. Thank you very much for all your help.
summerfed
15
Years of Service
User Offline
Joined: 14th Jun 2011
Location:
Posted: 24th Jun 2011 14:21
Sir, please look this program, and tell me how can I add enemies to the screen, I cant figure out how to add enemies to this work of mine. Thank you so much
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 25th Jun 2011 01:21
When displaying multiple sprites of the same type at once I like to add 'sprite_num' and 'image_num' in the type. These variables will hold the sprite numbers and image numbers of each enemy or monsters in your array. If your program is going to be handling heaps of sprites, you have 1000 monsters, it is probably a good idea to track the sprite numbers and image numbers in an excel spreadsheet or equivalent. Once you have that set up you can simply do this


I just went through your code (attached one) again and I think I see what you are doing. It appears that you are creating mutliple arrays to hold the x, y values etc for your monsters. This isn't necessary, you only need one array of monsters as a monster_type which contains the x, y etc.

Here's a small example of UDTs and arrays


So have another go at restructuring your code, you are getting close.

A clever person solves a problem, a wise person avoids it - Albert Einstein

Login to post a reply

Server time is: 2026-07-10 13:44:33
Your offset time is: 2026-07-10 13:44:33