赞
踩
SnakeGame.h文件
- #pragma once
- #include<iostream>
- #include<list>
- #include<conio.h>
- #include<Windows.h>
- #include<time.h>
- #include<string>
- using namespace std;
- class SnakeGame
- {
- public:
- SnakeGame();
- virtual ~SnakeGame();
- private:
- int dir;//方向
- string q[4] = { "你真厉害! ",
- "666 (⊙o⊙) ",
- "通关有奖励哦 QWQ",
- "加油 (*>.<*) " };
- int grades;//分数
- COORD coord;//保存下一次前进的点
- list<COORD>Snake;//保存蛇身
- int mp[26][26];//保存地图
- public:
- void CreateMp();//初始化地图
- void SetCOORD(int x, int y);//移动光标
- void Move();//蛇移动
- bool GameOver(int x, int y);//判断蛇是否死亡
- void CreateFood();//创建一个食物
- int ChangeDir(char ch);//将字符转换成数字w:1 a:2 s:3 d:4
- void Mood();//打印表情QWQ
- void SetColor();//设置颜色
- // void Create();
- void CreateBarrier();//第二关
- void SetCustom2();//第三关
- private:
- int Custom;//记录关卡
- public:
- void init();//初始化
- void StartGame();//开始游戏
- };
-
- #include "SnakeGame.h"
-
- SnakeGame::SnakeGame()
- {
-
- }
-
-
- SnakeGame::~SnakeGame()
- {
- }
-
-
- void SnakeGame::CreateMp()
- {
- HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hCon, (12) | (0));//地图设置为红色
-
- for (int i = 0; i < 21; i++)
- {
- mp[i][0] = 1;
- mp[0][i] = 1;
- SetCOORD(0, i);
- cout << "■";
- Sleep(10);
- SetCOORD(i * 2, 0);
- cout << "■";
- Sleep(10);
- }
- for (int i = 0; i < 21; i++)
- {
- mp[20][i] = 1;
- mp[i][20] = 1;
- SetCOORD(20*2, i);
- cout << "■";
- Sleep(10);
- SetCOORD(i * 2, 20);
- cout << "■";
- Sleep(10);
- }
- //CreateBarrier();
- SetCOORD(10 * 2, 10);
- cout << " ";
- SetCOORD(6 * 2, 7);
- cout << "第" << Custom + 1 << "关";
- SetCOORD(6 * 2, 10);
- cout << "请按任意键开始游戏";
- SetCOORD(2, 23);
- cout << "方向控制:";
- SetCOORD(2, 24);
- cout << "上:W 下:S 左:A 右:D";
- SetCOORD(2, 25);
- cout << "鼠标点击屏幕暂停游戏";
- SetCOORD(24*2, 2);
- cout << "你的得分为:" << grades;
- SetCOORD(2, 26);
- cout << "制作:ZZK" << endl;
-
- SetCOORD(24 * 2, 4);
- cout << "游戏图标说明:";
- SetCOORD(24 * 2, 5);
- cout << "障碍:■";
- SetCOORD(24 * 2, 6);
- cout << "食物:★";
- SetCOORD(24 * 2, 11);
- cout << "第" << Custom + 1 << "关";
- SetCOORD(24 * 2, 12);
- cout << "吃到40个五角星进入下一关";
-
- }
-
-
- void SnakeGame::SetCOORD(int x, int y)//设置光标
- {
- COORD xx;//windows.h里的一个结构体,有两个参数 X 和 Y 分别代表列和行
- xx.X = x;
- xx.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xx);//windows.h里的函数--把光标移到xx处
- }
-
-
- void SnakeGame::Move()//移动
- {
- char ch = 'w';
- while (true)
- {
- ch = _getch();//立即从键盘的到一个字符
- while (!_kbhit())//_kbhit()函数没有字符时输入返回0
- {
- int kk = ChangeDir(ch);
- if (kk != dir&&abs(kk - dir) != 2)//判断是否能改变方向
- {
- dir = kk;
- }
- coord = Snake.front();
- switch (dir)//1上2左3下4右
- {
- case 1:coord.Y--; break;
- case 2:coord.X--; break;
- case 3:coord.Y++; break;
- case 4:coord.X++; break;
- }
- if (GameOver(coord.X, coord.Y))//判读蛇是否死掉
- {
- system("cls");
- COORD s;
- s.X = 10 * 2;
- s.Y = 10;
- SetCOORD(s.X, s.Y);
- cout << "游戏结束,你挂了QWQ";
- SetCOORD(s.X, s.Y + 1);
- cout << "你的分数为:" << grades;
- SetCOORD(s.X, s.Y + 2);
- cout << "要重新开始咩?";
- SetCOORD(s.X, s.Y + 3);
- cout << "1:重开就重开 2:算了吧";
- SetCOORD(s.X, s.Y + 4);
- cout << "你的选择:";
- int kkk;
- cin >> kkk;
- if (kkk == 1)
- {
- system("cls");
- init();
- StartGame();
- return;
- }
- return;
- }
- if (mp[coord.Y][coord.X] == 2)//吃到食物
- {
- SetColor();
- grades+=1;
- if (grades == 40)//达到40分进入下一关
- {
- Custom++;
- SetCOORD(24 * 2, 12);
- cout << "恭喜你进入下一关";
- SetCOORD(24 * 2, 13);
- cout << "进入下一关吗? 1:肯定进入啊 2:算了吧,我认怂QWQ";
- SetCOORD(24 * 2, 14);
- cout << "你的选择是:";
- int mm;
- cin >> mm;
- if (mm != 2)
- {
- system("cls");
- SetCOORD(10 * 2, 10);
- cout << "离进入下一关还有3秒";
- Sleep(3000);
- //判断下一关是第几关
- if (Custom == 1)
- {
- init();
- Custom = 1;
- CreateBarrier();
- }
- else if (Custom == 2)
- {
- init();
- Custom = 2;
- SetCustom2();
- }
- else
- {
- system("cls");
- SetCOORD(10 * 2, 10);
- cout << "恭喜你,通关了,你真6";
- return;
- }
- StartGame();
- return;
- }
- else
- {
- SetCOORD(24 * 2, 15);
- cout << "你怎么能这么怂";
- SetCOORD(24 * 2, 16);
- cout << "唉继续游戏吧!";
- }
- }
- SetCOORD(24 * 2, 2);
- cout << "你的得分为:" << grades;
- SetCOORD(coord.X * 2, coord.Y);
- cout << "★";
- mp[coord.Y][coord.X] = 3;
- CreateFood();
- COORD ss;
- ss = Snake.front();
- SetCOORD(ss.X * 2, ss.Y);
- cout << "●";
- Snake.push_front(coord);
- Mood();
-
- }
- else//没吃到食物
- {
- SetColor();
- SetCOORD(coord.X * 2, coord.Y);
- cout << "★";
- mp[coord.Y][coord.X] = 3;
-
- COORD ss;
-
- ss = Snake.front();
- SetCOORD(ss.X * 2, ss.Y);
- cout << "●";
- Snake.push_front(coord);
- ss = Snake.back();
- Snake.pop_back();
- mp[ss.Y][ss.X] = 0;
- SetCOORD(ss.X * 2, ss.Y);
- cout << " ";
- }
- if (grades * 10 == 250)
- {
- Sleep(250);
- }
- else
- Sleep(500-grades*10);
- }
- }
- }
-
-
- bool SnakeGame::GameOver(int x, int y)
- {
- if (mp[y][x] == 1||mp[y][x]==3)
- return true;
- else
- return false;
- }
-
-
- void SnakeGame::CreateFood()
- {
- int x, y;
- do
- {
- x = rand() % 20;
- y = rand() % 20;
- if (mp[y][x] == 0)
- {
- break;
- }
- } while (1);
- mp[y][x] = 2;//食物标记为2
- SetCOORD(x*2 , y);
- SetColor();
- cout << "★";
- }
-
-
- int SnakeGame::ChangeDir(char ch)
- {
- switch (ch)
- {
- case 'w':
- case 'W':return 1;
- case 'a':
- case 'A':return 2;
- case 's':
- case 'S':return 3;
- case 'd':
- case 'D':return 4;
- default:return dir;
- }
- return 0;
- }
-
-
- void SnakeGame::Mood()
- {
- int x = 25, y = 7;
- SetCOORD(x * 2, y);
- int k;
- k = rand() % 4;
- cout << q[k];
-
- }
-
-
- void SnakeGame::SetColor()
- {
- HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
- unsigned short x;
- x = rand() % 15+1;
- SetConsoleTextAttribute(hCon, (x) | (0));
- }
-
-
- //void SnakeGame::Create()
- //{
- //}
-
-
- void SnakeGame::CreateBarrier()
- {
- for (int i = 3; i <= 16; i++)
- {
- SetCOORD(i * 2, 4);
- mp[4][i] = 1;
- cout << "■";
- }
- for (int i = 3; i <= 16; i++)
- {
- SetCOORD(i * 2, 16);
- cout << "■";
- mp[16][i] = 1;
- }
-
- }
-
-
- void SnakeGame::SetCustom2()
- {
- for (int i = 3; i <= 6; i++)
- {
- SetCOORD(i * 2, 3);
- cout << "■";
- mp[3][i] = 1;
- SetCOORD(i * 2, 17);
- cout << "■";
- mp[17][i] = 1;
- SetCOORD(3*2, i);
- cout << "■";
- mp[i][3] = 1;
- SetCOORD(17 * 2, i);
- cout << "■";
- mp[i][17] = 1;
- }
- for (int i = 14; i <= 17; i++)
- {
- SetCOORD(3 * 2, i);
- cout << "■";
- mp[i][3] = 1;
- SetCOORD(17 * 2, i);
- cout << "■";
- mp[i][17] = 1;
- SetCOORD(i * 2, 17);
- cout << "■";
- mp[17][i] = 1;
- SetCOORD(i * 2, 3);
- cout << "■";
- mp[3][i] = 1;
- }
- for (int i = 9; i <= 10; i++)
- {
- SetCOORD(i * 2, 9);
- cout << "■";
- mp[9][i] = 1;
- SetCOORD(i * 2, 11);
- cout << "■";
- mp[11][i] = 1;
- }
- }
-
-
- void SnakeGame::init()
- {
- dir = 1;
- grades = 0;
- Custom = 0;
- Snake.clear();
- memset(mp, 0, sizeof(mp));
- return;
- }
-
-
- void SnakeGame::StartGame()
- {
- grades = 0;
- dir = 1;
- unsigned int t = clock();
- srand(t);
- CreateMp();
- CreateFood();
- dir = 1;//先向上走
- coord.X = 3;
- coord.Y = 7;
- mp[7][3] = 3;
- SetCOORD(coord.X * 2, coord.Y);
- printf("●");
- //system("pause");
- Snake.push_front(coord);
- while (!_kbhit())
- {
- }
- SetCOORD(6 * 2, 7);
- cout << " ";
- SetCOORD(6 * 2, 10);
- cout << " ";
- Move();
- }
主函数
- #include<stdio.h>
- #include"SnakeGame.h"
- int main()
- {
- SnakeGame a;//创建一个贪吃蛇游戏
- a.init();
- a.StartGame();
- system("pause");//系统暂停
- return 0;
- }
程序运行起来是这样子QWQ:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。