直接上效果图 使用:按空格伸出钩子,再没抓到东西之前,再按一次空格,钩子会收回。 由于图片音乐资源的限制,这里不好放,所以直接给网盘链接。
网盘链接点我 提取码:vdyl 程序代码,注意看有一个头文件tools.hpp
#include<stdio.h>
#include<easyx.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include"tools.hpp"
#pragma comment(lib,"Winmm.lib");
#define MAX_ANGLE 80
#define MANHATTAN_DIS(hook) sqrt((hook->x-hook->endx)*(hook->x-hook->endx)+(hook->y-hook->endy)*(hook->y-hook->endy));
#define MAXNUM 10
int flag = 0;
enum Dir { Left,Right };
enum HookState {Normal,Long,Short};
enum CoinType{Gold,Wallet,Stone,Max};
IMAGE img_bk;
IMAGE imgs[4];
struct Role {
float x;
float y;
int width;
int height;
int money;
};
struct Hook {
float x;
float y;
float endx;
float endy;
float lenth;
float angle;
int state;
int dir;
int index ;
};
struct Object {
int x;
int y;
int width;
int height;
int value;
bool isdie;
int type;
};
Role role;
Hook hook;
Object ob[MAXNUM];
void init();
void initResource();
void draw();
void initResource() {
loadimage(&img_bk, "./res/bk.jpg",0,getheight()-120);
for (int i = 0; i < 4; i++) {
char str[40] = { 0 };
sprintf(str, "./res/%d.png", i);
loadimage(imgs + i, str);
}
}
void init_ob(Object* ob,int x,int y,int type) {
ob->x = x;
ob->y = y;
ob->type = type;
ob->isdie = false;
ob->height = imgs[ob->type].getheight();
ob->width = imgs[ob->type].getwidth();
}
void ob_draw(Object* ob) {
for (int i = 0; i < MAXNUM; i++) {
if(!ob->isdie)
drawImg(ob->x, ob->y, imgs+ob->type);
}
}
void init_role(Role* role,int x,int y) {
role->x = x;
role->y = y;
role->width = imgs[3].getwidth();
role->height = imgs[3].getheight();
role->money = 0;
}
void role_draw(Role* role) {
drawImg((getwidth() - imgs[3].getwidth())/2, 0, imgs + 3);
settextstyle(48, 0, "正楷");
settextcolor(RED);
setbkmode(TRANSPARENT);
char buf[256] = { 0 };
sprintf(buf, "金币数:%d ", role->money);
outtextxy(20, (role->height - textheight(buf)) / 2, buf);
}
void init_hook(Hook* hook,int x,int y) {
hook->x = x;
hook->y = y;
hook->lenth = 50;
hook->endx = x;
hook->endy = y + hook->lenth;
hook->angle = 0;
hook->state=Normal;
hook->dir = Right;
hook->index = -1;
}
void hook_draw(Hook* hook) {
setlinestyle(PS_SOLID, 3);
setlinecolor(BLACK);
line(hook->x, hook->y, hook->endx, hook->endy);
setfillcolor(BROWN);
solidcircle(hook->endx, hook->endy,5);
}
void hook_swing(Hook* hook,float speed) {
if (hook->state == Normal) {
if (hook->angle > MAX_ANGLE) {
hook->dir = Left;
}
else if (hook->angle < -MAX_ANGLE) {
hook->dir = Right;
}
if (hook->dir == Left) {
hook->angle -= speed;
}
else if (hook->dir == Right) {
hook->angle += speed;
}
hook->endx = sin(3.1415 / 180 * hook->angle) * hook->lenth + hook->x;
hook->endy = cos(3.1415 / 180 * hook->angle) * hook->lenth + hook->y;
}
}
void control_hook(Hook* hook,float speed) {
if (GetAsyncKeyState(VK_SPACE) & 0x8000&&hook->state==Normal) {
hook->state = Long;
flag = 1;
}
if (GetAsyncKeyState(VK_SPACE) & 0x8000 && hook->state == Long && flag == 1&&hook->lenth>280) {
hook->state = Short;
flag = 0;
}
if (hook->state == Long) {
hook->lenth += speed;
hook->endx = hook->x + sin(3.1415 / 180 * hook->angle) * hook->lenth;
hook->endy = hook->y + cos(3.1415 / 180 * hook->angle) * hook->lenth;
if (hook->endx<0 || hook->endx>getwidth() ||
hook->endy > getheight()) {
hook->state = Short;
}
}
if (hook->state == Short) {
hook->lenth -= speed;
hook->endx = hook->x + sin(3.1415 / 180 * hook->angle) * hook->lenth;
hook->endy = hook->y + cos(3.1415 / 180 * hook->angle) * hook->lenth;
if (hook->lenth < 50) {
hook->state = Normal;
}
}
}
void HookGet(Object*ob) {
if (hook.state == Normal && hook.index != -1) {
ob[hook.index].isdie = true;
role.money += rand() % 100;
hook.index = -1;
}
if (hook.state == Normal) {
return;
}
for (int i = 0; i < MAXNUM; i++) {
if (!(ob+i)->isdie&&hook.endx > (ob + i)->x && hook.endx<(ob+i)->x+(ob+i)->width &&
hook.endy>(ob + i)->y && hook.endy < (ob+i)->y+(ob + i)->height) {
hook.index = i;
break;
}
}
if (hook.index != -1) {
hook.state = Short;
(ob + hook.index)->x = hook.endx-(ob + hook.index)->width/2;
(ob + hook.index)->y = hook.endy-(ob+hook.index)->height/2;
}
}
void draw() {
putimage(0, 120, &img_bk);
setfillcolor(YELLOW);
solidrectangle(0, 0, getwidth(), imgs[3].getheight());
role_draw(&role);
hook_draw(&hook);
for (int i = 0; i < MAXNUM; i++) {
if(!ob[i].isdie)
ob_draw(&ob[i]);
}
}
void init() {
mciSendString("./res/open aa.mp3 ", 0, 0, 0);
mciSendString("play ./res/aa.mp3 repeat", 0, 0, 0);
initResource();
init_role(&role, (getwidth() - imgs[3].getwidth()) / 2, 0);
init_hook(&hook, role.x + 40, role.y + 95);
for (int i = 0; i < MAXNUM; i++) {
int x = rand() % (getwidth() - 40) + 20;
int y = rand() % (getheight() - role.height) + role.height;
init_ob(&ob[i], x,y ,rand()%Max);
}
}
int main() {
initgraph(1080, 640, EW_SHOWCONSOLE);
srand(time(0));
init();
while (1){
BeginBatchDraw();
draw();
EndBatchDraw();
hook_swing(&hook,0.3);
control_hook(&hook,3);
HookGet(ob);
}
return 0;
}
|