Hi i was trying to make tank-shooting game. I am new to proggraming so i thought it would be nice to try out allegro. I have a tank that moves, when you press space a bullet appers and fly's in the direction of tank's movment and when the bullet touches the wall it is trnasfered out of the screen.
So i have 2 questions ...
-How can i make multiple bullets at the same time?
-as for now the bullet after hitting the wall goes to x=-100 y=-100 but does not dissapear ... how do i make it dissapear as an imige?
my code:
-How can i make multiple bullets at the same time?
Have an array of bullets. I's suggest you create a struct to hold info about bullets. something like this:
typedef struct { int active; int x; int y; int w; int h; } BULLET;
Now you can create an array of as many bullets as you need. eg:
BULLET bullets[100];
as for now the bullet after hitting the wall goes to x=-100 y=-100 but does not dissapear ... how do i make it dissapear as an imige?
By using the active member of the struct we just used. When a bullet is created, set active to 1. When a bullet goes off screen (or is no longer needed for some other reason), set active to 0.
Only draw or update bullets where active == 1.
One thing I noticed in your code is that you have a HUGE memory leak, as you are constantly loading bitmaps. Each time you call load_bitmap() and assign the result to an already existing pointer. Any BITMAP that pointer was already pointing to now becomes inaccessible, but is not freed from memory, so your program will take up more and more resources constantly. You need to call destroy_bitmap() for each time you call load_bitmap() in order to free the memory.
This still isn't the ideal solution however, as calling load_bitmap() constantly not only keeps allocating more memory, it is also very slow.
A better way to do this is load all the player and bullet BITMAPS you will need at the start, and then create a current player and bullet BITMAP pointer that can point to any of the bitmaps you loaded initially. Each time you need to change the BITMAP, just change where the current BITMAP pointer is pointing to.
so in main function:
in shooting
and when deleting bullet a--;
something like this? i'm not sure how to use strucktures since i did not have it on my lessons ;/
If i got it your code, when you are shooting, you are moving ALL the array, and loading each time 100 images, what will cause a great memory leak. Other thing, the struct doesnt need to be in main function, is better to be outside any function.
About the shoot, you could make this way...
this is not the proper way... but is a little start to understand the object oriented programing
this is not the proper way... but is a little start to understand the object oriented programing
He's using C, not C++.
So instead:
for(int i = 0; i < 100; i++) { if(!bullet[i].active) // here you check if the bullet is not active { bullet[i].active = 1; // here you make it active init_bullet(bullet[i]); } }
The init_bullet() function will check which direction the ship is facing and set the x and y values accordingly.
A deInit() function isn't really required.
He's using C, not C++.
Sorry, i didnt saw that...
I get error
164 C:\Documents and Settings\kuba\Pulpit\Projekt Infa\main.c structure has no member named `active'
but in structure there is int active or am i missing something?
should there be something in the () of buller ini function?
structure has no member named `active'
I get those errors all the time, due to my mixing up '.' and '->'.
You only need to load each bitmap one time. All 100 bullets can draw the same bitmap. For small games it is normal to load all bitmaps one time at the beginning of the program (i.e., right after initializing Allegro) and keeping them in memory for the life of the program.
We load the bullet sprite once, use it for all 100 bullets, and we destroy it once when we're completely finished with it (i.e., when the user exits).
I'm glad to see you checking return values. Your indentation is a little bit sporadic though.
I'm not sure why you're using load_bmp directly. I think that's a slightly lower-level function. I think the correct front-end is load_bitmap, which does pretty much the same thing, but I think supports a few variations of formats. That said, if you're using load_bmp for a good reason then that's fine.
I used load_bmp 'couse i didn't know any other functions as i said i am proggraming for one and a half week and learning in allegro is something that i do for myself becouse in school we have basics right now.
there is error in loading scrbuf (what it supose to do anyway?)
It is wrong yet. Pay close attention, your are loading the bitmaps on key press. So, with you press lets say, key down 300 times, u will load 300 images. This is memory leak! Load all bitmaps you will need ONLY ONE TIME. Then use them.
For example...
int main() { BITMAP *player[4]; player[0] = load_bitmap("player_UP.bmp", default_pallete); player[1] = load_bitmap("player_DOWN.bmp", default_pallete); player[2] = load_bitmap("player_LEFT.bmp", default_pallete); player[3] = load_bitmap("player_RIGHT.bmp", default_pallete); // In your draw function masked_blit(player[face], bufor, 0, 0 player_x, player_y, player[face]->w, player[face]->h);
There still errors in shooting bullets, but try to figure out first. Try to compile the code in your head and see what are happening.
player[face] don't give errors but there is game error and it doesn't compile
old code without shooting to test out
scrbuf = bufor
Its just a name for buffer BITMAP.
EDIT
Hint: destroy_bitmap only destroys one bitmap.
So i deleted the scrbuf completly and in draw_sprite i put bufor and it compiles (as well with your player[4] instad of puttinig images)
so i will try to make it fire multiple times. if i fail completly i will as you guys again for help but i hope i won't need to
thank you all again!
Sorry, I didn't have the energy to write a fully working program. There are implicit parts missing. Also, note that I passed the two bitmaps in the wrong order to draw_sprite (it's been a while since I've written A4)... Fixed.
ok i have multiple bullets and showing them depending on face of the tank. now i work on their movment and collision with wall and i have problem of making sprite move
code:
In that code you have there, you set bullets[i].active to 1 and then in the next line check if it equals 1, which doesn't make much sense.
Also, without seeing the rest of the code you have, I'm guessing that all your bullets will move in whatever direction the ship is currently facing, while they should be moving in the direction the ship was facing when they were fired. You can fix this by either storing the direction they are traveling in the bullet struct, or store the x and y component of their velocities.
Does this make any sense?
i have added face in bullets structure
First please specify what you are trying to do right now? The code you posted doesn't make sense (i.e., if face is 0 then set face to 0; but it already is ). It looks like you don't understand what it is you're trying to do and if you don't know that then we can't help you.
I would recommend that you abstract the concept of "shooting" into a function. For [crude] example:
Then if you want to fire a bullet when you press the space bar then you could just do something like:
if(key[KEY_SPACE]) { fire_bullet(NUM_BULLETS, bullets, player_x, player_y, face); }
Of course, this only shows how you can record a newly fired bullet. You still need to write the logic to move and draw active bullets (independent of the player). Does this make sense to you? Copy/paste will get you nowhere.
face was the facing of the tank and i wanted to store that variable in bullet structer so that's why there is bullet.face
but yeah making other function for shooting would be better