gpt4 book ai didi

c - C 中的 Snake - 整数被更改但没有更改 - 有什么问题?

转载 作者:行者123 更新时间:2023-11-30 20:47:56 25 4
gpt4 key购买 nike

(程序语言是c - 纠正时请只使用菜鸟可以做的东西,我从1周开始就开始学习这个)

问题是:参见“int Snakelen = 1;”在 main 的开头?

我从不改变那个整数。但是当我结束游戏时它是0。为什么?

如果我尝试在游戏中更改蛇形,游戏就会完全崩溃,并且我会听到 Windows 错误声音。怎么了?

(虽然我使用随机发生器,但食物不会随机生成。它要么只生成底部,要么只生成顶部 - 大约每 5 分钟改变一次。另一个奇怪的故障。)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <math.h>

#define SIZE 2048 //32(*2 because every 2nd one is a space) * 32
#define HSIZE 1024 //32 * 32
#define UP_ 1
#define DOWN_ 3
#define LEFT_ 4
#define RIGHT_ 2
#define YSIZE 64 //length of a row (including spaces)

//TO PLAY: Start the game once, right-click the command-promt at the top, click Settings -> Layout . Then change the window size to 64 (hor) * 33 (vert)

int counter = 0;
int gamespeed = 400;//Intervall in which a game-frame gets rendered

int spawnfood(int* freetable, int snakelen)
{
srand ( time(NULL) ); //randomize
int randomIndex = rand()%(HSIZE - 1); //take random non-x-filled position from array
int randomValue = freetable[randomIndex]; //output the random position
return randomValue;
}

int main()
{
int nofood = 1; //is there food in the game? 1 = no
//int tmp = 0; //temporary memory for later
int tmp2 = 0; //temporary memory2
int snakelen = 1; //length of snake
int snakedir = RIGHT_; //Position the snake is looking
int snakeheadxpos = 0; //x-position of snake
int snakeheadypos = 0; //y-position of snake
char q;//The button that was pressed last


char gametable[SIZE]; //the game-screen //fill it with spaces
for(int i = 0; i < SIZE; i++)
{
gametable[i]=' ';
}
gametable[SIZE] = '\0';


int freetable[(HSIZE)]; // 32*32 list of all pixels, which shows whether a pixel is an x or not
for(int i = 0; i < (HSIZE); i++)
{
freetable[i]= i*2; //fill the array with its numbers
}


//START OF GAME
printf("Press any Button to start the game!\n");
getch();
for(int i = 0; i < 31; i++){
printf("\n");
}

while(q != 27)
{
counter++;
if(kbhit()) //if button is pressed
{
q = getch(); //q = that button
switch(q) //change the way the snake looks via WASD
{
case 'w':
if(snakedir != DOWN_)
snakedir = UP_;
break;
case 'a':
if(snakedir != RIGHT_)
snakedir = LEFT_;
break;
case 's':
if(snakedir != UP_)
snakedir = DOWN_;
break;
case 'd':
if(snakedir != LEFT_)
snakedir = RIGHT_;
break;
default:
break;
}

}
if(counter%gamespeed == 0) //Renders a game-frame at the intervall of gamespeed
{
switch(snakedir)
{

case UP_:
if(snakeheadypos==0)
{
goto exit_loop; //ran into a wall
}
snakeheadypos--;
break;

case DOWN_:
if(snakeheadypos==31)
{
goto exit_loop; //ran into a wall
}
snakeheadypos++;
break;

case RIGHT_:
if(snakeheadxpos==31)
{
goto exit_loop; //ran into a wall
}
snakeheadxpos++;
break;

case LEFT_:
if(snakeheadxpos==0)
{
goto exit_loop; //ran into a wall
}
snakeheadxpos--;
break;
default:
break;
}

if((gametable[snakeheadypos*YSIZE + 2*snakeheadxpos] == 'o'))
{
//snakelen++; //<-- WHEN YOU REMOVE THE FIRST //, THE GAME STARTS TO BUG AND I GET WINDOWS ERROR SOUNDS!
nofood = 1; //no more food is in the game
}
gametable[snakeheadypos*YSIZE + 2*snakeheadxpos] = 'x'; //set the pixel ur at the the moment to 'x'

gametable[tmp2] =' ';
tmp2 = snakeheadypos*64+snakeheadxpos*2;
//spawn food if there is none
if(nofood)
{
gametable[spawnfood(freetable, snakelen)] = 'o';
nofood = 0; //food is already placed
}
printf("%s", gametable); // print the gametable
}
}
exit_loop: ; //if you ran into a wall
printf("Game Over - Score: %d", snakelen);
}

最佳答案

 char gametable[SIZE]; //the game-screen //fill it with spaces
...
gametable[SIZE] = '\0';

这是错误的。您可以使用索引 0 到 N-1 来索引大小为 N 的数组。 gametable[SIZE] 位于数组之外,对其进行赋值会调用未定义的行为。

关于c - C 中的 Snake - 整数被更改但没有更改 - 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53414703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com