大家好啊,如果有一样是编程0基础的大一本科生,相信大家现在都对自己学习了一段时间的程序设计课程还没有过实战经验;
最近也是考试刚完,持着练手的心态我编写了这样一个经典的小游戏-贪吃蛇;
由于是第一次发表,文章描述不够充分的地方希望各位前辈们帮忙指出;
先去看过一些代码,对于初学者来说理解太过困难,而且一些思路太过繁琐,在这里我借用easyx函数库实现程序相对的简化,使初学者可以无障碍理解;
下面是运行后的界面:
由于时间原因也是比较简单,各位可以按照自己要求添加一些点缀;
储备知识:代码虽然简单,但是对于初学者,应该对于C语言的基本知识有所了解,对与easyx函数库有所了解;
原理在代码中已有详细注释就不在多说;
下面是源码:
/*出自自武汉大学本科生王利伟
*西安电子科技大学本科生高翔
转载请注明出处;
*/
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>
/*
*考虑要分析的数据类型: 考虑实现操作的几个重要函数类别:
*蛇的定义-0; *蛇与食物的显示-0
*食物定义-1; *蛇的移动操作-1
*分数计算-3; *蛇与食物的生成-2
*蛇与食物的坐标;
*/
#define windowswidth 960
#define windowsheight 640
#define right 100
#define left 97
#define up 119
#define down 115
HWND hwnd = NULL;
typedef struct pointxy { int x, y; }Mypoint;
int i;
struct Snake { int num; int dir; Mypoint MAX_LENGTH[500]; }snake;//蛇应该拥有长度,方向,移动速度
struct Food { Mypoint xy; int flag; }food;//食物是否存在,以免同时存在多个食物
/*
*初定义函数-蛇,食物
*画画函数
*操作函数
*指令函数
*吃食物函数
*死亡函数
*/
void Initsnake();
void Initfood();
void Drawsnake();
void Drawfood();
void Movesnake();
void Keyuser();
void Eatfood();
int Die();
void showwriter();
int main()
{
srand((unsigned int)time(NULL));
hwnd = initgraph(windowswidth, windowsheight);
setbkcolor(WHITE);
cleardevice();
Initsnake();
while (Die()) {
cleardevice();
showwriter();
if (food.flag == 0)
{
Initfood();
Drawfood();
}
Drawsnake();
Drawfood();
Movesnake();
if (_kbhit())
{
Keyuser();
}
Sleep(50);
Eatfood();
}
getchar();
closegraph();
return 0;
}
void Initsnake()
{
snake.MAX_LENGTH[0].x = 20;
snake.MAX_LENGTH[0].y = 0;
snake.MAX_LENGTH[1].x = 10;
snake.MAX_LENGTH[1].y = 0;
snake.MAX_LENGTH[2].x = 0;
snake.MAX_LENGTH[2].y = 0;
snake.num = 3;
snake.dir = right;
}
void Drawsnake()
{
int i;
for (i = 0; i< snake.num; i++)
{
setlinecolor(BLACK);
setfillcolor(GREEN);
fillrectangle(snake.MAX_LENGTH[i].x, snake.MAX_LENGTH[i].y, snake.MAX_LENGTH[i].x + 10, snake.MAX_LENGTH[i].y + 10);
}
}
void Movesnake()
{
int i;
for (i = snake.num-1; i > 0; i--)
{
snake.MAX_LENGTH[i].x = snake.MAX_LENGTH[i - 1].x;
snake.MAX_LENGTH[i].y = snake.MAX_LENGTH[i - 1].y;
}
switch (snake.dir)
{
case right: snake.MAX_LENGTH[0].x += 10; break;
case left:snake.MAX_LENGTH[0].x -= 10; break;
case up: snake.MAX_LENGTH[0].y -= 10; break;
case down:snake.MAX_LENGTH[0].y+= 10; break;
default:break;
}
}
void Keyuser()
{
char keyuser;
keyuser = getch();
switch (keyuser)
{
case right: snake.dir = right; break;
case down:snake.dir = down; break;
case left: snake.dir = left; break;
case up:snake.dir = up; break;
}
}
void Initfood()
{
int flag1=1;
food.xy.x = rand() % 96 * 10;
food.xy.y = rand() % 64 * 10;
food.flag = 0;
while (flag1)
{
flag1= 0;
for (i = 0; i < snake.num; i++)
{
if (food.xy.x == snake.MAX_LENGTH[i].x&&food.xy.y == snake.MAX_LENGTH[i].y)//防止食物与蛇重叠
{
food.xy.x = rand() % 96 * 10;
food.xy.y = rand() % 64 * 10;
flag1++;
}
}
}
}
void Drawfood()
{
setlinecolor(RED);
setfillcolor(RED);
fillrectangle(food.xy.x,food.xy.y,food.xy.x+10,food.xy.y+10 );
food.flag = 1;
}
void Eatfood()
{
if (snake.MAX_LENGTH[0].x == food.xy.x&&snake.MAX_LENGTH[0].y == food.xy.y)
{
int change_x,change_y;
change_x = snake.MAX_LENGTH[snake.num - 2].x - snake.MAX_LENGTH[snake.num - 1].x;
change_y = snake.MAX_LENGTH[snake.num - 2].y - snake.MAX_LENGTH[snake.num - 1].y;
snake.num++;
snake.MAX_LENGTH[snake.num - 1].x = snake.MAX_LENGTH[snake.num - 2].x - change_x;
snake.MAX_LENGTH[snake.num - 1].y = snake.MAX_LENGTH[snake.num - 2].y - change_y;
food.flag =0;
}
}
int Die()
{
if (snake.MAX_LENGTH[0].x < 0 || snake.MAX_LENGTH[0].x>960 || snake.MAX_LENGTH[0].y < 0 || snake.MAX_LENGTH[0].y>640)
{
outtextxy(200, 200, "你死了");
MessageBox(hwnd, "Gameover!","你死了",MB_OK);
return 0;
}
return 1;
}
/*
作者:
武汉大学---王利伟
西安电子科技大学--高翔
*/
void showwriter()
{
setbkmode(TRANSPARENT);
settextcolor(LIGHTBLUE);
outtextxy(800, 10, "作者:武大王利伟");
outtextxy(830, 40, "西安电子高翔");
}
//由于时间原因并未设置蛇无法同向掉头以及碰及自身会死亡的规定,各位对此有兴趣的同学可以自己进行编写,或者和作者私聊互相思路;