I am currently developing a Duck Hunt-style game with C language for a university class, but I'm having a problem that I can't move the player's aim while keeping the targets moving, for some reason, I can only move them separately. Here is a condensed version of the code, I would be very glad if someone could help.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include <windows.h>
#define Projetil 207
#define ESC 27
//formato da parabola (fazer ainda)
//substituir o sleep
int cx = 55;
int cy = 25;
void atirarpcima(){
int q;
q = cy+1;
while (q!=0){
gotoxy(cx+4, q);
printf("%c", Projetil);
Sleep(37);
gotoxy(cx+4, q);
printf(" ");
q--;
}
}
void pacman(){
gotoxy(cx, cy);
printf(" * *");
gotoxy(cx, cy+1);
printf("*** ***");
gotoxy(cx, cy+2);
printf("**** ****");
gotoxy(cx, cy+3);
printf(" *******");
}
void fantasmasaida(){
srand(time(NULL));
int y = (rand() % 9)+1;
int x = (rand() % 7)+14;
while(x<100){
gotoxy(x, y);
printf(" ****** ");
gotoxy(x, y+1);
printf("** ** **");
gotoxy(x, y+2);
printf("********");
gotoxy(x, y+3);
printf("** ** **");
gotoxy(x, y);
printf(" ");
gotoxy(x, y+1);
printf(" ");
gotoxy(x, y+2);
printf(" ");
gotoxy(x, y+3);
printf(" ");
x++;
}
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main(){
system("mode con: lines=30 cols=120");
char tecla;
pacman();
while (1){
while(!kbhit()){
fantasmasaida();
}
tecla = getch();
if (tecla == 'q'){
atirarpcima();
}
if (tecla == ESC){
break;
}
}
return 0;
}
Ok here we go video game development 101
The normal way to do this really is single threaded code; however a loop per thing displayed (sprite) to animate is wrong, as you've already learned. I really wish they taught this in the old books at entry level. I always wanted to know how as a teen and couldn't find out. There are three basic concepts.
To start off with, we have the master loop. Almost everything goes in the master loop as follows:
Now everything moves at once, but the game's impossible because the target jumps around too much. We need to fix that.
That's the easy one out of the way.
You will now notice that it takes waaay too long for a shot to cross the field. We will get to that, but first we need to clean up some repeated junk and at the same time get rid of the ridiculous hardcoding of the objects on the screen (hereafter known as sprites).
Now for the speed problem. The best fix is to track the position in units smaller than display units. We edit the
structas follows:struct sprite { void (*update_sprite)(struct sprite *sprite); void (*erase_sprite)(struct sprite *sprite); void (*draw_sprite)(struct sprite *sprite); int old_x; int old_y; int sp_x; int sp_y; int x; int y; };
Your code that moves it at some velocity would have looked something like this:
But now we do the following:
And we give the
apply_deltavin units a thousand times smaller than the display unit. (Historically this was 256 but that's because 8 bit CPUs.)Now if you tune your constants it's smooth.
It gets deeper. If you keep going you will encounter non-solid background, overlapping sprites, z-order, etc. But this is enough to get started.