ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

《C语言课程设计与游戏开发实践课程》1-3章总结

2021-11-23 22:35:04  阅读:147  来源: 互联网

标签:map 课程设计 10 int life C语言 sy 课程 include


目录:

一、知识点归纳

      第二章 知识点

       第三章 生命游戏思考题

二、代码实践

       2.1飞机大战

   2.2反弹小球

  2.3flappying bird

  3.1生命游戏

一、知识点归纳

//第一章
system("cls");   // 清屏函数

Sleep(50);  // 等待若干毫秒

if(kbhit())  // 判断是否有输入
        {
            input = getch();  // 不必输入回车
                 }
View Code
 1 //第二章
 2 
 3 //飞机游戏
 4 //解决画面闪烁严重
 5 #include <windows.h>
 6 void gotoxy(int x,int y)  //光标移动到(x,y)位置
 7 {
 8     HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 9     COORD pos;
10     pos.X = x;
11     pos.Y = y;
12     SetConsoleCursorPosition(handle,pos);
13 }
14 
15 void show()  // 显示画面
16 {
17     gotoxy(0,0);  // 光标移动到原点位置,以下重画清屏
18 }
19 
20
21 
22 //解决光标闪烁严重
23 void HideCursor() // 用于隐藏光标
24 {
25     CONSOLE_CURSOR_INFO cursor_info = {1, 0};  // 第二个值为0表示隐藏光标
26     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
27 }
28 
29 void startup() // 数据初始化
30 {
31     HideCursor(); // 隐藏光标
32 }
33 
34 //增加esc键游戏暂停
35 if(input==27)
36 
37 system("pause");
38 
39 
40 //2.2反弹球

1 //开头加,因为kbhit()报4996错
2 #pragma warning(disable:4996);
3 #include <cwindow.h>需要改成#include <windows.h>
判断接触障碍物
if ( (ball_y>=left) && (ball_y<=right) )   // 被挡板挡住
未接触
if ((bird_x >= bar1_xDown) && (bird_x <= bar1_xTop)) //小鸟通过障碍

生成一个范围内的随机数
block_y = rand()%width;  // 产生范围内的新的方块

2.3 flappy bird

障碍物循环出现
if (bar1_y <= 0)  // 再新生成一个障碍物
    {
        bar1_y = width;//从最右边移动
        int temp = rand() % int(high * 0.8);//画面内随机一个缝隙
        bar1_xDown = temp - high / 10;//缝隙点上下留high/10
        bar1_xTop = temp + high / 10;
    }
1 第三章 
2 
3 3.1生命游戏 又称细胞自动机、康威生命游戏
4 规则:以格子内是否存活,与周围8个格子有关,3个生为生,2个为生死不明,1个为死
5 简单的就上下左右加一遍判断
6 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];

思考题: (比较有趣)
尝试修改规则,比如初始化不同的数据,或者3,2的时候都增加;增加另一个物种,食肉动物、食草动物,互相抑制;增加不同地形,比如某块区域有水源,生命更容易生存;交互投食,按+加速、按-减速;模拟生态进化、还有模拟大气污染、谣言传播等等。

