目录
前言
代码参考了《C语言课程设计与游戏开发实践教程》
游戏介绍:
假设有一个像棋盘一样的方格网,每个方格中放置一个生命细胞,生命细胞只有两种状态:“生”或“死”。游戏规则如下:
1.如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生,即该细胞若原先为死,则转为生,若原先为生,则保持不变;
2.如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
3.在其它情况下,该细胞为死,即该细胞若原先为生,则转为死,若原先为死,则保持不变设定图像中每个像素的初始状态后依据上述的游戏规则演绎生命的变化,由于初始状态和迭代次数不同,将会得到令人叹服的优美图案。
一、游戏代码
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define High 25 // 游戏画面尺寸
#define Width 50
// 全局变量
int cells[High][Width]; // 所有位置细胞生1或死0
void gotoxy(int x, int y) // 光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void startup() // 数据初始化
{
for (int i = 0; i < High; i++) // 初始化
for (int j = 0; j < Width; j++)
{
cells[i][j] = 1;
}
}
void show() // 显示画面
{
gotoxy(0, 0); // 光标移动到原点位置,以下重画清屏
for (int i = 1; i <= High - 1; i++)
{
for (int j = 1; j <= Width - 1; j++)
{
if (cells[i][j] == 1)
printf("*"); // 输出活的细胞
else
printf(" "); // 输出空格
}
printf("\n");
}
Sleep(50);
}
void updateWithoutInput() // 与用户输入无关的更新
{
int NewCells[High][Width]; // 下一帧的细胞情况
int NeibourNumber; // 统计邻居的个数
for (int i = 1; i <= High - 1; i++)
{
for (int j = 1; j <= Width - 1; j++)
{
NeibourNumber = cells[i - 1][j - 1] + cells[i - 1][j] + cells[i - 1][j + 1] + cells[i][j - 1] + cells[i][j + 1] + cells[i + 1][j - 1] + cells[i + 1][j] + cells[i + 1][j + 1];
if (NeibourNumber == 3)
NewCells[i][j] = 1;
else if (NeibourNumber == 2)
NewCells[i][j] = cells[i][j];
else
NewCells[i][j] = 0;
}
}
for (int i = 1; i <= High - 1; i++)
for (int j = 1; j <= Width - 1; j++)
cells[i][j] = NewCells[i][j];
}
int main()
{
startup(); // 数据初始化
while (1) // 游戏循环执行
{
show(); // 显示画面
updateWithoutInput(); // 与用户输入无关的更新
}
return 0;
}
二、代码解析
1、初始化
假设有 50 * 50 个小格子(用数组来实现)(数组我在前面的博客中写过,这里就不介绍了)。每个格子里面都有生命存活(值为 1)或者死亡(值为 0),将所有有生命的通过图案来显示。代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define High 25 // 游戏画面尺寸
#define Width 50
// 全局变量
int cells[High][Width]; // 所有位置细胞生1或死0
void gotoxy(int x, int y) // 类似于清屏函数
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void startup() // 数据初始化
{
int i, j;
for (i = 0; i < High; i++) // 随机初始化
for (j = 0; j < Width; j++)
{
cells[i][j] = rand() % 2;
}
}
void show() // 显示画面
{
gotoxy(0, 0); // 清屏
int i, j;
for (i = 0; i <= High; i++)
{
for (j = 0; j <= Width; j++)
{
if (cells[i][j] == 1)
printf("*"); // 输出活的细胞
else
printf(" "); // 输出空格
}
printf("\n");
}
Sleep(50);
}
void updateWithoutInput() // 与用户输入无关的更新
{
}
void updateWithInput() // 与用户输入有关的更新
{
}
void main()
{
startup(); // 数据初始化
while (1) // 游戏循环执行
{
show(); // 显示画面
updateWithoutInput(); // 与用户输入无关的更新
updateWithInput(); // 与用户输入有关的更新
}
}
2、繁衍或死亡
规则:
1. 如果一个细胞周围有3个细胞为生,则该细胞为生(即该细胞若原先为死,则转为生,若原先为生,则保持不变)
2. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变
3. 在其它情况下,该细胞为死(即该细胞若原先为生,则转为死,若原先为死,则保持不变)
在空缺的函数中补充相应的代码(这里只是实现了这个小游戏的最基本功能):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define High 25 // 游戏画面尺寸
#define Width 50
// 全局变量
int cells[High][Width]; // 所有位置细胞生1或死0
void gotoxy(int x, int y) // 光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void startup() // 数据初始化
{
for (int i = 0; i < High; i++) // 初始化
for (int j = 0; j < Width; j++)
{
cells[i][j] = 1;
}
}
void show() // 显示画面
{
gotoxy(0, 0); // 光标移动到原点位置,以下重画清屏
for (int i = 1; i <= High - 1; i++)
{
for (int j = 1; j <= Width - 1; j++)
{
if (cells[i][j] == 1)
printf("*"); // 输出活的细胞
else
printf(" "); // 输出空格
}
printf("\n");
}
Sleep(50);
}
void updateWithoutInput() // 与用户输入无关的更新
{
int NewCells[High][Width]; // 下一帧的细胞情况
int NeibourNumber; // 统计邻居的个数
for (int i = 1; i <= High - 1; i++)
{
for (int j = 1; j <= Width - 1; j++)
{
NeibourNumber = cells[i - 1][j - 1] + cells[i - 1][j] + cells[i - 1][j + 1] + cells[i][j - 1] + cells[i][j + 1] + cells[i + 1][j - 1] + cells[i + 1][j] + cells[i + 1][j + 1];
if (NeibourNumber == 3)
NewCells[i][j] = 1;
else if (NeibourNumber == 2)
NewCells[i][j] = cells[i][j];
else
NewCells[i][j] = 0;
}
}
for (int i = 1; i <= High - 1; i++)
for (int j = 1; j <= Width - 1; j++)
cells[i][j] = NewCells[i][j];
}
int main()
{
startup(); // 数据初始化
while (1) // 游戏循环执行
{
show(); // 显示画面
updateWithoutInput(); // 与用户输入无关的更新
}
return 0;
}
总结
这个小游戏是对之前对数组的知识点的补充,后面可能还会在这个基础上添加一些功能。