New Game

This is a discussion on New Game within the Game Programming forums, part of the General Programming Boards category; does anyone have a good example of colision etection? i ned to figure this stuff out ya know...

  1. #31
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    does anyone have a good example of colision etection?

    i ned to figure this stuff out ya know
    What is C++?

  2. #32
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    how bout this


    Code:
    #include <graphics.h>
    #include <conio.h>
    #include <dos.h>
    
    int main(void)
    {
    	int gdriver = DETECT, gmode, errorcode;
    	int r1x=0, r1y=0, r1height=10, r1width=20, r2x=300, r2y=300, r2height=20, r2width=10;
    
    	initgraph(&gdriver, &gmode, "C:\\program\\bc5\\bgi");
    
    	while(1)
    	{
    		setfillstyle(SOLID_FILL,BLACK);
    		bar(r1x,r1y,r1x+r1width,r1y+r1height);
    		setfillstyle(SOLID_FILL,BLACK);
    		bar(r2x,r2y,r2x+r2width,r2y+r2height);
    		r1x++;
    		r1y++;
    		r2x--;
    		r2y--;
    		setfillstyle(SOLID_FILL,RED);
    		bar(r1x,r1y,r1x+r1width,r1y+r1height);
    		setfillstyle(SOLID_FILL,WHITE);
    		bar(r2x,r2y,r2x+r2width,r2y+r2height);
    		delay(50);
    
    		if(r1x >= r2x-r1width && r1x <= r2x+r2width && r1y >= r2y-r1height && r1y < r2y+r2height)
    		{
    			setfillstyle(SOLID_FILL,RED);
    			bar(0,0,getmaxx(),getmaxy());
    			break;
    		}
    	}
    
    	getch();
    	closegraph();
    }

  3. #33
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    On the making bullets, I did not really do it right, I just drew a filled circle in the middle of the crosshair to represent a bullet. I did not do any collison detection on them or anything. I am coding an object oriented version of the sniper program now, maybe I will be done by tonight , Did you try to compile the code or did you just look at it?

  4. #34
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    i have a function:
    Code:
    void bullet()
    {
       buly = y;
       delay(200);
       circle ( x, y, 1 );
       
        line ( x, buly - 5, x, buly );
        
       delay(200);
            if ( buly != 20 ){
                 buly--;
            }
    }
    the line stays in one place and will not move... why?
    PLEASE HELP!
    What is C++?

  5. #35
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    Hmm, I do not really understand your code, where are you getting the x and y from? Here is how I do it:

    Code:
    fire(int x, int y)
    {
      for(int i = 5; i > 1; i++)
        circle(x, y, i);
    }
    This draws 5 circles, inside eachother, or I guess it is like a filled circle, is there a function for a filled circle? The x and the y are the middle of the crosshair in my game. That way it puts the bullet in between the crosshair where it goes.

  6. #36
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    ... um.. im doing a 2d-top-down game...

    i need the bullets to go on a y-axis. I cant figure out how to do this.
    What is C++?

  7. #37
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    Are you trying to make a bullet in the shape of a line? I think I can code you a function to do that:

    Code:
    void fire()
    {
      for(int i = 0; i < 100; i++)
      {
        bullet(300, i, RED);
        delay(200);
        bullet(300, i, BLACK);
      }
    }
    
    void bullet(int x, int y, int color)
    {
      line(x, y, color);
    }
    Fire calls bullet, bullet draws the line at the specified location, you can pass it a color, therefore you can delete the bullet.

    I am not sure, but that should work. Try it out

  8. #38
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    but I need it to move by itself...




    | << bullet that keeps going up
    ^ << ship


    [edit]

    OMg... im so stupid... ill try that
    [/edit]
    What is C++?

  9. #39
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    That does move by itself, did you try compiling it? And what part of Alabama are you from?

  10. #40
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    ill try it...

    and im from Montgomery
    What is C++?

  11. #41
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    If you want I can code a function that fires a bullet up from a specified x and y location, and write a driver program that shows how to use it, and you can plug it into your code. Do you have AIM or an IRC client? This is pretty much realtime anyway.

  12. #42
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    [edit]This code works, but the bullet wont stop for some reason, I will figure it out in a second[/edit]

    Code:
    #include <conio.h>
    #include <dos.h>
    #include <graphics.h>
    
      int fire(int x, int y);
    
    int main()
    {
      int graphdriver = 9;
      int graphmode   = 2;
      initgraph(&graphdriver, &graphmode, "c:\\tc\\bgi");
    
      // Okay, we have are graphics initialized
      // graphdriver = 9 means use vga, I hope you have
      // at least a vga card :)
      // int graphmode = 2 set the display to
      // 640 x 480 with 16 colors
    
      // Okay lets shoot a bullet from location 100(x), 400(y)
      fire(100, 400);
      // You would just plug in where the gun is on your ship for the x and y
      // when the fire button is pushed
    
      getch();
    
      closegraph();
      return 0;
    }
    
    int fire(int x, int y)
    {
      int tempy = y; // Temporary y
    
      for(int i = 480-y; i > 0; i--)
      {
        setcolor(RED);
        line(x, tempy, x, tempy-20);
        delay(200); // If the bullet moves to slow, change this
        setcolor(BLACK);
        line(x, tempy, x, tempy-20); // Erase bullet (so it doesnt stay on the screen and you get one long line going up the screen)
        tempy--;  
      }
    }
    That code is slow, change delay() to something lower so it will move further, and when it calls line(), change the tempy-20 to tempy-40 or something to make the bullets bigger, be sure to do it on both calls to that function!
    Last edited by JoshG; 05-30-2002 at 07:01 PM.

  13. #43
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    sigh...
    I think it works but th line goes to fast..
    i tried delay(200) but it froze up
    Code:
    #include <iostream.h>
    #include <graphics.h>
    #include <conio.h>
    #include <dos.h>
    
    int GraphicsDriver;
    int GraphicsMode;
    
    int x = 300;
    int y = 430;
    int i;
    int buly;
    int quit = 0;
    int mve;
    int count = 0;
    
    void game();
    void playermve();
    void shoot();
    
    
    int main()
    {
        for ( count = 0; !quit; count++ ){
        	game();
          }
    
    closegraph();
    return 0;
    }
    
    void game()
    {
    	initgraph ( &GraphicsDriver, &GraphicsMode, "c:\\bc5\\bgi" );
    
      while ( !quit )
      {
          gotoxy(1,1);
      		cout << "X: " << x << "  Y: " << y;
    
          settextstyle ( 1, HORIZ_DIR, 2 );
          setcolor(RED);
          outtextxy ( 230, -5, "Sonic Space" );
    
          setcolor (BLUE);
          rectangle ( 140, 20, 440, 470 );
    
          playermve();
       }
    }
    
    void playermve()
    {
    	 mve = getch();
    
          switch ( mve )
          {
             case 77: if( x != 430 ) x += 5; break;
             case 75: if( x != 150 ) x -= 5; break;
             case 's': shoot(); break;
             case 'q': quit=1; break;
          }
          	cleardevice();
             setfillstyle ( SOLID_FILL, BLUE );
             setcolor (RED);
             pieslice ( x, y, 240, 300, 15 );
    }
    
    void shoot()
    {
       buly = y;
       setcolor (WHITE);
       circle ( x, y, 1 );
       delay(200);
    	for ( buly = 0; buly < 100; buly -= 20 ){
           line ( x, buly + 5, x, buly );
          }
    }


    here is the exe...
    Attached Files Attached Files
    What is C++?

  14. #44
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Someone out there HAS to know how to make a bullet fly across the screen


    Lamb?
    Prelude?

    .....

    Invincible?

    What is C++?

  15. #45
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    This function will work, trust me this time . The code you wrote looks weird, I don't understand it. You initialize the graphics in game(), but you call game multiple times within a for loop?

    Code:
    #include <conio.h>
    #include <dos.h>
    #include <graphics.h>
    
      int fire(int x, int y);
    
    int main()
    {
      int graphdriver = 9;
      int graphmode   = 2;
      initgraph(&graphdriver, &graphmode, "c:\\tc\\bgi");
    
      // Okay, we have are graphics initialized
      // graphdriver = 9 means use vga, I hope you have
      // at least a vga card :)
      // int graphmode = 2 set the display to
      // 640 x 480 with 16 colors
    
      // Okay lets shoot a bullet from location 100(x), 400(y)
      fire(100, 400);
      // You would just plug in where the gun is on your ship for the x and y
      // when the fire button is pushed
    
      closegraph();
      return 0;
    }
    
    int fire(int x, int y)
    {
      int tempy = y; // Temporary y
    
      for(int i = y; i > 0; i--)
      {
        setcolor(RED);
        line(x, tempy, x, tempy-50);
        delay(6); // If the bullet moves to slow, change this
        setcolor(BLACK);
        line(x, tempy, x, tempy-50); // Erase bullet (so it doesnt stay on the screen and you get one long line going up the screen)
        tempy--;
      }
    }
    That code works, the only thing is, once you call the fire() function it will not let you move your ship or execute any of the other code you have untill it returns, meaning until the bullet is off the screen. I have been thinking and I can only see one way around this, code a process() function, and a new fire function that does part of the bullet, then returns. Have process get keyboard input, move the ship, detect the fire button has been hit, then have it call the fire() function, that will execute a little, have process check for more keyboard input, so you can still move the ship, then call fire() and have it execute some more. See what I am saying? I think it will work.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 11:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-25-2002, 12:00 AM

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