飞机射靶 ZANAC NEO

用c语言实现飞机射靶子的游戏
windows平台下实现
代码如下:
game.h:

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

void HideCursor();
void gotoxy(int x, int y);

test.cpp:
这个里面的kbhit()是用来检测是否有输入的

#include "game.h"
int main()
{
	HideCursor();
	int i, j;
	int x = 5, y = 10;
	char input;
	int isFired = 0;
	int target = 20;
	int isKilled = 0;
	while (1)
	{
		//gotoxy(0, 0);
		system("cls");
		if (isKilled == 0)
		{
			for (i = 0; i < target; i++)
			{
				printf(" ");
			}
			printf("+\n");
		}
		if (isFired == 0)
		{
			for (i = 0; i < x; i++)
			{
				printf("\n");
			}
		}
		else
		{
			for (i = 0; i < x; i++)
			{
				for (j = 0; j < y; j++)
				{
					printf(" ");
				}
				printf("  |\n");
			}
			if (y + 2 == target)
				isKilled = 1;
			isFired = 0;
		}
		for (j = 0; j < y; j++)
		{
			printf(" ");
		}
		printf("  *\n");
		for(j = 0; j < y; j++)
		{
			printf(" ");
		}
		printf("*****\n");
		for (j = 0; j < y; j++)
		{
			printf(" ");
		}
		printf(" * *\n");
		//scanf_s("%c", &input);
		if (_kbhit())
		{
			input = _getch();
			if (input == 'w')
				x--;
			if (input == 's')
				x++;
			if (input == 'a')
				y--;
			if (input == 'd')
				y++;
			if (input == ' ')
			{
				isFired = 1;
			}
		}
	}
	return 0;
}

fix.cpp:
这个cpp文件主要是用来修复闪屏的问题的,如果不用这个修复文件goto函数可以用cls函数实现,在本代码中使用的是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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值