Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » shot and diagonal movents

This thread is locked; no one can reply to it. rss feed Print
shot and diagonal movents
dengue
Member #12,790
April 2011

hi ,i dont speak very well english(i speak spanish) so i will try be very puntual in the things that i need help.
I need to do sometime like the game geometry wars.
The first problem is that i have adaptet de diagonal movent but i can't adapt the movent to diagonal imagenes, i dont know how do it, the second problem is that i need that when the users press the arrow keys, a shot go in the direction of the arrow key, the problem is that i dont know how make that the shot continue in the direction that the arrow key indicates, i think that i need like use the timer but i haven´t seen a example for that, please help, and sorry for my english;D

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Dengue said:

The first problem is that i have adaptet de diagonal movent but i can't adapt the movent to diagonal imagenes, i dont know how do it,

You need to keep track of position, speed, and the angle of your object :

#SelectExpand
1#include <math.h> 2 3typedef struct Moveable { 4 float x; // x position 5 float y; // y position 6 float xvel; // x velocity 7 float yvel; // y velocity 8 float speed; // total speed 9 float angle; // angle in degrees 10}; 11 12Moveable NewMoveable(int x , int y , float speed , float angle) { 13 Moveable m; 14 m.x = x; 15 m.y = y; 16 SetSpeedAndAngle(&m , speed , angle); 17 return m; 18} 19 20void SetSpeedAndAngle(Moveable* m , float speed , float angle) { 21 m->speed = speed; 22 m->angle = angle; 23 m->xvel = speed*cos(angle*((2.0f*M_PI)/360.0f)); 24 m->yvel = speed*sin(angle*((2.0f*M_PI)/360.0f)); 25} 26 27void Move(Moveable* m) { 28 m->x += m->xvel; 29 m->y += m->yvel; 30}

Example usage :

Moveable m = NewMoveable(x , y , 25.0f/60.0f , 45.0);// Moves 25 pixels per second down and right

weapon_S
Member #7,859
October 2006
avatar

I can't really figure out what you are doing at the moment, but it seems you use a fixed set of bitmaps. And you seem to have player input combined with updating the ship.
What you'll want to do is something like:
detect input;
update world (use input; move according to time passed; handle collisions/other game events);
blit to screen;
repeat;

You might want to look into storing the player's (and bullet's) directions as an angle and using rotate_sprite. Then it will also get a speed. (And only in the 'update' part of the program, the speed will have effect.)

Some other remarks.
if(x==0){rest(50);}
Very bad way to handle boundaries, at a very bad place in your code (player input != collision detection). Also don't use rest...
[edit]Beaten by 20 minutes WTF I didn't use so much time writing this.
There are enough examples of timers. Search for 'timer' & 'speed', or 'game loop'.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I didn't see the attached file before, but after looking at it I have a few comments :

main.cpp said:

class Nave {
//...
        BITMAP *imagenes[9];
};
//...
Nave::~Nave(){
    delete [] imagenes;
};

Don't use delete on a pointer that you didn't use new to create. Use destroy_bitmap on imagenes[0] thru imagenes[8] instead.

Your MoverX and MoverY functions should not draw anything to the screen, they should only change the position of your objects.

Separate logic and drawing.

You don't clear_to_color on your buffer, so your program must look pretty messy right now.

You use draw_sprite to draw the buffer, but you should use blit instead.

Desmond Taylor
Member #11,943
May 2010
avatar

Thanks Edgar Reynaldo, That code that you posted helped me :) I was trying to do this too but as normal I use Search and Recent on these boards to see if it's been covered else I was just about to post :P

Thanks again

dengue
Member #12,790
April 2011

thanks Edgar Reynaldo and weapon_S, i am reading the articles of the angles and i am editing my code, thanks a lot ;D

Go to: