1. 程式人生 > 其它 >《C語言課程設計與遊戲開發實踐課程》1-3章總結

《C語言課程設計與遊戲開發實踐課程》1-3章總結

目錄:

一、知識點歸納

第二章 知識點

第三章 生命遊戲思考題

二、程式碼實踐

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 }
生命遊戲(細胞分裂)