C++用easy2d库制作捕鱼达人
首先安装easy2d
配置好环境,自己慢慢去配吧
直接上代码了
fish.h
#pragma once
#include<easy2d/easy2d.h>
#include<list>
void LoadSource();
void RemoveSource();
class fish :public easy2d::Sprite
{
protected:
float scale_s;
time_t pre, next;
std::list<char*>::iterator it;
std::list<char*> *anim,*dieanim;
int now;
public:
bool isdie;
int HP,animspeed;
float _speed;
fish();
bool forward();
bool die(int h);
};
/*
Game_inf::Die_gold_shark_animpath = new std::list<char*>();
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_0.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_1.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_2.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_3.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_4.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_5.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_6.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_7.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_8.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_9.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_10.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_11.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_12.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_13.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_14.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_15.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_16.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_17.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_18.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_19.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_20.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_21.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_22.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_23.png");
*/
fish.cpp
#include "fish.h"
#include<time.h>
#include"Game_inf.h"
#include"gun.h"
void LoadSource()
{
Game_inf::green_turtle_animpath = new std::list<char*>();
Game_inf::green_turtle_animpath->push_back("img/乌龟_0.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_1.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_2.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_3.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_4.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_5.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_6.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_7.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_8.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_9.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_10.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_11.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_12.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_13.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_14.png");
Game_inf::green_turtle_animpath->push_back("img/乌龟_15.png");
Game_inf::Die_green_turtle_animpath = new std::list<char*>();
Game_inf::Die_green_turtle_animpath->push_back("img/乌龟死亡_0.png");
Game_inf::Die_green_turtle_animpath->push_back("img/乌龟死亡_1.png");
Game_inf::Die_green_turtle_animpath->push_back("img/乌龟死亡_2.png");
Game_inf::Die_green_turtle_animpath->push_back("img/乌龟死亡_3.png");
Game_inf::Die_green_turtle_animpath->push_back("img/乌龟死亡_4.png");
Game_inf::Die_green_turtle_animpath->push_back("img/乌龟死亡_5.png");
Game_inf::Die_green_turtle_animpath->push_back("img/乌龟死亡_6.png");
Game_inf::Cricket_fish_animpath = new std::list<char*>();
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_0.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_1.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_2.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_3.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_4.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_5.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_6.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_7.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_8.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_9.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_10.png");
Game_inf::Cricket_fish_animpath->push_back("img/蟋蟀鱼_11.png");
Game_inf::Die_Cricket_fish_animpath = new std::list<char*>();
Game_inf::Die_Cricket_fish_animpath->push_back("img/蟋蟀鱼死亡_0.png");
Game_inf::Die_Cricket_fish_animpath->push_back("img/蟋蟀鱼死亡_1.png");
Game_inf::Die_Cricket_fish_animpath->push_back("img/蟋蟀鱼死亡_2.png");
Game_inf::Die_Cricket_fish_animpath->push_back("img/蟋蟀鱼死亡_3.png");
Game_inf::Die_Cricket_fish_animpath->push_back("img/蟋蟀鱼死亡_4.png");
Game_inf::globefish_animpath = new std::list<char*>();
Game_inf::globefish_animpath->push_back("img/刺猬鱼_0.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_1.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_2.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_3.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_4.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_5.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_6.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_7.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_8.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_9.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_10.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_11.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_12.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_13.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_14.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_15.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_16.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_17.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_18.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_19.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_20.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_21.png");
Game_inf::globefish_animpath->push_back("img/刺猬鱼_22.png");
Game_inf::Die_globefish_animpath = new std::list<char*>();
Game_inf::Die_globefish_animpath->push_back("img/刺猬鱼死亡_0.png");
Game_inf::Die_globefish_animpath->push_back("img/刺猬鱼死亡_1.png");
Game_inf::Die_globefish_animpath->push_back("img/刺猬鱼死亡_2.png");
Game_inf::lantern_animpath = new std::list<char*>();
Game_inf::lantern_animpath->push_back("img/灯笼鱼_0.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_1.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_2.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_3.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_4.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_5.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_6.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_7.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_8.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_9.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_10.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_11.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_12.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_13.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_14.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_15.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_16.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_17.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_18.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_19.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_20.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_21.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_22.png");
Game_inf::lantern_animpath->push_back("img/灯笼鱼_23.png");
Game_inf::Die_lantern_animpath = new std::list<char*>();
Game_inf::Die_lantern_animpath->push_back("img/灯笼鱼死亡_0.png");
Game_inf::Die_lantern_animpath->push_back("img/灯笼鱼死亡_1.png");
Game_inf::Die_lantern_animpath->push_back("img/灯笼鱼死亡_2.png");
Game_inf::Die_lantern_animpath->push_back("img/灯笼鱼死亡_3.png");
Game_inf::fishweb_animpath = new std::list<char*>();
Game_inf::fishweb_animpath->push_back("img/web-1.png");
Game_inf::fishweb_animpath->push_back("img/web-2.png");
Game_inf::fishweb_animpath->push_back("img/web-3.png");
Game_inf::fishweb_animpath->push_back("img/web-4.png");
Game_inf::fishweb_animpath->push_back("img/web-5.png");
Game_inf::fishweb_animpath->push_back("img/web-6.png");
Game_inf::fishweb_animpath->push_back("img/web-7.png");
Game_inf::fishweb_animpath->push_back("img/web-8.png");
Game_inf::fishweb_animpath->push_back("img/web-9.png");
Game_inf::fishweb_animpath->push_back("img/web-10.png");
Game_inf::fishweb_animpath->push_back("img/web-11.png");
Game_inf::fishweb_animpath->push_back("img/web-12.png");
Game_inf::gold_shark_animpath = new std::list<char*>();
Game_inf::gold_shark_animpath->push_back("img/金鲨_0.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_1.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_2.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_3.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_4.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_5.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_6.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_7.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_8.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_9.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_10.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_11.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_12.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_13.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_14.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_15.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_16.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_17.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_18.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_19.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_20.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_21.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_22.png");
Game_inf::gold_shark_animpath->push_back("img/金鲨_23.png");
Game_inf::Die_gold_shark_animpath = new std::list<char*>();
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_0.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_1.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_2.png");
Game_inf::Die_gold_shark_animpath->push_back("img/金鲨死亡_3.png");
}
void RemoveSource()
{
Game_inf::green_turtle_animpath->clear();
delete Game_inf::green_turtle_animpath;
Game_inf::fishweb_animpath->clear();
delete Game_inf::fishweb_animpath;
Game_inf::Cricket_fish_animpath->clear();
delete Game_inf::Cricket_fish_animpath;
Game_inf::globefish_animpath->clear();
delete Game_inf::globefish_animpath;
Game_inf::lantern_animpath->clear();
delete Game_inf::lantern_animpath;
Game_inf::gold_shark_animpath->clear();
delete Game_inf::gold_shark_animpath;
}
fish::fish()
{
this->isdie = false;
int index = -1;
int tmp = rand() % 100;
if (tmp > 95)
index = 80;
else
{
index = rand() % FISHCOUNT;
}
switch (index)
{
case 80://鲨鱼
{
this->anim = Game_inf::gold_shark_animpath;
this->dieanim = Game_inf::Die_gold_shark_animpath;
this->HP = 140;
}
break;
case 0://蟋蟀鱼
{
this->anim = Game_inf::Cricket_fish_animpath;
this->dieanim = Game_inf::Die_Cricket_fish_animpath;
this->HP = 5;
}
break;
case 1://河豚
{
this->anim = Game_inf::globefish_animpath;
this->dieanim = Game_inf::Die_globefish_animpath;
this->HP = 60;
}
break;
case 2://乌龟
{
this->anim = Game_inf::green_turtle_animpath;
this->dieanim = Game_inf::Die_green_turtle_animpath;
this->HP = 100;
}
break;
case 3://灯鱼
{
this->anim = Game_inf::lantern_animpath;
this->dieanim = Game_inf::Die_lantern_animpath;
this->HP = 80;
}
break;
}
if (index == 80)
{
scale_s = 1;
animspeed = 150;
int t = rand() % (Game_inf::ScreenSize.y-300);
t += 200;
switch (rand() % 2)
{
case 0:
this->setPos(0,t );//向右
this->setRotation(0);
break;
case 1:
this->setPos(Game_inf::ScreenSize.x, t);
this->setRotation(180);
break;
}
this->_speed = 1;
}
else
{
scale_s = 0.6f;
animspeed = 80;
switch (rand() % 4)
{
case 0:
this->setPos(rand() % Game_inf::ScreenSize.x, 0);//向下
this->setRotation(rand() % 90 + 45);
break;
case 1:
this->setPos(rand() % Game_inf::ScreenSize.x, Game_inf::ScreenSize.y);//向上
this->setRotation(rand() % 90 + 225);
break;
case 2:
this->setPos(0, rand() % Game_inf::ScreenSize.y);//向右
this->setRotation(rand() % 2 == 0 ? rand() % 45 : rand() % 45 + 315);
break;
case 3:
this->setPos(Game_inf::ScreenSize.x, rand() % Game_inf::ScreenSize.y);
this->setRotation(rand() % 90 + 135);
break;
}
this->_speed = 2;
}
pre = next = 0;
now = 1;
this->it = this->anim->begin();
this->open(*(this->it));
}
bool fish::forward()
{
next = GetTickCount();
if ((long)next - (long)pre > animspeed)
{
if (now == this->anim->size())
{
if (this->isdie)
return true;
this->it = this->anim->begin();
now = 1;
}
else
{
this->it++;
now++;
}
this->open(*(this->it));
this->setScale(scale_s);
pre = next;
}
if (!isdie)
{
float alpha = this->_rotation*3.14f / 180.0f;
this->movePos(cos(alpha) * _speed, sin(alpha) * _speed);
if (
this->getPos().x<0 || this->getPos().x>Game_inf::ScreenSize.x ||
this->getPos().y<0 || this->getPos().y>Game_inf::ScreenSize.y
)
{
return true;
}
}
return false;
}
bool fish::die(int h)
{
if (h > this->HP)
{
this->isdie = true;
this->anim = this->dieanim;
now = 1;
this->it = this->anim->begin();
this->open(*(this->it));
return true;
}
else
{
this->HP -= 5;
}
return false;
}
Game_inf.h
#pragma once
#include <easy2d/easy2d.h>
#include<list>
#include<set>
#include"fish.h"
#define FISHCOUNT 4
#define FISHNUM 15
template<class T>
struct Vector_2
{
T x, y;
void Set(T x, T y)
{
this->x = x;
this->y = y;
}
Vector_2() {}
Vector_2(T x, T y)
{
this->x = x;
this->y = y;
}
Vector_2 operator += (Vector_2 &other)
{
return Vector_2(this->x + other.x, this->y + other.y);
}
T GetAngle()
{
return tanh(x / y)*180.0f/3.14f;
}
};
class Game_inf
{
public:
static std::list<char*>
*Cricket_fish_animpath,
*green_turtle_animpath,
*globefish_animpath,
*lantern_animpath,
*fishweb_animpath,
*gold_shark_animpath;
;
static std::list<char*>
*Die_Cricket_fish_animpath,
*Die_green_turtle_animpath,
*Die_globefish_animpath,
*Die_lantern_animpath,
*Die_gold_shark_animpath;
;
static Vector_2<int> ScreenSize;
static Vector_2<int> paoguanpos;
Game_inf();
~Game_inf();
void Fill(HWND hwnd);
static easy2d::Sprite* LoadFillImage(char*path);
static easy2d::Button* LoadButton(char*path,int x,int y, int width, int height, const easy2d::Function<void()> fun = nullptr);
};
Game_inf.cpp
#include "Game_inf.h"
std::list<char*> *Game_inf::fishweb_animpath;
std::list<char*> *Game_inf::Cricket_fish_animpath;
std::list<char*> *Game_inf::green_turtle_animpath;
std::list<char*> *Game_inf::globefish_animpath;
std::list<char*> *Game_inf::lantern_animpath;
std::list<char*> *Game_inf::gold_shark_animpath;
std::list<char*> *Game_inf::Die_Cricket_fish_animpath;
std::list<char*> *Game_inf::Die_green_turtle_animpath;
std::list<char*> *Game_inf::Die_globefish_animpath;
std::list<char*> *Game_inf::Die_lantern_animpath;
std::list<char*> *Game_inf::Die_gold_shark_animpath;
Vector_2<int> Game_inf::ScreenSize;
Vector_2<int> Game_inf::paoguanpos;
Game_inf::Game_inf()
{
Game_inf::ScreenSize.Set(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
}
Game_inf::~Game_inf()
{
}
void Game_inf::Fill(HWND hwnd)
{
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_CAPTION);
MoveWindow(hwnd, 0, 0, Game_inf::ScreenSize.x, Game_inf::ScreenSize.y, true);
}
easy2d::Sprite * Game_inf::LoadFillImage(char * path)
{
easy2d::Sprite* btnSprite = easy2d::gcnew easy2d::Sprite(path);
btnSprite->setSize(Game_inf::ScreenSize.x, Game_inf::ScreenSize.y);
return btnSprite;
}
easy2d::Button * Game_inf::LoadButton(char * path, int x, int y, int width, int height,const easy2d::Function<void()> fun)
{
easy2d::Sprite* btnSprite = easy2d::gcnew easy2d::Sprite(path);
btnSprite->setSize(width, height);
easy2d::Button*exit = easy2d::gcnew easy2d::Button(btnSprite,fun);
exit->setSize(width, height);
exit->setPos(x, y);
return exit;
}
gun.h
#pragma once
#include"Game_inf.h"
class gun:public easy2d::Sprite
{
float alpha;
public:
float _speed;
bool forward();
gun(easy2d::Sprite *start);
~gun();
};
class fishweb :public easy2d::Sprite
{
int nowanim;
std::list<char*>::iterator it;
long p1, p2;
bool quit;
public:
fishweb();
void onUpdate();
~fishweb();
};
gun.cpp
#include "gun.h"
bool gun::forward()
{
this->movePos(cos(alpha) * _speed, sin(alpha) * _speed);
if (
this->getPos().x<0 || this->getPos().x>Game_inf::ScreenSize.x ||
this->getPos().y<0 || this->getPos().y>Game_inf::ScreenSize.y
)
{
return true;
}
return false;
}
gun::gun(easy2d::Sprite *start)
{
this->open("gun/bulletP0_1.png");
this->setRotation(start->getRotation());
float alpha = (start->getRotation() - 90)*3.14f / 180.0f;
this->setPosX(start->getPosX() + cos(alpha)*100);
this->setPosY(start->getPosY() + sin(alpha) * 100);
this->_speed = 8;
this-> alpha = (this->_rotation - 90)*3.14f / 180.0f;
}
gun::~gun()
{
}
fishweb::fishweb()
{
this->nowanim = 0;
this->it = Game_inf::fishweb_animpath->begin();
this->open(*it);
this->setAnchor(0.5, 0.5);
p1 = p2 = 0;
quit = false;
}
void fishweb::onUpdate()
{
if (quit)return;
p1 = GetTickCount();
if (p1 - p2 > 50)
{
p2 = p1;
nowanim++;
if (nowanim == Game_inf::fishweb_animpath->size())
{
quit = true;
this->getParent()->removeChild(this);
}
else
{
this->it++;
this->open(*it);
}
}
}
fishweb::~fishweb()
{
}
weapon.h
#pragma once
#include"Game_inf.h"
#include"gun.h"
#include"fish.h"
#define GUNMAXCOUNT 30
class weapon:public easy2d::Sprite
{
fish **fishs;
gun **guns;
public:
void Collision_calculation();
void makefish();
Vector_2<float> fx;
easy2d::Node *gamesc;
bool shoot;
weapon();
void onUpdate();
~weapon();
};
weapon.cpp
#include "weapon.h"
#include"gun.h"
void weapon::Collision_calculation()
{
bool ok;
int x, y;
for (int i = 0; i < GUNMAXCOUNT; i++)//每一颗子弹
{
if (this->guns[i] != NULL)
{
if (this->guns[i]->forward())
{
this->gamesc->removeChild(this->guns[i]);
this->guns[i] = NULL;
continue;
}
ok = false;
for (int j = 0; j < FISHNUM; j++)
{
if (this->fishs[j] != NULL&&
!this->fishs[j]->isdie&&
this->fishs[j]->getBoundingBox().intersects(this->guns[i]->getBoundingBox()))
{
fishweb*web = easy2d::gcnew fishweb();
web->setPosX(this->guns[i]->getPosX());
web->setPosY(this->guns[i]->getPosY());
web->setScale(0.5f);
this->gamesc->addChild(web);
if (this->fishs[j]->die(rand() % 120))
{
ok = true;
}
for (j = 0; j < FISHNUM; j++)
{
if (this->fishs[j] != NULL)
{
x = this->fishs[j]->getPosX() - this->guns[i]->getPosX();
y = this->fishs[j]->getPosY() - this->guns[i]->getPosY();
if (sqrt(x*x + (y*y)) < 130)
{
if (this->fishs[j]->die(rand() % 120))
{
ok = true;
}
}
}
}
if(ok)
easy2d::MusicPlayer::play("audio\\FX_获分_02.wav");
this->gamesc->removeChild(this->guns[i]);
this->guns[i] = NULL;
break;
}
}
}
}
for (int j = 0; j < FISHNUM; j++)
{
if (this->fishs[j] != NULL)
{
if (this->fishs[j]->forward())
{
this->gamesc->removeChild(this->fishs[j]);
this->fishs[j] = NULL;
}
}
}
this->makefish();
}
void weapon::makefish()
{
for (int i = 0; i < FISHNUM; i++)
{
if (this->fishs[i] == NULL)
{
fish *g = easy2d::gcnew fish();
this->fishs[i] = g;
this->gamesc->addChild(g);
}
}
}
weapon::weapon()
{
this->guns = new gun*[GUNMAXCOUNT];
this->fishs = new fish*[FISHNUM];
for (int i = 0; i < GUNMAXCOUNT; i++)
{
this->guns[i] = NULL;
}
for (int i = 0; i < FISHNUM; i++)
{
this->fishs[i] = NULL;
}
this->open("UI\\炮管.png");
this->setSize(60, 80);
this->setAnchor(0.5, 0.8);
Game_inf::paoguanpos.Set(Game_inf::ScreenSize.x / 2, Game_inf::ScreenSize.y - 30);
this->setPos(Game_inf::ScreenSize.x / 2 , Game_inf::ScreenSize.y-30);
this->shoot = false;
}
void weapon::onUpdate()
{
if(shoot)
{
shoot = false;
easy2d::MusicPlayer::play("audio\\FX_发炮_01.wav");
fx.Set(this->getPos().x - easy2d::Input::getMouseX(),
this->getPos().y - easy2d::Input::getMouseY());
float a = -fx.GetAngle();
this->setRotation(a);
gun*g = easy2d::gcnew gun(this);
for (int i = 0; i < GUNMAXCOUNT; i++)
{
if (this->guns[i] == NULL)
{
this->guns[i] = g;
break;
}
}
this->gamesc->addChild(g);
}
this->Collision_calculation();
}
weapon::~weapon()
{
delete this->guns;
}
main.cpp
#pragma warning(disable:4244)
#include"Game_inf.h"
#include"fish.h"
#include"weapon.h"
#include<mmsystem.h>
#pragma comment (lib, "winmm.lib")
using namespace easy2d;
Game_inf *gameinf;
weapon *paoguan;
long p1, p2;
void clickscreen(Event*e)
{
if (e->type == Event::MouseDown)
{
p1= GetTickCount();
if (p1 - p2 > 100)
{
p2 = p1;
if (!paoguan->shoot)
paoguan->shoot = true;
}
}
}
int WINAPI WinMain(HINSTANCE h1,HINSTANCE h2,LPSTR cmd,int show)
{
//Logger::showConsole(true);
p1 = p2 = 0;
LoadSource();
gameinf = new Game_inf();
//gameinf->ScreenSize.Set(800, 600);
if (Game::init("捕鱼达人",gameinf->ScreenSize.x,gameinf->ScreenSize.y))
{
gameinf->Fill(easy2d::Window::getHWnd());//全屏
/* 设计游戏内容 */
MusicPlayer::preload("audio\\背景乐_03.mp3");
MusicPlayer::preload("audio\\FX_发炮_01.wav");
MusicPlayer::preload("audio\\FX_获分_02.wav");
MusicPlayer::play("audio\\背景乐_03.mp3",-1);
Scene *scene = gcnew Scene;
Node *game = gcnew Node;
game->setSize(Game_inf::ScreenSize.x, Game_inf::ScreenSize.y);
game->setPos(0, 0);
scene->addChild(game);
Node *UI = gcnew Node;
UI->setSize(Game_inf::ScreenSize.x, Game_inf::ScreenSize.y);
UI->setPos(0, 0);
scene->addChild(UI);
SceneManager::enter(scene);
//UI层
{
auto t = [] {
easy2d::Game::quit();
};
Button *exitbt = Game_inf::LoadButton("UI\\ui_fish_back1.png", 0, 0, 103, 73,t);
UI->addChild(exitbt);
Sprite*top = gcnew Sprite("UI\\wpBottom.png");
top->setSize(200, 75);
top->setPos(Game_inf::ScreenSize.x / 2-100, 0);
UI->addChild(top);
Sprite*bot = gcnew Sprite("UI\\wpCover.png");
bot->setSize(320, 150);
bot->setPos(Game_inf::ScreenSize.x / 2 - 160, Game_inf::ScreenSize.y - 110);
UI->addChild(bot);
Sprite*bottom = gcnew Sprite("UI\\炮台.png");
bottom->setSize(75, 56);
bottom->setPos(Game_inf::ScreenSize.x / 2 - 38, Game_inf::ScreenSize.y-56);
UI->addChild(bottom);
paoguan = new weapon();
UI->addChild(paoguan);
paoguan->gamesc = game;
}
game->addListener(clickscreen);
//游戏层
{
Sprite*bg = Game_inf::LoadFillImage("img\\背景_01.png");
game->addChild(bg);
}
Game::start();
RemoveSource();
UI->removeAllChildren();
game->removeAllChildren();
scene->removeAllChildren();
}
/* 销毁资源 */
Game::destroy();
delete gameinf;
return 0;
}