gpt4 book ai didi

c - C语言中关于switch语句的贪吃蛇游戏

转载 作者:行者123 更新时间:2023-11-30 21:02:52 25 4
gpt4 key购买 nike

这是一个简单的游戏,每次只需输入一个字符,移动蛇并打印表格即可。主函数中的 switch 语句设置了 4 种情况'a' 's' 'd' 'w',没有默认,但是程序运行时,如果输入了这四种情况之外的字符,程序仍然会使用react并输出table(另外,table会输出两次)。为什么?此外,它也让我感到困惑,如果输入位于箱子中,蛇将无法正确移动。

忘记在之前的情况后添加“break”:)但问题仍然让我困惑。现在如果我在默认情况下使用“break”,程序仍然打印表格两次,如果我在默认情况下使用“Continue”,程序不会对错误的输入使用react。但蛇仍然无法正确移动。:(

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SNAKE_MAX_LENGTH 20
void snakeMove(int, int);
void put_money(void);
void output(void);
void gameover(void);
char map[12][12] =
{"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"};
int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int game=1,origin=1,eaten=0;
int main() {
char ch;
srand(time(NULL));
put_money();
output();
origin=0;
while(game) {
scanf("%c",&ch);
switch(ch) {
case 'w':
snakeMove(1,1);
break;
case 's':
snakeMove(2,1);
break;
case 'a':
snakeMove(3,1);
break;
case 'd':
snakeMove(4,1);
break;
default:
continue;
}
if(eaten==1) {
put_money();
eaten=0;
}
output();
}
gameover();
return 0;
}
void snakeMove(int direction, int distance) {
int i,move=1;
switch(direction) {
case 1:
if(snakeY[3]==snakeY[4]+distance)
move=0;
else
snakeY[4]=snakeY[4]+distance;
break;
case 2:
if(snakeY[3]==snakeY[4]-distance)
move=0;
else
snakeY[4]=snakeY[4]-distance;
break;
case 3:
if(snakeX[3]==snakeX[4]-distance)
move=0;
else
snakeX[4]=snakeX[4]-distance;
break;
case 4:
if(snakeX[3]==snakeX[4]+distance)
move=0;
else
snakeX[4]=snakeX[4]+distance;
}
if (move==1) {
for(i=3;i>=0;i--){
snakeX[i]=snakeX[i+1];
snakeY[i]=snakeY[i+1];
}
}
}
void output(void) {
int x, y,i;
if(origin==0) {
for(x=1;x<11;x++) {
for(y=1;y<11;y++) {
if(map[x][y]!='$')
map[x][y]=' ';
}
}
if(map[snakeX[4]][snakeY[4]]=='$')
eaten=1;
for(i=0;i<4;i++)
map[snakeX[i]][snakeY[i]]='X';
map[snakeX[4]][snakeY[4]]='H';
if(snakeX[4]==0||snakeX[4]==11||snakeY[4]==0||snakeY[4]==11)
game=0;
for(i=0;i<4;i++) {
if(snakeX[4]==snakeX[i]&&snakeY[4]==snakeY[i])
game=0;
}
}
for(x=0;x<12;x++) {
for(y=0;y<12;y++)
printf("%c",map[x][y]);
printf("\n");
}
}
void gameover(void) {
printf("Game Over!!!\n");
exit(0);
}
void put_money(void) {
int foodx,foody,done=0;
while(done==0) {
foodx=rand()%12;
foody=rand()%12;
if(map[foodx][foody]==' ') {
map[foodx][foody]='$';
done=1;
}
}


}

最佳答案

有趣。你的 switch 语句很好。这是一个工作代码,其中注释了更改,后面是整个代码:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SNAKE_MAX_LENGTH 20
void snakeMove(int, int);
void put_money(void);
void update_snake(void);
void output(void);
void gameover(void);
char map[12][12] =
{"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"};
int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int game=1,origin=1,eaten=0;

int main() {
char ch;
srand(time(NULL));
put_money();
output();
origin=0;
while(game) {

正如 Cool Guy 所说,scanf 中的空格是有效的。这是因为当您按 Enter 键时,换行符将被计为一个字符。

                scanf(" %c",&ch);
switch(ch) {
case 'w':
snakeMove(1,1);
break;
case 's':
snakeMove(2,1);
break;
case 'a':
snakeMove(3,1);
break;
case 'd':
snakeMove(4,1);
break;
default:
continue;
}
if(eaten==1) {
put_money();
eaten=0;
}
output();
}
gameover();
return 0;
}

添加此更新功能是因为您需要在更新头部之前更新蛇。否则,当您尝试将其发回时,您将丢失原始头。

void update_body()
{
int i;
for(i=0;i<4;i++){
snakeX[i]=snakeX[i+1];
snakeY[i]=snakeY[i+1];
}

}
void snakeMove(int direction, int distance) {
int move=1;

switch(direction) {
case 1:

第三个问题是,在图形中,原点(0, 0)位于左上角而不是左下角。这意味着要向上,您必须减去,向下,您必须添加。

                if(snakeY[3]==snakeY[4]-distance)
move=0;
else{
update_body();
snakeY[4]=snakeY[4]-distance;
}
break;
case 2:
if(snakeY[3]==snakeY[4]+distance)
move=0;
else{
update_body();
snakeY[4]=snakeY[4]+distance;
}
break;
case 3:
if(snakeX[3]==snakeX[4]-distance)
move=0;
else{
update_body();
snakeX[4]=snakeX[4]-distance;
}
break;
case 4:
if(snakeX[3]==snakeX[4]+distance)
move=0;
else{
update_body();
snakeX[4]=snakeX[4]+distance;
}
}

}
void output(void) {
int x, y,i;
if(origin==0) {
for(x=1;x<11;x++) {
for(y=1;y<11;y++) {
if(map[x][y]!='$')
map[x][y]=' ';
}
}

最后一个问题是 map 颠倒了。这意味着 X 和 Y 坐标是第 1 和第 2,而它们本应是第 2 和第 1。

                if(map[snakeY[4]][snakeX[4]]=='$')
eaten=1;
for(i=0;i<4;i++)
map[snakeY[i]][snakeX[i]]='X';
map[snakeY[4]][snakeX[4]]='H';
if(snakeX[4]==0||snakeX[4]==11||snakeY[4]==0||snakeY[4]==11)
game=0;
for(i=0;i<4;i++) {
if(snakeX[4]==snakeX[i]&&snakeY[4]==snakeY[i])
game=0;
}
}
for(x=0;x<12;x++) {
for(y=0;y<12;y++)
printf("%c",map[x][y]);
printf("\n");
}
}
void gameover(void) {
printf("Game Over!!!\n");
exit(0);
}
void put_money(void) {
int foodx,foody,done=0;
while(done==0) {
foodx=rand()%12;
foody=rand()%12;
if(map[foodx][foody]==' ') {
map[foodx][foody]='$';
done=1;
}
}
}

这是完整的代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SNAKE_MAX_LENGTH 20
void snakeMove(int, int);
void put_money(void);
void update_body();
void output(void);
void gameover(void);
char map[12][12] =
{"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"};

int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int game=1,origin=1,eaten=0;
int main() {
char ch;
srand(time(NULL));
put_money();
output();
origin=0;
while(game) {
scanf(" %c",&ch);
switch(ch) {
case 'w':
snakeMove(1,1);
break;
case 's':
snakeMove(2,1);
break;
case 'a':
snakeMove(3,1);
break;
case 'd':
snakeMove(4,1);
break;
default:
continue;
}
if(eaten==1) {
put_money();
eaten=0;
}
output();
}
gameover();
return 0;
}
void update_body()
{
int i;
for(i=0;i<4;i++){
snakeX[i]=snakeX[i+1];
snakeY[i]=snakeY[i+1];
}

}
void snakeMove(int direction, int distance) {
int move=1;
switch(direction) {
case 1:
if(snakeY[3]==snakeY[4]-distance)
move=0;
else{
update_body();
snakeY[4]=snakeY[4]-distance;
}
break;
case 2:
if(snakeY[3]==snakeY[4]+distance)
move=0;
else{
update_body();
snakeY[4]=snakeY[4]+distance;
}
break;
case 3:
if(snakeX[3]==snakeX[4]-distance)
move=0;
else{
update_body();
snakeX[4]=snakeX[4]-distance;
}
break;
case 4:
if(snakeX[3]==snakeX[4]+distance)
move=0;
else{
update_body();
snakeX[4]=snakeX[4]+distance;
}
}

}
void output(void) {
int x, y,i;
if(origin==0) {
for(x=1;x<11;x++) {
for(y=1;y<11;y++) {
if(map[x][y]!='$')
map[x][y]=' ';
}
}
if(map[snakeY[4]][snakeX[4]]=='$')
eaten=1;
for(i=0;i<4;i++)
map[snakeY[i]][snakeX[i]]='X';
map[snakeY[4]][snakeX[4]]='H';
if(snakeX[4]==0||snakeX[4]==11||snakeY[4]==0||snakeY[4]==11)
game=0;
for(i=0;i<4;i++) {
if(snakeX[4]==snakeX[i]&&snakeY[4]==snakeY[i])
game=0;
}
}
for(x=0;x<12;x++) {
for(y=0;y<12;y++)
printf("%c",map[x][y]);
printf("\n");
}
}
void gameover(void) {
printf("Game Over!!!\n");
exit(0);
}
void put_money(void) {
int foodx,foody,done=0;
while(done==0) {
foodx=rand()%12;
foody=rand()%12;
if(map[foodx][foody]==' ') {
map[foodx][foody]='$';
done=1;
}
}
}

关于c - C语言中关于switch语句的贪吃蛇游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27651992/

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