写一个大鱼吃小鱼代码,C语言,在Dev-C++中可以运行
时间: 2024-02-15 17:01:28 浏览: 127
大鱼吃小鱼源代码C++
好的,这是一份简单的大鱼吃小鱼游戏的代码,可以在Dev-C++中运行:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
// 定义屏幕宽度和高度
#define WIDTH 60
#define HEIGHT 30
// 定义鱼的大小
#define FISH_SIZE 3
// 定义食物的大小
#define FOOD_SIZE 1
// 定义方向
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
// 定义鱼的结构体
typedef struct fish {
int x;
int y;
int direction;
int size;
struct fish *next;
} Fish;
// 定义食物的结构体
typedef struct food {
int x;
int y;
struct food *next;
} Food;
// 初始化屏幕
void InitScreen()
{
system("mode con cols=60 lines=30");
}
// 绘制边框
void DrawBorder()
{
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) {
printf("#");
} else {
printf(" ");
}
}
printf("\n");
}
}
// 绘制鱼
void DrawFish(Fish *fish)
{
int i;
for (i = 0; i < fish->size; i++) {
if (fish->direction == UP) {
printf("%c\n", 'A');
} else if (fish->direction == DOWN) {
printf("%c\n", 'V');
} else if (fish->direction == LEFT) {
printf("%c", '<');
} else {
printf(">%c", '\n');
}
}
}
// 绘制食物
void DrawFood(Food *food)
{
printf("@\n");
}
// 初始化鱼
Fish *InitFish(int x, int y, int direction, int size)
{
Fish *fish = (Fish *)malloc(sizeof(Fish));
fish->x = x;
fish->y = y;
fish->direction = direction;
fish->size = size;
fish->next = NULL;
return fish;
}
// 初始化食物
Food *InitFood(int x, int y)
{
Food *food = (Food *)malloc(sizeof(Food));
food->x = x;
food->y = y;
food->next = NULL;
return food;
}
// 生成食物
void GenerateFood(Food **head, Fish *fish)
{
int x, y;
do {
x = rand() % (WIDTH - 2) + 1;
y = rand() % (HEIGHT - 2) + 1;
} while (x == fish->x && y == fish->y);
Food *food = InitFood(x, y);
food->next = *head;
*head = food;
}
// 移动鱼
void MoveFish(Fish *fish)
{
if (fish->direction == UP) {
fish->y--;
} else if (fish->direction == DOWN) {
fish->y++;
} else if (fish->direction == LEFT) {
fish->x--;
} else {
fish->x++;
}
}
// 改变鱼的方向
void ChangeDirection(Fish *fish)
{
char ch = getch();
if (ch == 'W' || ch == 'w') {
fish->direction = UP;
} else if (ch == 'S' || ch == 's') {
fish->direction = DOWN;
} else if (ch == 'A' || ch == 'a') {
fish->direction = LEFT;
} else if (ch == 'D' || ch == 'd') {
fish->direction = RIGHT;
}
}
// 判断是否吃到食物
int EatFood(Fish *fish, Food **head)
{
Food *prev = NULL;
Food *current = *head;
while (current != NULL) {
if (fish->x == current->x && fish->y == current->y) {
if (prev == NULL) {
*head = current->next;
} else {
prev->next = current->next;
}
free(current);
return 1;
}
prev = current;
current = current->next;
}
return 0;
}
// 判断是否碰到边界或自身
int IsGameOver(Fish *fish)
{
if (fish->x == 0 || fish->x == WIDTH - 1 || fish->y == 0 || fish->y == HEIGHT - 1) {
return 1;
}
Fish *current = fish->next;
while (current != NULL) {
if (fish->x == current->x && fish->y == current->y) {
return 1;
}
current = current->next;
}
return 0;
}
// 添加新的鱼身体
void AddFishBody(Fish *fish)
{
Fish *newBody = InitFish(fish->x, fish->y, fish->direction, fish->size);
newBody->next = fish->next;
fish->next = newBody;
fish->size++;
}
// 释放链表内存
void FreeList(Fish *fish, Food *food)
{
while (fish != NULL) {
Fish *temp = fish;
fish = fish->next;
free(temp);
}
while (food != NULL) {
Food *temp = food;
food = food->next;
free(temp);
}
}
int main()
{
srand((unsigned)time(NULL)); // 设置随机种子
InitScreen(); // 初始化屏幕
DrawBorder(); // 绘制边框
Fish *fish = InitFish(WIDTH / 2, HEIGHT / 2, UP, FISH_SIZE); // 初始化鱼
Food *foodList = NULL; // 初始化食物链表
int score = 0; // 初始化得分
while (1) {
system("cls"); // 清屏
DrawBorder(); // 绘制边框
DrawFish(fish); // 绘制鱼
DrawFood(foodList); // 绘制食物
if (EatFood(fish, &foodList)) { // 判断是否吃到食物
AddFishBody(fish); // 添加新的鱼身体
score++; // 更新得分
}
if (IsGameOver(fish)) { // 判断是否游戏结束
printf("Game Over!\n");
printf("Your Score: %d\n", score);
break;
}
ChangeDirection(fish); // 改变鱼的方向
MoveFish(fish); // 移动鱼
if (rand() % 10 == 0) { // 有一定概率生成新的食物
GenerateFood(&foodList, fish);
}
Sleep(100); // 延迟一段时间,控制游戏速度
}
FreeList(fish, foodList); // 释放链表内存
return 0;
}
```
运行后,你可以使用 W、S、A、D 控制鱼的方向,尝试吃到食物并且不碰到边界或自身,得到更高的分数。
阅读全文