学习了一年C语言和C++,一直感觉学了这么长时间,也没能做出一些什么来。这个暑假,呆在家,看看书,上网找找资料,今天略感觉无聊,决定用这一年来学到的东西做一个贪吃蛇。虽然只是控制台项目,虽然反应有点迟钝,虽然有很多漏洞,但是对于我这个小菜鸟来说,已经很有成就感了,最起码能对得起我今天一天的努力。
<global_variable.h>全局变量文件:
const int MAXN_i = 10; //定义边框的大小
const int MAXN_j = 20;
int heart_i,heart_j;
char map[MAXN_i][MAXN_j];
<heart.h>文件:
#include <ctime> #include <cstring> using namespace std; const char Heart = 3;//ASCII值3为心形 void CreatHeart () {//随机产生心形 srand((unsigned)time(NULL)); do { heart_i = rand() % (MAXN_i - 2) + 1; //产生各个随机数 heart_j = rand() % (MAXN_j - 2) + 1; }while (map[heart_i][heart_j]); map[heart_i][heart_j] = Heart; } void HeartInit()//初始化 {//创建边框 memset(map, 0, sizeof(map)); for (int i = 1; i < MAXN_i - 1; i++) { map[i][0] = map[i][MAXN_j - 1] = '|'; } for (int i = 1; i < MAXN_j - 1; i++) { map[0][i] = map[MAXN_i - 1][i] = '-'; } CreatHeart (); }
<snake.h>文件
#include <list> #include <conio.h> using namespace std; const char SnakeHead = 2; const char SnakeBody = 1; typedef struct{//方向 int x; int y; }direction; typedef struct { int snake_i; int snake_j; }Snake; typedef struct { Snake head; Snake tail; direction dir; }snake; snake s; list <Snake> LS; Snake SChange;//保存修改的 void SnakeInit () { s.dir.x = 0; s.dir.y = 1; s.head.snake_i = MAXN_i / 2; s.head.snake_j = MAXN_j / 2; s.tail.snake_i = MAXN_i / 2; s.tail.snake_j = MAXN_j / 2 - 1; map[s.head.snake_i][s.head.snake_j] = SnakeHead; map[s.tail.snake_i][s.tail.snake_j] = SnakeBody; LS.insert(LS.end(),s.head); LS.insert(LS.end(),s.tail); } void Scan() { char ch = getch(); switch (ch) { case 72: { if(!(s.dir.x == 1 && s.dir.y == 0)) { s.dir.x = -1; s.dir.y = 0; } break; } case 75: { if(!(s.dir.x == 0 && s.dir.y == 1)) { s.dir.x = 0; s.dir.y = -1; } break; } case 80: { if(!(s.dir.x == -1 && s.dir.y == 0)) { s.dir.x = 1; s.dir.y = 0; } break; } case 77: { if(!(s.dir.x == 0 && s.dir.y == -1)) { s.dir.x = 0; s.dir.y = 1; } break; } } }
<difficulty_value.h>难易程度的控制文件:
const int TimeDelay = 100000000; void delay()//延时 { int i = 0; while (i++ < TimeDelay); }
<plan.h>:
#include <stdlib.h> #include "global_variable.h" #include "difficulty_value.h" #include "snake.h" #include "heart.h" using namespace std; void Init () { SnakeInit (); HeartInit(); SChange = s.head; } void Print () { for (int i = 0; i < MAXN_i; i++) { for (int j = 0; j < MAXN_j; j++) { cout<<map[i][j]; } cout<<endl; } } void clean() { delay (); system ("cls"); } bool update () {//这段代码最纠结,小蛇的行动 map[LS.begin()->snake_i][LS.begin()->snake_j] = SnakeBody; if (!map[LS.begin()->snake_i + s.dir.x][LS.begin()->snake_j + s.dir.y])//什么都没遇上 { map[LS.begin()->snake_i + s.dir.x][LS.begin()->snake_j + s.dir.y] = SnakeHead; Snake S; S.snake_i = LS.begin()->snake_i + s.dir.x; S.snake_j = LS.begin()->snake_j + s.dir.y; LS.push_front(S); map[LS.back().snake_i][LS.back().snake_j] = 0; LS.pop_back(); return true; } else if (map[LS.begin()->snake_i + s.dir.x][LS.begin()->snake_j + s.dir.y] == Heart)//遇到心 { CreatHeart (); map[LS.begin()->snake_i + s.dir.x][LS.begin()->snake_j + s.dir.y] = SnakeHead; Snake S; S.snake_i = LS.begin()->snake_i + s.dir.x; S.snake_j = LS.begin()->snake_j + s.dir.y; LS.push_front(S); map[LS.back().snake_i][LS.back().snake_j] = 0; return true; } else { return false; } }
主函数
#include <iostream> #include "plan.h" using namespace std; int main() { Init (); Print (); bool OK = true; while(OK) { while (!_kbhit() && OK) { clean (); OK = update (); Print (); } Scan (); } cout<<"游戏结束..."<<endl; return 0; }