EasyX

EasyX

#include 
#include 

int main() {
	initgraph(1280, 720);
	int x = 300;
	int y = 300;
	BeginBatchDraw();
	while (true) {
		// 鼠标移动 点击 键盘打字 都被称为消息,当系统捕获消息时 将消息放入消息队列中
		ExMessage msg; // 这个结构体用于保存鼠标消息
		while (peekmessage(&msg)) { // 读取操作(); 这个函数用于获取一个消息,并立即返回, 并将消息保存到msg中
			// 在这里执行消息处理逻辑
			if (msg.message == WM_MOUSEMOVE) {
				x = msg.x; //  处理数据(); 更新鼠标当前的 x 坐标 赋值 给x重新绘图
				y = msg.y; // 更新鼠标当前的 y 坐标
			}
		}
		cleardevice();
		solidcircle(x, y, 100); // 绘制画面();
		FlushBatchDraw();
	}
	EndBatchDraw();
	return 0;
}
初始化();
while (true) {
 读取操作();
 处理数据();
 绘制画面();
}
释放资源();

井字棋游戏

#include 
#include 
#include 

char board_data[3][3] = {
	{'-', '-', '-'},
	{'-', '-', '-'},
	{'-', '-', '-'}
};

char current_piece = 'O';  // 初始化为O

// 检测指定棋子的玩家是否获胜
bool CheckWin(char c) {
	// 枚举所有情况
	if (board_data[0][0] == c && board_data[0][1] == c && board_data[0][2] == c) {
		return true;
	}
	if (board_data[1][0] == c && board_data[1][1] == c && board_data[1][2] == c) {
		return true;
	}
	if (board_data[2][0] == c && board_data[2][1] == c && board_data[2][2] == c) {
		return true;
	}
	if (board_data[0][0] == c && board_data[1][0] == c && board_data[2][0] == c) {
		return true;
	}
	if (board_data[0][2] == c && board_data[1][2] == c && board_data[2][2] == c) {
		return true;
	}
	if (board_data[0][0] == c && board_data[0][1] == c && board_data[0][2] == c) {
		return true;
	}
	if (board_data[0][0] == c && board_data[1][1] == c && board_data[2][2] == c) {
		return true;
	}
	if (board_data[0][2] == c && board_data[1][1] == c && board_data[2][0] == c) {
		return true;
	}
	return false;
}

// 检测当前是否出现平局
bool CheckDraw() {
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			if (board_data[i][j] == '-') {
				return false;
			}
		}
	}
	return true;
}

// 绘制棋盘网格
void DrawBoard() {
	line(0, 200, 600, 200);
	line(0, 400, 600, 400);
	line(200, 0, 200, 600);
	line(400, 0, 400, 600);
}

// 绘制棋子 
void DrawPiece() {
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			switch (board_data[i][j])
			{
			case 'O':
				circle(200 * j + 100, 200 * i + 100, 100);
				break;
			case 'X':
				line(200 * j, 200 * i, 200 * (j + 1), 200 * (i + 1));
				line(200 * (j + 1), 200 * i, 200 * j, 200 * (i + 1));
				break;
			case '-':
				break;
			}
		}
	}
}

// 绘制提示信息
void DrawTipText() {
	static TCHAR str[64];
	_stprintf_s(str, _T("当前棋子类型: %c"), current_piece);
	settextcolor(RGB(225, 175, 45));
	outtextxy(0, 0, str);
}


