精华内容
下载资源
问答
  • C++贪吃蛇小游戏

    2011-11-06 13:43:23
    C++贪吃蛇小游戏,来看看啊 超精简,适合刚上手的人练练手
  • c++贪吃蛇小游戏

    2018-12-19 21:45:01
    代码可完美运行,界面美观颜色多样,有文件记录最高分及排行榜
  • c++ 贪吃蛇小游戏

    2019-09-30 20:48:12
    (地图大小为25*25,蛇初始位置为数组的map[3][3]~map[3][6],蛇头为map[3][6],方向向右) #include <iostream>#include <Windows.h> #include <cstdlib>#include <time.h>...

    (地图大小为25*25,蛇初始位置为数组的map[3][3]~map[3][6],蛇头为map[3][6],方向向右)

    #include <iostream>
    #include <Windows.h>
    #include <cstdlib>
    #include <time.h> 
    #include <conio.h>
    using namespace std;

    #define MAX_WIDE 25
    #define MAX_HIGH 25

    char map[MAX_HIGH][MAX_WIDE];    //地图大小

    struct node
    {
    int x, y;
    };
    node snack[(MAX_WIDE - 2)*(MAX_HIGH - 2)], food;    //定义蛇身和食物坐标

    int lenth, direct, live = 1;                  //蛇的长度,方向和存活
    int speech = 200;

    void initMap(node *n, int len) {              //初始化地图
      for (int i = 0; i < MAX_HIGH; i++)
        for (int j = 0; j < MAX_WIDE; j++)
          map[i][j] = ' ';
      for (int i = 0; i < MAX_HIGH; i++)          //设置左右的墙
        map[i][0] = map[i][MAX_WIDE - 1] = '|';
      for (int j = 0; j < MAX_WIDE; j++)
        map[0][j] = map[MAX_HIGH - 1][j] = '-';     //设置上下的墙
      for (int j = 1; j < len; j++)
        map[n[j].x][n[j].y] = '*';             //设置蛇身
      map[n[0].x][n[0].y] = '#';              //设置蛇头
    }  

     

    void showMap() {                     //输出数组map
      system("cls");                    //清屏
      for (int i = 0; i < MAX_HIGH; i++) {         
        for (int j = 0; j < MAX_WIDE; j++) {
          cout << map[i][j];
        }
        cout << endl;
      }
    }

     

    void showFood() {                    //产生食物,坐标在map中且不为蛇上
      srand((unsigned)time(NULL));            //食物的产生点随机
      do {
        food.x = rand() % MAX_WIDE;
        food.y = rand() % MAX_HIGH;
      } while (map[food.x][food.y] != ' ');
      map[food.x][food.y] = 'o';
    }

    int updataGame() {                   //更新数组
      long start = clock();
      int a, b;                       //暂存 移动后蛇头的位置
      while (!_kbhit() && (clock() - start <= speech));   //直到有按键或者时间过了speech则跳出循环
      if (_kbhit()) {
        _getch();                    //上、下、左、右键是二个字节的,取第二个字节分别为(72,80,75,77)  
        direct = _getch();
      }
      switch (direct)
      {
      case 72:                       //up
        a = snack[0].x - 1;
        b = snack[0].y;
        break;
      case 80:                       //down
        a = snack[0].x + 1;
        b = snack[0].y;
        break;
      case 75:                       //left
        a = snack[0].x;
        b = snack[0].y - 1;
        break;
      case 77:                       //right
        a = snack[0].x;
        b = snack[0].y + 1;
        break;
      default:
        break;
      }
      if (a == 0 || a == MAX_HIGH - 1 || b == 0 || b == MAX_WIDE - 1)    //撞墙
        live = 0;
      if (map[a][b] == '*')                            //撞自身
        live = 0;
      map[snack[0].x][snack[0].y] = '*';                     //将蛇头置为蛇身
      map[snack[lenth - 1].x][snack[lenth - 1].y] = ' ';              //蛇尾置为空
      map[a][b] = '#';                              //更新蛇头
      for (int i = lenth - 1; i > 0; i--)                       //更新蛇数组
        snack[i] = snack[i - 1];
      snack[0].x = a;
      snack[0].y = b;
      if (a == food.x && b == food.y) {                      //蛇吃了食物
        map[snack[lenth - 1].x][snack[lenth - 1].y] = '*';            //取消蛇尾置空
        lenth++;
        showFood();                               //产生新食物
        speech -= lenth;                             //加速
      }
      showMap();
      return live;
    }


    int main() {
      snack[0].x = 3;
      snack[0].y = 6;
      lenth = 4;
      for (int i = 1; i < lenth; i++) {
        snack[i].x = 3;
        snack[i].y = snack[i - 1].y - 1;
      }
      direct = 77;
      initMap(snack, lenth);
      showFood();
      showMap();
      do {
        updataGame();
      } while (live);
      cout << "得分为:" << lenth-4 << endl;
      system("pause");
      return 0;
    }

    转载于:https://www.cnblogs.com/Mrtree/p/6720553.html

    展开全文
  • C++ 贪吃蛇 小游戏

    2019-02-06 12:41:19
    #define _CRT_SECURE_NO_WARNINGS #include&amp;lt;iostream&amp;gt; #include&amp;lt;string&amp;gt; #include&amp;lt;time.h&amp;gt; ...#define M 28//方框大小 必须是
    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    #include<string>
    #include<time.h>
    #include<conio.h>
    #include <windows.h>
    #define M 28//方框大小 必须是方的
    #define N 28
    #define TIME 200//刷新时间 单位:毫秒
    int flag = 0;//用于判断是否删除尾节点
    typedef struct Body//蛇的身体
    {
    	int x;
    	int y;
    	struct Body *pnext;
    }body, *pbody;
    
    pbody phead = NULL;
    pbody ptail = NULL;
    
    void insert_head(pbody *pphead, pbody *pptail, int x_new, int y_new)//头插法
    {
    	pbody pnew;
    	pnew = (pbody)calloc(1, sizeof(body));
    	pnew->x = x_new;
    	pnew->y = y_new;
    	if (NULL == *pptail)//如果链表为空
    	{
    		*pphead = pnew;
    		*pptail = pnew;//不用担心尾节点不是NULL,因为calloc的pnew中的pnext成员本身就是null
    	}
    	else
    	{
    		(*pptail)->pnext = pnew;//原有链表尾的pnext成员指向新节点
    		*pptail = pnew;//新节点成为新的链表尾
    	}
    }
    //删除
    void delete_tail(pbody *pphead, pbody *pptail)
    {
    	pbody pbefore = *pphead;
    	if (NULL == *pptail)//如果链表为空
    	{
    		printf("链表为空\n");
    		system("pause");
    	}
    	*pphead = (*pphead)->pnext;
    	free(pbefore);//?对吗
    }
    
    class Snake
    {
    public:
    	std::string name;
    	int length;
    	int head_locate[M][N] = { 0 };//运行过程中,保证整个范围内只有一个坐标为1
    
    	int m, n;//用来记录当前为1的位置
    	char direction;//默认一开始向U走
    	Snake(std::string name);//构造器
    	~Snake();//析构器
    	void snake_go(Snake snake);
    };
    
    class Apple
    {
    public:
    	int apple_locate[M][M] = { 0 };
    	Apple();
    	void apple_change_location();
    };
    
    class Printer
    {
    public:
    	int p[M][N] = { 0 };
    	void print_coder();
    	void print_user();
    	void synchronize(Snake *snake, Apple *apple);
    };
    
    //构造器
    Snake::Snake(std::string name)//传入player1
    {
    	this->name = name;
    	this->length = 0;
    	this->head_locate[M / 2][N / 2] = 1;
    	this->m = M / 2;
    	this->n = N / 2;
    	this->direction = 'L';
    }
    
    Apple::Apple()
    {
    	apple_change_location();
    }
    
    //析构器
    Snake::~Snake()
    {
    	//	std::cout << "对象已被删除" << std::endl;
    }
    
    //函数
    int rand_number()
    {
    	int num;
    	num = rand() % (M - 2);
    	return num + 1;
    }
    
    char keyboard()
    {
    	char key;
    	int ch1 = 0;
    	int ch2 = 0;
    	if (ch1 = _getch())//蜜汁下划线
    	{
    		ch2 = _getch();//第一次调用getch(),返回值224
    		switch (ch2)//第二次调用getch()
    		{
    		case 72: key = 'U'; break;
    		case 80: key = 'D'; break;
    		case 75: key = 'L'; break;
    		case 77: key = 'R'; break;
    		}
    	}
    	return key;
    }
    
    void Snake::snake_go(Snake snake)
    {
    
    	if (snake.direction == 'U')//上
    	{
    		this->head_locate[this->m][this->n] = 0;
    		m--;
    		if (m < 0)
    		{
    			std::cout << "\n\n你碰壁了!\n\n";
    			system("pause");
    		}
    		this->head_locate[this->m][this->n] = 1;
    		insert_head(&phead, &ptail, this->m, this->n);//头插法
    	}
    	else if (snake.direction == 'D')//下
    	{
    		this->head_locate[this->m][this->n] = 0;
    		m++;
    		if (m >= M)
    		{
    			std::cout << "\n\n你碰壁了!\n\n";
    			system("pause");
    		}
    		this->head_locate[this->m][this->n] = 1;
    		insert_head(&phead, &ptail, this->m, this->n);//头插法
    	}
    	else if (snake.direction == 'L')//左
    	{
    		this->head_locate[this->m][this->n] = 0;
    		n--;
    		if (n < 0)
    		{
    			std::cout << "\n\n你碰壁了!\n\n";
    			system("pause");
    		}
    		this->head_locate[this->m][this->n] = 1;
    		insert_head(&phead, &ptail, this->m, this->n);//头插法
    	}
    	else if (snake.direction == 'R')//右
    	{
    		this->head_locate[this->m][this->n] = 0;
    		n++;
    		if (n >= N)
    		{
    			std::cout << "\n\n你碰壁了!\n\n";
    			system("pause");
    		}
    		this->head_locate[this->m][this->n] = 1;
    		insert_head(&phead, &ptail, this->m, this->n);//头插法
    	}
    	if (flag == 0 && phead->pnext != NULL)delete_tail(&phead, &ptail);//删除一个尾节点
    	system("cls");//放在主函数里的话看不到最后结果
    }
    
    void Apple::apple_change_location()
    {
    	//找到两个坐标
    	int x, y;
    	x = rand_number();//产生随机数 
    	y = rand_number();//产生随机数
    	this->apple_locate[x][y] = 1;
    }
    
    void Printer::synchronize(Snake *snake, Apple *apple)
    {
    	int m, n;
    	//先清零
    	for (m = 0; m < M; m++)
    	{
    		for (n = 0; n < N; n++)
    		{
    			this->p[m][n] = 0;
    		}
    	}
    	//snake同步
    	pbody pcur = phead;
    	while (pcur != NULL)
    	{
    		this->p[pcur->x][pcur->y] = 1;
    		pcur = pcur->pnext;
    	}
    	//apple同步
    	for (m = 0; m < M; m++)
    	{
    		for (n = 0; n < N; n++)
    		{
    			if (apple->apple_locate[m][n] == 1)
    			{
    				this->p[m][n] = this->p[m][n] + 2;
    				if (this->p[m][n] == 3)
    				{
    					snake->length++;
    					apple->apple_locate[m][n] = 0;
    					apple->apple_change_location();
    					flag = 1;
    				}
    			}
    		}
    	}
    	//同步结果p[]:1蛇头 2苹果 3吃了
    }
    
    void Printer::print_coder()
    {
    	int m, n;
    	for (m = 0; m < M; m++)
    	{
    		for (n = 0; n < N; n++)
    		{
    			std::cout << this->p[m][n] << ' ';
    		}
    		std::cout << std::endl;
    	}
    }
    
    void Printer::print_user()
    {
    	int m, n;
    	for (m = 0; m < M; m++)
    	{
    		for (n = 0; n < N; n++)
    		{
    			if (this->p[m][n] != 0)
    			{
    				std::cout << this->p[m][n];
    				std::cout << " ";
    			}
    			else if (n == 0 || n == N - 1)//横
    			{
    				std::cout << "*";
    				std::cout << " ";
    			}
    			else if (m == 0 || m == M - 1)//竖
    			{
    				std::cout << "*";
    				std::cout << " ";
    			}
    			else std::cout << "  ";
    		}
    		std::cout << std::endl;
    	}
    }
    
    
    int main()
    {
    	srand((int)time(NULL));//产生随机数的种子
    	char exitflag = '\0';
    	Snake snake("player1");
    	Apple apple;
    	Printer printer;
    
    	//开始
    	printer.synchronize(&snake, &apple);
    	printer.print_user();
    	//测试
    	for (int i = 1; ; i++)
    	{
    		if (_kbhit())
    		{
    			// 函数名: getch()
    			// 功能及返回值: 从键盘上读取到的字符
    			exitflag = _getch();
    			if (exitflag == 72 || exitflag == 80 || exitflag == 75 || exitflag == 77)
    			{
    				switch (exitflag)
    				{
    				case 72: snake.direction = 'U'; break;
    				case 80: snake.direction = 'D'; break;
    				case 75: snake.direction = 'L'; break;
    				case 77: snake.direction = 'R'; break;
    				default:std::cout << "输入错误\n"; break;
    				}
    			}
    		}
    		snake.snake_go(snake);//走完看flag,如果是0就删
    		flag = 0;
    		printer.synchronize(&snake, &apple);//同步完改flag,如果吃了,flag改成1
    		printer.print_user();
    
    		Sleep(TIME);
    	}
    	system("pause");
    }
    
    展开全文
  • C++ 贪吃蛇小游戏

    2019-02-15 20:13:00
    。。 转载于:https://www.cnblogs.com/HonkerYblogs/p/10385687.html

    。。

    转载于:https://www.cnblogs.com/HonkerYblogs/p/10385687.html

    展开全文
  • c++贪吃蛇小游戏,复制即可在c++上运行,可用来玩和参考学习
  • DevC++贪吃蛇小游戏

    2020-11-01 20:06:35
    自己用DevC++编写的C++控制台贪吃蛇小游戏,带有难度选择,可以选择4个难度,还有计时、加速、暂停功能。需要的可以参考。
  • C++贪吃蛇小游戏-源码

    2021-04-11 19:06:51
    c++编写的一个贪吃蛇小游戏
  • 我的思路是用数组把游戏每时每刻的状态记录下来(记录蛇身,食物及围墙的位置)。 先写3个类3个类的功能如下 1class weiqiang//围墙类 ,这个类中的数据包括一个二维数组(这个数组要记录蛇身,食物,及围墙的位置...
  • C++ 贪吃蛇小游戏(CLI)

    2020-12-15 23:45:55
    我大二的时候用c写过一个跑出五格就越界的贪吃蛇,现在看来简直是一堆垃圾。 我现在用cplusplus重写它,那就叫它垃圾plusplus好了(狗头)。 设计思路 贪吃蛇行为过程: 1.蛇吃果子 2.蛇以speed运动 3.蛇在ground上...

    前言

    我大二的时候用c写过一个跑出五格就越界的贪吃蛇,现在看来简直是一堆垃圾。
    我现在用cplusplus重写它,那就叫它垃圾plusplus好了(狗头)。

    设计思路

    贪吃蛇行为过程:
    1.蛇吃果子
    2.蛇以speed运动
    3.蛇在ground上运动
    4.果子以frquency为间隔出现
    5.果子出现在在ground上
    6.蛇的头撞到了空气和果子之外的物体会死

    实现过程

    backg.h文件:

    #include<vector>
    #include<windows.h>
    #include<iostream>
    #include<conio.h>
    #include<queue>
    #include<thread>
    #include<random>
    #include<time.h>
    class backg {
    public:
    	std::vector<std::vector<int>> ground;
    	HANDLE hout;
    	backg();
    	void jump(int x,int y);
    	void plant();
    	void startplant();
    };
    

    backg.c文件:

    #include<backg.h>
    backg::backg()
    {
    	int m=29;/*高*/int n=91;/*长*/
    	std::vector<std::vector<int>> t(m, std::vector<int>(n));
    	ground = t;
    	hout = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
    	SetConsoleCursorInfo(hout, &cursor_info);
    	int i = 0;
    	while (i < m || i < n)
    	{
    		if (i < m) 
    		{ ground[i][0] = ground[i][n - 1] = 1; jump(0, i); std::cout << '|'; jump(n - 1, i); std::cout << '|'; }
    		if (i < n) 
    		{ ground[0][i] = ground[m - 1][i] = 1; jump(i, 0); std::cout << '-'; jump(i, m - 1); std::cout << '-'; }
    		i++;
    	}
    }
    void backg::jump(int x, int y)
    {
    	COORD coord;
    	coord.X = x;
    	coord.Y = y;
    	SetConsoleCursorPosition(hout, coord);
    }
    void backg::plant()
    {
    	srand(time(NULL));
    	while (1)
    	{
    		int n = rand() % (89 * 27);
    		int i = 1; int j = 1;
    		while (1)
    		{
    			if (ground[i][j] == 0)
    				n--; if (n == 0)break;
    			i++; j++;
    			if (j == 90)j = 1;
    			if (i == 28)i = 1;
    		}
    		ground[i][j] = 2; jump(j, i); std::cout << '$';
    		Sleep(4000);
    	}
    }
    void backg::startplant()
    {
    	std::thread t2(&backg::plant, this);
    	t2.detach(); 
    }
    
    

    snake.h文件

    #include<backg.h>
    class snake :public backg {
    public:
    	std::queue<std::vector<int>> body;
    	char dir;
    	double speed;
    	snake();
    	void walk();
    	void startwalk();
    };
    

    snake.cpp文件

    #include<snake.h>
    snake::snake()
    {
    	dir = 77;
    	std::vector<int> t(2);
    	std::queue<std::vector<int>> b;
    	t[0] = ground.size() / 2; t[1] = ground[0].size() / 2; 
    	for (int i = 0; i < 4; i++)
    	{	
    		ground[t[0]][t[1]] = 1;
    		jump(t[1], t[0]); 
    		if (i != 3)
    			std::cout << '*'; 
    		else std::cout << '@'; b.push(t);
    		t[1]++;
    	}
    	body = b;
    	speed = 0.3;
    	jump(91, 4);
    	std::cout << "speed:" << speed;
    }
    void snake::walk()
    {
    	std::vector<int> t;
    	while (1)
    	{
    		t = body.back(); jump(t[1], t[0]); std::cout << '*';
    		if (dir == 72)
    		{
    			t[0]--; body.push(t); jump(t[1], t[0]); std::cout << '@';
    		}else
    		if (dir == 80)
    		{
    			t[0]++; body.push(t); jump(t[1], t[0]); std::cout << '@';
    		}else
    		if (dir == 75)
    		{
    			t[1]--; body.push(t); jump(t[1], t[0]); std::cout << '@';
    		}else
    		if (dir == 77)
    		{
    			t[1]++; body.push(t); jump(t[1], t[0]); std::cout << '@';
    		}
    		if (ground[t[0]][t[1]] == 2)
    		{
    			ground[t[0]][t[1]] = 1;		
    			if (body.size() % 10 == 0) 
    			{ jump(91, 4); std::cout << "                 ";			jump(91, 4); speed -= 0.001; }
    			std::cout << "speed:" << speed;
    		}
    		else
    		{
    			if (ground[t[0]][t[1]])//error
    				break;
    			else {
    				ground[t[0]][t[1]] = 1;
    				t = body.front();
    				jump(t[1], t[0]); std::cout << ' ';
    				ground[t[0]][t[1]] = 0;
    				body.pop();
    			}
    		}
    		jump(91, 3);
    		std::cout << "                 "; jump(91, 3);
    		std::cout << "score:" << body.size();
    
    		Sleep(speed * 1000);
    	}
    	jump(91, 4);
    	std::cout << "                                       ";
    	jump(91, 4);
    	std::cout << "byebye";
    }
    void snake::startwalk()
    {
    	std::thread t1(&snake::walk, this);
    	t1.detach();
    }
    int main()
    {
    	snake a;
    	//a.walk();
    	int b;
    	int i = 0;
    	a.startwalk();
    	Sleep(2000);
    	a.startplant();
    	while (1)
    	{
    		b = _getch();
    		if ((a.dir == 72 || a.dir == 80) && (b == 75 || b == 77))a.dir = b;
    		else if ((a.dir == 75 || a.dir == 77) && (b == 72 || b == 80)) a.dir = b;;
    		b = 0;
    	}
    }
    

    运行测试

    在这里插入图片描述在这里插入图片描述

    展开全文
  • #include<cstdio> #include<cstring> #include<cstdlib> #include<ctime> #include<conio.h> #include<windows.h> #include<iostream>...const int d...
  • 游戏结束 按1退出 " ; char uuu; while (uuu!= ' 1 ' ) uuu = getch(); system( " cls " ); if (s> d) { cout " 破记录了 " ; std::ofstream outFile; outFile.open( " D:\\max.data " ); outFile ...
  • c++类 创建头文件 #include"coordinate.h" struct coord//坐标 { int x; int y; }; 1、蛇的处理 创建头文件 #include"snake.h" #pragma once//防止头文件包含 #include #include<graphics.h>//图形库函数 #...
  • C++贪吃蛇小游戏(源码+思路)

    千次阅读 多人点赞 2019-07-07 19:46:08
    2、定义贪吃蛇:数组 SData arrSnake[15] ={}; //蛇头。蛇头坐标初始化。 arrSnake[0] = SData(1,1); 当前蛇的长度:int nLength = 1; 3、蛇的移动。自己动 定义一个蛇的移动方向 nDir = E_DIR_NONE //按键...
  • 基于QT5.4的C++贪吃蛇小游戏

    千次阅读 2018-10-02 11:18:16
    游戏核心代码: void myWidget::timerEvent(QTimerEvent *e){//定时器 if(e-&gt;timerId() == timerId &amp;&amp; gamePause){ if(direction == s_right){ goRight(); }else if(directio...
  • 贪吃蛇我们都玩过,就是小蛇去吃到食物 在写代码之前需要下载(下载easyx链接: https://easyx.cn/. 具体思路: 定义一个坐标结构体(用来储存食物和蛇结构的坐标),当然了也要了解c++程序中的 坐标轴是怎样的我我画...
  • C++贪吃蛇小游戏
  • c++ 贪吃蛇 代码

    2010-10-24 13:07:41
    c++ 贪吃蛇小游戏 源代码 c++ 贪吃蛇小游戏 源代码 c++ 贪吃蛇小游戏 源代码 c++ 贪吃蛇小游戏 源代码
  • 一个用C++写的控制台贪吃蛇小游戏,可以在游戏的开始生成随机的地图,有计分功能,基本实现了贪吃蛇应有的功能,只是还有一些bug。注意用VS2015来打开该文件。

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 534
精华内容 213
关键字:

c++贪吃蛇小游戏

c++ 订阅