SNAKE

#include<stdio.h>
#include<graphics.h> //图形库 easyx
#include<conio.h>

IMAGE  beijin;//背景图片
IMAGE  wanjia[2];//玩家图片
IMAGE  zidan[2];//子弹
enum  My
{
	WIDTH = 591,  //枚举窗口的高宽
	HIGHT = 864,
	BULLLET_NUM = 500//枚举玩家子弹数量
};

struct Planmove//飞机移动状态
{
	int x;
	int y;
	bool live;//是否活着

}player, bull[BULLLET_NUM];

//加载图片
void jiazaitupian()
{
	//加载图片
	loadimage(&beijin, "./images/background.jpg");
	loadimage(&wanjia[0], "./images/planeNormal_1.jpg");//掩码图和原图
	loadimage(&wanjia[1], "./images/planeNormal_2.jpg");
	loadimage(&zidan[0], "./images/bullet1.jpg");//子弹图片两张
	loadimage(&zidan[1], "./images/bullet2.jpg");
}

//游戏初始化
void gameInit()
{
	jiazaitupian();
	player.x = WIDTH / 2 - 45;
	player.y = HIGHT - 120;
	player.live = true;
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		bull[i].x = 0;
		bull[i].y = 0;
		bull[i].live = false;
	}
}

//创建子弹1
void creataBullet()
{
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		if (!bull[i].live)
		{
			bull[i].x = player.x + 60;
			bull[i].y = player.y;
			bull[i].live = true;
			break;
		}
	}
}
//创建子弹2
void creataBullet2()
{
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		if (!bull[i].live)
		{
			bull[i].x = player.x + 120;
			bull[i].y = player.y;
			bull[i].live = true;
			break;
		}
	}
}
//创建子弹3
void creataBullet3()
{
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		if (!bull[i].live)
		{
			bull[i].x = player.x + 0;
			bull[i].y = player.y;
			bull[i].live = true;
			break;
		}
	}
}
//游戏桌面绘制函数
void GameDraw()
{
	//背景图放在窗口上
	putimage(0, 0, &beijin);//0,0开始放置图面左上角
	putimage(player.x, player.y, &wanjia[0], NOTSRCCOPY);
	putimage(player.x, player.y, &wanjia[1], SRCINVERT);
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		if (bull[i].live) //为真的  才画子弹
		{
			putimage(bull[i].x, bull[i].y, &zidan[0], NOTSRCCOPY);
			putimage(bull[i].x, bull[i].y, &zidan[1], SRCINVERT);
		}
	}
}

//角色移动,获取键盘,上下左右
void Playermove(int speed)//玩家移动
{
	/*第一种方法*/
	/*
	//扫描键盘是否按下返回1  0
	if (_kbhit)
	{
		char  key = _getch();
		switch (key)
		{
		case'W':
		case'w':
			player.y -= speed;
			break;
		case'a':
		case'A':
			player.x -= speed;
			break;
		case's':
		case'S':
			player.y += speed;
			break;
		case'd':
		case'D':
			player.x += speed;
			break;
		}
	}*/
	/*第二种  使用windows AIP*/
	if (GetAsyncKeyState(VK_UP)) //非阻塞,异步的 很流畅
	{
		if (player.y > 0)
			player.y -= speed;
	}
	if (GetAsyncKeyState(VK_DOWN))
	{
		if (player.y < HIGHT - 120)
		{
			player.y += speed;
		}
	}
	if (GetAsyncKeyState(VK_LEFT))
	{
		if (player.x + 60 > 0)
		{
			player.x -= speed;
		}
	}
	if (GetAsyncKeyState(VK_RIGHT))
	{
		if (player.x - 60 < WIDTH - 117)
		{
			player.x += speed;
		}
	}
	static DWORD t1=0,t2=0; //延迟

	if (GetAsyncKeyState(VK_SPACE)&& t2-t1>100)
	{
		//按下空格就创建一个子弹
		creataBullet();
		creataBullet3();
		creataBullet2();

		t1 = t2;
	}
	t2 = GetTickCount();
}

//子弹移动
void BulletMove()
{
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		if (bull[i].live)
		{
			bull[i].y -= 1.2;
			if (bull[i].y < 0)
			{
				bull[i].live = false;
			}
		}
	}
}

int  main()
{
	//游戏各个参数初始化
	gameInit();
	//创建图形窗口
	initgraph(WIDTH, HIGHT);
	GameDraw();
	//双缓冲绘图
	BeginBatchDraw();


	while (true)
	{

		GameDraw();
		FlushBatchDraw();//双缓冲中间暂停一帧	
		Playermove(1);	
		BulletMove();

	}
	EndBatchDraw();//双缓冲绘图至此处
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值