“脉冲星”:它周期为3,看起来像一颗爆发的星星

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<time.h>
  4 
  5 const int DATH = 0;
  6 const int ALIVE = 1;
  7 const int maxn = 50;
  8 const int maxr = 100,maxl = 100;
  9 const int dx[] = { -1,-1,-1,0,1,1,1,0 }, dy[] = { -1,0,1,1,1,0,-1,-1 };
 10 
 11 int map[maxr][maxl], newmap[maxr][maxl];
 12 int m, n, general = 0;;
 13 
 14 //初始化,生成随机数(无法避免随机数的浪费)
 15 void rule1()
 16 {
 17     srand(time(NULL));
 18     for(int i = 0;i < m;i++)
 19         for (int j = 0; j < n; j++)
 20             map[i][j] = rand() % 2;        //假设约n/2
 21 }
 22 
 23 //"脉冲星",周期为3
 24 void rule2()
 25 {
 26     for (int i = 0; i < m; i++)
 27         for (int j = 0; j < n; j++)
 28             map[i][j] = 0;
 29     map[4][2] = map[5][2] = map[6][2] = 1;
 30     map[4][7] = map[5][7] = map[6][7] = 1;
 31     map[2][4] = map[2][5] = map[2][6] = 1;
 32     map[7][4] = map[7][5] = map[7][6] = 1;
 33 
 34     map[10][2] = map[11][2] = map[12][2] = 1;
 35     map[10][7] = map[11][7] = map[12][7] = 1;
 36     map[9][4] = map[9][5] = map[9][6] = 1;
 37     map[14][4] = map[14][5] = map[14][6] = 1;
 38 
 39     map[4][9] = map[5][9] = map[6][9] = 1;
 40     map[4][14] = map[5][14] = map[6][14] = 1;
 41     map[2][10] = map[2][11] = map[2][12] = 1;
 42     map[7][10] = map[7][11] = map[7][12] = 1;
 43 
 44     map[10][9] = map[11][9] = map[12][9] = 1;
 45     map[10][14] = map[11][14] = map[12][14] = 1;
 46     map[9][10] = map[9][11] = map[9][12] = 1;
 47     map[14][10] = map[14][11] = map[14][12] = 1;
 48 }
 49 
 50 //计算(x,y)周围存活细胞的个数
 51 int neighbor_num(int x, int y,int map[][maxl])
 52 {
 53     int cnt = 0;
 54     for (int i = 0; i < 8; i++)
 55     {
 56         int nx = x + dx[i], ny = y + dy[i];
 57         if (nx >= 0 && nx < m && ny >= 0 && ny < n && map[nx][ny])  cnt++;
 58     }
 59     return cnt;
 60 }
 61 
 62 //打印第i代的结果
 63 void print_general()
 64 {
 65     printf("第%d代:\n", general);
 66     for (int i = 0; i < m; i++)
 67     {
 68         for (int j = 0; j < n; j++)
 69             if (map[i][j])  printf("■");
 70             else printf("□");
 71         printf("\n");
 72     }
 73 }
 74 
 75 //将map复制到tmp_map
 76 void copy_map(int map[][maxl], int tmp_map[][maxl])
 77 {
 78     for (int i = 0; i < m; i++)
 79         for (int j = 0; j < n; j++)
 80             tmp_map[i][j] = map[i][j];
 81 }
 82 
 83 //得到下一代
 84 void iteration()
 85 {
 86     int tmp_map[maxr][maxl];
 87     copy_map(map, tmp_map);        //保存之前图像,使得当前元素状态的改变还是基于之前的地图,而不是被修改了的
 88     for(int i = 0;i < m;i++)
 89         for (int j = 0; j < n; j++)
 90         {
 91             int cnt = neighbor_num(i, j, tmp_map);
 92             switch (cnt)
 93             {
 94             case 2: continue;
 95             case 3: map[i][j] = ALIVE; break;
 96             default: map[i][j] = DATH; break;
 97             }
 98         }
 99 
100     general++;
101     print_general();
102 }
103 
104 int main()
105 {
106     scanf("%d%d", &m, &n);
107     rule1();
108     print_general();
109     while (1)
110         iteration();
111 
112     return 0;
113 }
脉冲星版

“滑翔者”:每4个回合它会延右下方移动一格,虽然细胞早就不是原来的细胞,但它能保持原来额形状

“轻量级飞船”:它周期为4,每两个“回合”向右走一格

“滑翔者枪”:它会不断的产生一个有一个“滑翔者”

 

 1 for (int sy = 2; sy < 11; sy++) {
 2           for (int ss = 0; ss < 240; ss++) {
 3               s=ss/40;
 4               s=s*40;
 5               if(sy==2){life[sy][s+4]=1;life[sy][s+10]=1;life[sy][s+11]=1;}
 6               else if(sy==3){life[sy][s+2]=1;life[sy][s+6]=1;life[sy][s+10]=1;life[sy][s+11]=1;life[sy][s+12]=1;}
 7               else if(sy==4){life[sy][s+6]=1;life[sy][s+12]=1;life[sy][s+13]=1;life[sy][s+15]=1;life[sy][s+24]=1;}
 8               else if(sy==5){life[sy][s+1]=1;life[sy][s+7]=1;life[sy][s+12]=1;life[sy][s+15]=1;life[sy][s+22]=1;life[sy][s+24]=1;}
 9               else if(sy==6){life[sy][s+1]=1;life[sy][s+2]=1;life[sy][s+12]=1;life[sy][s+13]=1;life[sy][s+15]=1;life[sy][s+21]=1;life[sy][s+23]=1;}
10               else if(sy==7){life[sy][s+10]=1;life[sy][s+11]=1;life[sy][s+12]=1;life[sy][s+20]=1;life[sy][s+23]=1;life[sy][s+35]=1;life[sy][s+36]=1;}
11               else if(sy==8){life[sy][s+10]=1;life[sy][s+11]=1;life[sy][s+21]=1;life[sy][s+23]=1;life[sy][s+35]=1;life[sy][s+36]=1;}
12               else if(sy==9){life[sy][s+22]=1;life[sy][s+24]=1;}
13               else if(sy==10){life[sy][s+24]=1;}
14           }
15       }

 

