贪吃蛇(c++版)

这个小游戏是博主一年前写的,大概是2020年1月份左右,现在发在csdn里面记录一下生活,当时c++功底很low,虽然现在也很low,所以写得很简陋,还请各位大佬多多谅解小白

第一步:墙的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||((i == (ROW-1)/2)&&(j>=(COL - 1)/5 && j<= (COL - 1)/1.3))|| ((j == (COL - 1) / 2) && (i >= (ROW - 1) /5 && i <= (ROW - 1) / 1.3))|| ((i == (ROW - 1) /5) && (j >= (COL - 1) / 3 && j <= (COL - 1) /1.5)) || ((i == (ROW - 1) / 1.3) && (j >= (COL - 1) / 5 && j <= (COL - 1) / 1.3)))
            {
                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 << "英文键盘下:d键,w键,s键开始!come  on!";
        }
        if (i == 6) {
            cout << "a键向左";

        }
        if (i == 7) {
            cout << "d键向右";

        } 
        if (i == 8) {
            cout << "w键向上";

        }
        if (i == 11) {
            cout << "制作人:寻梦 ";
        }
        if (i == 9) {
            cout << "s键向下";
          
        }
        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];

}

第二步:墙的头文件代码

#pragma once
#ifndef WALL
#define WALL
#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 "snake.h"
#include<windows.h>

using namespace std;

/*光标定位代码1*/
void gotoxy(HANDLE hOut, int x, int y) {
	COORD c;
	c.X = x;
	c.Y = y;
	SetConsoleCursorPosition(hOut, c);
}
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);



Snake::Snake(Wall&tempwall,Food & tempfood):wall(tempwall),food(tempfood)
{
	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, '=');
		gotoxy( hOut, pHead->y*2, pHead->x);
		cout << "=";
	}
	newpoint->next = pHead;
	pHead = newpoint;

	wall.setwall(pHead->x, pHead->y, '@');
	gotoxy(hOut, pHead->y * 2, pHead->x);
	cout << "@";
}

void Snake::delpoint()
{

	if (pHead == NULL || pHead->next == NULL)
	{
		return;
	}
	Point* pcur = pHead->next;
	Point* pre = pHead;

	while (pcur->next != NULL) {

		pre = pre->next;
		pcur = pcur->next;
	}

	wall.setwall(pcur->x, pcur->y, ' ');
	gotoxy(hOut, pcur->y * 2, pcur->x);
	cout << " ";

		delete pcur;

		pcur = NULL;
		pre->next = NULL;
}

bool Snake::move(char keys)
{
	int x = pHead->x;
	int y = pHead->y; 

	switch (keys)
	{
	case  UP:
		x--;
		break;
	case  DOWN:
		x++;
		break;
	case  RIGHT:
		y++;
		break;
	case  LEFT:
		y--;
		break;
	}

	Point* pcur = pHead->next;
	Point* pre = pHead;

	while (pcur->next != NULL) {

		pre = pre->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;
			if (getscore() <= 300) {
				cout << "再接再厉哦!";
			}
			else if (500 > getscore() >300) {
				cout << "恭喜你呀,超过了百分之四十的人,继续加油!";
			}
			else {
				cout << "你真优秀!"<<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, '@');
			gotoxy(hOut, y * 2, x);
			cout << "@";
		}

	}
	return true;



}

int Snake::getSleeptime()
{
	int sleeptime = 0;
	int size = countlist();
	if (size < 4) {
		sleeptime = 180;
	}
	else if (size >=4 && size <= 15) {
		sleeptime = 160;
	}
	else  if (size > 15 && size <=20) {
		sleeptime = 155;
	}
	else  if (size > 20 && size <=25) {
		sleeptime = 150;
	}
	else  if (size >25 && size <= 40) {
		sleeptime = 145;
	}
	else  if (size >40 && size <= 50) {
		sleeptime = 138;
	}
	else  if(size > 50 && size <= 80) {
		sleeptime = 130;
	}
	else  if (size >80 && size <= 90) {
		sleeptime = 125;
	}
	else  if (size > 90 && size <=100) {
		sleeptime = 120;
	}
	else  if (size > 100 && size <= 120) {
		sleeptime = 110;
	}
	else {
		sleeptime = 100;
	}

	return sleeptime;
}

