简单的飞机大战(C语言版)
这个游戏的编译环境为visual studio2019(我编写时是这个)这个游戏呢也是写了一天半,是太累了,要写学校的作业(唉!)。有些问题调试了很久结果是因为自己写程序已经走火入魔了,有些细小的问题都没发现导致这么长的开发时间。废话不多说,上代码。大家有兴趣可以自己拿。
//
#include<stdio.h>
#include <graphics.h>
#include <Windows.h>
#include <conio.h>
#include <stdlib.h>
#define all_X 900
#define Win_X 600
#define Win_Y 480
#define ene_speed 1 //敌人的速度
#define bull_speed 7 //子弹的速度
#define my_speed 5 //自己的速度
#define my_side 30 //我方宽度
#define ene_side 10 //敌方宽度
#define steup_X 200 //
#define steup_Y 0 //初始位置
#define bull_side_X 5 //子弹的宽度
#define bull_side_Y 13 //子弹长度
#define ski_long 3 //技能宽度
#define ski_speed 5 //速度
typedef struct
{
int y;
int ski_move;
}ski_s; //技能结构体
typedef struct //自己的结构体
{
int x;
int y;
}MY;
typedef struct //敌人的结构体
{
int x;
int y;
int ene_move;
}ENE;
typedef struct //子弹的结构体
{
int x;
int y;
int bull_move;
}BULL;
void cle(int x1, int y1, int x2, int y2) //清空长方形区域参数分别
{
HRGN rgn = CreateRectRgn(x1, y1, x2, y2); //
setcliprgn(rgn);
clearcliprgn();
}
void beg(void) //充入缓冲区
{
BeginBatchDraw();
cleardevice();
}
void end(void) //显示在屏幕上
{
FlushBatchDraw();
}
void draw_ene(int x, int y)
{
for (int i = x; i <= x + ene_side; i++)
for (int j = y; j <= y + ene_side; j++)
putpixel(i, j, RED);
}
void draw_my(int x, int y)
{
for (int i = x; i <= x + my_side; i++)
for (int j = y; j <= y + my_side; j++)
putpixel(i, j, GREEN);
}
void draw_bull(int x, int y)
{
//cle(x, y, x + bull_side_X, y + bull_side_Y);
for(int i = x; i <= x + bull_side_X; i++)
for(int j = y; j <= y + bull_side_Y; j++)
putpixel(i, j, BLUE);
}
void GetKey(MY* p) //我方移动的函数
{
if (p->x < Win_X && p->x > 0 && p->y < Win_Y && p->y > 0)
{
if (GetKeyState('W') < 0)
p->y -= my_speed;
if (GetKeyState('S') < 0)
p->y += my_speed;
if (GetKeyState('A') < 0)
p->x -= my_speed;
if (GetKeyState('D') < 0)
p->x += my_speed;
}
else
{
if (p->x >= Win_X)
{
if (GetKeyState('A') < 0)
p->x -= my_speed;
}
else if(p->x <= 0)
{
if (GetKeyState('D') < 0)
p->x += my_speed;
}
else if (p->y >= Win_Y)
{
if (GetKeyState('W') < 0)
p->y -= my_speed;
}
else if (p->y <= 0)
{
if (GetKeyState('S') < 0)
p->y += my_speed;
}
}
}
int drand(int Max, int Min)
{
return (rand() % Max + Min);
}
void draw_ski(int y)
{
for (int i = y; i <= y + ski_long; i++)
line(0, i, Win_X, i);
}
int main()
{
int ski_move = 0;
ski_s ski;
ski.ski_move = 0;
Beep(2000, 30);
IMAGE buffer;
TCHAR text[20];
int score = 0;
MY my; //自己的结构体
ENE ene[2010]; //敌人的结构体
BULL bull[2010]; //子弹的结构体
for (int i = 0; i <= 2000; i++)
{
ene[i].x = 0;
ene[i].y = 0;
}
int ene_move = 0;
int bull_move = 0;
int ene_num = 0;
int temp[2000];
for (int i = 0; i <= 2000; i++)//防连续
temp[i] = 0;
for(int j = 0; j <= 200; j++)
my.x = steup_X;
my.y = steup_Y;
initgraph(all_X, Win_Y);
while (1) //what the fuck
{
ene_num++;
beg();
_stprintf_s(text, _T("得分:%d"), score);
settextcolor(BLUE);
settextstyle(30, 0, _T("宋体"));
outtextxy(Win_X, 30, text);
line(Win_X, 0, Win_X, Win_Y);
GetKey(&my);
draw_my(my.x, my.y);
Sleep(1);
if (ene_num % 100 == 0) //zheli
{
ene[ene_move].ene_move = 1;
ene[ene_move].x = drand(Win_X, 20);
ene[ene_move].y = 0;
ene_move++;
}
if (GetKeyState(' ') < 0)
{
bull[bull_move].bull_move = 1;
bull[bull_move].x = my.x + my_side / 2;
bull[bull_move].y = my.y;
bull_move++;
while (GetKeyState(' ') < 0){
for (int i = 0; i <= bull_move; i++)
{
if (bull[i].bull_move == 0)
{
cleardevice();
}
}
}
}
if (GetKeyState('E') < 0)
{
if (ski.ski_move == 0)
{
ski.ski_move = 1;
ski.y = my.y;
}
}
if (ski.ski_move == 1)
{
if (ski.y > 0)
{
ski.y -= ski_speed;
draw_ski(ski.y);
}
else
ski.ski_move = 0;
}
for (int j = 0; j <= ene_move; j++)
{
if (ene[j].ene_move)
{
ene[j].y += ene_speed;
draw_ene(ene[j].x, ene[j].y);
if (ene[j].y > Win_Y)
{
ene[j].ene_move = 0;
}
}
}
for (int i = 0; i <= bull_move; i++)
{
if (bull[i].bull_move)
{
bull[i].y -= bull_speed;
draw_bull(bull[i].x, bull[i].y);
if (bull[i].y < 0)
{
bull[i].bull_move = 0;
temp[i] = -10;
}
}
}
if (ene_move > 0 && bull_move > 0)
{
for (int bull_i = 0; bull_i <= bull_move; bull_i++)
{
for (int ene_i = 0; ene_i <= ene_move; ene_i++)//山穷水尽疑无路,柳岸花明又一村
{//众里寻他千百度,那人却在灯火阑珊处
if (bull[bull_i].bull_move == 1 && ene[ene_i].ene_move == 1)
{
if ((bull[bull_i].y >= ene[ene_i].y - 2 * ene_side) && (bull[bull_i].y <= ene[ene_i].y + 2 * ene_side) &&
(bull[bull_i].x >= ene[ene_i].x - 2 * ene_side) && (bull[bull_i].x <= ene[ene_i].x + 2 * ene_side))
{
Beep(1500, 100);
bull[bull_i].bull_move = 0;//这里本是关闭屏幕
ene[ene_i].ene_move = 0;
score++;
cleardevice();
break;
}
}
if (ski.ski_move == 1 && ene[ene_i].ene_move == 1)
{
if ((ski.y >= ene[ene_i].y - 2 * ene_side) && (ski.y <= ene[ene_i].y + 2 * ene_side))
{
ene[ene_i].ene_move = 0;
score++;
}
}
}
}
}
Sleep(3);
end();
}
getchar();
return 0;//what the fuck
//愿君历尽风帆,归来仍是少年
}