我打算用visual studio2019编一个黄金矿工小游戏,代码是照着视频看了敲的,但是为什么我的钩子没有办法转起来啊,求解
#include<stdio.h>
#include<graphics.h>
#include<cmath>
#include<math.h>
#define WIDTH 1080 //调整窗口的宽和高
#define HEIGHT 640
#define PI 3.14159
//定义不同图片所对应的下标
enum Index {
i_gold = 0,
i_start = 1,
i_catch = 2,
i_stone = 3,
i_ghost = 4,
i_box = 5,
i_enter = 6,
i_fail = 7,
i_success = 8,
i_store = 9,
};
/*
钩子摆动状态,正常摆动,伸长,缩短,停止
*/
enum ATTR
{
LEFT,
RIGHT,
M_NORMAL,
M_LONG,
M_SHORT
};
struct Role //定义结构体Role
{
int x; //图片的坐标
int y;
int width; //图片的长和宽
int height;
int coin; //获得总金钱数
};
struct Role role;
struct Hook
{
int x;
int y;
int endx, endy;
int len; //钩子长度
double angle; //钩子当前角度
int dir; //钩子摆动方向 左 ,右
int vx, vy; //速度变化量
int state; //当前状态
};
struct Hook hook;
struct Mine { //物品
int x ;
int y;
int type;
int size;
bool flag;
int gold;
};
struct Mine mine[10];
//把图片加载进程序
IMAGE img[11];
void loadImg() {
for (int i = 0; i < 10; i++) {
char fileName[20] = "";
sprintf(fileName, "./images/%d.jpg", i);
loadimage(img + i, fileName,0,0,false);
}
loadimage(img+10,"./images/bk.jpg",1080,640,false);
}
void gameInit() {
loadImg();
role.width = 65;
role.height = 58;
role.x = WIDTH / 2 - role.x / 2;
role.y = 0;
hook.len = 50;
hook.x = role.x+25;
hook.y = role.y+42;
hook.endx = hook.x;
hook.endy = hook.y+hook.len;
hook.angle = 0.0;
hook.dir = RIGHT;
hook.state = M_NORMAL;
}
/*
初始化
绘制
抓 endx,endy, 是否在物品内 左上角x,y,size
*/
//图像实现函数
void gameDraw() {
setfillcolor(YELLOW); //设置背景颜色
solidrectangle(0, 0, WIDTH, role.height); //定义上方实心矩形
putimage(0, role.height, img + 10); //显示图片
putimage(role.x, role.y, img + i_catch);
//输出我的金钱总数
setbkmode(TRANSPARENT); //设置背景透明
settextstyle(40, 0, "黑体"); //设置字体
settextcolor(BLACK); //设置字体颜色
char coin[20] = "";
sprintf(coin, "金钱:%d", role.coin);
outtextxy(20, 20, coin); //不能用printf,因为它是向控制台输出
//绘制钩子
setlinecolor(BLACK); //设置钩子颜色
setlinestyle(PS_SOLID, 4); //设置钩子实线,粗
line(hook.x,hook.y,hook.endx,hook.endy);
//点的实现
setfillcolor(WHITE);
solidcircle(hook.endx, hook.endy, 5);
}
//钩子摆动函数
void hookRock() {
hook.angle++;
if (hook.state == M_NORMAL)
{
if (hook.dir == RIGHT) {
hook.angle--;
}
else if (hook.dir == LEFT) {
hook.angle++;
}
if (hook.angle > 80) {
hook.dir = LEFT;
}
else if (hook.angle < -80)
{
hook.dir = RIGHT;
}
hook.endx = hook.x + sin(hook.angle * PI / 180) * hook.len;
hook.endx = hook.y + cos(hook.angle * PI / 180) * hook.len;
}
}
//求两点之间距离
double distance(Hook h) {
return sqrt((hook.endx - hook.x) * (hook.endx - hook.x) + (hook.endy - hook.y) * (hook.endy - hook.y));
}
//钩子伸缩函数
void keyControl(int speed) {
if (GetAsyncKeyState(VK_SPACE)) { //virtual key获取键盘空格信息
hook.state = M_LONG;
hook.vx = sin(PI / 180 * hook.angle) * speed;
hook.vy= cos(PI / 180 * hook.angle) * speed;
}
if (hook.state == M_LONG) {
hook.endx += hook.vx;
hook.endy += hook.vy;
}
else if(hook.state==M_SHORT)
{
hook.endx -= hook.vx;
hook.endy -= hook.vy;
if (distance(hook)<=hook.len) {
hook.state = M_NORMAL;
}
}
//碰到边界返回
if (hook.endx<0 || hook.endx>WIDTH || hook.endy<0||hook.endy > HEIGHT) {
hook.state = M_SHORT;
}
}
int main() {
initgraph(WIDTH, HEIGHT,EW_SHOWCONSOLE);
gameInit();
while (1)
{
//双缓冲绘图,防止闪屏
BeginBatchDraw();
gameDraw();
EndBatchDraw();
hookRock();
keyControl(5);
}
getchar(); //防止闪退
return 0;
}
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。