#include<iostream>
#include<easyx.h>
#include<time.h>
#include<cstdlib>
#include<vector>
#include<list>
#define KUAI_W 60
#define BK_W 640
#define BK_H 960
using namespace std;
class IM {
public:
IMAGE bk;
IMAGE im[13];
void init();
}Im;
class Kuai { //图片类
public:
Kuai(int x,int y,int image_id);
void Draw();
bool Contral(ExMessage msg);
int x;
int y;
int image_id;
bool isclock; //能否被点击
bool isexist; //是否存在
};
class Game {
public:
Game();
void initArr(); //地图初始化
void draw(); //游戏绘制
void updata(); //游戏数据更新
void KuaiUpdata(); //更新块的数据
void sameRemove(); //YiXuan中某个图片有三个一样的就删除该图片
void judgeEnd(); //判断游戏是否结束
list<Kuai>Kuai_arr; //存储所有图片的链表
list<Kuai>YiXuan; //存储框中图片的链表
int grade; //当前关卡
}GAME;
int main() {
srand((unsigned int)time(NULL));
initgraph(BK_W, BK_H,EX_SHOWCONSOLE);
Im.init();
GAME.initArr();
BeginBatchDraw();
while (1) {
GAME.updata();
GAME.draw();
FlushBatchDraw();
}
EndBatchDraw();
return 0;
}
void IM::init()
{
loadimage(&bk, "./images/bk.jpg", BK_W, BK_H);
for (int i = 0; i < 13; i++) {
char s[132] = { 0 };
sprintf(s, "./images/%d.jpg", i + 1);
loadimage(im + i, s, KUAI_W, KUAI_W);
}
}
Kuai::Kuai(int x, int y, int image_id):x(x),y(y),image_id(image_id)
{
isclock = true;
isexist = true;
}
void Kuai::Draw()
{
putimage(x-KUAI_W/2, y - KUAI_W / 2, Im.im+image_id);
}
bool Kuai::Contral(ExMessage msg)
{
//如果鼠标指针在摁钮矩形框内 but.flag=true
if (msg.x > x-KUAI_W/2 && msg.x<x + KUAI_W/2 && msg.y>y - KUAI_W/2 && msg.y < y + KUAI_W/2&&isclock) {
if (msg.lbutton)return true;
}
return false;
}
Game::Game() {
grade = 1;
}
void Game::initArr() {
if (grade == 1) {
Kuai_arr.push_back(Kuai(160, 160, 0));
Kuai_arr.push_back(Kuai(160, 160 * 2, 1));
Kuai_arr.push_back(Kuai(160, 160 * 3, 2));
Kuai_arr.push_back(Kuai(160 * 2, 160, 1));
Kuai_arr.push_back(Kuai(160 * 2, 160 * 2, 1));
Kuai_arr.push_back(Kuai(160 * 2, 160 * 3, 0));
Kuai_arr.push_back(Kuai(160 * 3, 160, 0));
Kuai_arr.push_back(Kuai(160 * 3, 160 * 2, 2));
Kuai_arr.push_back(Kuai(160 * 3, 160 * 3, 2));
}
else if (grade == 2) {
/*
* 图片产生范围100-540 100+%22 160-560 160+%20
* 最初图片布局
* (170,190) (290,190)(350,190) (470,190)
* (170,250) (470,250)
* (200,310)(260,310)(320,310)(380,310)(440,310)
* (200,370)(260,370)(320,370)(380,370)(440,370)
* (170,430) (470,430)
* (170,490) (290,490)(350,490) (470,490)
* (290,550)(350,550)
* 。。。。(270,550) (370,550)。。。。
*/
int temp_arr[13] = {0};
int temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(170, 190, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(290, 190, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(350, 190, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(470, 190, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(170, 250, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(470, 250, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(200, 310, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(260, 310, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(320, 310, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(380, 310, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(440, 310, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(200, 370, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(260, 370, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(320, 370, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(380, 370, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(440, 370, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(170, 430, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(470, 430, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(170, 490, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(470, 490, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(290, 490, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(350, 490, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(290, 550, temp));
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(350, 550, temp));
for (int i = 0; i < 100; i++) {
temp = rand() % 13;
temp_arr[temp]++;
Kuai_arr.push_back(Kuai(100 + rand() % 22*20, 160 + rand() % 20*20, temp));
}
for (int i = 0; i < 13; i++) { //如果单张图片的数量不是3的倍数,则增加该图片
while (temp_arr[i] % 3 != 0) {
temp_arr[i]++;
Kuai_arr.push_back(Kuai(100 + rand() % 22 * 20, 160 + rand() % 20 * 20, i));
}
}
}
else
return;
}
void Game::draw() {
putimage(0, 0, &Im.bk); //背景绘制
for (auto a = Kuai_arr.rbegin(); a != Kuai_arr.rend(); a++) { //地图绘制
a->Draw();
}
int i = 0;
for (auto a : YiXuan) { //框中图片绘制
a.x = 80 + (KUAI_W + 20) * i;
a.y = 810;
a.Draw();
i++;
}
}
void Game::updata() {
sameRemove(); //有三张相同的图片就删除
judgeEnd(); //判断游戏结束,或进入下一关
KuaiUpdata(); //所有图片数据更新
ExMessage msg;
if (peekmessage(&msg, WH_MOUSE))//如果获取到一条鼠标消息
{
for (auto a = Kuai_arr.begin(); a != Kuai_arr.end();a++) {
if (a->Contral(msg)) {
YiXuan.push_back(Kuai(80 + (KUAI_W+20) * YiXuan.size(), 810,a->image_id)); //将被点击的图片加入到框中
Kuai_arr.erase(a);
}
}
}
}
void Game::KuaiUpdata() {
for (auto i = Kuai_arr.begin(); i != Kuai_arr.end(); i++) {
Kuai temp = *i;
i->isclock = true;
for (auto j = Kuai_arr.begin(); j != i; j++) {
if (temp.x - j->x<KUAI_W && temp.x - j->x > -KUAI_W && temp.y - j->y < KUAI_W && temp.y - j->y >- KUAI_W) {
i->isclock = false;
break;
}
}
}
}
void Game::sameRemove() {
int id_arr[13]={0};
for (auto a = YiXuan.begin(); a != YiXuan.end(); a++) {
id_arr[a->image_id]++;
}
for (int i = 0; i < 13; i++) {
if (id_arr[i] == 3){
for (int j = 0; j < 3; j++) {
auto a = YiXuan.begin();
for (a ; a != YiXuan.end(); a++) {
if (a->image_id == i)break;
}
YiXuan.erase(a);
for (auto a : YiXuan) {
cout << a.image_id << "\t";
}
cout << endl;
}
}
}
}
void Game::judgeEnd() {
if (Kuai_arr.empty() && YiXuan.empty()) {
grade++;
initArr();
}
if (YiXuan.size() >= 7) {
setfillcolor(WHITE);
setlinecolor(BLACK);
settextcolor(BLACK);
setbkmode(TRANSPARENT);
settextstyle(80, 0, "黑体");
fillrectangle(100, 200, 540, 300);
outtextxy(130, 210, "游戏失败");
EndBatchDraw();
Sleep(5000);
exit(0);
}
if (grade == 3) {
cout<<"游戏胜利"<< endl;
setfillcolor(WHITE);
setlinecolor(BLACK);
settextcolor(BLACK);
setbkmode(TRANSPARENT);
settextstyle(80, 0, "黑体");
fillrectangle(100, 200, 540, 300);
outtextxy(130, 210, "游戏胜利");
EndBatchDraw();
Sleep(5000);
exit(0);
}
}
c/c++ easyx 羊了个羊带第二关源码
需积分: 0 160 浏览量
更新于2022-12-19
9
收藏 1.15MB ZIP 举报
《C/C++ 使用EasyX库开发“羊了个羊”游戏详解》
在游戏开发领域,C/C++语言凭借其高效性和灵活性,常被用于构建各种类型的游戏。在本项目中,“羊了个羊”是一款简单的200行代码实现的游戏,采用C++语言,并结合EasyX图形库进行开发。下面我们将深入探讨这款小游戏的开发过程和涉及的关键知识点。
1. EasyX图形库介绍:
EasyX是一个轻量级的Windows图形库,专为初学者设计,简化了C++绘制图形的复杂性。它提供了一系列简单的函数,如DrawLine、DrawCircle等,使得开发者能够快速创建出2D图形界面。在“羊了个羊”游戏中,EasyX库是构建游戏画面的基础。
2. 游戏逻辑与数据结构:
“羊了个羊”的核心逻辑是基于消除类游戏规则,玩家需要通过匹配相同的图形来消除。这涉及到对游戏元素(如羊、草等)的存储和管理。通常,可以使用数组或链表等数据结构来存储游戏元素的位置和状态,通过遍历和比较这些元素来判断是否可以进行消除操作。
3. 渲染与用户交互:
游戏画面的渲染是通过EasyX库中的绘图函数完成的,包括背景绘制、游戏元素的绘制以及特效的展示。同时,游戏需要响应用户的鼠标点击事件,这需要设置事件监听器,当用户点击屏幕时,检查点击位置上的游戏元素并执行相应的操作。
4. 游戏状态管理:
在200行代码中,游戏状态的管理是关键。这包括游戏的开始、进行、暂停、结束等状态。为了实现这些状态切换,需要定义状态枚举类型和状态机,根据当前游戏状态执行不同的逻辑。
5. 关卡设计与难度递增:
“羊了个羊”包含第二关,这意味着游戏设计了多关卡的概念。每关的布局和难度可以通过随机算法或者预设的关卡数据来实现。随着关卡的推进,元素数量、排列方式以及消除条件可能会逐渐复杂,增加游戏挑战性。
6. 资源管理:
压缩包中的“images”目录包含游戏所需的所有图像资源。在C++中,可以使用EasyX库加载和显示位图,将图片资源转换为屏幕上的游戏元素。正确管理和加载这些资源是保证游戏正常运行的重要环节。
7. 项目构建与编译配置:
项目文件如".vcxproj"和".filters"是Visual Studio的工程文件,它们包含了编译和链接的设置,如源代码文件、库依赖、目标平台等。".vcxproj.user"文件则包含用户特定的设置,如调试器设置等。这些文件帮助开发者在IDE环境中快速构建和运行项目。
“羊了个羊”游戏虽然只有200行代码,但它涵盖了游戏开发中的基础概念,包括图形渲染、用户交互、数据结构应用、状态管理、关卡设计和资源管理等多个方面。对于初学者来说,这是一个很好的实践项目,可以锻炼C++编程能力和游戏开发思维。

_lWill
- 粉丝: 105
最新资源
- CAD转JPG.doc
- 计算机组成原理试题库集及答案.doc
- V42电算化会计软件安装、考核操作流程.ppt
- 从配音秀看互联网+时代网络配音的特点与发展趋势.docx
- 用Excel求置信区间知识讲稿.ppt
- 现代无线通信原理习题说课讲解.ppt
- 计算机绘图形考作业一,.doc
- 营销型网站建设重视用户需求就是发掘网站潜在价值.doc
- 计算机键盘操作基础知识.doc
- 信息化背景下高校财务管理建设存在的问题与对策.docx
- 工程项目管理软件应用知识课件.ppt
- 自动化技术在建筑机械工程中的应用及发展.docx
- 郑州市统计局网站介绍教学文案.ppt
- 人工智能背景下人力资源开发与管理面临的挑战与机遇(1).docx
- 基于Android的微课分享平台的设计与实现.docx
- 浅谈互联网金融支持中小企业的策略(1).docx