Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help with game loop...I think

Hello all,



I've been programming in C++ for almost a year now and I've finally got up the ambition to venture into game programming. The main thing that I'm struggling with is the main game loops. I know that the loops are timed based but that is about all. Here is the little game I'm working on now. I'm using DJGPP. I'd like some of you out there to compile it and see what suggestion and what advise you can give me. I'm trying to work this out on my own but I've hit a wall. Take into consideration that this is a work in progress and that I'm not really any good. :) Here we go -



#include

#include

#include

#include

#include



void main_screen(void);

void move_you(int input);

void move_them(void);

void shoot(void);

void display_score(void);



struct coord {

int x, y;

} you, them;



struct bullet {

int x,y;

};



bool them_alive = true;

int score = 0;



int main()

{

int input;

you.x = 10;

you.y = 10;

them.x = 80;

them.y = random()%31; //10;



clock_t FRAMERATE = 10; //20 ms = 50 fps

bool running = true;



_setcursortype(_NOCURSOR); //conio

main_screen();

clrscr();

gotoxy(you.x,you.y);

cprintf("X-");



while(running)

{

clock_t start = clock(); //remember start time

display_score();

if(them_alive)

{

move_them();

}

if( kbhit() ) //if a key is hit

{

input=getch();

if(input=='q')

{

running = false;

}

else if(input=='f')

{

shoot();

}

move_you(input);

}

while((clock() - start) < FRAMERATE);

}



clrscr();

cprintf("Game over
");

getch();

_setcursortype(_NORMALCURSOR);

return 0;

}



void move_you(int input)

{

gotoxy(you.x,you.y);

cprintf(" ");

if(input==77)

you.x++;

else if(input==75)

{

you.x--;

if(you.x==0)

{

you.x=1;

}

}

else if(input==72)

{

you.y--;

if(you.y==0)

{

you.y=1;

}

}

else if(input==80)

{

you.y++;

if(you.y==26)

{

you.y=25;

}

}

gotoxy(you.x,you.y);

cprintf("X-");

}



void move_them(void)

{

gotoxy(them.x,them.y);

cprintf(" ");

them.x--;

while(them.x<1);<br>
{

gotoxy(them.x,them.y);

cprintf("()");

}

if(them.x==1)

{

them.y = random() % 30;

them.x=80;

}

}



void shoot(void)

{

int x = (you.x + 2);

int y = you.y;

while(x<80)<br>
{

gotoxy(x, y);

cprintf("");

x++;

if(x==them.x && y==them.y)

{

them_alive = false;

score = 10;

}

}

}



void main_screen(void)

{

clrscr();

gotoxy(1,1);

cprintf("My First Game v.01a");

gotoxy(1,2);

cprintf("Programmed by Matthew Kris");

gotoxy(1,3);

cprintf("Directions:");

gotoxy(1,4);

cprintf("Arrow keys move");

gotoxy(1,5);

cprintf("F to fire");

gotoxy(1,6);

cprintf("Q to quit");

getch();

}



void display_score(void)

{

gotoxy(1,1);

cprintf("Score: %i", score);

}








Sign In or Register to comment.