game.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>
int main(){
//添加随机种子
srand((unsigned int)time(NULL));
//是否死亡标示
bool isDead = false;
char preKey = NULL;
Wall wall;
wall.initWall();
Food food(wall);
food.setFood();
Snake snake(wall, food);
snake.initSnake();
//snake.move('w');
//snake.move('w');
//snake.move('a');
wall.drawWall();
//接受用户输入
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();
Sleep(100);
}
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;
system("pause");
return EXIT_SUCCESS;
}
wall.h
#ifndef _WALL_HEAD
#define _WALL_HEAD
//#pragma once
#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"
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, '#');
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);
Point * pHead;
Wall & wall;
Food & food;
bool isRool; //判断循环标示
};
snake.cpp
#include "snake.h"
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, '=');
}
newPoint->next = pHead;
pHead = newPoint; //更新头部
wall.setWall(pHead->x, pHead->y, '@');
}
//删除节点
void Snake::delPoint()
{
//两个节点以上 才去做删除操作
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, ' ');
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 << "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, '@');
}
}
return true;
}
本文介绍了一个使用C++实现的贪吃蛇游戏代码,包括游戏的基本逻辑、蛇的移动、食物生成及碰撞检测等功能。游戏利用了墙、蛇和食物三个类,通过键盘输入控制蛇的移动方向,实现了蛇吃食物增长、边界和身体碰撞检测。
660

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



