本帖最后由 慕鸢灬 于 2018-3-26 14:22 编辑
[C] 纯文本查看 复制代码 // 项目名称:贪吃蛇
// by: 慕鸢
#include<stdio.h>
#include<graphics.h> //图形库
#include<conio.h> //键盘按键相关
#include<time.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define MAX 1000 //蛇的最大节数
// 点 坐标 的改变 int x , int y
struct COOR{
int x;
int y;
};
//上下左右四个方向
enum CH {
up = 72,
down = 80,
left = 75,
right = 77
};
//蛇 n节 数组
struct SNAKE {
int n; //蛇的节数
struct COOR scr[MAX]; // 1000节蛇的坐标
enum CH ch; // 蛇的方向
}snake;
struct FOOD
{
struct COOR fsr;
int flag;//标记食物有没有被吃掉 1 0
}food;
void initGame(); //初始化地图
void draw_snake(); //画蛇
void snake_move(); //蛇移动
void change_snake_ch(); //改变蛇的方向
void food_coor(); //食物坐标
void draw_food(); //绘制食物
void eat_food(); //吃掉食物
void game_break(); //判断游戏是否结束
void game_over(); //游戏结束提示
//主函数
//
//
//在此
int main() {
initGame();
mciSendString(L"open 甩葱歌.mp3 alias scg", 0, 0, 0);
mciSendString(L"play scg repeat", 0, 0, 0);
while (1) {
while (!kbhit()) //检测按键
{
if (food.flag == 0) {
food_coor();
}
cleardevice();
draw_food();
draw_snake();
snake_move();
Sleep(100); //动一下停一百毫秒
eat_food();
game_break();
}
change_snake_ch();
}
return 0;
}
//游戏界面初始化
void initGame() {
//创建自己的窗口
initgraph(640, 480); //设置窗口大小 三个参数大小,前两个是长和宽,最后一个是样式.
srand((unsigned)time(NULL));
setbkcolor(RGB(255, 128, 128));// 背景颜色
cleardevice(); // 刷新
snake.n = 1;
snake.scr[0].x = 0;
snake.scr[0].y = 0;
snake.ch = down;
//rectangle(0,0,10,10);
food.flag = 0;
}
//绘制蛇
void draw_snake() {
//数组下标
for (int i = snake.n - 1; i >= 0; i--) {
setlinecolor(RGB(rand() % 180, rand() % 180, rand() % 180));
rectangle(snake.scr[i].x, snake.scr[i].y, snake.scr[i].x + 10, snake.scr[i].y + 10);
}
}
//蛇移动
void snake_move() {
for (int i = snake.n - 1; i > 0; i--) {
snake.scr[i].x = snake.scr[i - 1].x;
snake.scr[i].y = snake.scr[i - 1].y;
}
switch (snake.ch) {
case up:
snake.scr[0].y -= 10;
break;
case down:
snake.scr[0].y += 10;
break;
case left:
snake.scr[0].x -= 10;
break;
case right:
snake.scr[0].x += 10;
break;
}
}
//改变蛇的方向,通过键盘去控制
void change_snake_ch() {
switch (getch()) {//获取键盘敲下的字符
case 72:
if (snake.ch != down) {
snake.ch = up;
}
break;
case 80:
if (snake.ch != down) {
snake.ch = down;
}
break;
case 75:
if (snake.ch != right) {
snake.ch = left;
}
break;
case 77:
if (snake.ch != left) {
snake.ch = right;
}
break;
}
}
//食物坐标 随机
void food_coor() {
food.fsr.x = rand() % 64 * 10; // 0-630
food.fsr.y = rand() % 48 * 10;
food.flag = 1; //食物初始化为没有被吃掉
}
//绘制食物 1.28
void draw_food() {
setfillcolor(LIGHTRED);
solidroundrect(food.fsr.x,
food.fsr.y,
food.fsr.x + 10,
food.fsr.y + 10,
10,
10);
}
//吃食物
void eat_food() {
if (snake.scr[0].x == food.fsr.x && snake.scr[0].y == food.fsr.y) {
snake.n++;
food.flag = 0;
}
}
//判断游戏是否结束
void game_break() {
//撞墙
if (snake.scr[0].x < 0 || snake.scr[0].y > 630 || snake.scr[0].y < 0 || snake.scr[0].y > 470) {
game_over();
}
//头撞到身体
for (int i = snake.n - 1; i > 0; i--) {
if (snake.scr[0].x == snake.scr[i].x && snake.scr[0].y == snake.scr[i].y) {
game_over();
}
}
}
void game_over() { //游戏结束提示.
cleardevice();
settextcolor(LIGHTGREEN);
settextstyle(50, 0, L"黑体");
outtextxy(200, 200, L"Game Over");
Sleep(3000);
exit(0);
}
没谁了
|