int main() {

	initgraph(600, 600);

	bool running = true;

	ExMessage msg;

	BeginBatchDraw();

	while (running) {
		DWORD start_time = GetTickCount();
		while (peekmessage(&msg)) {
			// 检查鼠标左键按下消息
			if (msg.message == WM_LBUTTONDOWN) {
				// 计算点击位置
				int x = msg.x;
				int y = msg.y;

				int index_x = x / 200;
				int index_y = y / 200;
				// 尝试落子
				if (board_data[index_y][index_x] == '-') {
					board_data[index_y][index_x] = current_piece;
					// 切换棋子类型
					if (current_piece == 'O') {
						current_piece = 'X';
					}
					else {
						current_piece = 'O';
					}
				}
			}

		}
		cleardevice();
		DrawBoard();
		DrawPiece();
		DrawTipText();
		FlushBatchDraw();
		if (CheckWin('X')) {
			MessageBox(GetHWnd(), _T("X 玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}
		else if (CheckWin('O')) {
			MessageBox(GetHWnd(), _T("O 玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}
		else if (CheckDraw()) {
			MessageBox(GetHWnd(), _T("平局"), _T("游戏结束"), MB_OK);
			return running = false;
		}
		DWORD end_time = GetTickCount();
		DWORD delta_time = end_time - start_time;
		if (delta_time < 1000 / 60) {
			Sleep(1000 / 60 - delta_time);
		}
	}
	EndBatchDraw();
	return 0;
}

未完成代码

#include 
#include 
#include 

// 存储当前动画的帧索引
int idx_current_anim = 0;

const int PLAYE_ANIM_NUM = 6;

const int PLAYER_WIDTH = 80;	// 玩家宽度
const int PLAYER_HEIGHT = 80;	// 玩家高度
const int SHADOW_WIDTH = 80;	// 阴影宽度

IMAGE img_player_left[PLAYE_ANIM_NUM];
IMAGE img_player_right[PLAYE_ANIM_NUM];

POINT player_pos = { 500, 500 };

const int PLAYER_SPEED = 3;

#pragma comment(lib, "MSIMG32.LIB")

inline void putimage_alpha(int x, int y, IMAGE* img) {
	int w = img->getwidth();
	int h = img->getheight();
	AlphaBlend(GetImageHDC(NULL), x, y, w, h,
		GetImageHDC(img), 0, 0, w, h, { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA });
}

void LoadAnimation() {
	for (int i = 0; i < PLAYE_ANIM_NUM; i++) {
		std::wstring path = L"img/player_left_" + std::to_wstring(i) + L".png";
		loadimage(&img_player_left[i], path.c_str());
	}
	for (int i = 0; i < PLAYE_ANIM_NUM; i++) {
		std::wstring path = L"img/player_right_" + std::to_wstring(i) + L".png";
		loadimage(&img_player_right[i], path.c_str());
	}
}

class Animation {
public:
	Animation(LPCTSTR, int, int);
	~Animation();
	void Play(int, int, int);

private:
	int timer = 0;		// 动画计时器
	int idx_frame = 0;	// 动画帧索引
	int interval_ms = 0;
	std::vector frame_list;
};

Animation::Animation(LPCTSTR path, int num, int interval) {
	interval_ms = interval;

	TCHAR path_file[256];
	for (int i = 0; i < num; i++) {
		_stprintf_s(path_file, path, i);
		IMAGE* frame = new IMAGE();
		loadimage(frame, path_file);
		frame_list.push_back(frame);
	}
}

Animation::~Animation() {
	for (int i = 0; i < frame_list.size(); i++) {
		delete frame_list[i];
	}
}

void Animation::Play(int x, int y, int delta) {
	timer += delta;
	if (timer >= interval_ms) {
		idx_frame = (idx_frame + 1) % frame_list.size();
		timer = 0;
	}
	putimage_alpha(x, y, frame_list[idx_frame]);
}

Animation anim_left_player(_T("img/player_left_%d.png"), 6, 45);
Animation anim_right_player(_T("img/player_right_%d.png"), 6, 45);

IMAGE img_shadow;

void DrawPlayer(int delta, int dir_x) {

	int pos_shadow_x = player_pos.x + (PLAYER_WIDTH / 2 - SHADOW_WIDTH / 2);
	int pos_shadow_y = player_pos.y + PLAYER_HEIGHT - 8;
	putimage_alpha(pos_shadow_x, pos_shadow_y, &img_shadow);

	static bool facing_left = false;
	if (dir_x < 0) {
		facing_left = true;
	}
	else if (dir_x > 0) {
		facing_left = false;
	}
	if (facing_left) {
		anim_left_player.Play(player_pos.x, player_pos.y, delta);
	}
	else {	
		anim_right_player.Play(player_pos.x, player_pos.y, delta);
	}
}

int main() {

	initgraph(1280, 720);

	bool running = true;

	ExMessage msg;
	IMAGE img_backgroud;

	bool is_move_up = false;
	bool is_move_down = false;
	bool is_move_left = false;
	bool is_move_right = false;

	LoadAnimation();
	loadimage(&img_shadow, _T("img/shadow_player.png"));
	loadimage(&img_backgroud, _T("img/background.png"));

	BeginBatchDraw();

	while (running) {
		DWORD start_time = GetTickCount();
		while (peekmessage(&msg)) {
			if (msg.message == WM_KEYDOWN) {
				switch (msg.vkcode) {
				case VK_UP:
					is_move_up = true;
					break;
				case VK_DOWN:
					is_move_down = true;
					break;
				case VK_LEFT:
					is_move_left = true;
					break;
				case VK_RIGHT:
					is_move_right = true;
					break;
				}
			}
			else if (msg.message == WM_KEYUP) {
				switch (msg.vkcode) {
				case VK_UP:
					is_move_up = false;
					break;
				case VK_DOWN:
					is_move_down = false;
					break;
				case VK_LEFT:
					is_move_left = false;
					break;
				case VK_RIGHT:
					is_move_right = false;
					break;
				}
			}
		}

		if (is_move_up) {
			player_pos.y -= PLAYER_SPEED;
		}
		if (is_move_down) {
			player_pos.y += PLAYER_SPEED;
		}
		if (is_move_left) {
			player_pos.x -= PLAYER_SPEED;
		}
		if (is_move_right) {
			player_pos.x += PLAYER_SPEED;
		}

		static int counter = 0;

		if (++counter % 5 == 0) {
			idx_current_anim++;
		}

		// 使动画循环播放
		idx_current_anim = idx_current_anim % PLAYE_ANIM_NUM;

		cleardevice();
		putimage(0, 0, &img_backgroud);
		DrawPlayer(1000 / 144, is_move_right - is_move_left);
		// putimage_alpha(player_pos.x, player_pos.y, &img_player_left[idx_current_anim]);
		FlushBatchDraw();
		DWORD end_time = GetTickCount();
		DWORD delta_time = end_time - start_time;
		if (delta_time < 1000 / 60) {
			Sleep(1000 / 60 - delta_time);
		}
	}

	return 0;
}

你可能感兴趣的:(C/C++,c++,c语言)