[英]Snake Game with C
以下是用 C 語言編寫的蛇游戲的代碼。 在這種情況下,我們希望在初始情況下 (****) 以一條由 4 顆星組成的蛇開始,並且在游戲開始時蛇會自動向右移動。 我在這段代碼中進行了試驗,但我看到蛇以 3 星的形式開始,當我按下任何一個方向按鈕時,它會變成 4 星的蛇。 如何解決這個問題?
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
bool gameOver;
bool hit = false;
const width = 50;
const height = 20;
int x,y,fruitX,fruitY,score;
int tailX[200],tailY[200];
int ntail=3;
char snake[200];
typedef enum {STOP = 0,LEFT,RIGHT,UP,DOWN} Direction;
Direction Dir;
void menu(){
int a;
printf("\n\n\n\n\n\n");
printf(" THE SNAKE GAME \n\n\n");
printf(" 1) NEW GAME \n");
printf(" 2GAME WITH BORDERS \n");
printf(" 3.QUIT THE GAME \n\n");
printf(" Please enter your choice: ");
scanf("%d", &a);
}
// This function is used to initialize(reset the game when it begins
void setup(){
// This function will be used to print the snake in each of its different cases inside its fame
gameOver = false;
Dir = STOP;
x= width/2;
y= height/2;
fruitX = rand()%width;
fruitY = rand()%height;
score = 0;
}
void draw(){
// This is used to take inputs when we press on any key on the keyboard and send it to the logic function to be interpreted
// This is used to
system("cls");
printf("Score:%d",score);
printf("\n");
int i;
for(i= 0 ;i<width+1;i++){ // first row
printf("=");
}
printf("\n");
int p;
for(p= 0 ;p<height;p++){
int q;
for(q= 0 ;q<width;q++){
if(q==0 || q==width-1){ // first last elt
printf("!");
}
if(p == y && q == x){//head coordinates
printf("*");
}else if(p == fruitY && q == fruitX){
printf("#");
}else{
int k=0;
bool print = false;
for(k=0;k<ntail;k++){
if(tailX[k]==q && tailY[k]==p){
printf("*");
print = true;
}
}
if(!print){printf(" ");}
}
}
printf("\n");
}
int j;
for(j= 0 ;j<width+1;j++){
printf("=");
}
}
void input(){
// The logic function
if(_kbhit()){
switch(_getch()){
case '4':
Dir = LEFT;
hit= true;
break;
case '8':
Dir = UP;
hit= true;
break;
case '6':
Dir = RIGHT;
hit= true;
break;
case '2':
Dir = DOWN;
hit= true;
break;
case 'x':
gameOver = true;
break;
}
}else if(!hit){
x++;
}
}
void logic(){
// main function
int lastX = tailX[0]; // The one before 0
int lastY = tailY[0];
int last2X, last2Y;
tailX[0]=x;
tailY[0]=y;
int i=0;
for(i=1; i<ntail;i++){
last2X = tailX[i];// 2 takes place of 1
last2Y = tailY[i];
tailX[i]=lastX; // 1 takes place of 0
tailY[i]=lastY;
lastX = last2X; // 1 takes place of 1
lastY = last2Y;
}
switch(Dir){
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
if(x<0 || width<x || y<0 || height<y){
gameOver = true;
system("cls");
printf("#******#######################****# GAME OVER #***#######################******#");
}
if(x==fruitX && y==fruitY){
ntail++;
score+=10;
fruitX = rand()%width;
fruitY = rand()%height;
}
}
int main(){
menu();
setup();
draw();
while(!gameOver){// each time we repeat a new picture will new drawn and we will see the snake in its new state
draw();
input();
logic();
}
return 0;
}
我在這段代碼中進行了試驗,但我看到蛇以 3 星的形式開始
根據tailX[0]=x; tailY[0]=y;
tailX[0]=x; tailY[0]=y;
您使用tailX[0]
和tailY[0]
作為頭部的位置。 你初始化int ntail=3;
並從 1 迭代到小於 3 ( for(i=1; i<ntail;i++
),所以你的尾巴長度為 2。總而言之,你的蛇在開始時有 3 個元素長。
當我按下任何一個方向按鈕時,它會變成一條 4 星的蛇
第一次按下按鈕后DIR != STOP
,並在設置tailX
和tailY
后遞增或遞減x
或y
。 現在x != tail[0]
或y != tail[0]
並且您的蛇的總長度為 4。
我的解決方案是移動 else 情況,即從函數input
增加x
並將其放入函數logic
:
void input(){
if(_kbhit()){
switch(_getch()){
case '4': Dir = LEFT; break;
case '8': Dir = UP; break;
case '6': Dir = RIGHT; break;
case '2': Dir = DOWN; break;
case 'x': gameOver = true; break;
}
}
// from here ---->
}
void logic(){
for(int i=ntail-1; i>0;i--){
tailX[i]=tailX[i-1];
tailY[i]=lastY[i-1];
}
tailX[0]=x;
tailY[0]=y;
switch(Dir){
case UP: y--; break;
case DOWN: y++; break;
case LEFT: x--; break;
case RIGHT: x++; break;
case STOP: x++; break; // <----- to here
}
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.