New Game [Archive] - C Board

PDA

View Full Version : New Game


Vicious
05-29-2002, 06:54 PM
I need some suggestions from some of you people.

What would be a good first game using graphics?
I just figured out how to use graphics.h and I need some info on a project that would be simple for a first!

JoshG
05-29-2002, 07:48 PM
I just thought of a new game, it could be two player, or one against the AI. There is a runner and a catcher. Both players control circle, the runner circle is smaller that the catcher. If you are the runner, you are suppose to avoid the catcher for a certain time. If you are the catcher you are suppose to catch the runner. By catch, it means touch them. It looks better in my head than it sounds :).

jdinger
05-29-2002, 08:13 PM
If you want to get some graphics experience without worrying about having to conquer anything too new, I'd say take a game you've already written in console and make a graphical version of it. Like say a graphical tic-tac-toe or hangman.

If you want something totally fresh, then maybe make a side-scrolling shooter or top-down shooter. And of course there's always everyone's (? maybe) first graphical game, Pong.

Vicious
05-29-2002, 08:30 PM
Chokes and spits out drink when sees pong...

PONG!?!?!

No thank you.. no bounce and angles and crap :D

I have a problem though... how do I use graphics with arrays?

Is there any good examples of programs

jdinger
05-29-2002, 09:22 PM
I'm not familiar with the graphics.h file you mentioned but here's a generic example of using arrays with graphics:


CEnemy cdBadGuys[3];
CHero chYou;
int i;

MainDrawingLoop()
{
for(i=0;i<3;i++)
{
if(cdBadGuys[i].IsAlive())
{
cdBadGuys[i].DrawBaddy(cdBadGuys[i].MapX(),cdBadGuys[i].MapY);
}
}
chYou.DrawHero(chYou.MapX,chYou.MapY);
}


Not sure if that's what you're looking for or not. If you could post a generic blit routine that you might you use, I can offer more specific help.

Jason

Vicious
05-29-2002, 09:29 PM
I really just want to know how to draw.. say a circle and move the circle across the screen.

And is it dificult to use images?

Im not sure how I would make a ship in a shooter?

jdinger
05-29-2002, 10:20 PM
What compiler are you using? Did graphics.h come with it or is it something you downloaded from somewhere else? This is the first I've heard of it so I'm not sure if it's a stand-alone library or a wrapper for another. If I can find out what API/library it's using then I can look into it and give you a better answer.

Vicious
05-29-2002, 10:58 PM
Borland C++ 5.02

Its BGI graphics.


#include <iostream.h>
#include <graphics.h>

int GraphicsDriver;
int GraphicsMode;

int gridx [50];
int gridy [50];

int main()
{
initgraph ( &GraphicsDriver, &GraphicsMode, "c:\\bc5\\bgi" );

setcolor (RED);
outtext ("So sue me.. ");

setcolor (YELLOW);

setfillstyle ( SOLID_FILL, YELLOW );

pieslice ( 100, 100, 30, 330, 15 );

setcolor (WHITE);
setfillstyle ( SOLID_FILL, WHITE );
pieslice ( 140, 100, 1, 360, 2 );
pieslice ( 160, 100, 1, 360, 2 );
pieslice ( 180, 100, 1, 360, 5 );


cin.get();
closegraph();
return 0;
}

cant get any farther... :(

Invincible
05-29-2002, 11:22 PM
You finally figured that thing out huh? That's some funky old DOS library J :) Why don't you give tetris a try Vicious? You'd get to learn basic collision among cool stuff.

Vicious
05-29-2002, 11:24 PM
I would but all I know how to do is put out the graphics...

I cant make it move or get key input and move the pieces or whatever.

:rolleyes:

Vicious
05-29-2002, 11:27 PM
oh and if anybody knows this...

How do you make a tetris block?

:confused:

Invincible
05-29-2002, 11:40 PM
Whoa... I think you're going to need to learn how to get keyboard input if you're going to make a game there buddy :)

Tetris blocks? Think about it... every block is made up of four smaller blocks.

You could either code some rectangles, or use bitmaps.

