#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,"winmm.lib")
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("open Daylight.mp3 alias music", 0, 0, 0);
mciSendString("play music", 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[i].x = i * 10 + 20;
snake.zuobiao[i].y = 50;
}
huitu();
while (1)
{
anjian();
yundong();
shiwu();
chengfa();
huitu();
Sleep(snake.间隔时间);
}
}
void huitu()
{
BeginBatchDraw();
//窗口初始化
setbkcolor(RGB(32,32,32));
cleardevice();
//蛇初始化
outtextxy(10, 10, "按空格暂停");
outtextxy(10, 30, "按M以修改速度");
for (int i = snake.geshu - 1; i >= 0; i--)
{
srand((unsigned)time(NULL));
setfillcolor(RGB(rand(), rand() + 300, rand() + 500));
solidcircle(snake.zuobiao[i].x, snake.zuobiao[i].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[i].x = snake.zuobiao[i + 1].x;
snake.zuobiao[i].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 'w':
case 'W':
if (snake.fangxiang != down)
snake.fangxiang = up;
break;
case 's':
case 'S':
if (snake.fangxiang != up)
snake.fangxiang = down;
break;
case 'a':
case 'A':
if (snake.fangxiang != right)
{
snake.fangxiang = left;
}
break;
case 'd':
case 'D':
if (snake.fangxiang != left)
snake.fangxiang = right;
break;
case ' ':
mciSendString((LPCTSTR)"pause music", 0, 0, 0);
while (_getch() != ' ');
mciSendString((LPCTSTR)"resume music", 0, 0, 0);
break;
case 'm':
case 'M':
char B[1000];
sprintf(B, "请输入要调节为的运行间隔时间(要求间隔时间大于0),目前间隔时间为:%d", snake.间隔时间);
int a=0;
int if_continue = 0;
outtextxy(50, 150, B);
do {
if_continue = scanf("%d", &a);
if (!(a > 0)||if_continue!=1) {
outtextxy(50, 200, "输入有误,请重新输入");
while (getchar() != '\n');
}
} while (!(a > 0)||if_continue!=1);
while (getchar() != '\n');
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[i].x = snake.zuobiao[i - 1].x;
snake.zuobiao[i].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[i].x, 2) + pow(snake.zuobiao[snake.geshu - 1].y - snake.zuobiao[i].y, 2)) < 10)
{
settextstyle(30, 15, (LPCTSTR)"楷体");
char A[100];
sprintf(A, "得分为:%d", snake.geshu - 3);
outtextxy(180, 150,(LPCTSTR)A);
outtextxy(180, 240,"游戏结束,按ESC退出");
while (_getch() != 27);
exit(1);
}
}
}
大一上学期写的,命名不规范,存在bug,现在修复了bug,增加了了背景音乐功能,暂停功能,游戏中调速功能