msdos asteroids

This is a discussion on msdos asteroids within the Game Programming forums, part of the General Programming Boards category; how do you make bullets?...

  1. #1
    Registered User seditee's Avatar
    Join Date
    Apr 2002
    Posts
    82

    Question msdos asteroids

    how do you make bullets?

    lebios

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    metal casing filled with gunpowder


  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    #define PI 3.14159
    #define DEGTORAD(angle) (double)((angle)*(PI)/(180.0))
    
    struct Bullet
    {
      double x;
      double y;
      double speed;
      int angle;
    };
    
    void UpdateBullet(Bullet bt)
    {
      bt.x+=(cos(DEGTORAD(bt.angle))*bt.speed);
      bt.y+=(sin(DEGTORAD(bt.angle))*bt.speed);
    }

    This is more suited to use in C++ through the use of classes. But this will work for plain C programming. It is not pretty since UpdateBullet and Bullet struct are really not logically grouped together.

    class Bullet
    {
    double x;
    double y;
    int angle;
    double speed;
    public:
    Bullet(void);
    Update(void);
    };

    This class provides a much nicer way to deal with a bullet object and is more extensible and flexible (you can add more functions, data members, even derive off of this)

  4. #4
    Registered User seditee's Avatar
    Join Date
    Apr 2002
    Posts
    82
    that looks right; thanks. now if i can just figure out how to apply it...to this...







    Code:
    #include <stdio.h>
    #include <math.h>
    #include <graphics.h>
    #include <conio.h>
    #include <dos.h>
    #define rotate 3
    #define speed 0.12
    
    typedef struct
      {
       float x, y;
      } vec;
    
    typedef struct
      {
       int num;
       vec array[3];
      } form;
    
    typedef struct
      {
       vec   pixel;
       int   view;                               // ship direction.
       vec   move;                               // move direction.
       form  make;                               // make form.
       form  dos;                                // screen points.
      } image;
    
        image viper;
    
    void array(vec*dest,vec*scr,int degree)      // return dest - points to x, y floats.
      {
       double rad;                               // radius.
       rad=degree*0.01745;
       dest->x=scr->x*cos(rad)-scr->y*sin(rad);  // standard library functions.
       dest->y=scr->x*sin(rad)+scr->y*cos(rad);
      }
    
    void mode(image*var)                         // return value of type image.
      {
       int i;
       for(i=0;i<var->make.num;i++)              // rotate.
        {
         array(&var->dos.array[i],&var->make.array[i],var->view);
         var->dos.array[i].x+=var->pixel.x;
         var->dos.array[i].y+=var->pixel.y;
        }
      }
    
    void draw(image*var)
      {
       int i;
       for(i=1;i<var->make.num;i++)
         {
          line(var->dos.array[i-1].x,var->dos.array[i-1].y,
    	   var->dos.array[i].x,var->dos.array[i].y);
         }
       line(var->dos.array[i-1].x,var->dos.array[i-1].y,
    	var->dos.array[0].x,var->dos.array[0].y);
      }
    
    void power(image*var)
     {
      var->pixel.x+=var->move.x;var->pixel.y+=var->move.y;    // move.
      if(var->pixel.x<0)var->pixel.x=var->pixel.x+640;        // border.
      if(var->pixel.x>639)var->pixel.x=var->pixel.x-640;
      if(var->pixel.y<0)var->pixel.y=var->pixel.y+480;
      if(var->pixel.y>479)var->pixel.y=var->pixel.y-480;
     }
    
    void hold(){while(inp(0x3da)&8);while(!(inp(0x3da)&8));}  // prevent flash.
    
    void init(void)
      {
      int GraphicsDriver=VGA;
      int GraphicsMode=VGAHI;
      initgraph(&GraphicsDriver,&GraphicsMode,NULL);
      setwritemode(XOR_PUT);
      viper.make.num = 3;
      viper.make.array[ 0 ].x =  0; viper.make.array[ 0 ].y = -16;   // coords.
      viper.make.array[ 1 ].x = -8; viper.make.array[ 1 ].y =  +8;
      viper.make.array[ 2 ].x = +8; viper.make.array[ 2 ].y =  +8;
      viper.view = 0; viper.pixel.x = 320; viper.pixel.y = 240;      // center.
      viper.move.x = 0; viper.move.y = 0;
      }
    
    void main(void)
      {
        int c=0; unsigned short keyboard;
        init();
        while(c!='q'&&c!='Q')
           {
    	mode(&viper);draw(&viper);
    	keyboard = *(unsigned short far *)MK_FP( 0x40, 0x17 );
    	power(&viper);
    	if(kbhit()){c=getch();}
    	if(keyboard&0x0002)
    	  {viper.view-=rotate;if(viper.view<0)viper.view=360-rotate;}
    	if(keyboard&0x0001)
    	  {viper.view+=rotate;if(viper.view>359)viper.view=0+rotate;}
    	if(keyboard&0x0004)
    	  {
    	    // fire bullet.
    	  }
    	if(keyboard&0x0008)
    	  {
    	   double rad; rad = viper.view*0.01745;
    	   viper.move.x+=sin(rad)*speed;viper.move.y-=cos(rad)*speed;
    	  }
    	hold();draw(&viper);
    
           }
       closegraph();
      }
    Last edited by seditee; 04-30-2002 at 01:19 AM.
    lebios

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C system call and msdos batch files.
    By esbo in forum C Programming
    Replies: 32
    Last Post: 01-20-2009, 01:30 PM
  2. Asteroids
    By IdioticCreation in forum Game Programming
    Replies: 16
    Last Post: 05-15-2007, 08:04 PM
  3. How to output an JPEG/GIF image in MSDOS
    By Suvro in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-11-2004, 01:46 AM
  4. ai for enemy ships in asteroids
    By ichijoji in forum Game Programming
    Replies: 7
    Last Post: 04-09-2003, 05:09 PM
  5. MSDOS ncurses? What is equivalent?
    By Kev2 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-15-2002, 02:02 AM

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