I'm trying to make a snake game in which if the snake eats the first fruit and in 2 seconds eats the second fruit then it's score multiplied by 2. If the snake eats the second fruit in 1 second then the score is multiplied by 3. If the snake eats the second fruit in 3 seconds then the score is increased just by 1 and so on ... But my fruit doesn't appear more than 1 time after the snake touches it. Why is that?
void make_stage() {
for (int i = 1; i <= 17; i++) {
gotoxy(i, 1);
printf("#");
gotoxy(1, i);
printf("#");
gotoxy(17, i);
printf("#");
gotoxy(i, 17);
printf("#");
}
x = height / 2;
y = width / 2;
fruitx = rand() % 17;
fruity = rand() % 17;
if (fruity == 0)
score = 0;
gotoxy(fruitx, fruity);
printf("@");
if (x == fruitx && y == fruity) {
fruitx = rand() % 17;
fruity = rand() % 17;
score += 1;
}}
And this is my whole code just for in case!
#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
int i, j, height = 17, width = 17;
int score;
int x, y, fruitx, fruity, flag;
typedef struct RECORD {
char name[100];
int score;
int time;
}record; //Name, Points,Time
record nowrec;
void gotoxy(int x, int y); //Input location
void make_stage(); //stage
int getCommand(); // Keyboard input
void gameover(); //Gameover screen
void startscr(); //Start screen
void snake_move(); //Snake movements
void rank_call(); //Displaying rank
void rankrecord(); //personal records
void cursor(int i); //커서 상태 변경
void time_show();
int main(void) {
startscr();
return 0;
}
void gotoxy(int x, int y) {
COORD pos = { 30 + x * 2, 10 + y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void make_stage() {
for (int i = 1; i <= 17; i++) {
gotoxy(i, 1);
printf("#");
gotoxy(1, i);
printf("#");
gotoxy(17, i);
printf("#");
gotoxy(i, 17);
printf("#");
}
x = height / 2;
y = width / 2;
fruitx = rand() % 17;
fruity = rand() % 17;
if (fruity == 0)
score = 0;
gotoxy(fruitx, fruity);
printf("@");
if (x == fruitx && y == fruity) {
fruitx = rand() % 17;
fruity = rand() % 17;
score += 1;
}}
void cursor(int i) {
CONSOLE_CURSOR_INFO cursorInfo = { 0, };
cursorInfo.dwSize = 1;
cursorInfo.bVisible = i;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}
void rank_call() {
FILE* rank;
char reading[100];
if (fopen_s(&rank, "rank.txt", "r") != 0) printf("no record\n");
else {
printf("\n");
while ((fgets(reading, 100, rank) != NULL)) printf("%s", reading);
fclose(rank);
printf("\n");
}
}
void rankrecord() {
printf("\npress enter to proceed...\n");
while (getchar() != '\n');
printf("press r to record your ranking...");
char input = _getch();
if (input == 'r') {
FILE* rank;
fopen_s(&rank, "rank.txt", "a");
while (getchar() != '\n');
printf("enter your name: ");
gets_s(nowrec.name, sizeof(nowrec.name));
fprintf(rank, "%s %d %d\n", nowrec.name, nowrec.score, nowrec.time);
fclose(rank);
}
}
void startscr()
{
system("mode con cols=100 lines=40");
start:
system("cls");
printf(" ****** ** * * * * ****** ****** * ** ** ****** \n");
printf(" * * * * * * * * * * * * * * * * * \n");
printf(" ****** * * * ***** **** ****** * *** ***** * * * * ****** \n");
printf(" * * * * * * * * * * * * * * ** * * \n");
printf(" ****** * ** * * * * ****** ****** * * * ** * ****** \n");
printf("\npress s to start game\n");
printf("press r to see ranking\n");
printf("press x to exit\n:");
char input = _getch();
if (input == 's') {
system("cls");
cursor(0);
make_stage();
snake_move();
}
else if (input == 'r') {
rank_call();
printf("press enter to continue...");
while (getchar() != '\n');
goto start;
}
else if (input == 'x') exit(0);
else {
printf("wrong input\n");
printf("press enter to continue...");
while (getchar() != '\n');
goto start;
}
}
void gameover() {
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n");
printf(" ****** * ** ** ****** **** * * ****** ***** \n");
printf(" * * * * * * * * * * * * * * * \n");
printf(" * *** ***** * * * * ****** * * * * ****** ***** \n");
printf(" * * * * * ** * * * * * * * * * \n");
printf(" ****** * * * ** * ****** **** * ****** * * \n");
cursor(1);
rankrecord();
}
void snake_move() {
int x = 9, y = 9;
int x1 = 8, y1 = 9;
int x2 = 7, y2 = 9;
int x3 = 6, y3 = 9;
int x4 = 5, y4 = 9;
char dir = 'd';
char input = 'e';
gotoxy(x, y);
printf("a");
gotoxy(x1, y1);
printf("*");
gotoxy(x2, y2);
printf("*");
gotoxy(x3, y3);
printf("*");
gotoxy(x4, y4);
printf("*");
while (1) {
input = _getch();
if ((dir == 'w' && input != 's') || (dir == 'a' && input != 'd') || (dir == 's' && input != 'w') || (dir == 'd' && input != 'a')) {
if (input == 'w') {
gotoxy(x4, y4);
printf(" ");
x4 = x3; x3 = x2; x2 = x1; x1 = x;
y4 = y3; y3 = y2; y2 = y1; y1 = y;
y = y - 1;
if (y == 1) {
gameover();
break;
}
if (x == x4 && y == y4) {
gameover();
break;
}
gotoxy(x, y);
printf("a");
gotoxy(x1, y1);
printf("*");
gotoxy(x2, y2);
printf("*");
gotoxy(x3, y3);
printf("*");
gotoxy(x4, y4);
printf("*");
dir = 'w';
}
if (input == 'a') {
gotoxy(x4, y4);
printf(" ");
x4 = x3; x3 = x2; x2 = x1; x1 = x;
y4 = y3; y3 = y2; y2 = y1; y1 = y;
x = x - 1;
if (x == 1) {
gameover();
break;
}
if (x == x4 && y == y4) {
gameover();
break;
}
gotoxy(x, y);
printf("a");
gotoxy(x1, y1);
printf("*");
gotoxy(x2, y2);
printf("*");
gotoxy(x3, y3);
printf("*");
gotoxy(x4, y4);
printf("*");
dir = 'a';
}
if (input == 's') {
gotoxy(x4, y4);
printf(" ");
x4 = x3; x3 = x2; x2 = x1; x1 = x;
y4 = y3; y3 = y2; y2 = y1; y1 = y;
y = y + 1;
if (y == 17) {
gameover();
break;
}
if (x == x4 && y == y4) {
gameover();
break;
}
gotoxy(x, y);
printf("a");
gotoxy(x1, y1);
printf("*");
gotoxy(x2, y2);
printf("*");
gotoxy(x3, y3);
printf("*");
gotoxy(x4, y4);
printf("*");
dir = 's';
}
if (input == 'd') {
gotoxy(x4, y4);
printf(" ");
x4 = x3; x3 = x2; x2 = x1; x1 = x;
y4 = y3; y3 = y2; y2 = y1; y1 = y;
x = x + 1;
if (x == 17) {
gameover();
break;
}
if (x == x4 && y == y4) {
gameover();
break;
}
gotoxy(x, y);
printf("a");
gotoxy(x1, y1);
printf("*");
gotoxy(x2, y2);
printf("*");
gotoxy(x3, y3);
printf("*");
gotoxy(x4, y4);
printf("*");
dir = 'd';
}
}
if (input == 'p') {
gameover();
break;
}
}
}
void time_show() { //아직 미완성(not completed)
clock_t s, n;
s = clock();
while (1) {
while (1) {
n = clock();
printf("\r"); //커서 왼쪽 끝으로 이동(스테이지의 위치에 따라 수정할 예정)
printf("Time-\t %d : %d : %d ", ((n - s) / 1000) / 60, ((n - s) / 1000) % 60, (n - s) % 1000);
if (_kbhit()) break; //키보드 입력이 들어오면 break;
}
if (_getch() == 'p') { //만약 그것이 p이면
printf("\rTime-\t %d : %d : %d ", ((n - s) / 1000) / 60, ((n - s) / 1000) % 60, (n - s) % 1000);
_getch();
}
else break;
s = s + (clock() - n);
}
printf("\rTime-\t %d : %d : %d ", ((n - s) / 1000) / 60, ((n - s) / 1000) % 60, (n - s) % 1000);
_getch(); _getch();
printf("\n\n\n");
return;
}