does anyone have a good example of colision etection?
i ned to figure this stuff out ya know
does anyone have a good example of colision etection?
i ned to figure this stuff out ya know
What is C++?
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(); }
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?
gcc -lalleg
http://www.ciusa.net/~jrgrant/
i have a function:
the line stays in one place and will not move... why?Code:void bullet() { buly = y; delay(200); circle ( x, y, 1 ); line ( x, buly - 5, x, buly ); delay(200); if ( buly != 20 ){ buly--; } }
PLEASE HELP!
What is C++?
Hmm, I do not really understand your code, where are you getting the x and y from? Here is how I do it:
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.Code:fire(int x, int y) { for(int i = 5; i > 1; i++) circle(x, y, i); }
gcc -lalleg
http://www.ciusa.net/~jrgrant/
... 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++?
Are you trying to make a bullet in the shape of a line? I think I can code you a function to do that:
Fire calls bullet, bullet draws the line at the specified location, you can pass it a color, therefore you can delete the bullet.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); }
I am not sure, but that should work. Try it out
gcc -lalleg
http://www.ciusa.net/~jrgrant/
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++?
That does move by itself, did you try compiling it? And what part of Alabama are you from?
gcc -lalleg
http://www.ciusa.net/~jrgrant/
ill try it...
and im from Montgomery
What is C++?
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.
gcc -lalleg
http://www.ciusa.net/~jrgrant/
[edit]This code works, but the bullet wont stop for some reason, I will figure it out in a second[/edit]
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!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--; } }
Last edited by JoshG; 05-30-2002 at 06:01 PM.
gcc -lalleg
http://www.ciusa.net/~jrgrant/
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...
What is C++?
Someone out there HAS to know how to make a bullet fly across the screen
Lamb?
Prelude?
.....
Invincible?
What is C++?
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?
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.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--; } }
gcc -lalleg
http://www.ciusa.net/~jrgrant/