“繁殖者”:它会向右进行,留下一个接一个的“滑翔者枪”

 

参看了该篇博客https://www.cnblogs.com/lfri/p/9733883.html

二、代码实战

 

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

// 全局变量
int position_x,position_y; // 飞机位置
int bullet_x,bullet_y; // 子弹位置
int enemy_x,enemy_y; // 敌机位置
int high,width; //  游戏画面尺寸
int score; // 得分

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 HideCursor() // 用于隐藏光标
{
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};  // 第二个值为0表示隐藏光标
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}


void startup() // 数据初始化
{
    high = 20;
    width = 30;
    position_x = high/2;
    position_y = width/2;
    bullet_x = -2;
    bullet_y = position_y;
    enemy_x = 0;
    enemy_y = position_y;
    score = 0;

    HideCursor(); // 隐藏光标
}

void show()  // 显示画面
{
    gotoxy(0,0);  // 光标移动到原点位置,以下重画清屏
    int i,j;
    for (i=0;i<high;i++)
    {
        for (j=0;j<width;j++)
        {
            if ((i==position_x) && (j==position_y))
                printf("*");  //   输出飞机*
            else if ((i==enemy_x) && (j==enemy_y))
                printf("@");  //   输出敌机@
            else if ((i==bullet_x) && (j==bullet_y))
                printf("|");  //   输出子弹|
            else
                printf(" ");  //   输出空格
        }
        printf("\n");
    }
    printf("得分:%d\n",score);
}    

void updateWithoutInput()  // 与用户输入无关的更新
{    
    if (bullet_x>-1)
        bullet_x--; 
    
    if ((bullet_x==enemy_x) && (bullet_y==enemy_y))  // 子弹击中敌机
    {
        score++;                // 分数加1
        enemy_x = -1;           // 产生新的飞机
        enemy_y = rand()%width;
        bullet_x = -2;          // 子弹无效
    }
    if (enemy_x>high)   // 敌机跑出显示屏幕
    {
        enemy_x = -1;           // 产生新的飞机
        enemy_y = rand()%width;
    }
    
    // 用来控制敌机向下移动的速度。每隔几次循环,才移动一次敌机
    // 这样修改的话,用户按键交互速度还是保持很快,但我们NPC的移动显示可以降速
    static int speed = 0;  
    if (speed<20)
        speed++;
    if (speed == 20)
    {
        enemy_x++;            
        speed = 0;
    }
}

void updateWithInput()  // 与用户输入有关的更新
{
    char input;
    if(kbhit())  // 判断是否有输入
    {
        input = getch();  // 根据用户的不同输入来移动,不必输入回车
        if (input == 'a')   
            position_y--;  // 位置左移
        if (input == 'd')
            position_y++;  // 位置右移
        if (input == 'w')
            position_x--;  // 位置上移
        if (input == 's')
            position_x++;  // 位置下移
        if (input == ' ')  // 发射子弹
        {
            bullet_x = position_x-1;  // 发射子弹的初始位置在飞机的正上方
            bullet_y = position_y;
        }
        
    }
}