int Snake::countlist()
{
	int bodylong = 0;
	Point* curpoint = pHead;
	while (curpoint != NULL)
	{
		bodylong++;
		curpoint = curpoint->next;
	}
	return bodylong;
}

int Snake::getscore()
{
	int size = countlist();
	int score =  (size-3) * 10;

	return score;
}


第四步:蛇的头文件代码

#pragma once
#include<iostream>
#include"wall.h"
#include"foods.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 keys);


	int getSleeptime();

	int countlist();
	
	int   getscore();

	Point* pHead;

	Wall& wall;


	Food  & food;

	bool  isrool;
};

第五步:食物的cpp代码

#include"foods.h"
#include<Windows.h>
void gotoxy2(HANDLE hOut2, int x, int y) {
	COORD c;
	c.X = x;
	c.Y = y;
	SetConsoleCursorPosition(hOut2, c);
}
HANDLE hOut2 = GetStdHandle(STD_OUTPUT_HANDLE);

Food::Food(Wall& tempWall):wall(tempWall)
{


}

void Food::setfood()
{
	while (true) {
		xfood = rand() % (Wall::ROW - 2) + 1;
		yfood = rand() % (Wall::COL - 2) + 1;

		if (wall.getwall(xfood, yfood) == ' ')
		{
			wall.setwall(xfood, yfood, '#');
			gotoxy2( hOut2, yfood*2, xfood);
			cout << "#";
			break;
		}

	}

}

第六步:食物的头文件代码

#pragma once
#include"wall.h"
#include<iostream>
using namespace std;

class Food {
public:
	Food(Wall& tempWall);

	void setfood();

	int xfood;
	int yfood;

	Wall& wall;

private:


};

第七步:主函数代码

#define CRT
#include<iostream>
using namespace std;
#include"wall.h"
#include"snake.h"
#include"foods.h"
#include<ctime>
#include<conio.h>
#include<Windows.h>
void gotoxy1(HANDLE hOut1, int x, int y) {
	COORD c;
	c.X = x;
	c.Y = y;
	SetConsoleCursorPosition(hOut1, c);
}
HANDLE hOut1 = GetStdHandle(STD_OUTPUT_HANDLE);

/*光标定位代码2*/
/*void gotoxy(int x,int y){
   COORD c;
   c.X = x - 1;
   c.Y = y - 1;
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 }
 */


int main() {

l:
	char ctn = 'y';
	srand((unsigned int)time(NULL));


	bool isDead = false;



	char prekeys = NULL;

	Wall wall;
	wall.initWall();
	wall.drawwall();


	Food food(wall);
	food.setfood();


	Snake snake(wall, food);
	snake.initSnake();




	gotoxy1(hOut1, 0, Wall::ROW);
	cout << "你的得分为:" << snake.getscore() << "分" << endl;


	while (!isDead) {
		char keys = _getch();
		if (keys == snake.LEFT && prekeys == NULL)
		{
			continue;
		}

		do
		{
			if (keys == snake.UP || keys == snake.DOWN || keys == snake.RIGHT || keys == snake.LEFT)
			{
				if ((keys == snake.LEFT && prekeys == snake.RIGHT) || (keys == snake.RIGHT && prekeys == snake.LEFT)
					|| (keys == snake.DOWN && prekeys == snake.UP) || (keys == snake.UP && prekeys == snake.DOWN))
				{
					keys = prekeys;
				}
				else {
					prekeys = keys;
				}

				if (snake.move(keys) == true)
				{
					//system("cls");
					//wall.drawwall();
					gotoxy1(hOut1, 0, Wall::ROW);
					cout << "你的得分为:" << snake.getscore() << "分"<<endl;
					if (snake.getscore() <=300) {
						cout << "再接再厉哦!" << endl;
					}
					else if (500 >= snake.getscore() > 300) {
						cout << "恭喜你呀,超过了百分之四十的人,继续加油!" << endl;
					}
					else {
						cout << "你真是太优秀了!" << endl;
					}
					Sleep(snake.getSleeptime()); //毫秒
				}
				else {
					isDead = true;
					break;
				}
			}
			else {
				keys = prekeys;
			}


		} while (!_kbhit());//_kbhit当没有键盘输入时返回0;
		
	}
	system("pause");
		system("cls");
		goto l;

	system("pause");


}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寻梦&之璐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值