飞机游戏
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<windows.h> int position_x, position_y;//飞机位置 int bullet_x, bullet_y;//子弹位置 int high, width;//游戏界面边框 int enem_x, enem_y;//敌机位置 int score=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 HideCursor()//隐藏光标函数 { CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; // 第二个值为0表示隐藏光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void startup()//初始化,对数据初始化 { high = 18; width = 30; position_x = high / 2; position_y = width / 2; bullet_y = position_y; bullet_x = -1; enem_x = 0; enem_y = 5; } void show()//绘图更新函数 { int i, j; gotoxy(0, 0); for (i = 0; i < high;i++) { for (j = 0; j < width; j++) { if ((position_x == i) && (position_y == j)) { printf("*");//画飞机 } else if ((i == bullet_x) && (j == bullet_y)) { printf("|");//画子弹 } else if ((i == enem_x) && (j == enem_y)) { printf("@");//画敌机 } else { printf(" "); } } printf("\n"); } printf("得分:%d", score); } void updateWithoutInput() { if ((bullet_x == enem_x) && (bullet_y == enem_y))//如果子弹命中敌机,那么敌机更新,子弹消失,得分加1 { score++; enem_x = 0; enem_y = rand() % width; bullet_x = -1; } if (bullet_x > -1)//如果子弹没有飞出屏幕,就继续飞,飞出去了,就不管了 { bullet_x--; } static int speed = 0; if (speed<5)//设置敌机的飞行速度 { speed++; } else { if (enem_x > high)//如果敌机飞出去了,那么画一个新的敌机 { enem_x = 0; enem_y = rand() % width; } else { enem_x++; speed = 0; } } } void updateWithInput()//用户输入asdw { char input; if (kbhit())//如果有按键,就获取按键 { input = getch(); switch (input) { case 's': position_x++; break; case 'w': position_x--; break; case 'a': position_y--; break; case 'd': position_y++; break; case ' ': bullet_x = position_x-1; bullet_y = position_y; break; default:break; } } } int main() { HideCursor(); startup(); while (1) { show(); updateWithoutInput(); updateWithInput(); } return 0; }弹球
#include<stdio.h> #include<conio.h> #include<Windows.h> int high, width;//画面尺寸 int ball_x, ball_y;//小球位置 int ball_vx, ball_vy;//小球的速度 int speed = 0;//控制小球的飞行速度 int position_x, position_y;//挡板的坐标 int radius;//挡板的半径 int left, right;//挡板的左右宽度 int ball_num;//小球和挡板的碰撞次数 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 }; // 第二个值为0表示隐藏光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void startup()//游戏参数的初始化 { high = 18; width = 25; ball_x = 1; ball_y = width / 2; ball_vx = 1; ball_vy = 1; position_x = high; position_y = width / 2; radius = 5; left = position_y-radius; right = position_y + radius; ball_num = 0; } void show()//绘图 { gotoxy(0, 0);//将光标设置在0,0的位置 int i, j; for (i = 0; i <= high+1; i++) { for (j = 0; j < width; j++) { if ((i == ball_x) && (j == ball_y)) { printf("o");//画小球 } else if (i == high + 1) { printf("-");//画下边界 } else if ((i == high) && (j>left&&j < right)) { printf("*");//画挡板 } else { printf(" "); } } printf("|\n");//画右边界 } printf("反弹小球数:%d\n", ball_num); } void updateWithoutInput() { if (speed < 5)//设置小球的飞行速度 { speed++; } else { if ((ball_x == high-1)) { if (ball_y > left&&ball_y < right)//如果小球碰到了挡板 { ball_vx *= -1; //ball_vy *= -1; ball_num++; } else//没碰到就结束 { printf("游戏结束\n"); exit(0); } } ball_x += ball_vx; ball_y += ball_vy; if ((ball_x == 0)||(ball_x == high)) { ball_vx *= -1; } if ((ball_y == 0) || (ball_y == width-1)) { ball_vy *= -1; } speed = 0; } } void updateWithInput() { char input; if (kbhit()) { input = getch(); switch (input) { case 'a': left--; right--; break; case 'd': left++; right++; break; } } } int main() { HideCursor(); startup(); while (1) { show(); updateWithoutInput(); updateWithInput(); } return 0; }飞鸟
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<Windows.h> int high, width;//游戏界面大小 int brid_x, brid_y;//鸟的位置 int speed=0; int bar_y, bar_xTop, bar_xDown;//障碍物坐标 int score;//得分 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 }; // 第二个值为0表示隐藏光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void startup() { high = 15; width = 25; brid_x = 0; brid_y = width / 5; bar_y = width; bar_xTop = high / 4; bar_xDown = high / 2; score = 0; } void show() { gotoxy(0, 0); int i, j; for (i = 0; i <= high; i++) { for (j = 0; j <= width; j++) { if ((i == brid_x) && (j == brid_y)) { printf("@");//输出小鸟 } else if (i == high) { printf("-");//输出下边界 } else if ((j == bar_y) && (i<=bar_xTop || i>=bar_xDown)) { printf("*"); } else { printf(" "); } } printf("|\n");//输出右边界 } printf("得分:%d\n", score); } void updateWithoutInput() { if (speed < 15) { speed++; } else { brid_x++; if (bar_y > 0) { bar_y--; } else { int randP = rand() % high-5; bar_xTop = randP; bar_xDown = randP + high / 4; bar_y = width; } if ((bar_y == brid_y) ) { if ((brid_x < bar_xDown) && (brid_x > bar_xTop)) { score++; } else { printf("游戏结束\n"); exit(0); } } speed = 0; } } void updateWithInput() { char input; if (kbhit()) { input = getch(); switch (input) { case ' ': brid_x -= 1; break; } } } int main() { HideCursor(); startup(); while (1) { show(); updateWithoutInput(); updateWithInput(); } return 0; }飞机游戏2
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<Windows.h> #define High 20 #define Width 50 #define EnemyNum 5 int canvas[High][Width] = { 0 };//二维数组记录游戏画面中对应的元素 //0输出空格,1输出飞机,2输出子弹,3输出敌机 int position_x, position_y;//飞机的位置 int enemy_x[EnemyNum], enemy_y[EnemyNum];//敌机的位置 int score = 0;//得分 int BulletWidth;//子弹宽度 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 }; // 第二个值为0表示隐藏光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void startup() { position_x = High / 2; position_y = Width / 2; canvas[position_x][position_y] = 1; int k; for (k = 0; k < EnemyNum; k++)//初始化敌机 { enemy_x[k] = rand()%3; enemy_y[k] = rand()%Width; canvas[enemy_x[k]][enemy_y[k]] = 3; } BulletWidth = 5; } void show() { gotoxy(0, 0); int i, j; for (i = 0; i <= High - 1; i++) { for (j = 0; j <= Width - 1; j++) { if (canvas[i][j] == 1) { printf("*");//输出飞机 } else if (canvas[i][j] == 2) { printf("|");//输出子弹 } else if (canvas[i][j] == 3) { printf("@");//输出敌机 } else { printf(" "); } } printf("\n"); }printf("得分:%d\n", score); } void updateWithoutInput() { int i, j,k; for (i = 0; i <= High - 1; i++) { for (j = 0; j <= Width - 1; j++) { if (canvas[i][j] == 2) { for (k = 0; k < EnemyNum; k++) { //击中敌机 if (i == enemy_x[k] && j == enemy_y[k]) { canvas[enemy_x[k]][enemy_y[k]] = 0; enemy_x[k] = rand() % 3; enemy_y[k] = rand() % Width; canvas[enemy_x[k]][enemy_y[k]] = 3; score++; } } //输出子弹 canvas[i][j] = 0; if (i > 0) { canvas[i - 1][j] = 2; } } } } for (k = 0; k<EnemyNum; k++) { if (enemy_x[k] > High)//敌机飞出边界 { canvas[enemy_x[k]][enemy_y[k]] = 0; enemy_x[k] = 0; enemy_y[k] = rand() % Width; canvas[enemy_x[k]][enemy_y[k]] = 3; score--; } } static int speed = 0; if (speed<5) { speed++; } else { for (k = 0; k < EnemyNum; k++) { canvas[enemy_x[k]][enemy_y[k]] = 0; enemy_x[k]++; canvas[enemy_x[k]][enemy_y[k]] = 3; } speed = 0; } } void updateWithInput() { char input; if (kbhit()) { input = getch(); switch (input) { case 'a': canvas[position_x][position_y] = 0; position_y--; canvas[position_x][position_y] = 1; break; case 'd': canvas[position_x][position_y] = 0; position_y++; canvas[position_x][position_y] = 1; break; case 'w': canvas[position_x][position_y] = 0; position_x--; canvas[position_x][position_y] = 1; break; case 's': canvas[position_x][position_y] = 0; position_x++; canvas[position_x][position_y] = 1; break; case ' ': int left, right; left = position_y - BulletWidth; if (left < 0) { left = 0; } right = position_y + BulletWidth; if (right>Width-1) { right = Width-1; } for (int x = left; x < right + 1; x++) { canvas[position_x - 1][x] = 2; } break; default: break; } } } int main() { HideCursor(); startup(); while (1) { show(); updateWithoutInput(); updateWithInput(); } return 0; }