⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functions.cpp

📁 书上的Delphi例程
💻 CPP
字号:
#include "stdafx.h"
#include "myGraphics.cpp"
#include "functions.h"

const int Yoko=37;
const int Tate=27;
const int DYoko=Yoko*16;
const int DTate=Tate*16;
const int PtFull=16;
const int ChMax=30;

//角色结构
struct PatDt
{
	BYTE	used, sban, smov, slife, count;
	int		xpos, ypos;
};

//角色图案
const BYTE spr[3][3]={ {1, 1, 0}, {1, 1, 0x13}, {1, 1, 0x17} };

//角色数组
PatDt	ChPon[ChMax];

HWND	dest_wnd;
HDC		bg_dc, off_dc, dest_dc;
HBITMAP	bg_bmp, off_bmp;
Bitmap	*load_bmp;	//载入的位图

//-------------------------------
// Init() 初始化
//-------------------------------
void Init(HWND hwnd)
{
	int i, j;
	
	dest_wnd=hwnd;
	SetClientSize(dest_wnd, DYoko, DTate);
	dest_dc=GetDC(hwnd);
	load_bmp=new Bitmap("Pat_Sample.bmp", true, RGB(255, 255, 255));

	//绘制背景
	bg_dc=CreateCompatibleDC(dest_dc);
	bg_bmp=CreateCompatibleBitmap(dest_dc, DYoko+32, DTate+32);
	SelectObject(bg_dc, bg_bmp);
	for(i=0; i<Tate; i++)
		for(j=0; j<Yoko; j++)
			load_bmp->Draw(bg_dc, j*16+16, i*16+16, 16, 16, 2*16, 0*16);

	//准备缓冲设备场景
	off_dc=CreateCompatibleDC(dest_dc);
	off_bmp=CreateCompatibleBitmap(dest_dc, DYoko+32, DTate+32);
	SelectObject(off_dc, off_bmp);
	BitBlt(off_dc, 0, 0, DYoko+32, DTate+32, bg_dc, 0, 0, SRCCOPY);

	srand(time(0));
	//初始化角色数组
	for(i=0; i<ChMax; i++)
	{
		ChPon[i].used=0;
		ChPon[i].count=rand()%15;
	}
}

//-------------------------------
// Free() 释放资源
//-------------------------------
void Free()
{
	ReleaseDC(dest_wnd, dest_dc);
	delete load_bmp;
}

//-------------------------------
// Paint() 刷屏
//-------------------------------
void Paint()
{
	BitBlt(dest_dc, 0, 0, DYoko, DTate, off_dc, 16, 16, SRCCOPY);
}

//-------------------------------
// StChk() 新星诞生
//-------------------------------
void StChk(PatDt &pd)
{
	if( ( pd.count > 20 ) && ( rand() % 100 > 3 ) )
	{
		pd.used=1;
		pd.sban=1;
		pd.smov=0;
		pd.count=0;
		pd.xpos=rand()%(DYoko-16);
		pd.ypos=rand()%(DTate-16);
		pd.slife=100+rand()%80;		
	}
}

//-------------------------------
// Stars() 星星的行动
//-------------------------------
void Stars(PatDt &pd)
{
	switch(pd.smov)
	{
	case 0:
		if(pd.count>pd.slife)
			pd.smov=1, pd.count=0;
		break;
	case 1:
		if(pd.count<=16)
			pd.sban=pd.count&1;
		else
			pd.smov=2, pd.sban=2, pd.count=0;
		break;
	case 2:
		if(pd.count>5)
			pd.smov=3, pd.count=0;
		break;
	case 3:
		pd.ypos++;
		if(pd.count>7)
			pd.smov=4, pd.count=0;
		break;
	case 4:
		pd.ypos+=pd.count;
	}

	if(pd.ypos>DTate)
		pd.used=0, pd.count=0;
}

//-------------------------------
// SbanDi() 画出角色
//-------------------------------
void SbanDi(const BYTE *sary, int x, int y)
{
	int i, j, n;
	n=2;
	for(j=0; j<sary[1]; j++)
		for(i=0; i<sary[0]; i++)
		{
			if( x+i*16>=0 && x+i*16<DYoko+16 && y+j*16>=0 && y+j*16<DTate+16 )
				load_bmp->Draw(off_dc, x+i*16, y+j*16, 16, 16, (sary[n]&0xf)*16, sary[n]&0xf0);
			n++;
		}
}

//-------------------------------
// SbanDi() 清除角色
//-------------------------------
void SbanCl(const BYTE *sary, int x, int y)
{
	int i, j;
	for(j=0; j<sary[1]; j++)
		for(i=0; i<sary[0]; i++)
			if( x+i*16>=0 && x+i*16<DYoko+16 && y+j*16>=0 && y+j*16<DTate*16 )
				BitBlt(off_dc, x+i*16, y+j*16, 16, 16, bg_dc, x+i*16, y+j*16, SRCCOPY);
}

//-------------------------------
// Update() 刷新
//-------------------------------
void Update()
{
	int i;
	for(i=0; i<ChMax; i++)
	{
		ChPon[i].count++;
		if(ChPon[i].used)
			Stars(ChPon[i]);
		else
			StChk(ChPon[i]);
	}

	for(i=0; i<ChMax; i++)
		if(ChPon[i].used)
			SbanDi(&spr[ChPon[i].sban][0], ChPon[i].xpos, ChPon[i].ypos);

	Paint();

	for(i=0; i<ChMax; i++)
		if(ChPon[i].used)
			SbanCl(&spr[ChPon[i].sban][0], ChPon[i].xpos, ChPon[i].ypos);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -