能在dev-c++上运行通过的扫雷,贪吃蛇,等小游戏的c语言代码

如题所述

#includestdc++.h>#include#include#define LEFT 0x4B00#define RIGHT 0x4D00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011B
int i, key;
int score = 0;
int gameSpeed = 32000;
struct Food {
int x; /* 食物的横坐标 */
int y; /* 食物的纵坐标 */
int exists; /* 食物是否存在的变量 */
} food;
struct Snake {
int x[N];
int y[N];
int length; /* 蛇的节数 */
int direction; /* 蛇的方向 */
int alive; /* 蛇的生命,0活着,1死亡 */
} snake;
void Initialize(void); /* 图形驱动 */
void CloseGame(void); /* 关闭游戏函数 */
void DrawGame(void); /* 画图函数 */
void GameOver(void); /* 输出失败函数 */
void PlayGame(); /* 游戏控制函数 主要控制序列 */
void Delay(char ch); /* 调节游戏速度 */
/* 主函数 */
int main(void) {
int choice;
choice = Menu(); /* 游戏开始菜单 */
Initialize();
DrawGame();
PlayGame(choice);
CloseGame();
return 0;
}
/* 游戏开始菜单 */
int Menu() {
char ch;
printf("请选择游戏速度:\n");
printf("1-快速 2-正常 3-慢速\n");
printf("\n请按数字键...\n");
do {
ch = getch();
} while (ch != '1' && ch != '2' && ch != '3');
clrscr();
return ch;
}
/* 初始化图形驱动 */
void Initialize(void) {
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\tc");
cleardevice();
}
/* 绘制游戏界面 */
void DrawGame(void) {
setcolor(11);
setlinestyle(SOLID_LINE, 0, THICK_WIDTH);
for (i = 50; i <= 600; i += 10) {
rectangle(i, 40, i + 10, 49); /* 画出上边框 */
rectangle(i, 451, i + 10, 460); /* 画出下边框 */
}
for (i = 40; i <= 450; i += 10) {
rectangle(50, i, 59, i + 10); /* 画出左边框 */
rectangle(601, i, 610, i + 10); /* 画出右边框 */
}
}
/* 游戏结束 */
void GameOver(void) {
cleardevice();
setcolor(RED);
settextstyle(0, 0, 4);
outtextxy(200, 200, "GAME OVER");
getch();
}
/* 输出分数 */
void PrintScore(void) {
char str[10];
setfillstyle(SOLID_FILL, YELLOW);
bar(50, 15, 220, 35);
setcolor(6);
settextstyle(0, 0, 2);
sprintf(str, "Score: %d", score);
outtextxy(55, 20, str);
}
/* 关闭游戏 */
void CloseGame(void) {
getch();
closegraph();
}
/* 游戏主循环 */
void PlayGame(int choice) {
randomize(); /* 随机数发生器 */
food.exists = 1; /* 设置食物存在 */
snake.alive = 0;
snake.direction = 1;
snake.x[0] = 100;
snake.y[0] = 100;
snake.length = 2;
PrintScore();
while (1) { /* 游戏循环 */
while (!kbhit()) { /* 检查是否有按键 */
if (food.exists == 1) { /* 需要食物 */
food.x = rand() % 400 + 60;
food.y = rand() % 350 + 60; /* 使用rand函数随机产生食物坐标 */
while (food.x % 10 != 0) food.x++;
while (food.y % 10 != 0) food.y++; /* 确保食物在整格中 */
food.exists = 0; /* 食物现在出现 */
}
if (food.exists == 0) { /* 食物出现后显示 */
setcolor(GREEN);
rectangle(food.x, food.y, food.x + 10, food.y - 10);
}
for (i = snake.length - 1; i > 0; i--) { /* 贪吃蛇移动算法 */
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}
switch (snake.direction) { /* 控制蛇头移动方向 */
case 1: snake.x[0] += 10; break;
case 2: snake.x[0] -= 10; break;
case 3: snake.y[0] -= 10; break;
case 4: snake.y[0] += 10; break;
}
for (i = 3; i < snake.length; i++) { /* 判断蛇头是否与身体相撞 */
if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0]) {
GameOver();
snake.alive = 1;
break;
}
}
/* 判断是否撞到墙壁 */
if (snake.x[0] 55 || snake.x[0] > 595 || snake.y[0] 55 || snake.y[0] > 455) {
GameOver();
snake.alive = 1;
}
if (snake.alive == 1) break; /* 如果死亡则退出循环 */
if (snake.x[0] == food.x && snake.y[0] == food.y) { /* 判断蛇是否吃到食物 */
setcolor(0);
rectangle(food.x, food.y, food.x + 10, food.y - 10); /* 吃掉食物后用黑色擦去 */
snake.x[snake.length] = -20;
snake.y[snake.length] = -20; /* 暂时将增加的一节放到看不到的地方 */
snake.length++;
score += 10;
PrintScore();
}
setcolor(4); /* 每次移动后擦除后面的身体 */
for (i = 0; i < snake.length; i++) rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] - 10);
Delay(choice);
setcolor(0);
for (i = snake.length - 1; i > 0; i--) rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] - 10);
}
key = bioskey(0); /* 接受按键 */
if (key == ESC) break;
else if (key == UP && snake.direction != 4) snake.direction = 3;
else if (key == RIGHT && snake.direction != 2) snake.direction = 1;
else if (key == LEFT && snake.direction != 1) snake.direction = 2;
else if (key == DOWN && snake.direction !=
温馨提示:答案为网友推荐,仅供参考
相似回答