Thread: gamecode

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    gamecode

    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!

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    .................a week later...

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    More like 3 days but anyway

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

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    ok that one i cracked yesterday

    but my big problem is the spaceships and the laserbeams

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    to Barjor: are u really 7.5?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    What problem are you having with the beams?
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    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.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    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!

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    to Barjor:u mean like that 12 year old programmer??!

  10. #10
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128
    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
    Code:
    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 )
    Muhammad Haggag

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    sorry man,
    i didnt get that

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    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
    Last edited by Justin W; 03-14-2002 at 09:52 AM.
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    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

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    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.
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    do u have an example?

Popular pages Recent additions subscribe to a feed