猫史档案馆


上“坚持100秒”的代码(这个帖子很长,慎入)

用户:小萝卜cXsQ小萝卜cXsQ查看:7 回复:6 评论:7 创建时间:2022-07-03T09:35:23


上代码:

#include <graphics.h>
#include <conio.h>
#include <time.h>
#include "EasyXPng.h"
#pragma comment(lib,"Winmm.lib")
#define WIDTH 560
#define HEIGHT 800
#define MaxBulletNum 200
bool isfail = false;

void sleep(DWORD ms)
{
	static DWORD oldtime = GetTickCount();
	while (GetTickCount() - oldtime < ms)
		Sleep(1);
	oldtime = GetTickCount();
}

void PlayMusicOnce(TCHAR filename[80])
{
	TCHAR cmdString1[50];
	_stprintf(cmdString1,_T("open %s alias tmpmusic"),filename);
	mciSendString(_T("close tmpmusic"),NULL,0,NULL);
	mciSendString(cmdString1,NULL,0,NULL);
	mciSendString(_T("play tmpmusic"),NULL,0,NULL);
}

class Rocket
{
public:
	IMAGE im_rocket;
	IMAGE im_blowup;
	float x,y;
	float width,height;
	int liveSecond;
	int life;

	void draw()
	{

		for (int i=0;i<life;i++)
			putimage(i*width*0.9,0,&im_rocket);


		TCHAR s[20];
		setbkmode(TRANSPARENT);
		_stprintf(s,_T("%d��"),liveSecond);
		settextcolor(WHITE);
		settextstyle(40,0,_T("����"));
		outtextxy(WIDTH*0.85,20,s);

		if (life>0)
			putimagePng(x - width/2,y-height/2,&im_rocket);
		else
			putimagePng(x - width/2,y-height/2,&im_blowup);
	}

	void update(float mx,float my)
	{

		x=mx;
		y=my;
	}

	void updateWhenLifeLost(){
		PlayMusicOnce(_T("explode.mp3"));
		life --;
	}
};

class Bullet
{
public:
	IMAGE im_bullet;
	float x,y;
	float 喵,vy;
	float radius;

	void draw()
	{
		putimagePng(x - radius,y-radius,&im_bullet);
	}

	void update()
	{

		x += 喵;
		y+= vy;
		if (x<=0 || x>WIDTH)
			喵 = -喵;
		if (y<=0 || y>= HEIGHT)
			vy = -vy;
	}

	bool isCollideRocket(Rocket rocket)
	{
		float distancex = abs(rocket.x - x);
		float distancey = abs(rocket.y - y);
		if (distancex < rocket.width/2 && distancey < rocket.height/2)
			return true;
		else
			return false;
	}
};

class SmartUFO:public Bullet
{
public:
	void updatevelfortarge(Rocket targetrocket)
	{
		float scalar = rand()/double(RAND_MAX) + 1;
		if (targetrocket.x>x)
			喵 = scalar;
		else if (targetrocket.x<x)
			喵 = -scalar;
		if (targetrocket.y>y)
			vy = scalar;
		else if (targetrocket.y>y)
			vy = -scalar;
	}
};

IMAGE im_bk,im_bullet,im_rocket,im_blowup,im_UFO;
Bullet bullet[MaxBulletNum];
Rocket rocket;
SmartUFO ufo;
int bulletNum = 0;

void startup()
{
	mciSendString(_T("open game_music.mp3 alias bkmusic"),NULL,0,NULL);
	mciSendString(_T("play bkmusic repeat"),NULL,0,NULL);
	srand(time(0));
	loadimage(&im_bk,_T("background.png"));
	loadimage(&im_bullet,_T("bullet.png"));
	loadimage(&im_rocket,_T("rocket.png"));
	loadimage(&im_blowup,_T("blowup.png"));
	loadimage(&im_UFO,_T("ufo.png"));

	rocket.im_rocket = im_rocket;
	rocket.im_blowup = im_blowup;
	rocket.width = im_rocket.getwidth();
	rocket.height = im_rocket.getheight();
	rocket.life = 5;
	ufo.x = WIDTH/2;
	ufo.y = 10;
	ufo.im_bullet = im_UFO;
	ufo.radius = im_UFO.getwidth()/2;
	ufo.updatevelfortarge(rocket);

	initgraph(WIDTH,HEIGHT);
	BeginBatchDraw();
}

void show()
{
	putimage(0,0,&im_bk);
	for (int i=0;i<bulletNum;i++)
		bullet[i].draw();
	rocket.draw();
	ufo.draw();
	FlushBatchDraw();
	sleep(10);
}

void updatewithoutinput()
{
	if (rocket.life<=0){
		isfail = true;
		IMAGE im_reset;
		loadimage(&im_reset,_T("reset.png"));
		putimagePng(WIDTH/2,HEIGHT/2,&im_reset);
		return;
	}
	static int lastsecond = 0;
	static int nowsecond = 0;
	static clock_t start = clock();
	clock_t now = clock();

	nowsecond =( int(now-start)/CLOCKS_PER_SEC);
	rocket.liveSecond = nowsecond;
	if (nowsecond==lastsecond+2)
	{
		lastsecond=nowsecond;
		if (bulletNum<MaxBulletNum)
		{
			bullet[bulletNum].x = WIDTH/2;
			bullet[bulletNum].y = 10;
			float angle = (rand()/double(RAND_MAX)-0.5)*0.9*PI;
			float scalar = 2*rand()/double(RAND_MAX) + 2;
			bullet[bulletNum].喵 = scalar*sin(angle);
			bullet[bulletNum].vy = scalar*cos(angle);
			bullet[bulletNum].im_bullet = im_bullet;
			bullet[bulletNum].radius = im_bullet.getwidth()/2;
			bulletNum++;
		}
	}

	if (nowsecond==lastsecond+1)
		ufo.updatevelfortarge(rocket);

	for (int i=0;i<bulletNum;i++)
	{
		bullet[i].update();
		if (bullet[i].isCollideRocket(rocket))
		{
			rocket.updateWhenLifeLost();
			bullet[i].x = 5;
			bullet[i].y = 5;
			break;
		}
	}

	ufo.update();
	if (ufo.isCollideRocket(rocket))
	{
		rocket.updateWhenLifeLost();
		ufo.x = 5;
		ufo.y = 5;
	}
}

void updatewithinput()
{
	MOUSEMSG m;
	while (MouseHit())
	{
		m = GetMouseMsg();
		if(m.uMsg == WM_MOUSEMOVE && isfail == false)
			rocket.update(m.x,m.y);
		if (m.uMsg == MOUSEEVENTF_XDOWN)
			if (m.x >= WIDTH/2 || m.x <= WIDTH/2 || m.y >= HEIGHT/2 || m.y <= HEIGHT/2 && isfail)
				startup();
	}
}
int main(){
	startup();
	while (1){
		show();
		updatewithoutinput();
		updatewithinput();
	}
	return 0;
}

 

 


回复

上一页1 页 / 共 1下一页
小萝卜cXsQ小萝卜cXsQ

依赖 shequ.codemao.cn/community/468487 素材 shequ.codemao.cn/community/468486

点赞0


评论


张允旭张允旭

C++?

点赞0


评论


yee089yee089

少用中文。保存容易出bug。

点赞0


评论


树林归来树林归来

666

点赞0


评论


Cecilia_VentiCecilia_Venti

这个效果吧……用Python三十多行就能实现……

点赞0


评论


该账号已被封禁该账号已被封禁

真就让看代码的人坚持一百秒呗

点赞1


评论