飞机大战游戏加强版

  • Post author:
  • Post category:其他


Aircraft wars PLUS

用c语言实现打飞机的效果,子弹可以加强效果

windows平台下实现

代码如下:

game.h:

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

void HideCursor();
void gotoxy(int x, int y);
void DataInit();
void Show();
void UpdateWithoutInput();
void UpdateWithInput();

#define HIGH 15
#define WIDTH 25
#define ENEMYNUM 5

test.cpp:

#include "game.h"

int position_x, position_y;
int enemy_x[ENEMYNUM];
int enemy_y[ENEMYNUM];
int canvas[HIGH][WIDTH] = {0};
int BulletWidth;
int EnemyMoveSpeed;
int score;

int main()
{
	HideCursor();   //隐藏光标
	DataInit();     //数据初始化
	while (1)
	{
		Show();         //显示画面
		UpdateWithoutInput();     //与用户无关的数据更新
		UpdateWithInput();        //与用户有关的数据更新
	}
	return 0;
}

game.cpp:

#include "game.h"

extern int position_x, position_y;
extern int enemy_x[ENEMYNUM];
extern int enemy_y[ENEMYNUM];
extern int canvas[HIGH][WIDTH];
extern int BulletWidth;
extern int EnemyMoveSpeed;
extern int score;

void DataInit()
{
	position_x = HIGH  - 1;
	position_y = WIDTH / 2;
	canvas[position_x][position_y] = 1;

	int k;
	for (k = 0; k < ENEMYNUM; k++)
	{
		enemy_x[k] = 0;
		enemy_y[k] = rand() % WIDTH;
	}
	
	score = 0;
	BulletWidth = 0;
	EnemyMoveSpeed = 20;
}

void Show()
{
	gotoxy(0, 0);

	int i, j;
	for (i = 0; i < HIGH; i++)
	{
		for (j = 0; j < WIDTH; j++)
		{
			if (canvas[i][j] == 0)
			{
				printf(" ");
			}
			else if (canvas[i][j] == 1)//自己
			{
				printf("*");
			}
			else if (canvas[i][j] == 2)//子弹
			{
				printf("|");
			}
			else if (canvas[i][j] == 3)//敌机
			{
				printf("@");
			}
		}
		printf("\n");
	}

	printf("the score is: %d\n", score);
}

void UpdateWithoutInput()
{
	int i = 0, j = 0, k = 0;
	for (i = 0; i < HIGH; i++)
	{
		for (j = 0; j < WIDTH; j++)
		{
			if (canvas[i][j] == 2)
			{
				for (k = 0; k < ENEMYNUM; k++)//子弹击中敌机
				{
					if (i == enemy_x[k] && j == enemy_y[k])
					{
						score++;

						if (score % 5 == 0 && EnemyMoveSpeed > 3)//敌机升级
						{
							EnemyMoveSpeed--;
						}

						if (score % 5 == 0 && BulletWidth < 4)//子弹升级
						{
							BulletWidth++;
						}

						canvas[enemy_x[k]][enemy_y[k]] = 0;//产生新的敌机
						enemy_x[k] = rand() % 2;
						enemy_y[k] = rand() % WIDTH;
						canvas[enemy_x[k]][enemy_y[k]] = 3;
						canvas[i][j] = 0;

					}
				}

				canvas[i][j] = 0;//子弹移动
				if (i > 0)
				{
					canvas[i - 1][j] = 2;
				}
			}
		}
	}

	static int speed = 0;
	if (speed < EnemyMoveSpeed)
	{
		speed++;
	}

	for (k = 0; k < ENEMYNUM; k++)
	{
		if (enemy_x[k] == position_x && enemy_y[k] == position_y)//与敌机碰撞
		{
			printf("WASTED!!!!\n");
			system("pause");
			exit(0);
		}

		if (enemy_x[k] > HIGH) //敌机跑出屏幕
		{
			canvas[enemy_x[k]][enemy_y[k]] = 0;
			enemy_x[k] = rand() % 2;
			enemy_y[k] = rand() % WIDTH;
			canvas[enemy_x[k]][enemy_y[k]] = 3;
			score--;
		}

		if (speed == EnemyMoveSpeed)//敌机位置更新
		{
			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 == 'w' && position_x > 1)
		{
			canvas[position_x][position_y] = 0;
			position_x--;
			canvas[position_x][position_y] = 1;
		}
		if (input == 's' && position_x < HIGH - 1)
		{
			canvas[position_x][position_y] = 0;
			position_x++;
			canvas[position_x][position_y] = 1;
		}
		if (input == 'a' && position_y > 1)
		{
			canvas[position_x][position_y] = 0;
			position_y--;
			canvas[position_x][position_y] = 1;
		}
		if (input == 'd' && position_y < WIDTH - 1)
		{
			canvas[position_x][position_y] = 0;
			position_y++;
			canvas[position_x][position_y] = 1;
		}

		if (input == ' ')
		{
			int left = position_y - BulletWidth;
			int right = position_y + BulletWidth;

			if (left < 0)
			{
				left = 0;
			}
			if (right > WIDTH)
			{
				right = WIDTH;
			}

			int k = 0;
			for (k = left; k <= right; k++)
			{
				canvas[position_x - 1][k] = 2;
			}

		}
	}
}

fix.cpp:

主要用来修复画面闪屏和光标隐藏问题

可用cls函数代替实现

#include "game.h"
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}



版权声明:本文为m0_60867520原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。