I'm about to start working on a tetris game myself. I've put it off way too long. I just can't decide which library to use. No, I don't think I'll use BGI :rolleyes:

Vicious
05-29-2002, 11:41 PM
#include <iostream.h>
#include <graphics.h>
#include <conio.h>

int GraphicsDriver;
int GraphicsMode;

int x = 25;
int y = 25;
int quit = 0;
int mve;


int main()
{
initgraph ( &GraphicsDriver, &GraphicsMode, "c:\\bc5\\bgi" );

while ( !quit )
{
gotoxy(1,1);
cout << "X: " << x << " Y: " << y;

mve = getch();

switch ( mve )
{
case 'w': y--; break;
case 's': y++; break;
case 'd': x++; break;
case 'a': x--; break;
case 'q': quit=1; break;
}
cleardevice();
circle ( x, y, 10 );
}
closegraph();
return 0;
}


Thanx again Lamb. :D

Vicious
05-29-2002, 11:43 PM
OOOPS!

Forgot to post my question.

How can I make it move faster??

Invincible
05-29-2002, 11:56 PM
Here's a tetris tutorial that looks promising... (http://www.suomipelit.com/muut/line_a/cw35.html)

Vicious
05-30-2002, 12:05 AM
:eek:

It would have been if I could have seen the source... and it was a dos app using graphics.h

:p

Okiesmokie
05-30-2002, 12:36 AM
>> How can I make it move faster??

instead of:
y--,
y++,
x-- AND
x++

try:
y-=5,
y+=5,
x-=5 AND
x+=5

Unregistered
05-30-2002, 06:18 AM
here's a link to a pacman game that uses bgi:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3031&lngWId=3

It's from planet-source-code.com

agerealm
05-30-2002, 08:44 AM
i thought you couldnt use BGI in windows...am i wrong?

pode
05-30-2002, 09:43 AM
how can u make it move by itself
you know like if you hit down key move down untill other direction pressed?!

Vicious
05-30-2002, 02:02 PM
Nope BGI is not in windows...

You have to make your target project a DOS app.

To make it move by itself just do like this:



x=250;
y=400;

for (/*your loop stuff here*/){
circle ( x, y, 10 );
y++
}



its something like that... I cant get to my source right noe though.

Vicious
05-30-2002, 02:04 PM
I have started a space shooter game (like galaga).
Its called Sonic Space. I have th "map" and the "players ship" set up... and it moves side to side. (I finally figured out that y += 5 instead of y++ on my own) now I need to make bullets and bad guys... and make the badguys shoot and make stuff blow up when its hit...

JoshG
05-30-2002, 02:26 PM
I am coding a sniper game using Turbo C++ 3 and BGI. I have alot of it done already, I just need to make it so the crosshair doesn't go off the screen and I need to make targets for you to shoot at. I will gladly share the source when it is more complete.

JoshG
05-30-2002, 02:29 PM
I remember you were having problems with keyboard input earlier, so I will post what I have done:

You may need to change the initgraph() function so that the BGI part points to the right location, then it should compile. Again I used Turbo C++ 3.0

By the way, I know the up and down keys are flipped, that is easily fixed.

Vicious
05-30-2002, 02:34 PM
Bullets.. this is just a guess.


.
.
.
case 's': bullet(); break;
.
.
.
void bullet()
{
circle ( x, y, 1 );

for() {
delay(30);
y++;
}
}


:confused:

Vicious
05-30-2002, 02:43 PM
#include <iostream.h>
#include <graphics.h>
#include <conio.h>

int GraphicsDriver;
int GraphicsMode;

int x = 300;
int y = 430;
int quit = 0;
int mve;


int main()
{
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 );

mve = getch();

switch ( mve )
{
case 'd': if( x != 430 ) x += 5; break;
case 'a': if( x != 150 ) x -= 5; break;
case 'q': quit=1; break;
}
cleardevice();
setfillstyle ( SOLID_FILL, BLUE );
setcolor (RED);
pieslice ( x, y, 240, 300, 15 );
}
closegraph();
return 0;
}

there is what I have so far

