pic movement

This is a discussion on pic movement within the Game Programming forums, part of the General Programming Boards category; have variables hold the X and Y values of the bullet, and everytime you need to move the bullet, just ...

  1. #16
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    have variables hold the X and Y values of the bullet, and everytime you need to move the bullet, just change the X and Y variables and redraw the bullet.
    LOL I remember learning that in grade 7 or 8 in a Java course!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #17
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    yeah but its not that easy using allegro







    LOL I remember learning that in grade 7 or 8 in a Java course!
    and what grade are u in now?

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    10
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Pode, what you can do is take a look here for a basic tutorial on how to move a picture through user input.

  5. #20
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    Wow, I wish I could have taken a Java class in the 8th grade. If you search to board you should find this discussion between me and 'Vicous', I gave a couple examples and a complete program that moves bullets in Allegro. It is really simple. In your game loop you have something like this:

    getinput()
    procesinput()
    redraw()

    You need to add update() which will update the bullets coordinates and check to see if it hit something. Have redraw() draw the bullet at its x and y coordinates, if update() changes the y coordinate by 5 each time, then it will appear that the bullet is moving. It is hard to explain, search for the examples.

  6. #21
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    Here is a copy of a post in that thread where I explained bullets:

    I did my bullets like this:

    Code:
    #define MAX_BULLETS 10
    
    struct bullet {
      int x, y;
      int exists;
    } bullets[MAX_BULLETS];
    Then every time right before I draw to the screen (well, the backbuffer) I call the function updatebullets() which goes through each bullet and subtracts from y, to make it move up the screen. I also have that function change exists to 0 if the bullet goes off the screen, later I will have the code for when a bullet hits something. I loop through it like this

    Code:
    for(int i = 0; i < MAX_BULLETS; i++)
    {
      if(bullets[i].exists == 1)
      {
        bullets[i].y -= 5;
      }
    }
    That code loops through the whole bullets array and for everyone that exists it subtract five from y, therfore all the bullets move up the screen.

    When I create a bullet I have to loop through the bullets array and find the first empty space. If I had an integer for how many bullets are on the screen, and add to it everytime I add a bullet, and delete from it everytime a bullet disapears, I would overwrite them. Here is an example of how that would not work (using the bullet struct from above)

    Code:
      int NumberOfBullets = 0;
    
      // Okay lets say the user presses space
      // We create a bullet
    
      bullets[NumberOfBullets].exists = 1;
      bullets[NumberOfBullets].x = 300;
      bullets[NumberOfBullets].y = 400;
    
        int NumberOfBullets++;
    
      // Now lets create another bullet
      // Note the first bullets location would be bullets[0] 
    
      bullets[NumberOfBullets].exists = 1;
      bullets[NumberOfBullets].x = 200;
      bullets[NumberOfBullets].y = 450;
    
      NumberOfBullets++;
    
      // Now lets say some time has passed in our game, the first
      // bullet is off screen now, but the second is still on screen.
      // Now our code would break
    
      // First since our first bullet went off screen we do this
      NumberOfBullets--;
    
      // Now what happends if we create a new bullet?
    
      bullets[NumberOfBullets].exists = 1;
      bullets[NumberOfBullets].x = 245;
      bullets[NumberOfBullets].y = 420;
    
      // Uh oh, we just over wrote our second bullet, they are both
      // bullet[1].
    I tried that method first, it would not work, obviously. So now everytime I create a bullet, I have it loop through the bullets array and find the first place where exists = 0; The code for this is not hard, I will show you.

    Code:
    for(int i = 0; i < MAX_BULLETS; i++)
    {
      if(bullets[i].exists == 0)
      {
        // We found a bullet in the array that does not exist yet
        // meaning we can use that place to hold a bullet
        // We want to give it coordinates, then set exists to 1 so
        // it gets drawn, and nothing overwrites it
        bullets[i].x = 200;
        bullets[i].y = 200;
        bullets[i].exists = 1;
    
        // A problem I had earlier was that I did not put a break
        // statement here, that made it create a bullet with the
        // same coordinates every place there was a empty bullet
        // which meant that I could only have one bullet at a time
        break;
      }
    }
    Well, I hope I explained everything, here is the code to my game so far, there are no enemies yet, but you can have up to 30 bullets on screen at once, there is no flicker in bullet movement or plane movement. I am still looking for a new plane graphic. I think I am gonna quit with this game, because it is starting to get lame. I need to design what I want the game to be more, before I code. I will work on another game shortly, probably either pong or a space attack game. Here is the full code to the airplane game:

    Code:
    #include <allegro.h>
    
    #define MAX_BULLETS 10
    
                        // Draws ship and bullets to buffer, then
      void redraw();    // blits buffer to screen
    
                        // Initializes Allegro, sets screen mode
      void setup();     // and loads bitmaps
    
      void shutdown();  // Destroys bitmaps and calls allegro_exit()
    
      void updatebullets(); // Updates bullet coordinates
    
      BITMAP *background = NULL;    // Clouds background
      BITMAP *bullet     = NULL;    // A bullet...
      BITMAP *plane      = NULL;    // Da plane! Da plan!
      BITMAP *buffer     = NULL;    // The buffer, reduces flicker
    
      // I found the easiest way to implement bullets is to create
      // an array of structures, so it is easy to keep track of
      // each bullets coordinates and state (exists or not)
      struct bullet
      {
        int x, y;
        int exists;
      } bullets[MAX_BULLETS];
    
      // The ships coordinates, maybe I should make a ship structure...
      int x = 300, y = 375;
    
    int main()
    {
      setup();
    
      while(!key[KEY_ESC])
      {
        if(key[KEY_UP] && y > 5)        // Move up
          y = y - 5;
    
        else if(key[KEY_DOWN] && y < 400)    // Move down
          y = y + 5;
    
        else if(key[KEY_LEFT] && x > 5)      // Move left
          x = x - 5;
    
        else if(key[KEY_RIGHT] && x < 525)   // Move right
          x = x + 5;
    
        else if(key[KEY_SPACE]) // Shoot, if there are already 10 bullets, do nothing
        {
          // Find an empty place in the bullets struct
          for(int i = 0; i < MAX_BULLETS; i++)
          {
            if(bullets[i].exists == 0) // We found an empty slot
            {
              bullets[i].exists = 1; // Make it exist and set its coords
              bullets[i].x = x;
              bullets[i].y = y;
              break;
            }
          }
        }
        updatebullets();
        redraw();
      }
    
      shutdown();
      return 0;
    }
    END_OF_MAIN();
    
    void redraw()
    {
      blit(background, buffer, 0, 0, 0, 0, 640, 480);
      draw_sprite(buffer, plane, x, y);
    
      for(int i = 0; i < MAX_BULLETS; i++)
      {
        if(bullets[i].exists == 1)
        {
          draw_sprite(buffer, bullet, bullets[i].x, bullets[i].y);
        }
      }
    
      blit(buffer, screen, 0, 0, 0, 0, 640, 480);
    }
    
    void setup()
    {
      allegro_init();
      install_keyboard();
    
      // Set colors depth and set graphics mode
      set_color_depth(32);
      set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
    
      // Load bitmaps
      background = load_bitmap("background.bmp", NULL);
      bullet     = load_bitmap("bullet.bmp", NULL);
      plane      = load_bitmap("plane.bmp", NULL);
      buffer     = create_bitmap(640, 480);
    
      // Make sure all bullets do not exist
      for(int i = 0; i < MAX_BULLETS; i++)
      {
        bullets[i].exists = 0;
      }
    }
    
    void shutdown()
    {
      destroy_bitmap(background);
      destroy_bitmap(plane);
    
      allegro_exit();
    }
    
    void updatebullets()
    {
       // Delete bullets that go past screen
      for(int j = 0; j < MAX_BULLETS; j++)
      {
        if(bullets[j].exists == 1)
        {
          if(bullets[j].y <= 0)
            bullets[j].exists = 0;
        }
      }
    
      // Move bullets up the screen
      for(int i = 0; i < MAX_BULLETS; i++)
      {
        if(bullets[i].exists == 1)
        {
          bullets[i].y -= 5;
        }
      }
    }
    Here is a link to the whole thread: http://www.cprogramming.com/cboard/s...threadid=19231

  7. #22
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    thanks
    i think this code is gonna help me alot thanks again

  8. #23
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    lol

    if you want, you can take a look at the link in my signature... it's not well coded, but it'll do. It's a "space attack game", just like JoshG's future bestseller
    Last edited by Hunter2; 08-20-2002 at 02:04 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #24
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    it's not that easy in Allegro
    look at my picture on the left, and just take a wild guess what API I use.

  10. #25
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    is that quote my?

    that's not what i wrote!



    look at my picture on the left, and just take a wild guess what API I use.
    you mean what library you use!?

    what tell me

  11. #26
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    it wasn't a direct quote

  12. #27
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    'future best seller' I quit coding that like a month ago, I could not do good enough graphics.

  13. #28
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I could not do good enough graphics.
    You mean you couldn't draw good graphics, or you couldn't find good graphics, or you couldn't figure out how to display them, or they were too slow, or they were ugly, or what?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #29
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    I know how to display and animate them, I just can not draw good graphics or find any good graphics.

  15. #30
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oh. Well then, try:

    1. Get a drawing book
    2. Ask a friend to draw
    3. Ask a sister to draw
    4. Get a gf and ask her to draw
    5. Get a bf and ask him to draw
    4. Hire someone to look for graphics
    5. Hire someone to draw graphics
    6. Hire someone to write your program

    If all of the above fails, try going in Paint, zoom in to 6X, and start randomly drawing pixels in different colours. It works sometimes!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get pixel values from pic
    By Leite33 in forum Windows Programming
    Replies: 4
    Last Post: 09-12-2006, 01:13 PM
  2. I need movement...
    By Finchie_88 in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2004, 03:10 PM
  3. natural leg movement
    By DavidP in forum Game Programming
    Replies: 32
    Last Post: 01-11-2004, 08:01 AM
  4. New game-Time-based movement?
    By napkin111 in forum Game Programming
    Replies: 6
    Last Post: 01-18-2003, 12:50 AM
  5. PIC Microcontrolers
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-28-2002, 11:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21