当前位置:   article > 正文

C语言实现贪吃蛇小游戏_c语言贪吃蛇游戏代码

c语言贪吃蛇游戏代码

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

控制台的欢乐就是这么简单;


提示:以下是本篇文章正文内容,下面案例可供参考

一、贪吃蛇实现的结构和方式

1.用枚举定义蛇的移动方向
enum Dir
{
	UP,
	DOWN,
	LEFT,
	RIGHT,//枚举不能用分号;
};
//创建结构体,对蛇的参数进行设置;
struct Snake
{
	int size;//蛇的节数;
	int dir;//蛇的方向;
	int speed;//蛇的移动速度;
	//用数组来表示蛇的坐标;
	POINT coor[SNAKE_SIZE];//蛇的最大节数;
}snake;
//食物的结构体;
struct Food
{
	int x;
	int y;
	int r;   //食物半径;
	bool flag;//用来判断食物是否被吃;
	DWORD color;//食物颜色;
}food;

# 二、使用步骤
## 1.引入库
代码如下(示例):

```c
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

2.对窗口进行设置;

代码如下(示例):

void GameDraw()
{
	//双缓冲绘图;
	BeginBatchDraw();
	//设置背景颜色;
	setbkcolor(RGB(28, 115, 119));
	cleardevice();
	//绘制蛇;
	setfillcolor(GREEN);//颜色的改变;
	for (int i = 0; i < snake.size; i++)
	{
		solidcircle(snake.coor[i].x, snake.coor[i].y, 5);//solidcircle函数可以用来描绘无边框填充函数;
	}
	//绘制食物;
	if (food.flag)
	{
		solidcircle(food.x, food.y, food.r);//solidcircle代表画圆;
	}
	EndBatchDraw();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3.对蛇进行初始化;

void GameInit()
{
	//播放背景音乐;
	mciSendString("open./mp3.music alias BGM", 0, 0, 0);
	mciSendString("play BGM repeat", 0, 0, 0);
	initgraph(640, 480 /*SHOWCONSOLE*/);
	//设计随机数种子;
	srand(GetTickCount());//GetTickCount()获取开机到现在的毫秒数;
	snake.size = 3;
	snake.speed = 10;
	snake.dir = RIGHT;
	for (int i = 0; i <= snake.size - 1; i++)
	{
		snake.coor[i].x = 10 * (2 - i) + 20;//向右偏移;
		snake.coor[i].y = 10;//确保蛇在同一水品线;
	}
	//食物的初始化;随机产生一个整数;设置随机种子,头文件是stdlib.h;
	food.x = rand() % 640;
	food.y = rand() % 480;
	food.color = RGB(rand() % 256, rand() % 256, rand() % 256);//颜色值为0到256;
	food.r = rand() % 10 + 5;
	food.flag = true;
}




  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

一、源代码

#include<stdio.h>
#include<conio.h>
#include<graphics.h>//不是库函数;
#include<stdlib.h>//随机数的产生;
#include<mmsystem.h>//导入背景音乐;
#pragma comment(lib,"winmm.lib")//导入背景音乐;
//结构体对snake初始化.
#define SNAKE_SIZE 500
/*typedef struct tagpoint
{
	LONG x;
	LONG y;
}POINT;*/
//用枚举表示蛇的方向;
enum Dir
{
	UP,
	DOWN,
	LEFT,
	RIGHT,//枚举不能用分号;
};
//创建结构体,对蛇的参数进行设置;
struct Snake
{
	int size;//蛇的节数;
	int dir;//蛇的方向;
	int speed;//蛇的移动速度;
	//用数组来表示蛇的坐标;
	POINT coor[SNAKE_SIZE];//蛇的最大节数;
}snake;
//食物的结构体;
struct Food
{
	int x;
	int y;
	int r;   //食物半径;
	bool flag;//用来判断食物是否被吃;
	DWORD color;//食物颜色;
}food;
//数据的初始化;
/*void GameInit()
{
	//初始化graph图形窗口,SHOWCONSOLE控制台;
	initgraph(640, 480, SHOWCONSOLE);
	snake.size = 0;
	snake.speed = 10;
	snake.dir;
	snake.coor[0].x =10;
	snake.coor[0].y = 10;
}*/
//数据的初始化;
void GameInit()
{
	//播放背景音乐;
	mciSendString("open./mp3.music alias BGM", 0, 0, 0);
	mciSendString("play BGM repeat", 0, 0, 0);
	initgraph(640, 480 /*SHOWCONSOLE*/);
	//设计随机数种子;
	srand(GetTickCount());//GetTickCount()获取开机到现在的毫秒数;
	snake.size = 3;
	snake.speed = 10;
	snake.dir = RIGHT;
	for (int i = 0; i <= snake.size - 1; i++)
	{
		snake.coor[i].x = 10 * (2 - i) + 20;//向右偏移;
		snake.coor[i].y = 10;//确保蛇在同一水品线;
	}
	//食物的初始化;随机产生一个整数;设置随机种子,头文件是stdlib.h;
	food.x = rand() % 640;
	food.y = rand() % 480;
	food.color = RGB(rand() % 256, rand() % 256, rand() % 256);//颜色值为0到256;
	food.r = rand() % 10 + 5;
	food.flag = true;
}
//绘制;
void GameDraw()
{
	//双缓冲绘图;
	BeginBatchDraw();
	//设置背景颜色;
	setbkcolor(RGB(28, 115, 119));
	cleardevice();
	//绘制蛇;
	setfillcolor(GREEN);//颜色的改变;
	for (int i = 0; i < snake.size; i++)
	{
		solidcircle(snake.coor[i].x, snake.coor[i].y, 5);//solidcircle函数可以用来描绘无边框填充函数;
	}
	//绘制食物;
	if (food.flag)
	{
		solidcircle(food.x, food.y, food.r);//solidcircle代表画圆;
	}
	EndBatchDraw();
}
//蛇的移动;
//坐标的改变;
void snakemove()
{
	//让头部后面的跟着头走;
	for (int i = snake.size - 1; i > 0; i--)
	{
		snake.coor[i] = snake.coor[i - 1];
	}
	switch (snake.dir)
	{
	case RIGHT:
		snake.coor[0].x += snake.speed;
		if (snake.coor[0].x - 10 >= 640)
		{
			snake.coor[0].x = 0;
		}
		break;
	case LEFT:
		snake.coor[0].x -= snake.speed;
		if (snake.coor[0].x + 10 <= 0)
		{
			snake.coor[0].x = 640;
		}
		break;
	case UP:
		snake.coor[0].y -= snake.speed;
		if (snake.coor[0].y + 10 <= 0)
		{
			snake.coor[0].y = 480;
		}
		break;
	case DOWN:
		snake.coor[0].y += snake.speed;
		if (snake.coor[0].y - 10 >= 480)
		{
			snake.coor[0].y = 0;
		}
		break;
	default:
		break;
	}
}
//通过按键改变蛇的方向;
void KeyControl()
{
	//判断有没有按键;
	if (_kbhit())//检测有没有按键,如果有就返回真,否则返回假;
	{
		//72 80 75 77上下左右键值;
		switch (_getch())
		{
		case 'w':
		case 'W':
		case 72:
			if (snake.dir != DOWN)
			{
				snake.dir = UP;
			}
			break;
		case 's':
		case 'S':
		case 80:
			if (snake.dir != UP)
			{
				snake.dir = DOWN;
			}
			break;
		case 'a':
		case 'A':
		case 75:
			if (snake.dir != RIGHT)
			{
				snake.dir = LEFT;
			}
			break;
		case 'd':
		case 'D':
		case 77:
			if (snake.dir != LEFT)
			{
				snake.dir = RIGHT;
			}
			break;
		case ' ':
			while (1)
			{
				if (_getch() == ' ');
				return;
			}
			break;
		}
	}
}
//吃食物;
void EatFood()
{
	if (food.flag && snake.coor[0].x >= food.x - food.r && snake.coor[0].x <= food.x + food.r && snake.coor[0].y <= food.y + food.r && snake.coor[0].y >= food.y - food.r)
	{
		snake.size++;
		food.flag == false;
	}
	if (!food.flag)
	{
		food.x = rand() % 640;
		food.y = rand() % 480;
		food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
		food.r = rand() % 10 + 5;
		food.flag = true;
	}
}

int main()
{
	//init初始化graph ;
	initgraph(640, 480);
	//设置背景颜色;
	setbkcolor(RGB(28, 115, 119));//设置背景绘图颜色;
	cleardevice();
	GameInit();
	while (1)
	{
		snakemove();
		GameDraw();
		KeyControl();
		EatFood();
		Sleep(80);//时间延迟;
	}
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225

利用学会的知识做点小游戏

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/493560
推荐阅读
相关标签
  

闽ICP备14008679号