用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);
}