int main()
{
    startup();  // 数据初始化    
    while (1) //  游戏循环执行
    {
        show();  // 显示画面
        updateWithoutInput();  // 与用户输入无关的更新
        updateWithInput();  // 与用户输入有关的更新
    }
    return 0;
}
飞机游戏

 

  1 #pragma warning(disable:4996);
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <conio.h>
  5 #include <windows.h>
  6 
  7 // 全局变量
  8 int high, width; // 游戏画面大小
  9 int ball_x, ball_y; // 小球的坐标
 10 int ball_vx, ball_vy; // 小球的速度
 11 int position_x, position_y; // 挡板中心坐标
 12 int ridus;  // 挡板半径大小
 13 int left, right; // 挡板左右位置
 14 int ball_number;  // 反弹小球的次数
 15 int block_x, block_y; // 方块的位置
 16 int score; // 消掉方块的个数
 17 
 18 void gotoxy(int x, int y) //光标移动到(x,y)位置
 19 {
 20     HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 21     COORD pos;
 22     pos.X = x;
 23     pos.Y = y;
 24     SetConsoleCursorPosition(handle, pos);
 25 }
 26 
 27 void startup()  // 数据初始化
 28 {
 29     high = 13;
 30     width = 17;
 31     ball_x = 0;
 32     ball_y = width / 2;
 33     ball_vx = 1;
 34     ball_vy = 1;
 35     ridus = 6;
 36     position_x = high;
 37     position_y = width / 2;
 38     left = position_y - ridus;
 39     right = position_y + ridus;
 40     ball_number = 0;
 41     block_x = 0;
 42     block_y = width / 2 + 1;
 43     score = 0;
 44 }
 45 
 46 void show()  // 显示画面
 47 {
 48     gotoxy(0, 0);    // 光标移动到原点位置,以下重画清屏    
 49     int i, j;
 50     for (i = 0;i <= high + 1;i++)
 51     {
 52         for (j = 0;j <= width;j++)
 53         {
 54             if ((i == ball_x) && (j == ball_y))
 55                 printf("0");  //   输出小球
 56             else if (j == width)
 57                 printf("|");  //   输出右边框
 58             else if (i == high + 1)
 59                 printf("-");  //   输出下边框
 60             else if ((i == high) && (j > left) && (j < right))
 61                 printf("*");  //   输出挡板
 62             else if ((i == block_x) && (j == block_y))
 63                 printf("B");  //   输出方块
 64             else
 65                 printf(" ");  //   输出空格
 66         }
 67         printf("\n");
 68     }
 69     printf("反弹小球数:%d\n", ball_number);
 70     printf("消掉的方块数:%d\n", score);
 71 }
 72 
 73 void updateWithoutInput()  // 与用户输入无关的更新
 74 {
 75     if (ball_x == high - 1)
 76     {
 77         if ((ball_y >= left) && (ball_y <= right))   // 被挡板挡住
 78         {
 79             ball_number++;
 80             printf("\a"); // 响铃
 81             //ball_y = ball_y + rand()%4-2;
 82         }
 83         else    // 没有被挡板挡住
 84         {
 85             printf("游戏失败\n");
 86             system("pause");
 87             exit(0);
 88         }
 89     }
 90 
 91     if ((ball_x == block_x) && (ball_y == block_y))  // 小球击中方块
 92     {
 93         score++;                 // 分数加1
 94         block_y = rand() % width;  // 产生新的方块
 95     }
 96 
 97     ball_x = ball_x + ball_vx;
 98     ball_y = ball_y + ball_vy;
 99 
100     if ((ball_x == 0) || (ball_x == high - 1))
101         ball_vx = -ball_vx;
102     if ((ball_y == 0) || (ball_y == width - 1))
103         ball_vy = -ball_vy;
104 
105     Sleep(80);
106 }
107 
108 void updateWithInput()  // 与用户输入有关的更新
109 {
110     char input;
111     if (kbhit())  // 判断是否有输入
112     {
113         input = getch();  // 根据用户的不同输入来移动,不必输入回车
114         if (input == 'a')
115         {
116             position_y--;  // 位置左移
117             left = position_y - ridus;
118             right = position_y + ridus;
119         }
120         if (input == 'd')
121         {
122             position_y++;  // 位置右移
123             left = position_y - ridus;
124             right = position_y + ridus;
125         }
126     }
127 }
128 
129 int main()
130 {
131     startup();  // 数据初始化    
132     while (1)  //  游戏循环执行
133     {
134         show();  // 显示画面
135         updateWithoutInput();  // 与用户输入无关的更新
136         updateWithInput();     // 与用户输入有关的更新
137     }
138     return 0;
139 }
2.2反弹小球
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <conio.h>
  4 #include <cwindow.h>
  5 
  6 // 全局变量
  7 int high,width; // 游戏画面大小
  8 int bird_x,bird_y; // 小鸟的坐标
  9 int bar1_y,bar1_xDown,bar1_xTop; // 障碍物1的相关坐标
 10 int score; // 得分,经过障碍物的个数
 11 
 12 void gotoxy(int x,int y) //光标移动到(x,y)位置
 13 {
 14     HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 15     COORD pos;
 16     pos.X = x;
 17     pos.Y = y;
 18     SetConsoleCursorPosition(handle,pos);
 19 }
 20 
 21 void startup()  // 数据初始化
 22 {
 23     high = 20;
 24     width = 20;
 25     bird_x = high/2;
 26     bird_y = 3;
 27     bar1_y = width;
 28     bar1_xDown = high/3;
 29     bar1_xTop = high/2;
 30     score = 0;
 31 }
 32 
 33 void show()  // 显示画面
 34 {
 35     gotoxy(0,0);  // 光标移动到原点位置,以下重画清屏    
 36     int i,j;
 37     
 38     for (i=0;i<high;i++)
 39     {
 40         for (j=0;j<width;j++)
 41         {
 42             if ((i==bird_x) && (j==bird_y))
 43                 printf("@");  //   输出小鸟
 44             else if ((j==bar1_y) && ((i<bar1_xDown)||(i>bar1_xTop)) )
 45                 printf("*");  //   输出墙壁
 46             else
 47                 printf(" ");  //   输出空格
 48         }
 49         printf("\n");
 50     }
 51     printf("得分:%d\n",score);
 52 }    
 53 
 54 void updateWithoutInput()  // 与用户输入无关的更新
 55 {
 56     bird_x ++;
 57     bar1_y --;
 58     if (bird_y==bar1_y)
 59     {
 60         if ((bird_x>=bar1_xDown)&&(bird_x<=bar1_xTop))
 61             score++;
 62         else
 63         {
 64             printf("游戏失败\n");
 65             system("pause");
 66             exit(0);
 67         }
 68     }
 69     if (bar1_y<=0)  // 再新生成一个障碍物
 70     {
 71         bar1_y = width;
 72         int temp = rand()%int(high*0.8);
 73         bar1_xDown = temp - high/10;
 74         bar1_xTop = temp + high/10;
 75     }
 76     
 77     Sleep(150);
 78 }
 79 
 80 void updateWithInput()  // 与用户输入有关的更新
 81 {    
 82     char input;
 83     if(kbhit())  // 判断是否有输入
 84     {
 85         input = getch();  // 根据用户的不同输入来移动,不必输入回车
 86         if (input == ' ')  
 87             bird_x = bird_x - 2;
 88     }
 89 }
 90 
 91 int main()
 92 {
 93     startup();  // 数据初始化    
 94     while (1)  //  游戏循环执行
 95     {
 96         show();  // 显示画面
 97         updateWithoutInput();  // 与用户输入无关的更新
 98         updateWithInput();     // 与用户输入有关的更新
 99     }
100     return 0;
101 }
2.3flappy bird

 

 飞机游戏

