C语言写雷霆战机

C语言写雷霆战机

#include <stdio.h>
#include <stdlib.h>              //动态内存分配
#include <time.h>
#include <windows.h>
#include <conio.h>
#include <graphics.h>             //外部添加的画图头文件库
#include <mmsystem.h>    //音乐
#pragma comment(lib,"Winmm.lib")

/*
雷霆战机

定义一个结构体,表示子弹属性
 坐标
 int x;
 int y;

*/

//宏定义
#define WND_WIDTH 500 
#define WND_HEIGHT 700

//定义子弹结构体
typedef struct Node
{
	int x;
	int y;

	struct Node *pnext;     //指针域,存放下一个子弹的内存地址
}NODE;    //int整形,NODE子弹类型   没有内存,类型

NODE *pZidan = NULL;    //定义子弹头指针
NODE *pMine = NULL;   //自己的飞机
NODE *pDiji = NULL;    //敌机链表头指针

//创建链表
void CreateList()
{
	//创建子弹的链表
	pZidan = (NODE *)malloc(sizeof(NODE));
	if (pZidan == NULL)
	{
		perror("shibai1\n");
	}
	pZidan->x = 50;
	pZidan->y = 50;
	pZidan->pnext = NULL;

	//创建飞机的链表
	pMine = (NODE *)malloc(sizeof(NODE));
	if (pMine == NULL)
	{
		perror("shibai1\n");
	}
	pMine->x = 225;
	pMine->y = 620;
	pMine->pnext = NULL;


	//创建敌方飞机的链表
	pDiji = (NODE *)malloc(sizeof(NODE));
	if (pDiji == NULL)
	{
		perror("shibai1\n");
	}
	pDiji->x = 0;
	pDiji->y = 0;
	pDiji->pnext = NULL;
}
//增加子弹
void AddNode(int flag)
{
	if (flag == 0)
	{
			//1开辟内存
		NODE *pnew = (NODE *)malloc(sizeof(NODE));

		//填充数据
		pnew->x = pMine->x+21;      //第一个子弹在屏幕 的地方
		pnew->y = pMine->y-10;
		pnew->pnext = NULL;

		//把新节点查出链表(头插)
		pnew->pnext = pZidan->pnext;
		pZidan->pnext = pnew;
	}
	else if(flag == 1)
	{
		 
	}
}

//增加敌机
void AddNode_Diji()
{
	//1开辟内存
	NODE *pnew = (NODE *)malloc(sizeof(NODE));

	//填充数据
	pnew->x = rand()%(WND_WIDTH-20);      //第一个敌机在屏幕 的地方(去掉敌机宽度)
	pnew->y = 0;
	pnew->pnext = NULL;

	//把新节点查出链表(头插)
	pnew->pnext = pDiji->pnext;
	pDiji->pnext = pnew;
}

void Shoot()
{
	//敌机的两个零时指针
	NODE *pdr = pDiji->pnext;
	NODE *pdrpre = pDiji;

	//子弹
	NODE *pzd = pZidan->pnext;
	NODE *pzdpre = pZidan;

	while (pdr != NULL)
	{
		pzd = pZidan->pnext;
		pzdpre = pZidan;

		while (pzd != NULL)
		{
			if (pzd->x > pdr->x - 10 && pzd->x <= pdr->x + 50 &&
				pzd->y >= pdr->y - 15 && pzd->y <= pdr->y + 15)
			{
				pzdpre->pnext = pzd->pnext;
				free(pzd);

				pdrpre->pnext = pdr->pnext;
				free(pdr);

				pdr = pDiji->pnext;
				pdrpre = pDiji;
				break;
			}
			else
			{
				pzd = pzd->pnext;
				pzdpre = pzdpre->pnext;
			}
		}
		if (pdr != NULL)    //若已经为NULL就结束
		pdr = pdr->pnext;

		if (pdr != NULL)    //若已经为NULL就结束
		{
			pdrpre = pdrpre->pnext;
		}
	}
}

int main(void)
{
	
	char key;  //按键的输入
	DWORD t1, t2,tt1,tt2;

	IMAGE image_zidan;         //定义存放子弹的变量
	IMAGE image_diji;            //定义存放敌机的变量
	IMAGE image_bcground;     //定义背景变量
	IMAGE image_mephon;        //定义自己飞机

	//将图片加载进来  (自己的图片路径)
	loadimage(&image_zidan,TEXT("..//tupian//zidan2.jpg"),10, 15);
	//将敌机加载进来
	loadimage(&image_diji, TEXT("..//tupian//diji.jpg"), 20, 25);
	//将我机加载进来
	loadimage(&image_mephon, TEXT("..//tupian//我机1.jpg"), 50, 80);

	//将背景写进来
	loadimage(&image_bcground, TEXT("..//tupian//背景1.jpg"), 500, 700);

	initgraph(WND_WIDTH, WND_HEIGHT);        //做一个640,480的窗口
	srand(time(NULL));      //随机数种子

	CreateList();           //创建头节点
   
	t1 = GetTickCount();           //获取,时间
	tt1 = GetTickCount();          //获取,时间
	while (1)
	{
		t2 = GetTickCount();          //获取,时间
		tt2 = GetTickCount();          //获取,时间
		if (t2 - t1 > 2000)
		{
			//不断来添加节点
			AddNode_Diji(); //敌机
			t1 = t2;
		}
		if (tt2 - tt1 > 200)
		{
			//不断来添加节点
			AddNode(0);   //子弹
			tt1 = tt2;
		}
		

		BeginBatchDraw();        //开始批量绘图
		cleardevice();
		//显示背景
		putimage(0, 0, &image_bcground);
		//显示自己飞机宽50,高30
		//roundrect(pMine->x, pMine->y, pMine->x + 50, pMine->y + 30, 50, 20);
		putimage(pMine->x, pMine->y, &image_mephon);

		//显示节点内容(子弹)
		NODE *p = pZidan->pnext;
		while (p != NULL)
		{
			//输出子弹图片
			putimage(p->x, p->y, &image_zidan);

			p->y -= 20;             //y轴减少,子弹向上走
			p = p->pnext;
		}


		//显示敌机
		NODE *p_diji = pDiji->pnext;
		while (p_diji != NULL)
		{
			//输出敌机图片
			putimage(p_diji->x, p_diji->y,&image_diji);
			p_diji->y++;
			p_diji = p_diji->pnext;
		}
		Sleep(25);
		Shoot();
		EndBatchDraw();

		//按键控制飞机的位置pMine->x   pMine->y
		//如果没有按键输入
		//判断是否有按键
		if (_kbhit())     //kbhit()判断是否有按键,有返回1,否返回0
		{
			key = _getch();        //key获取按键内容
			//上下左右wasd上下左右
			switch (key)
			{
				case 119 ://上
					pMine->y -= 10;
					break;

				case 115://下
					pMine->y += 10;
					break;

				case 97://左
					pMine->x -= 10;
					break;

				case 100://右
					pMine->x += 10;
					break;

				default:
					break;
			}
		}
	}
	getchar();
	return 0;
}

  • 2
    点赞
  • 2
    评论
  • 13
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 1024 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值