pode
05-30-2002, 02:43 PM
none of the codes move by itself!?

Vicious
05-30-2002, 02:55 PM
what you mean?

any way after someone tells me bout the bullets.. how about being able to move and shot at the same time?

JoshG
05-30-2002, 03:06 PM
I added some more to my code, alot of stuff is wrong right now, if you try you can move the cursor off the screen to the right and bottom. The bullets stay of the screen. I think I need to redesign it. I need a linked list of bullets I guess. Then I need a different way to control it. Right now I have processinput() that moves the crosshair and all, but it stays in that function untill you hit escape, I need to code a processinput that gets passed the input and does stuff based on it, so I can use it for more than just moving the crosshair and drawing bullets. I also need to make moving the crosshair less jumpy. I think I should change all the if statements in processinput() to a switch statement.


// sniper.cpp
// A weak ass sniper game by Josh Grant
// jrgrant@ciusa.net
//
// Compiled using Turbo C++ 3.0

#include <graphics.h>
#include <conio.h>

#define ESC_KEY 27
#define SPACE_KEY 32
#define UP_KEY 72
#define RIGHT_KEY 77
#define DOWN_KEY 80
#define LEFT_KEY 75

void intro(); // Shows the intro to the game
void setup(); // Sets graphics mode
void desetup(); // Resets graphics mode and shows game credits

void drawcrosshair(int x, int y); // WTF do you think? :)
void erasecrosshair(int x, int y); // This draws the crosshair, but in black, so it erases it

void fire(int x, int y); // Draws a bullet, or a small explosion, well, just some dots

void processinput(); // When you hit a key, the crosshair moves, :)

int main()
{
setup();
intro();
processinput();
desetup();

return 0;
}

void intro()
{
setcolor(RED);
settextstyle(4, 0, 8);
outtextxy(200, 100, "Sniper");

settextstyle(0, 0, 1);
outtextxy(200, 250, "Welcome to sniper, ready to hunt?");
outtextxy(200, 300, "Hit any key to start...");

getch();
cleardevice();
}

void setup()
{
int graphdriver = 9;
int graphmode = 2;
initgraph(&graphdriver, &graphmode, "c:\\tc\\bgi");
}

void desetup()
{
closegraph();
}

void drawcrosshair(int x, int y)
{
// First we draw two lines, to make the + part
setcolor(RED);
// vertical line
line(x, y+50, x, y-50);
// horizontal line
line(x-50, y, x+50, y);
// circle
circle(x, y, 35);
}

void erasecrosshair(int x, int y)
{
setcolor(BLACK);
// All we are doing is drawing a cross hair, but making it black so it dissapears
// First we draw two lines, to make the + part
// vertical line
line(x, y+50, x, y-50);
// horizontal line
line(x-50, y, x+50, y);
// circle
circle(x, y, 35);
}

void fire(int x, int y)
{
setcolor(CYAN);
circle(x, y, 1);
circle(x, y, 2);
circle(x, y, 3);
circle(x, y, 4);
}

void processinput()
{
int x = 60, y = 60;
drawcrosshair(x, y);
int theinkey = 0;

while(theinkey != ESC_KEY)
{
if(theinkey == SPACE_KEY)
fire(x, y);
if(theinkey == UP_KEY && y > 61)
{
erasecrosshair(x, y);
y -= 25;
drawcrosshair(x, y);
}
if(theinkey == RIGHT_KEY)
{
erasecrosshair(x, y);
x += 25;
drawcrosshair(x, y);
}
if(theinkey == DOWN_KEY)
{
erasecrosshair(x, y);
y += 25;
drawcrosshair(x, y);
}
if(theinkey == LEFT_KEY && x > 61)
{
erasecrosshair(x, y);
x -= 25;
drawcrosshair(x, y);
}
theinkey = getch();
}
}

Vicious
05-30-2002, 03:14 PM
wow!

very nice... i love your comments :P

can you explain on making bullets?

Vicious
05-30-2002, 03:19 PM
does anyone have a good example of colision etection?

i ned to figure this stuff out ya know :)

pode
05-30-2002, 03:25 PM
how bout this



#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();
}

