代码是参考网上的(自己写了一遍 附在后面),主要是分享遇到的bug以及感悟 问题1.贪吃蛇初始方向不受控? 解决:一开始一直以为是自己控制方向的数组dy dx逻辑出错,后来才发现是自己把蛇头位置+蛇头方向 写成 蛇头位置+蛇身方向,所以乱了
问题2.贪吃蛇运动过程中,方向失控? 解决:主要是没看清楚代码 先入为主的把dy dx 方向改了
问题3:吃食物出现bug? 解决:还是move函数里面的判断出错了,逻辑问题,要多学会透过问题看本质
问题4:request时候,出bug? 解决:init函数没有及时重置相关全局变量
架构:
感悟: 1.看代码要仔细 2.透过现象看本质 3.初始函数一定记得及时初始化各个变量 优化: 代码之间的函数联系可不可再更简化 做到高内聚 低耦合
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <string.h>
#include <time.h>
int sum = 5;
int speed = 0;
char map[20][20];
int dy[4] = {0, 0, -1, 1};
int dx[4] = {-1, 1, 0, 0};
int flag = 0;
int x1 = 0;
int y2 = 0;
void init();
void show();
void check();
void turn();
void food();
void move();
struct snake
{
int x;
int y;
int dir;
}A[100];
int main() {
printf("Welcome to snack game\n");
printf("'w', 's', 'a', 'd' control\n");
printf("Enter any key to start");
char ch = _getch();
system("cls");
init();
show();
}
void init() {
sum = 5;
flag = 0;
speed = 500;
int i;
int j;
flag = 0;
for (i = 0; i < 100; i++) {
A[i].x = 0;
A[i].dir = 0;
A[i].y = 0;
}
A[0].x = 1; A[0].y = 1;
A[1].x = 1; A[1].y = 2;
A[2].x = 1; A[2].y = 3;
A[3].x = 1; A[3].y = 4;
A[4].x = 1; A[4].y = 5;
A[4].dir = 1;
for (i = 0; i < 20; i++) {
for (j = 0; j < 20; j++) {
map[i][j] = '*';
}
}
for (i = 1; i < 19; i++) {
for (j = 1; j < 19; j++) {
map[i][j] = ' ';
}
}
map[A[4].x][A[4].y] = 'H';
for (i = 0; i < 4; i++) {
map[A[i].x][A[i].y] = 'X';
}
food();
}
void show() {
int i, j, x, y;
int speed = 500;
for (i = 0; i < 20; i++) {
for (j = 0; j < 20; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
while (1) {
Sleep(speed);
turn();
move();
if (flag) {
printf("Input 'r' to restart\nInput 'q' to quit\n");
while (1) {
char ch = _getch();
if (ch == 113) {
return;
} else if (ch == 114) {
init();
break;
}
}
}
system("cls");
for (i = 0; i < 20; i++) {
for (j = 0; j < 20; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
}
}
void food () {
int x = 0;
int y = 0;
while (1) {
x = (int)(18 * rand() / (RAND_MAX + 1.0));
y = (int)(18 * rand() / (RAND_MAX + 1.0));
if(map[x][y] == ' ') {
map[x][y] = 'o';
break;
}
}
}
void turn () {
if (_kbhit()) {
char dir = _getch();
switch (dir)
{
case 119: A[sum - 1].dir = 0; break;
case 115: A[sum - 1].dir = 1; break;
case 97: A[sum - 1].dir = 2; break;
case 100: A[sum - 1].dir = 3; break;
}
}
}
void move () {
int x, i, j;
int t = sum;
check();
if (t == sum) {
for (i = 0; i < sum - 1; i++) {
if (i == 0) {
map[A[i].x][A[i].y] = ' ';
A[i].x = A[i + 1].x;
A[i].y = A[i + 1].y;
}
else {
A[i].x = A[i + 1].x;
A[i].y = A[i + 1].y;
}
map[A[i].x][A[i].y] = 'X';
}
A[sum - 1].x = A[sum - 1].x + dx[A[sum - 1].dir];
A[sum - 1].y = A[sum - 1].y + dy[A[sum - 1].dir];
map[A[sum - 1].x][A[sum - 1].y] = 'H';
}
else {
map[A[sum - 2].x][A[sum - 2].y] = 'X';
A[sum - 1].x = A[sum - 2].x + dx[A[sum - 2].dir];
A[sum - 1].y = A[sum - 2].y + dy[A[sum - 2].dir];
A[sum - 1].dir = A[sum - 2].dir;
map[A[sum - 1].x][A[sum - 1].y] = 'H';
food();
}
}
void check () {
int i, j;
x1 = A[sum - 1].x + dx[A[sum - 1].dir];
y2 = A[sum - 1].y + dy[A[sum - 1].dir];
if (map[x1][y2] == '*' || map[x1][y2] == 'X') {
if (x1 != A[0].x || y2 != A[0].y) {
map[8][4] = 'G'; map[8][5] = 'A'; map[8][6] = 'M'; map[8][7] = 'E';
map[8][9] = 'O'; map[8][10] = 'V'; map[8][11] = 'E'; map[8][12] = 'R';
map[8][8] = ' ';
system("cls");
for (i = 0; i < 20; i++) {
for (j = 0; j < 20; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
flag = 1;
}
}
else if (map[x1][y2] == 'o') {
sum++;
}
}
|