this is game:my project is a simple game which is a car that should get a way from random bars.and if car accident with bars game is over! i want to write a simple code to move some characters and i'm not going to use c++ libraries functions (only c).
alright,i have a car which move, so I CAN NOT USE SLEEP. because it doesn't let car to move at normal speed. i want this access to move the car and at the same time bars come down from up of page to down.
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <ctime>
void gotoxy(int x, int y){
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD cursorCoord;
cursorCoord.X = x;
cursorCoord.Y = y;
SetConsoleCursorPosition(consoleHandle, cursorCoord);
}
void sleep(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
void Road(){
int i;
int x = 15, y = 17;
for (i = 1; i < 22; i++)
printf("\\ / \n");
printf(" _________________________________");
gotoxy(x, y);
printf("****");
gotoxy(x, y + 1);
printf("****");
gotoxy(x, y + 2);
printf("****");
gotoxy(x, y + 3);
printf("****");
} //Road of game + car
void CarMove(){
int lastx, lasty;
int x = 15, y = 17;
char a;
lastx = x; lasty = y;
gotoxy(x, y);
printf("****");
gotoxy(x, y + 1);
printf("****");
gotoxy(x, y + 2);
printf("****");
gotoxy(x, y + 3);
printf("****");
if (_kbhit()){
gotoxy(lastx, lasty);
printf(" ");
gotoxy(lastx, lasty + 1);
printf(" ");
gotoxy(lastx, lasty + 2);
printf(" ");
gotoxy(lastx, lasty + 3);
printf(" ");
a = _getch();
if ((a == 77) && (x <= 28)) x++; else x--;
if ((a == 75) && (x >= 1)) x--; else x++;
gotoxy(x, y);
printf("****");
gotoxy(x, y + 1);
printf("****");
gotoxy(x, y + 2);
printf("****");
gotoxy(x, y + 3);
printf("****");
;
lastx = x; lasty = y;
}
//this function just make a car which move on the terminal
}
void main(){
Road();
int lastx;
int x = 15, y = 17;
int m, n, lastn;
int askichar;
int size, i, j, k;
lastx = x;
//car code
do{
if (_kbhit()){
gotoxy(lastx, y);
printf(" ");
gotoxy(lastx, y + 1);
printf(" ");
gotoxy(lastx, y + 2);
printf(" ");
gotoxy(lastx, y + 3);
printf(" ");
askichar = _getch();
if ((askichar == 75) && (x >= 2)) x--;
if ((askichar == 77) && (x <= 28)) x++;
gotoxy(x, y);
printf("****");
gotoxy(x, y + 1);
printf("****");
gotoxy(x, y + 2);
printf("****");
gotoxy(x, y + 3);
printf("****");
;
lastx = x;
}//car code finished
//bar movement code
srand(time(NULL));
size = rand() % (8) + 4;
m = rand() % (18) + 3;
n = 0; lastn = 0;
while (n <= 20){
gotoxy(m, lastn);
for (j = 1; j <= size; j++)
printf(" ");
n++; lastn = n;
gotoxy(m, n);
for (k = 1; k <= size; k++)
printf("+");
}
gotoxy(m, n);
for (i = 1; i <= size; i++)
printf("_");
//bar movement code finished
} while (m != x && n != y && n + 1 != y + 1 && n + 2 != y + 2 && n + 3 != y + 3);
printf(" GAME OVER \n");
}
what can i do what is different ways to do this ?
one more side question: at first i wrote 3 functions 1-road(not important here) 2- carmove (contain a car which move on left or right )
3- barMovement (bars with random situation and size that come down from top of terminal to end of it)
in the main function when i call carmove
then i call barMovemen
barMovemen never run
and this is the code:
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <ctime>
void gotoxy(int x, int y){
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD cursorCoord;
cursorCoord.X = x;
cursorCoord.Y = y;
SetConsoleCursorPosition(consoleHandle, cursorCoord);
}
void sleep(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
void Road(){
int i;
int x = 15, y = 17;
for (i = 1; i < 22; i++)
printf("\\ / \n");
printf(" _________________________________");
gotoxy(x, y);
printf("****");
gotoxy(x, y + 1);
printf("****");
gotoxy(x, y + 2);
printf("****");
gotoxy(x, y + 3);
printf("****");
} //Road of game + car
void CarMove(){
int lastx, lasty;
int x = 15, y = 17;
char a;
lastx = x; lasty = y;
gotoxy(x, y);
printf("****");
gotoxy(x, y + 1);
printf("****");
gotoxy(x, y + 2);
printf("****");
gotoxy(x, y + 3);
printf("****");
while (1){
if (_kbhit()){
gotoxy(lastx, lasty);
printf(" ");
gotoxy(lastx, lasty + 1);
printf(" ");
gotoxy(lastx, lasty + 2);
printf(" ");
gotoxy(lastx, lasty + 3);
printf(" ");
a = _getch();
if ((a == 77) && (x <= 28)) x++; else x--;
if ((a == 75) && (x >= 1)) x--; else x++;
gotoxy(x, y);
printf("****");
gotoxy(x, y + 1);
printf("****");
gotoxy(x, y + 2);
printf("****");
gotoxy(x, y + 3);
printf("****");
;
lastx = x; lasty = y;
}
//this function just make a car which move on the terminal
}
}
void BarMovement(){
int m, n, lastm, lastn;
int i, j, k;
int size;
srand(time(NULL));
size = rand() % (8) + 4;
m = rand() % (18) + 3;
n = 0; lastn = 0;
while (n <= 20){
gotoxy(m, lastn);
sleep(200);
for (j = 1; j <= size; j++)
printf(" ");
n++; lastn = n;
gotoxy(m, n);
for (k = 1; k <= size; k++)
printf("+");
}
gotoxy(m, n);
for (i = 1; i <= size; i++)
printf("_");
//bar movement code finished
}
void main(){
Road();
CarMove();
BarMovement();
}
and sorry for English