gamecode [Archive] - C Board

PDA

View Full Version : gamecode


pode
03-10-2002, 03:14 PM
note: allegro

how do you write the code for something like
if user hits key lets say 's'
then the "spaceship" shoots a laserbeam
do u know what keyboardfunctions to use
also i have problems makings short beams

please help!

pode
03-13-2002, 10:29 AM
.................a week later...

Barjor
03-13-2002, 10:33 AM
More like 3 days but anyway

use something like kbhit() and then check what key got pressed with a getchar.

pode
03-13-2002, 10:41 AM
ok that one i cracked yesterday

but my big problem is the spaceships and the laserbeams

pode
03-13-2002, 10:42 AM
to Barjor: are u really 7.5?

Justin W
03-13-2002, 10:48 AM
What problem are you having with the beams?

Barjor
03-13-2002, 10:57 AM
No I am way older then that, it is a sarcastic comment to people that use there young age as an excuse to bragg and behave poorly.

pode
03-13-2002, 11:04 AM
for now the only way i can make a "beam"
is like this example:

for(int beam=0;beam<140;beam++)
{
putpixel(buffer,beam,beam,1);
showBuffer();
}
this will draw i line now i want it to get no longer than half a cm.
and i need it to stay a half cm long then move at the direction
it points to untill its out of the screen or a enemy but i'm saving that part to later.

B.T.W

if u know a better way to make make i beam plese post!
thanx!

pode
03-13-2002, 11:06 AM
to Barjor:u mean like that 12 year old programmer??!

Coder
03-13-2002, 12:55 PM
To draw a 2D beam you need to use a general-case line-drawing algorithm ( assuming your beam can go in any direction ).
Bresenham's line drawing algorithm is the fastest ( you can get it by a simple search on google), although I think allegro must have some line drawing algorithm somewhere ( I've never used it, so I can't tell you. Search its documentation or look through the code )

You can use a simple struct ( or class ) for your beam that looks like the following

struct Vec2_t
{
int x,y;
};

struct Beam_t
{
Vec2_t vStart;
Vec2_t vDirection;
Vec2_t vVelocity;
float fLength;
};

Whenever something fires the beam from somewhere on the screen, that's the beam's start position. The direction is defined by the shooter's orientation (and it's a 2D unit vector), the lenght is how long you want your beam to be (in pixels).

When you draw your beam, you simply Multiply fLength by vDirection (scaling a 2D vector, in case you don't know this. Search some math encyclopaedia or notify me and I'll explain it) to get the beam end point.
Pass the start & end points to allegro's line-drawing routine, and you've got your beam.
Each frame update, increase the beam's vStart by vVelocity to update its position.

More about checking whether it's inside the screen or not later ( No time now )

pode
03-13-2002, 04:22 PM
sorry man,
i didnt get that

Justin W
03-14-2002, 09:48 AM
In the Allegro documentation under "Drawing Primitives" see:
line(BITMAP *bmp, int x1, int y1, int x2, int y2, int color);
and
do_line(BITMAP *bmp, int x1, y1, x2, y2, int d, void (*proc)());

Line is the basic thing, do_line is if you want to get all fancy with your beam.

If you want the beam to be of a certain length, then you will need to keep track of its head and tail with variables.
int hx, hy;
int tx, ty;

Give the head a head's start (no pun intended) from the origin then send the tail drawing after it, erasing what the head coordinates drew. Once the tail coordinates are less than or greater than the screen coordinates, the beam has left the screen and may be safely dismissed. Once the head coordinates equal that of an enemy ship's perimeter, then the beam has struck and big explosions should ensue. ;) Hope that makes sense.

-Justin

pode
03-14-2002, 12:42 PM
why cant u just do something like this

if(key[KEY_UP])
{


{//from x1,y1 to x2,y2
line(buffer,my_pic_x, my_pic_y,my_pic_x ,shot_y , 800);
_sleep(300);
line(buffer,my_pic_x, my_pic_y,my_pic_x ,shot_y , 0);
}
}
this doesnt work but why cant u just use a delay function
like sleep(); or a loop that counts to 10 or something

Justin W
03-14-2002, 01:02 PM
Because you are erasing the whole line, not just the part of it that is leftover trash from the previous draw operation.

If I start at (0,0) and paint one pixel, wait for the screen to update (consider this a "frame"), then paint another pixel next to it until I reach the end of the screen, I will end up with a line that extends from one side to the next.

If, instead, I wanted it to appear that the pixel is moving accross the screen, then I have to erase where I drew it in the previous frame so that it only appears in the new place I've placed it. Using a delay will only make either of these processes happen slower. Think flip book animation. It isn't really moving, it is tricking the eye.

pode
03-14-2002, 02:16 PM
do u have an example?

Justin W
03-14-2002, 09:22 PM
Sure. This example demonstrates cleaning up behind the pixel and not cleaning up behind the pixel. The principle is the same for a line. It also has a delay in the loop so that the animation slows down. Try playing around with it to see the differences.

Red pixel moves, blue pixel moves but leaves a tail. Press ESCape to quit.


#include <allegro.h>

int x; // X coordinate of our pixels.

int main()
{
// Initialize Allegro.
allegro_init();

// Set the color depth to 32 bit.
set_color_depth(32);

// Set the resolution to 640 by 480 with SAFE autodetection.
set_gfx_mode(GFX_SAFE, 640, 480, 0, 0);

// Installing the keyboard handler.
install_keyboard();

// Printing text to the screen.
textout(screen, font, "Moving Pixel:", 1, 1, makecol(255,0,0));
textout(screen, font, "Growing line:", 1, 22, makecol(0,0,255));

// Looping until the ESCape key is pressed.
while(! key[KEY_ESC])
{
// Erase old moving pixel.
putpixel(screen, x-1, 12, 0);
// Draw new moving pixel.
putpixel(screen, x, 12, makecol(255,50,50));
// Draw growing line from pixels.
putpixel(screen, x, 32, makecol(50,50,255));
x++;
if(x>640)
x=0;
rest(2); // Slow down animation.
}
// Exit program.
allegro_exit();
return 0;
}

// Some Allegro magic to deal with WinMain().
END_OF_MAIN();

pode
03-15-2002, 12:12 PM
i tried something like that with if(key[KEY_UP])

putpixel(....);
but the pixel wont move by itself
if i hold down the key then it will start moving
u know how to make it move with just one hit on the key

Justin W
03-15-2002, 12:15 PM
Here's some rough code to do the trick. Mind you this has the opposite effect that once it starts moving, it doesn't stop. Not difficult to see how to modify it to stop after a while though, or once they press another key, but I'll leave that to your imagination.


bool ShouldPixelMove=0; // Pixel should not move at first.

if(key[KEY_UP])
ShouldPixelMove=1; // Pixel should move now.

if(ShouldPixelMove)
pixelY++;