使用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;
# 2、使用步骤
## 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
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(); }
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; }
二、源代码
#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; }
利用学会的知识做点小游戏
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)