|
大一上的时候写了个贪吃蛇游戏,有个bug一直没有修复,最近想起来了,修复了bug然后增添了部分功能
算是我第一个项目吧(图书管理系统这种小白级别的除外),放这留作回忆。
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<math.h>
#include<Windows.h>
#include<mmsystem.h>
#pragma comment(lib,&#34;winmm.lib&#34;)
struct Food
{
int x;
int y;
int r;
bool cunzai;
}food;
struct snake {
int geshu;
int fangxiang;
int 间隔时间;
POINT zuobiao[500];
}snake;
enum a { left, right, up, down };
void huitu();
void yundong();
void anjian();
void shiwu();
void chengfa();
int main()
{
mciSendString(&#34;open Daylight.mp3 alias music&#34;, 0, 0, 0);
mciSendString(&#34;play music&#34;, 0, 0, 0);
initgraph(640, 500);
//初始化蛇
snake.fangxiang = right;
snake.geshu = 3;
snake.间隔时间 = 30;
srand((unsigned)time(NULL));
food.x = rand() % 634;
food.y = rand() % 494;
food.r = 6;
food.cunzai = true;
for (int i = snake.geshu - 1; i >= 0; i--)
{
snake.zuobiao.x = i * 10 + 20;
snake.zuobiao.y = 50;
}
huitu();
while (1)
{
anjian();
yundong();
shiwu();
chengfa();
huitu();
Sleep(snake.间隔时间);
}
}
void huitu()
{
BeginBatchDraw();
//窗口初始化
setbkcolor(RGB(32,32,32));
cleardevice();
//蛇初始化
outtextxy(10, 10, &#34;按空格暂停&#34;);
outtextxy(10, 30, &#34;按M以修改速度&#34;);
for (int i = snake.geshu - 1; i >= 0; i--)
{
srand((unsigned)time(NULL));
setfillcolor(RGB(rand(), rand() + 300, rand() + 500));
solidcircle(snake.zuobiao.x, snake.zuobiao.y, 5);
}
if (food.cunzai == 1)
{
setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
solidcircle(food.x, food.y, 6);
EndBatchDraw();
}
if (food.cunzai == 0)
{
food.x = rand() % 636;
food.y = rand() % 496;
food.r = 6;
food.cunzai = 1;
EndBatchDraw();
}
}
void yundong()
{
{
for (int i = 0; i < snake.geshu - 1; i++)
{
snake.zuobiao.x = snake.zuobiao[i + 1].x;
snake.zuobiao.y = snake.zuobiao[i + 1].y;
}
switch (snake.fangxiang)
{
case up:
if (snake.zuobiao[snake.geshu - 1].y - 10 <= 0)
snake.zuobiao[snake.geshu - 1].y = 490;
else
snake.zuobiao[snake.geshu - 1].y -= 10;
break;
case down:
if (500 - snake.zuobiao[snake.geshu - 1].y <= 10)
snake.zuobiao[snake.geshu - 1].y = 10;
else
snake.zuobiao[snake.geshu - 1].y += 10;
break;
case left:
if (snake.zuobiao[snake.geshu - 1].x - 10 <= 0)
snake.zuobiao[snake.geshu - 1].x = 630;
else
snake.zuobiao[snake.geshu - 1].x -= 10;
break;
case right:
if (640 - snake.zuobiao[snake.geshu - 1].x <= 10)
snake.zuobiao[snake.geshu - 1].x = 10;
else
snake.zuobiao[snake.geshu - 1].x += 10;
break;
}
return;
}
}
void anjian()
{
if (_kbhit())
{
switch (_getch())
{
case &#39;w&#39;:
case &#39;W&#39;:
if (snake.fangxiang != down)
snake.fangxiang = up;
break;
case &#39;s&#39;:
case &#39;S&#39;:
if (snake.fangxiang != up)
snake.fangxiang = down;
break;
case &#39;a&#39;:
case &#39;A&#39;:
if (snake.fangxiang != right)
{
snake.fangxiang = left;
}
break;
case &#39;d&#39;:
case &#39;D&#39;:
if (snake.fangxiang != left)
snake.fangxiang = right;
break;
case &#39; &#39;:
mciSendString((LPCTSTR)&#34;pause music&#34;, 0, 0, 0);
while (_getch() != &#39; &#39;);
mciSendString((LPCTSTR)&#34;resume music&#34;, 0, 0, 0);
break;
case &#39;m&#39;:
case &#39;M&#39;:
char B[1000];
sprintf(B, &#34;请输入要调节为的运行间隔时间(要求间隔时间大于0),目前间隔时间为:%d&#34;, snake.间隔时间);
int a=0;
int if_continue = 0;
outtextxy(50, 150, B);
do {
if_continue = scanf(&#34;%d&#34;, &a);
if (!(a > 0)||if_continue!=1) {
outtextxy(50, 200, &#34;输入有误,请重新输入&#34;);
while (getchar() != &#39;\n&#39;);
}
} while (!(a > 0)||if_continue!=1);
while (getchar() != &#39;\n&#39;);
snake.间隔时间 = a;
}
}
}
void shiwu()
{
if (food.cunzai == 1 && sqrt(pow(snake.zuobiao[snake.geshu - 1].x - food.x, 2) + pow(snake.zuobiao[snake.geshu - 1].y - food.y, 2)) <= 11)
{
snake.geshu++;
int _x = snake.zuobiao[0].x;
int _y = snake.zuobiao[0].y;
for (int i = snake.geshu - 1; i > 0; i--)
{
snake.zuobiao.x = snake.zuobiao[i - 1].x;
snake.zuobiao.y = snake.zuobiao[i - 1].y;
}
snake.zuobiao[0].x = _x;
snake.zuobiao[0].y = _y;
food.cunzai = 0;
}
}
void chengfa()
{
for (int i = 0; i < snake.geshu - 1; i++)
{
if (sqrt(pow(snake.zuobiao[snake.geshu - 1].x - snake.zuobiao.x, 2) + pow(snake.zuobiao[snake.geshu - 1].y - snake.zuobiao.y, 2)) < 10)
{
settextstyle(30, 15, (LPCTSTR)&#34;楷体&#34;);
char A[100];
sprintf(A, &#34;得分为:%d&#34;, snake.geshu - 3);
outtextxy(180, 150,(LPCTSTR)A);
outtextxy(180, 240,&#34;游戏结束,按ESC退出&#34;);
while (_getch() != 27);
exit(1);
}
}
}实现了背景音乐功能,暂停功能,调速功能。

 |
|