JoshG
05-30-2002, 03:47 PM
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?

Vicious
05-30-2002, 05:13 PM
i have a function:

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!

JoshG
05-30-2002, 05:25 PM
Hmm, I do not really understand your code, where are you getting the x and y from? Here is how I do it:


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.

Vicious
05-30-2002, 05:28 PM
... 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.

JoshG
05-30-2002, 05:32 PM
Are you trying to make a bullet in the shape of a line? I think I can code you a function to do that:


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 :)

Vicious
05-30-2002, 05:35 PM
but I need it to move by itself...




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




OMg... im so stupid... ill try that :rolleyes:

JoshG
05-30-2002, 05:37 PM
That does move by itself, did you try compiling it? And what part of Alabama are you from?

Vicious
05-30-2002, 05:37 PM
ill try it...

and im from Montgomery

JoshG
05-30-2002, 05:39 PM
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.

JoshG
05-30-2002, 05:50 PM
This code works, but the bullet wont stop for some reason, I will figure it out in a second


#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!

Vicious
05-30-2002, 06:09 PM
sigh...
I think it works but th line goes to fast..
i tried delay(200) but it froze up

#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 );
}
}


:confused:

here is the exe...

Vicious
05-30-2002, 10:30 PM
Someone out there HAS to know how to make a bullet fly across the screen
:(

Lamb?
Prelude?

.....

Invincible?

:rolleyes:

JoshG
05-30-2002, 10:55 PM
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?

#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.

Vicious
05-30-2002, 11:02 PM
call me stupid...
or sleepy...

but how can I apply that to my code..

(I plan on rewriting it to be more visually pleasing).

JoshG
05-30-2002, 11:05 PM
Copy my whole fire() function into your program, and get rid of shoot() in your program, and replace the call you made to shoot with a call to fire. Be sure to pass an x and a y.

I would have replied earlier, but my internet connection went down/

Vicious
05-31-2002, 01:46 PM
Check this out... I used kbhit and th problem now is the bullet erases when you move or shoot again....


#include <iostream.h>
#include <graphics.h>
#include <dos.h>
#include <conio.h>


int fire ( int x, int y );
int i;
int count = 0;
int quit = 0;
int mve, x = 300, y = 430;
int ch;

void game();
void playermve();

int main()
{
int graphdriver = DETECT;
int graphmode = 2;
initgraph ( &graphdriver, &graphmode, "C:\\BC5\\BGI" ); //Replace with you BGI dir.



for ( count = 0; !quit; count++ ){
game();

}


closegraph();
return 0;
}

void game()
{
while ( !quit )
{
gotoxy(1,1);
cout << "X: " << x << " Y: " << y;

settextstyle ( 1, HORIZ_DIR, 2 );
setcolor(RED);
outtextxy ( 230, -5, "Sonic Space" );

setcolor (GREEN);
rectangle ( 140, 20, 440, 470 );

playermve();
}
}

void playermve()
{
mve = getch();

switch ( mve )
{
case 77: if( x != 430 ) x += 10; break;
case 75: if( x != 150 ) x -= 10; break;
case 's': fire( x, y ); break;
case 'q': quit=1; break;
}
cleardevice();
setfillstyle ( SOLID_FILL, BLUE );
setcolor (RED);
pieslice ( x, y, 240, 300, 15 );
}


int fire ( int x, int y )
{
int tempy = y;

for ( i = y; 1 < 100; i-- )
{

setcolor(WHITE);
line(x, tempy, x, tempy-5);
delay(3);
setcolor(BLACK);
line(x, tempy, x, tempy-5);
tempy--;
if ( kbhit() ){
ch = getch();
if ( ch == 77 && x!= 430 ) x += 10; break;
if ( ch == 75 && x!= 150 ) x -= 10; break;
if ( ch = 's' ) fire ( x, y ); break;
if ( ch = 'q' ) quit = 1; break;
}

}
}


What can I do?

JoshG
05-31-2002, 04:15 PM
I do not understand while you have that for loop calling game, it only gets called once anyway, why not just put game(); Then the program will end when while loop becomes false.