飞翔的小鸟小游戏
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>#define a 2typedef struct Bird
{int width, high; //屏幕的寬、高 int bird_x, bird_y; //鳥的位置 int wall_top, wall_down; //上下障礙物的上表面與上表面位置 int wall_x; //障礙物出現的位置 int score; //分數
}Bird;void gotoxy(int x, int 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 };SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}void Init(Bird *bird)//初始化結構體成員
{bird->width = 30;bird->high = 30;bird->wall_top = 13;bird->wall_down = 18;bird->bird_x = 10;bird->bird_y = 10;bird->wall_x = 17;bird->score = 0;HideCursor();system("mode con cols=100 lines=80");
}void show(Bird *bird)//顯示游戲畫面
{gotoxy(0, 0);static int i = 0;i++;if (i == 10)//main函數的循環執行10次進行一次清屏 {system("cls");}for (int y = 0; y < bird->high; y++){for (int x = 0; x < bird->width; x++){if ((x == bird->wall_x) && ((y < bird->wall_top) || (y > bird->wall_down)))//顯示障礙物 printf("*");else if ((x == bird->bird_x) && (y == bird->bird_y)) //顯示鳥 printf("@");elseprintf(" ");}printf("\n");}printf("得分:%d\n", bird->score);
}void PeopleVin(Bird *bird)//玩家控制小鳥運動
{char input;if (kbhit()){input = getch();switch (input){case 32:bird->bird_y -= 2; break; //空格鍵上升case 80:bird->bird_y++; break; //下鍵下降}}
}int Updatecomputer(Bird *bird)//電腦自主更新的數據
{bird->bird_y++; //小鳥下降Sleep(100);bird->wall_x--; //墻往左移if (bird->bird_x == bird->wall_x)//當墻的位置與小鳥的位置相同時,小鳥從空白區域通過時分數加1{if ((bird->bird_y<bird->wall_down) && (bird->bird_y>bird->wall_top)){bird->score++;}else{bird->bird_x--;int t = 1;while (bird->bird_y < bird->high)//小鳥碰到障礙物時,小鳥沿墻壁加速下降,最終退出游戲{bird->bird_y = bird->bird_y+(t*t);show(bird);t++;Sleep(50);}printf("游戲結束\n");return 0;}}if (bird->wall_x < 0)//更新墻出現的位置,及安全區域 (bird->wall_top,bird->wall_down){bird->wall_x = rand() % 7 + 15;bird->wall_top = rand() % (bird->high / 2) + 5;bird->wall_down = bird->wall_top + 5;}return 1;
}int main()
{Bird bird;int flag = 1;Init(&bird);while (flag){show(&bird);PeopleVin(&bird);flag=Updatecomputer(&bird);}system("pause");return 0;
}
總結
- 上一篇: python爬虫程序requests采用
- 下一篇: Mapreduce的排序、全排序以及二次