我按照这个指南用c ++做了一个蛇游戏,但是我用c做了我的游戏。 https://www.youtube.com/watch?v=PSoLD9mVXTA
我只有一个问题,否则我已经设定了。我不能让蛇正确地与自身发生碰撞,我无法弄清楚为什么它对视频中的那个人起作用,但相同的代码对我来说没有用。
代码很丑陋而且组织得不好。不要评判我,我还在学习!
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
int gameOver = 1;
const int width = 20;
const int height = 20;
typedef enum {STOP = 0, LEFT, RIGHT, UP, DOWN} eDirection;
eDirection dir;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
void Setup(){
gameOver = 0;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}
void Draw(){
system("cls");
for(int i = 0; i < width +3; i++)
printf("#");
printf("\n");
for(int i = 0; i < height; i++){
for(int j = 0; j < width+2; j++){
if(j == 0){
printf("#");
}
if(j == width+1){
printf("#");
}
if(i == y && j == x){
printf("*");
}else if(i == fruitY && j == fruitX){
printf("F");
}else{
int print = 0;
for(int k = 0; k < nTail; k++){
if(tailX[k] == j && tailY[k] == i){
printf("*");
print = 1;
}
}
if(!print){
printf(" ");
}
}
}
printf("\n");
}
for(int i = 0; i < width +3; i++)
printf("#");
printf("\n");
printf("\nScore : %d",score);
}
void Input(){
if (kbhit()){
switch (getch()){
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
default:
break;
}
}
}
void Logic(){
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for(int 1 = 0; i < nTail; i++){
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
if(x > width || x < 0 || y > height-1 || y < 0){
gameOver = 1;
}
if(x == fruitX && y == fruitY){
fruitX = rand() % width;
fruitY = rand() % height;
score = score + 10;
nTail++;
}
//collision code
for(int i = 0; i < nTail; i++)
if(tailX[i] == x && tailY[i] == y){
gameOver = 1;
}
switch(dir){
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
}
int main(){
Setup();
while(!gameOver){
Draw();
Input();
Logic();
Sleep(100);
}
return 0;
}
答案 0 :(得分:1)
一旦你结果,你的节目就会失败。
<强>解释强>
当您的代码启动时,您没有尾部,即nTail
为0
。
在Logic()
你做:
tailX[0] = x;
tailY[0] = y;
因此索引零设置为当前位置(即使nTail
为零)。
当你点击水果时:
if(x == fruitX && y == fruitY){
fruitX = rand() % width;
fruitY = rand() % height;
score = score + 10;
nTail++; <---- Here you increment nTail so it becomes 1
}
然后你做
//collision code
for(int i = 0; i < nTail; i++)
if(tailX[i] == x && tailY[i] == y){
gameOver = 1;
}
因为nTail
是1
,所以它与:
if(tailX[0] == x && tailY[0] == y){
gameOver = 1;
}
这正是您在Logic
开始时所做的任务。换句话说 - 它是true
,gameover
将变为1。
也许解决方案是从1
而不是0
开始:
//collision code
for(int i = 1; i < nTail; i++)
^^^
与碰撞检测有关的另一个问题。
if(x == fruitX && y == fruitY){
fruitX = rand() % width;
fruitY = rand() % height;
score = score + 10;
nTail++; <---- Here you increment nTail but no value is
written to tailX[nTail-1] and tailY[nTail-1]
after the increment
}
//collision code
for(int i = 0; i < nTail; i++)
if(tailX[i] == x && tailY[i] == y){ <-- Here you use tailX[nTail-1] and tailY[nTail-1]
gameOver = 1;
}
所以,当你刚吃完水果时,你会与不属于尾巴的数值进行比较。