刚刚学完C语言想着写个小项目巩固一下基础知识,为下一步学习打下坚实的基础,然后开始在各大网站博客寻找能做的项目,最终选择了贪吃蛇小游戏,大部分都是调用的库函数,没有用到链表,指针等较复杂的知识点。
直接上代码!
#define _CRT_SECURE_NO_WARNINGS //防止编译出错
#include<stdio.h>
#include<graphics.h> //需按照Easy图形库
#include<conio.h>
#include<time.h>
#include<stdlib.h>
typedef struct point
{
int x;
int y;
}mypoint;
//蛇的属性
struct Snake
{
mypoint xy[100]; //蛇身最长100节
int num; //蛇的长度
char position; //方向
}snake;
//食物的属性
struct Food
{
mypoint fdxy;
int flag;
int grade;
}food;
//窗口句柄
HWND hwnd = NULL;
//方向枚举
enum position{up,down,left,right};
//初始化蛇
void initSnake()
{
snake.xy[2].x = 0;
snake.xy[2].y = 0;
snake.xy[1].x = 10;
snake.xy[1].y = 0;
snake.xy[0].x = 20; //初始化坐标
snake.xy[0].y = 0;
snake.num = 3; //初始化长度
snake.position = right; //初始化方向
}
//绘制蛇
void drawSnake()
{
for (int i = 0; i < snake.num; i++)
{
setlinecolor(BLUE); //矩形边框颜色
setfillcolor(RED); //矩形方块颜色
fillrectangle(snake.xy[i].x, snake.xy[i].y, snake.xy[i].x + 10, snake.xy[i].y + 10);//确定左上角和右下角的坐标绘制矩形
}
}
//移动蛇
void moveSnake()
{
for (int i = snake.num - 1; i > 0; i--) //前后位置互换实现移动
{
snake.xy[i].x = snake.xy[i-1].x;
snake.xy[i].y = snake.xy[i - 1].y;
}
switch (snake.position) //移动方向设定
{
case up:
snake.xy[0].y -= 10;
break;
case down:
snake.xy[0].y += 10;
break;
case left:
snake.xy[0].x -= 10;
break;
case right:
snake.xy[0].x += 10;
}
}
//移动控制
void keyDown()
{
char key = _getch(); //获取按键值
switch (key)
{
case 'w':
case 'W':
case 72:
if (snake.position != down)
snake.position = up;
break;
case 's':
case 'S':
case 80:
if (snake.position != up)
snake.position = down;
break;
case 'a':
case 'A':
case 75:
if (snake.position != right)
snake.position = left;
break;
case 'd':
case 'D':
case 77:
if (snake.position != left)
snake.position = right;
break;
}
}
//初始化食物
void initFood()
{
srand((unsigned int)time(NULL)); //随机种子
food.fdxy.x = rand() % 80 * 10; //确定随机出现的范围
food.fdxy.y = rand() % 60 * 10;
food.flag = 1; //食物出现标志
for (int i = 0; i < snake.num; i++) //判断蛇是否与食物重合,若重合即重新产生
{
if (food.fdxy.x == snake.xy[i].x&&food.fdxy.y == snake.xy[i].y)
{
food.fdxy.x = rand() % 80 * 10;
food.fdxy.y = rand() % 60 * 10;
}
}
}
//将食物绘制出来
void drawFood()
{
setlinecolor(BLUE); //设置矩形边框线
setfillcolor(BLUE); //设置矩形颜色
fillrectangle(food.fdxy.x, food.fdxy.y, food.fdxy.x + 10, food.fdxy.y + 10);
}
//蛇吃食物:判断食物与蛇头的坐标
void eatFood()
{
if (snake.xy[0].x == food.fdxy.x&&snake.xy[0].y == food.fdxy.y)
{
food.flag = 0; //将食物标志位清零
snake.num++; //蛇的长度加一
food.grade += 10; //分数加10
}
}
//显示分数
void showGread()
{
char grade[100] = "";
sprintf(grade, "分数:%d", food.grade);
setbkmode(TRANSPARENT); //将文字类型设置为透明
settextcolor(LIGHTBLUE); //将文字颜色设置为浅蓝
outtextxy(650, 30, grade); //文字显示的具体位置
}
//游戏结束
//1.蛇头的位置超出边框范围
//2.蛇头与蛇身的位置重合
int endSnake()
{
if (snake.xy[0].x < 0 || snake.xy[0].x>800 || snake.xy[0].y < 0 || snake.xy[0].y>600)
{
MessageBox(hwnd, "撞墙,游戏结束!", "游戏结束", 0);
return 1;
}
for (int i = 1; i < snake.num; i++)
{
if (snake.xy[0].x == snake.xy[i].x&&snake.xy[0].y == snake.xy[i].y)
{
MessageBox(hwnd, "自杀,游戏结束!", "游戏结束", 0);
return 1;
}
}
return 0;
}
int main()
{
//绘制窗口
hwnd=initgraph(800, 600);
setbkcolor(WHITE); //将窗口背景设置为白色
cleardevice(); //刷新界面
initSnake();
while (1)
{
cleardevice();//刷新界面
drawSnake();
moveSnake();
eatFood();
showGread();
drawFood();
//游戏结束就退出
if (endSnake()){
break;
exit(0);
}
//仅当食物被吃掉的时候才产新的食物
if (food.flag == 0){
initFood();
}
//判读键盘是否按下
if (_kbhit()){
keyDown();
}
//设置速度
Sleep(150);
}
_getch(); //防止屏幕闪烁
closegraph(); //关闭图形显示模式
return 0;
}
遇到的一些困难:
1.编译器报错
属于编译器问题,在代码上方添加
#define _CRT_SECURE_NO_WARNINGS
2.编译属性报错
右键打开选择属性->常规->字符集
将字符集改为使用多字符字符集
另外在游戏过程中有些许延迟,望大佬解答!