6、动画改进
今天又是新的一天,继续昨天的动画改进。
1) 动画的概念
动画的含义可以是动作/运动+画面显示
之前函数中无论是放大小球的动画,都涉及到了两个方面
- 运动变量的变化和动作位置的计算
- 画面显示
后续所有的动画效果都会根据这个来编写代码。
2)动画结构体的定义
定义动画类型,动画各种各样,我暂时设计五个,分别是初始化、静态、下降、碰撞和自定义,这个枚举后续可能还会发生改变:
//定义动画类型
typedef enum
{
ANIMA_OBJ_START, //初始化
ANIMA_OBJ_STATIC,
ANIMA_OBJ_DOWN,
ANIMA_OBJ_COLLISION, //碰撞
ANIMA_OBJ_CUSTOM, //自定义
}enanima_obj_type;
动画的结构体会有点复杂,需要一点点地增加,先写运动变量相关的参数。结构体按照我之前所说的运动变量和画面显示来定义。
当然,画面显示不可能一只用sleep后续肯定是要改成定时器来控制的有了定时器就可以计算画面的显示帧数了。所有的动画函数都会存入动画列表中。
//动作运动和画面显示
typedef struct animation
{
enanima_obj_type anima_obj_tag;
float x; //运动的位置x轴和y轴
float y;
float r; //物体的大小
float step; //运动变化量
int velocity;//运动速度
int acceleration;
//动画定时器
//回调函数列表。anima的操作->
}games_anima_s;
typedef games_anima_s* games_anima_t;
接着,在小球的结构体中加入动画结构体的指针:
typedef struct circle
{
float x;
float y;
float r;
games_anima_t anima;//操作动画的指针
}games_circle_s;
最后,在创建小球的函数graph_draw_circle_create中为动画开辟一块空间,在使用的时候改变小球动画的状态:
circle->anima = (games_anima_t)malloc(2 * sizeof(games_anima_s));
if (circle->anima == NULL)
{
perror("circle anima malloc error");
return NULL;
}
circle->anima->x = x;//记录物体的位置
circle->anima->y = y;
circle->anima->r = r;//记录物体的大小
circle->anima->step = 0;
circle->anima->anima_obj_tag = ANIMA_OBJ_COLLISION;
在主函数中可以任意赋值来改变动画效果
circle->anima->anima_obj_type = ANIMA_OBJ_DOWN;
circle->anima->step = 100;
3) 使用anima结构体,将之前的参数全部放入结构体当中,比如:
void games_circle_down(games_circle_t circle, int n)
{
for (int i = 0; i <= n; i++)
{
circle->anima->y = circle->anima->y + circle->anima->step;
cleardevice();
fillcircle(circle->anima->x, circle->anima->y, circle->anima->r);
Sleep(circle->anima->vec);
}
}
7、弹起动画实现
小球简单代码逻辑:判断小球当前位置是否会发生碰撞->如果发生了碰撞那就进行弹起的动画效果->计算弹起的数值->显示弹起动画
实现如下:
void games_obj_bounce(games_circle_t circle, n)
{
//判断是不是发生了碰撞
if (circle->anima->y >= screen_height - circle->anima->r)
{
y_collision = circle->anima->y;
//计算碰撞后小球位置的变化
for (int j = 0; j <= 10; j++)
{
y_collision = y_collision - 10;
cleardevice();
fillcircle(circle->anima->x, y_collision,circle->anima->r);
Sleep(speed);
}
circle->anima->y = y_collision;
}
}
2024/02/23