为什么我的水果在蛇游戏中会在边界外重生

  • 本文关键字:游戏 边界 重生 c
  • 更新时间 :
  • 英文 :


我正在尝试制作一个蛇游戏,如果蛇吃了第一个水果,然后在2秒内吃了第二个水果,那么它的分数乘以2。如果蛇在1秒内吃掉了第二个水果,那么分数乘以3。如果蛇在3秒内吃掉了第二个水果,那么分数只会增加1,以此类推。。。但在蛇吃掉大约5-6个果实后,第7个果实在边界外重生。这就是";绘制";功能

void make_stage() {
score = 0;
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("*");
for (int i = 1; i <= 17; i++) {
gotoxy(i, 1);
printf("#");
gotoxy(1, i);
printf("#");
gotoxy(17, i);
printf("#");
gotoxy(i, 17);
printf("#");
}
srand(time(NULL));
fruitx = rand() % 17;
fruity = rand() % 17;
gotoxy(fruitx, fruity);
printf("@");
}

这就是逻辑函数:

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';
while (1) {
srand(time(NULL));
if (x == fruitx && y == fruity) {
fruitx = rand() % 17;
fruity = rand() % 17;
score += 1;
gotoxy(fruitx, fruity);
printf("@");
}
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;
}
}
}

这是整个源代码以防万一!

#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<process.h>
int height = 17, width = 17;
typedef struct RECORD {
char name[100];
int score;
int minute;
int sec;
}record; //이름, 점수, 시간을 저장할 구조체
int score;
int fruitx, fruity;
record nowrec;

int over = 0;

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 stopwatch();               //stop watch 

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() {
score = 0;
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("*");
for (int i = 1; i <= 17; i++) {
gotoxy(i, 1);
printf("#");
gotoxy(1, i);
printf("#");
gotoxy(17, i);
printf("#");
gotoxy(i, 17);
printf("#");
}
srand(time(NULL));
fruitx = rand() % 17;
fruity = rand() % 17;
gotoxy(fruitx, fruity);
printf("@");
}

int getCommand() {
if (_kbhit()) {
return _getch();
}
return -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 recordn");
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("nnnnnnpress r to record your ranking...n");
char input = _getch();
if (input == 'r') {
FILE* rank;
fopen_s(&rank, "rank.txt", "a");
rerun:
printf("enter your name: ");
gets_s(nowrec.name, sizeof(nowrec.name));
if (strlen(nowrec.name) < 3) {
printf("name must be at least 3 words...nn");
goto rerun;
}
while (1) {
printf("your name is %s.nyour score is %d.nyour clear time is %d : %d.nn", nowrec.name, nowrec.score, nowrec.minute, nowrec.sec);
printf("if your name is incorrect, press n to correctnif not, press y to continue...nn");
input = _getch();
if (input == 'n') goto rerun;
else if (input == 'y') break;
else printf("wrong inputnn");
}
fprintf(rank, "%s %d %d : %dn", nowrec.name, nowrec.score, nowrec.minute, nowrec.sec);
fclose(rank);
printf("saved!n");
}
}

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 gamen");
printf("press r to see rankingn");
printf("press x to exitn:");
char input = _getch();
if (input == 's') {
system("cls");
cursor(0);
make_stage();
HANDLE thread1 = _beginthreadex(NULL, 0, (_beginthreadex_proc_type)stopwatch, NULL, 0, NULL);
Sleep(1);
HANDLE thread2 = _beginthreadex(NULL, 0, (_beginthreadex_proc_type)snake_move, NULL, 0, NULL);
WaitForSingleObject(thread2, INFINITE);

}
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 inputn");
printf("press enter to continue...");
while (getchar() != 'n');
goto start;
}
}

void gameover() {
over = 1;
system("cls");
printf("nnnnnnnnnn");
printf("        ******      *        **    **    ******            ****   *       *  ******   *****         n");
printf("        *          * *      *  *  *  *   *                *    *   *     *   *        *    *        n");
printf("        *  ***    *****     *  *  *  *   ******           *    *    *   *    ******   *****         n");
printf("        *    *   *     *   *    **    *  *                *    *     * *     *        *    *        n");
printf("        ******  *       *  *    **    *  ******            ****       *      ******   *     *      n");
cursor(1);
rankrecord();
printf("press any key to back to title...");
_getch();
startscr();
}

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';
while (1) {
srand(time(NULL));
if (x == fruitx && y == fruity) {
fruitx = rand() % 17;
fruity = rand() % 17;
score += 1;
gotoxy(fruitx, fruity);
printf("@");
}
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 stopwatch() {
clock_t s, n;
s = clock();
while (1) {
n = clock();
cursor(0);
gotoxy(14, 0);
printf(" %d : %d", ((n - s) / 1000) / 60, ((n - s) / 1000) % 60);
if (over == 1) {
break;
}
Sleep(1000);
}
gotoxy(14, 0);
printf(" ****  ");
nowrec.minute = ((n - s) / 1000) / 60;
nowrec.sec = ((n - s) / 1000) % 60;
return;
}

记住

rand() % x

将为您提供[0,x-1]范围内的值。

考虑到这一点,让我们看看

fruitx = rand() % 17;
fruity = rand() % 17;
gotoxy(fruitx, fruity);
printf("@");

当您像这样将rand() % 17馈送到gotoxy()时,您可以预期goto(0, 0)是可能的。

我修改了上面的代码,使其看起来像这个

fruitx = rand() % 17;
fruity = rand() % 17;
gotoxy(0, 0);
printf("@");

看看会发生什么,水果出现在左上角的田地外。

void gotoxy(int x, int y) {
COORD pos = { 30 + x * 2, 10 + y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

问题是,您对gotoxy()的定义是,将其作为坐标输入(0, 0)会使光标一开始就位于边界之外。此外,通过(1, 1)会把它放在角落里,使它与墙分开,我猜这也是违法的。

要解决这个问题,您需要在gotoxy()中调整数学,或者创建一个新函数,将场坐标转换为全局坐标。这样,(0, 0)将对应于实际的左上角(刚好在边界内(,依此类推

最简单的修复方法就是这样修改

fruitx = 2 + rand() % 15;
fruity = 2 + rand() % 15;
gotoxy(fruitx, fruity);
printf("@");

这应该发生在你产卵的任何地方

最新更新