game.cpp
// 55.tanchi.cpp : 定义控制台应用程序的入口点。
//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include "wall.h"
#include "snake.h"
#include "food.h"
#include <ctime>
#include <conio.h>
#include <Windows.h>
//更新光标位置
void gotoxy(HANDLE hOut, int x, int y)
{
COORD pos;
pos.X = x; //横坐标
pos.Y = y; //纵坐标
SetConsoleCursorPosition(hOut, pos);
}
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量
int main()
{
//添加随机种子
srand((unsigned int)time(NULL));
//是否死亡标示
bool isDead = false;
char preKey = NULL;
Wall wall;
wall.initWall();
wall.drawWall();
Food food(wall);
food.setFood();
Snake snake(wall,food);
snake.initSnake();
/*模拟移动
snake.move('w');
snake.move('w');
snake.move('a');
*/
gotoxy(hOut, 0, Wall::ROW);//定位光标然后画出来
cout << "得分:" << snake.getScore() << "分" << endl;
//持续移动
while (!isDead)
{
char key = _getch();
//判断 如果是第一次按了 左键 才不能激活游戏
// 判断 上一次 移动方向
if (preKey == NULL && key == snake.LEFT)
{
continue;
}
do
{
if (key == snake.UP || key == snake.DOWN || key == snake.LEFT || key == snake.RIGHT)
{
//判断本次按键 是否与上次冲突
if ((key == snake.LEFT && preKey == snake.RIGHT) ||
(key == snake.RIGHT && preKey == snake.LEFT) ||
(key == snake.UP && preKey == snake.DOWN) ||
(key == snake.DOWN && preKey == snake.UP))
{
key = preKey;//冲突按键就执行上一次按键
}
else
{
preKey = key; //不是冲突按键 可以更新按键
}
if (snake.move(key) == true)
{
//移动成功 代码
//system("cls");//清屏操作
//wall.drawWall();
gotoxy(hOut, 0, Wall::ROW);//定位光标然后画出来
cout << "得分:" << snake.getScore() << "分" << endl;
Sleep(snake.getSleepTime());
}
else
{
isDead = true;
break;
}
}
else
{
key = preKey;
}
} while (!_kbhit());//没有键盘输入的时候返回0
}
/*
//测试
wall.setWall(5, 4, '=');
wall.setWall(5, 5, '=');
wall.setWall(5, 6, '@');
cout << wall.getWall(0, 0) << endl;
cout << wall.getWall(5, 4) << endl;
cout << wall.getWall(5, 6) << endl;
cout << wall.getWall(1, 1) << endl;
*/
//wall.drawWall();
system("pause");
return EXIT_SUCCESS;
}
wall.h
#ifndef _WALL_HEAD
#define _WALL_HEAD
#include <iostream>
using namespace std;
class Wall
{
public:
enum {
ROW = 26,
COL = 26
};
//初始化墙壁
void initWall();
//画出墙壁
void drawWall();
//根据索引设置 二维数组里的内容
void setWall(int x, int y, char c);
//根据索引获取当前位置的符号
char getWall(int x, int y);
private:
char gameArray[ROW][COL];
};
#endif
wall.cpp
#include "wall.h"
void Wall::initWall()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
//放置墙壁
if (i == 0 || j == 0 || i == ROW - 1 || j == COL - 1)
{
gameArray[i][j] = '*';
}
else
{
gameArray[i][j] = ' ';
}
}
}
}
void Wall::drawWall()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
cout << gameArray[i][j] << " ";
}
if (i == 5)
{
cout << "create by zt";
}
if (i == 6)
{
cout << "a : left";
}
if (i == 7)
{
cout << "d : right";
}
if (i == 8)
{
cout << "w : up";
}
if (i == 9)
{
cout << "s : down";
}
cout << endl;
}
}
void Wall::setWall(int x, int y, char c)
{
gameArray[x][y] = c;
}
char Wall::getWall(int x, int y)
{
return gameArray[x][y];
}
food.h
#pragma once
#include <iostream>
#include "wall.h"
using namespace std;
class Food
{
public:
Food(Wall & tempWall);
//设置食物
void setFood();
int foodX;
int foodY;
Wall & wall;
};
food.cpp
#include "food.h"
#include <windows.h>
void gotoxy2(HANDLE hOut2, int x, int y)
{
COORD pos;
pos.X = x; //横坐标
pos.Y = y; //纵坐标
SetConsoleCursorPosition(hOut2, pos);
}
HANDLE hOut2 = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量
Food::Food(Wall & tempWall) : wall(tempWall)
{
}
void Food::setFood()
{
while (true)
{
foodX = rand() % (Wall::ROW - 2) + 1;
foodY = rand() % (Wall::COL - 2) + 1;
//如果随机的位置是蛇头或蛇身 就重新生成随机数
if (wall.getWall(foodX, foodY) == ' ')
{
wall.setWall(foodX, foodY, '#');
gotoxy2(hOut2, foodY * 2, foodX);//定位光标然后画出来
cout << "#";
break;
}
}
}
snake.h
#pragma once
#include <iostream>
#include "wall.h"
#include "food.h"
using namespace std;
class Snake
{
public:
Snake(Wall & tempWall, Food & food);
enum
{
UP = 'w', DOWN = 's', LEFT = 'a', RIGHT = 'd'
};
struct Point
{
//数据
int x;
int y;
//指针
Point *next;
};
//初始化蛇
void initSnake();
// 销毁节点
void destroyPoint();
// 添加节点
void addPoint(int x, int y);
// 删除节点
void delPoint();
//移动蛇操作
//返回值代表 移动是否成功
bool move(char key);
//设定难度
//获取刷屏时间
int getSleepTime();
//获取蛇身段
int countList();
//获取分数
int getScore();
//头节点
Point * pHead;
//维护一个墙对象
Wall & wall;
Food & food;
bool isRool; //判断循环标示
};
snake.cpp
#include "snake.h"
#include <windows.h>
void gotoxy1(HANDLE hOut1, int x, int y)
{
COORD pos;
pos.X = x; //横坐标
pos.Y = y; //纵坐标
SetConsoleCursorPosition(hOut1, pos);
}
HANDLE hOut1 = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量
Snake::Snake(Wall & tempWall, Food & tmpFood) : wall(tempWall), food(tmpFood)
{
pHead = NULL;
isRool = false;
}
void Snake::initSnake()
{
destroyPoint();
addPoint(5, 3);
addPoint(5, 4);
addPoint(5, 5);
}
void Snake::destroyPoint()
{
Point * pCur = pHead;
while (pHead != NULL)
{
pCur = pHead->next;
delete pHead;
pHead = pCur;
}
}
void Snake::addPoint(int x, int y)
{
Point * newPoint = new Point;
newPoint->x = x;
newPoint->y = y;
newPoint->next = NULL;
//如果原来头不为空 改为 身子
if (pHead != NULL)
{
wall.setWall(pHead->x, pHead->y, '=');
gotoxy1(hOut1, pHead->y * 2, pHead->x);//定位光标然后画出来
cout << "=";
}
newPoint->next = pHead;//原来的头部后移
pHead = newPoint; //更新头部
wall.setWall(pHead->x, pHead->y, '@');//更新头部的图片
gotoxy1(hOut1, pHead->y * 2, pHead->x);//定位光标然后画出来
cout << "@";
}
void Snake::delPoint()
{
//限制2个节点以上才会删除
if (pHead == NULL || pHead->next == NULL)
{
return;
}
Point * pCur = pHead->next;
Point * pPre = pHead;
//指针后移,直到移动到最后
while (pCur->next != NULL)
{
pPre = pPre->next;
pCur = pCur->next;
}
//删除尾节点
wall.setWall(pCur->x, pCur->y, ' ');
gotoxy1(hOut1, pCur->y * 2, pCur->x);//定位光标然后画出来
cout << " ";
delete pCur;
pCur = NULL;
pPre->next = NULL;
}
bool Snake::move(char key)
{
int x = pHead->x;
int y = pHead->y;
switch (key)
{
case UP:
x--;
break;
case DOWN:
x++;
break;
case LEFT:
y--;
break;
case RIGHT:
y++;
break;
default:
break;
}
//判断 如果是下一步碰到的是尾巴,不应该死亡
Point * pCur = pHead->next;
Point * pPre = pHead;
//指针后移,直到移动到最后
while (pCur->next != NULL)
{
pPre = pPre->next;
pCur = pCur->next;
}
if (pCur->x == x && pCur->y == y)
{
//碰到尾巴
isRool = true;
}
else
{
//判断用户到达位置是否成功
if (wall.getWall(x, y) == '*' || wall.getWall(x, y) == '=')
{
addPoint(x, y);
delPoint();
system("cls");
wall.drawWall();
cout << "得分:" << getScore() << "分" << endl;
cout << "GAME OVER!!!" << endl;
return false;
}
}
//移动成功 分两种
//吃到食物 未吃到食物
if (wall.getWall(x, y) == '#')
{
addPoint(x, y);
//重新设置食物
food.setFood();
}
else//没有吃到食物往前走
{
addPoint(x, y);
delPoint();
if (isRool == true)
{
wall.setWall(x, y, '@');
gotoxy1(hOut1, 2 * y, x);//定位光标然后画出来
cout << "@";
}
}
return true;
}
//刷新的时间
int Snake::getSleepTime()
{
int sleepTime = 0;
int size = countList();
if (size < 5)
{
sleepTime = 300;
}
else if (size >= 5 && size <= 8)
{
sleepTime = 200;
}
else
{
sleepTime = 100;
}
return sleepTime;
}
//长度
int Snake::countList()
{
int size = 0;
Point * curPoint = pHead;
while (curPoint != NULL)
{
size++;
curPoint = curPoint->next;
}
return size;
}
//得分
int Snake::getScore()
{
int size = countList();
int score = (size - 3) * 100;
return score;
}
本文介绍了一个使用C++编程语言实现的经典贪吃蛇游戏。游戏包括控制台界面,蛇、食物和墙的类定义,以及蛇的移动、食物的生成和碰撞检测等关键功能。通过使用Windows API进行光标位置更新,实现了游戏的动态效果。
660

被折叠的 条评论
为什么被折叠?



