#include<iostream>
#include<easyx.h>
#include<Windows.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
IMAGE img_bk; //图片类实例化
IMAGE img_item[3];
const int width = 400; // 窗口宽
const int height = 760; // 窗口高
const int interval = 48; // 小图片间隔
void loadImg() {
loadimage(&img_bk, "images/1.jpg", width, height); //加载图片映射为地址
loadimage(&img_item[0], "images/bili.jpg", 48, 48);
loadimage(&img_item[1], "images/cai.jpg", 48, 48);
loadimage(&img_item[2], "images/csdn.jpg", 48, 48);
}
int main() {
initgraph(width, height);
loadImg();
mciSendString(_T("open images/yang.mp3"), 0, 0, 0); // 打开音乐
mciSendString(_T("play images/yang.mp3"), 0, 0, 0); // 播放音乐
int map[3][3] = { {1,1,1},{2,2,2},{3,3,3} };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
{
srand((unsigned)time(NULL)); // 随机种子
int r1 = rand() % 3;
int c1 = rand() % 3;
int temp = map[r1][c1];
map[r1][c1] = map[i][j];
map[i][j] = temp;
}
}
int offsetx = (width - (2 * interval + 3 * 48)) / 2;
int offsety = (height - (2 * interval + 3 * 48)) / 2;
int store[7] = { 0 };
while (true) {
BeginBatchDraw(); // 解决图片闪烁问题
putimage(0, 0,& img_bk); // 按坐标显示图片
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
{
if (map[i][j]) {
putimage(offsetx + j * (48 + interval), offsety + i * (48 + interval), &img_item[map[i][j] - 1]);
}
}
}
for (int i = 0; i < 7; i++) {
if (store[i]) {
putimage((i * 50) + 26, 620, &img_item[store[i] - 1]);
}
}
EndBatchDraw(); // 解决图片闪烁问题
ExMessage msg = { 0 };
if (peekmessage(&msg) && msg.message == WM_LBUTTONDOWN) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
{
int x = offsetx + j * (48 + interval);
int y = offsety + i * (48 + interval);
if (msg.x > x && msg.x<x + 48 && msg.y>y && msg.y < y + 48) {
int col = (msg.x - offsetx) / (48 + interval);
int row = (msg.y - offsety) / (48 + interval);
for (int i = 0; i < 7; i++) {
if (store[i] == 0) {
store[i] = map[row][col];
break;
}
}
int cnt = 0;
for (int i = 0; i < 7; i++) {
if (store[i] == map[row][col]) {
cnt++;
}
}
if (cnt == 3) {
for (int i = 0; i < 7; i++) {
if (store[i] == map[row][col]) {
store[i] = 0;
}
}
}
map[row][col] = 0;
}
}
}
}
}
return 0;
}
c++游戏------羊了个羊消消乐
最新推荐文章于 2024-09-20 20:27:00 发布