反弹小球

 

 

 flappy bird

 

第三章

生命游戏

由于生命游戏很有意思,实现了更为复杂的版本

  1 #include<stdio.h>
  2 #include <unistd.h>
  3 #include<time.h>
  4 #include<memory.h>
  5 #include<string>
  6 #include <cstdlib>
  7 using namespace std;
  8 
  9 
 10 
 11 int life[80][240] = {0};
 12 int lifen[80][240]= {0};
 13 int mm, nn,s,times=0;
 14 int main(){
 15       for (int sj = 0; sj < 80; sj++) {
 16           for (int sm = 0; sm < 240; sm++) {
 17               life[sj][sm] =0;
 18               lifen[sj][sm] = 0;
 19           }
 20       }
 21       
 22       for (int sy = 2; sy < 11; sy++) {
 23           for (int ss = 0; ss < 240; ss++) {
 24               s=ss/40;
 25               s=s*40;
 26               if(sy==2){life[sy][s+4]=1;life[sy][s+10]=1;life[sy][s+11]=1;}
 27               else if(sy==3){life[sy][s+2]=1;life[sy][s+6]=1;life[sy][s+10]=1;life[sy][s+11]=1;life[sy][s+12]=1;}
 28               else if(sy==4){life[sy][s+6]=1;life[sy][s+12]=1;life[sy][s+13]=1;life[sy][s+15]=1;life[sy][s+24]=1;}
 29               else if(sy==5){life[sy][s+1]=1;life[sy][s+7]=1;life[sy][s+12]=1;life[sy][s+15]=1;life[sy][s+22]=1;life[sy][s+24]=1;}
 30               else if(sy==6){life[sy][s+1]=1;life[sy][s+2]=1;life[sy][s+12]=1;life[sy][s+13]=1;life[sy][s+15]=1;life[sy][s+21]=1;life[sy][s+23]=1;}
 31               else if(sy==7){life[sy][s+10]=1;life[sy][s+11]=1;life[sy][s+12]=1;life[sy][s+20]=1;life[sy][s+23]=1;life[sy][s+35]=1;life[sy][s+36]=1;}
 32               else if(sy==8){life[sy][s+10]=1;life[sy][s+11]=1;life[sy][s+21]=1;life[sy][s+23]=1;life[sy][s+35]=1;life[sy][s+36]=1;}
 33               else if(sy==9){life[sy][s+22]=1;life[sy][s+24]=1;}
 34               else if(sy==10){life[sy][s+24]=1;}
 35           }
 36       }
 37     int m,t,e;
 38     while(1){
 39 
 40         for(int y=1;y<79;y++)
 41         {
 42             for(int x=1;x<239;x++)
 43             {
 44                 if(life[y][x]==1)
 45                 {    
 46                     m=0;
 47                     m = life[y - 1][x - 1] + life[y - 1][x] + life[y - 1][x + 1] + life[y][x - 1] + life[y][x + 1] + life[y + 1][x - 1] + life[y + 1][x + 1] + life[y + 1][x];
 48                     if(m==2){
 49                         lifen[y][x]=1;
 50                     }
 51                     else if(m==3){
 52                         lifen[y][x]=1;
 53                     }
 54                     else lifen[y][x]=0;
 55                 }
 56                 if (life[y][x] == 0)
 57                 {
 58                     m = 0;
 59                     m = life[y - 1][x - 1] + life[y - 1][x] + life[y - 1][x + 1] + life[y][x - 1] + life[y][x + 1] + life[y + 1][x - 1] + life[y + 1][x +1] + life[y + 1][x];
 60 
 61                     if (m == 3) {
 62                         lifen[y][x] = 1;
 63                     }
 64                     else lifen[y][x] = 0;
 65                 }
 66             }
 67         }
 68         for(int my=0;my<80;my++)
 69         {
 70             for(int mx=0;mx<240;mx++)
 71             {
 72                 life[my][mx]=lifen[my][mx];
 73             }
 74         }
 75         //if (times %==1) {
 76             
 77             printf("start print:%d\n",times);
 78             for (int py = 0; py < 80; py++)
 79             {
 80                 for (int px = 0; px < 240; px++)
 81                 {
 82                     if (life[py][px] == 0) {
 83                         //color(7);
 84                         printf("\033[47m*\033[0m");
 85                         //color(1);
 86                     }
 87                     else {
 88                         //color(1);
 89                         printf("\033[41m*\033[0m");
 90                         //color(7);
 91                     }
 92                 }
 93                 printf("\n");
 94             }
 95             usleep(100000);
 96             system("clear");
 97         //}
 98         times++;
 99     }
100 }
生命游戏滑翔机版
  1 #include<stdio.h>
  2 #include <unistd.h>
  3 #include<time.h>
  4 #include<memory.h>
  5 #include<string>
  6 #include <cstdlib>
  7 using namespace std;
  8 
  9 
 10 
 11 int life[80][400];
 12 int lifen[80][400];
 13 int lifes[80][400];
 14 int period[80][400];
 15 int mm, nn,s,times=0,ry,rx;
 16 int main(){
 17       for (int sj = 0; sj < 80; sj++) {//初始化所有生命为死亡
 18           for (int sm = 0; sm < 400; sm++) {
 19               life[sj][sm] =0;
 20               lifen[sj][sm] = 0;
 21               lifes[sj][sm] = 0;
 22               period[80][400];
 23           }
 24       }
 25       life[40][120]=1;period[40][120]=5;lifes[40][120]=1;//再一个点初始化一个生命
 26       srand((unsigned)time(NULL));
 27     int m,t,e;
 28     while(1){
 29 
 30             if(life[ry=rand()%80][rx=rand()%400]==1){//生命会随机的发生位移
 31                life[ry][rx]==0;
 32                life[ry=rand()%80][rx=rand()%400]=1;period[ry][rx]=5;lifes[ry][rx]=1;
 33             }
 34         for(int y=1;y<79;y++)
 35         {//开始遍历数组
 36             for(int x=1;x<399;x++)
 37             {
 38                 if(life[y][x]==1)
 39                 {    
 40                     m=0;
 41                     m = life[y - 1][x - 1] + life[y - 1][x] + life[y - 1][x + 1] + life[y][x - 1] + life[y][x + 1] + life[y + 1][x - 1] + life[y + 1][x + 1] + life[y + 1][x];
 42                     if(m==0){
 43                         lifen[y][x]=1;
 44                     }
 45                     else if(m==1){
 46                         lifen[y][x]=1;
 47                     }
 48                     else if(m==2){
 49                         lifen[y][x]=1;
 50                     }
 51                     else {lifen[y][x]=0;period[y][x]==0;}
 52                     
 53                     if(period[y][x]!=0)
 54                        period[y][x]=period[y][x]-1;//生命周期减少
 55                     else if(period[y][x]==0)lifen[y][x] = 0;
 56                 }
 57                 else if (life[y][x] == 0)
 58                 {
 59                     m = 0;
 60                     mm=(rand()%2);
 61                     if(mm==1){
 62                     m = lifes[y - 1][x - 1] + lifes[y - 1][x] + lifes[y - 1][x + 1] + lifes[y][x - 1] + lifes[y][x + 1] + lifes[y + 1][x - 1] + lifes[y + 1][x +1] + lifes[y + 1][x];
 63                     if(lifes[y - 1][x - 1]==1)lifes[y - 1][x - 1]=0;
 64                     else if(lifes[y - 1][x]==1)lifes[y - 1][x]=0;
 65                     else if(lifes[y - 1][x+1]==1)lifes[y - 1][x+1]=0;
 66                     else if(lifes[y][x-1]==1)lifes[y][x-1]=0;
 67                     else if(lifes[y][x+1]==1)lifes[y][x+1]=0;
 68                     else if(lifes[y+1][x-1]==1)lifes[y + 1][x-1]=0;
 69                     else if(lifes[y+1][x]==1)lifes[y + 1][x]=0;
 70                     else if(lifes[y+1][x+1]==1)lifes[y + 1][x+1]=0;
 71                     }
 72                     if (m != 0) {
 73                         lifen[y][x] = 1;period[y][x]=5;//新生命诞生,初始化生命周期
 74                     }
 75                     else {lifen[y][x]=0;period[y][x]==0;}
 76                 }
 77             }
 78         }
 79 
 80         for(int my=0;my<80;my++)
 81         {
 82             for(int mx=0;mx<400;mx++)
 83             {
 84                 life[my][mx]=lifen[my][mx];
 85                 lifes[my][mx]=lifen[my][mx];
 86             }
 87         }
 88             printf("start print:%d\n",times);
 89             for (int py = 0; py < 80; py++)
 90             {
 91                 for (int px = 0; px < 400; px++)
 92                 {
 93                     if (life[py][px] == 0) {
 94                         printf("\033[47m*\033[0m");
 95                     }
 96                     else {
 97                         printf("\033[41m*\033[0m");
 98                     }
 99                 }
100                 printf("\n");
101             }
102             usleep(300000);
103             system("clear");
104         times++;
105     }
106 }
生命游戏(细胞分裂)

 

标签:map,课程设计,10,int,life,C语言,sy,课程,include
来源: https://www.cnblogs.com/sylvia1111/p/15595781.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有