//简易飞机大战游戏代码
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
//函数外全局变量定义
int high, width; //游戏画面尺寸
int position_x, position_y; //飞机位置
int bullet_x, bullet_y; //子弹位置
int enemy_x,enemy_y; //敌机位置
int score; //得分
void HideCursor() //隐藏光标函数
{
_CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; //第二值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
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 starup() //数据初始化
{
high = 18;
width = 30;
position_x = high / 2;
position_y = width / 2;
bullet_x = -1;
bullet_y = position_y;
enemy_x = 0;
enemy_y = width / 2;
HideCursor(); //隐藏光标
}
void show() //显示画面
{
gotoxy(0, 0); //光标移动到原点位置,一下重画清屏
int i, j;
//system("cls"); //清屏
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 == enemy_x) && (bullet_y == enemy_y))
{
score++;
enemy_x = 0;
enemy_y = rand() % width;
bullet_x = -1;
}
static int speed = 0; //用于控制敌机下落速度
if (speed < 10)
speed++;
if(bullet_x > -1) //子弹的移动
bullet_x--;
if (enemy_x > high)
{
enemy_x = 0;
enemy_y = rand() % width;
}
else {
if (speed == 10) {
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()
{
starup(); //数据初始化
while (1) { //游戏执行
show(); //显示画面
UpdateWithoutInput(); //与用户输入无关的更新
UpdateWithInput(); //与用户输入有关的更新
}
return 0;
}
生命周期游戏代码
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define Hight 40 //定义框架
#define Width 70
//函数外全局变量定义
int cells[Hight][Width];
void HideCursor() //隐藏光标函数
{
_CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; //第二值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
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 starup() //数据初始化
{
int i, j;
for (i = 0; i < Hight; i++)
for (j = 0; j < Width; j++)
cells[i][j] = rand() % 2; //细胞初始
HideCursor(); //隐藏光标
}
void show() //显示画面
{
gotoxy(0, 0); //光标移动到原点位置,一下重画清屏
int i, j;
for (i = 0; i < Hight; i++)
{
for (j = 0; j < Width; j++)
{
if (cells[i][j] == 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
void UpdateWithoutInput() //与用户输入无关的更新
{
int i, j;
int tempCells[Hight][Width]; //临时存储的二维数组
for (i = 0; i < Hight; i++)
for (j = 0; j < Width; j++)
tempCells[i][j] = cells[i][j];
int NeibourNumber = 0;
for (i = 1; i < Hight - 1; i++)
{
for (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] + cells[i][j + 1]
+ cells[i + 1][j - 1] + cells[i + 1][j] + cells[i + 1][j + 1];
if (NeibourNumber == 3)
tempCells[i][j] = 1;
else if (NeibourNumber == 2 || NeibourNumber == 4)
tempCells[i][j] = cells[i][j];
else
tempCells[i][j] = 0;
}
}
for (i = 0; i < Hight; i++)
for (j = 0; j < Width; j++)
cells[i][j] = tempCells[i][j];
Sleep(150);
}
void UpdateWithInput() //与用户输入有关的更新
{
}
int main()
{
starup(); //数据初始化
while (1) { //游戏执行
show(); //显示画面
UpdateWithoutInput(); //与用户输入无关的更新
UpdateWithInput(); //与用户输入有关的更新
}
return 0;
}
飞机大战1.0
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define High 18
#define Width 40
#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; //得分
void HideCursor() //隐藏光标函数
{
_CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; //第二值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
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 starup() //数据初始化
{
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;
}
score = 0;
HideCursor(); //隐藏光标
}
void show() //显示画面
{
gotoxy(0, 0); //光标移动到原点位置,一下重画清屏
int i, j;
//system("cls"); //清屏
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] = 0;
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 < 10)
speed++;
if (speed == 10) //子弹的移动
{
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();
if (input == 'a') {
canvas[position_x][position_y] = 0;
position_y--;
canvas[position_x][position_y] = 1;
}
if (input == 'd') {
canvas[position_x][position_y] = 0;
position_y++;
canvas[position_x][position_y] = 1;
}
if (input == 'w') {
canvas[position_x][position_y] = 0;
position_x--;
canvas[position_x][position_y] = 1;
}
if (input == 's') {
canvas[position_x][position_y] = 0;
position_x++;
canvas[position_x][position_y] = 1;
}
if (input == ' ')
{
canvas[position_x - 1][position_y] = 2;
}
}
}
int main()
{
starup(); //数据初始化
while (1) { //游戏执行
show(); //显示画面
UpdateWithoutInput(); //与用户输入无关的更新
UpdateWithInput(); //与用户输入有关的更新
}
return 0;
}
C语言简易飞机游戏
最新推荐文章于 2024-08-13 19:47:04 发布