#include "stdlib.h"
#include "GUI.H"
/*
颜色
*/
#define Black 0x000000
#define White 0xFFFFFF
#define Blue 0xFF0000
#define Red 0x0000FF
/*
画矩形区----------改造的和51一样的函数
*/
void Rectangle(int left,int up,int right,int down,U16 color)
{
GUI_SetColor(color);
GUI_DrawHLine(up,left,right);//横线
GUI_DrawHLine(down,left,right);//横线
GUI_DrawVLine(left,up,down);//垂线
GUI_DrawVLine(right,up,down);//垂线
}
/*
画线函数-----------改造的和51一样的函数
*/
void Line(int x0,int y0,int x1,int y1,U16 color)
{
GUI_SetColor(color);
GUI_DrawLine(x0,y0,x1,y1);
}
///////////////////////////////////////////////////////////////////////////
int b_x,b_y,b_l,b_w;//轨道的x,y,长,宽
//大车的位置,老位置,y值,宽,速度,加速度,左限位,右限位
int m_x,m_old_x,m_y,m_l,m_w,m_v,m_a,m_l_limit_p,m_r_limit_p;
//小车的位置,老位置,超出部分,速度,加速度,上限位,下限位
int s_y,s_old_y,s_extre_part,s_v,s_a,s_u_limit_p,s_d_limit_p;
int h_x,h_y,h_w;//横梁的x,y和宽
int move_x_v,move_y_v,move_a;//水平移动速度,
int line_x,line_old_x,line_y,line_l,line_old_l;//伸长部分x,老x,y,长,老长
//--------------------------------------------------------------
void init1()
{
b_x=40,b_y=120,b_l=100,b_w=420;
m_x=m_old_x=b_x+10,m_y=b_y-10,m_l=120,m_w=50,m_v=1,m_a=0,m_l_limit_p=40,m_r_limit_p=b_x+b_w-m_l_limit_p;
s_y=s_old_y=b_y+10,s_extre_part=10,s_v=1,s_a=0,s_u_limit_p=b_y+10,s_d_limit_p=b_y+b_l-10;
}
void init2()
{
h_x=b_x,h_y=b_y+b_l+60,h_w=b_w;
move_x_v=s_v*h_w/m_l,move_y_v=2,move_a=0;
line_x=line_old_x=100,line_y=h_y+3,line_l=line_old_l=50;
}
//---------------------------Draw-------------------------------
void drawBig()
{
Line(b_x,b_y,b_x+b_w,b_y,Black);
Line(b_x,b_y+b_l,b_x+b_w,b_y+b_l,Black);
}//主梁画
void drawMiddle()
{
if(m_x!=m_old_x)Line(m_old_x,m_y,m_old_x,m_y+m_l,White);
Line(m_x,m_y,m_x,m_y+m_l,Blue);
if(m_x!=m_old_x)Line(m_old_x+m_w,m_y,m_old_x+m_w,m_y+m_l,White);
Line(m_x+m_w,m_y,m_x+m_w,m_y+m_l,Blue);
}//画横梁
void drawSmall()
{
if(s_old_y!=s_y || m_x!=m_old_x)Line(m_old_x-s_extre_part,s_old_y,m_old_x+m_w+s_extre_part,s_old_y,White);
Line(m_x-s_extre_part,s_y,m_x+m_w+s_extre_part,s_y,Red);
}//画小车
void draw1()
{
drawBig();//画主梁
drawMiddle();//画横梁
drawSmall();//画小车
}//总画函数
//----------------------------run-----------------------------
void runMiddle()
{
m_old_x=m_x;
m_x+=m_v;
if(m_x+m_w>=m_r_limit_p || m_x<=m_l_limit_p)m_v*=-1;//达到警戒线
}//横梁移动
void runSmall()
{
s_old_y=s_y;
line_old_x=line_x;
s_y+=s_v;
line_x+=move_x_v;
if(s_y>=s_d_limit_p || s_y<=s_u_limit_p){
s_v*=-1;
move_x_v*=-1;
}//达到警戒线
}//小车移动
void run1()
{
runMiddle();
runSmall();
}//运动函数
/////////////////////////////////////////////////////////////////////////////////
//-------------------------draw--------------------------------
void drawGou()
{
if(line_old_x!=line_x || line_old_l!=line_old_l){
Rectangle(line_old_x-3,line_y+line_old_l,line_old_x+3,line_y+line_old_l+6,White);
Line(line_old_x,line_y+line_old_l+6,line_old_x,line_y+line_old_l+9,White);
Line(line_old_x,line_y+line_old_l+9,line_old_x+6,line_y+line_old_l+9,White);
Line(line_old_x+6,line_y+line_old_l+9,line_old_x+6,line_y+line_old_l+15,White);
Line(line_old_x+6,line_y+line_old_l+15,line_old_x-6,line_y+line_old_l+15,White);
Line(line_old_x-6,line_y+line_old_l+15,line_old_x-6,line_y+line_old_l+12,White);
}
Rectangle(line_x-3,line_y+line_l,line_x+3,line_y+line_l+6,Blue);
Line(line_x,line_y+line_l+6,line_x,line_y+line_l+9,Black);
Line(line_x,line_y+line_l+9,line_x+6,line_y+line_l+9,Black);
Line(line_x+6,line_y+line_l+9,line_x+6,line_y+line_l+15,Black);
Line(line_x+6,line_y+line_l+15,line_x-6,line_y+line_l+15,Black);
Line(line_x-6,line_y+line_l+15,line_x-6,line_y+line_l+12,Black);
}//画勾,x,y为绳的下端,r为大小
void drawH()
{
Line(h_x,h_y,h_x+h_w,h_y,Black);
}//画横梁
void drawHuaKuai()
{
if(line_old_x!=line_x)Rectangle(line_old_x-6*3/4,line_y-6,line_old_x+6*3/4,line_y,White);
Rectangle(line_x-6*3/4,line_y-6,line_x+6*3/4,line_y,Blue);
}//画滑块,x,y为绳的上端,r为大小
void drawLine()
{
if(line_old_l!=line_l || line_x!=line_old_x)Line(line_old_x,line_y,line_old_x,line_y+line_old_l,White);
Line(line_x,line_y,line_x,line_y+line_l,Black);
}//画线
void draw2()
{
drawH();
drawHuaKuai();
drawLine();
drawGou();
}
///////////////////////////////////////////////////////////////////////////
//-------------------------draw尺子--------------------------------
void draw0()
{
GUI_SetColor(GUI_YELLOW);
GUI_FillRect(b_x-15,b_y-15,b_x+b_w+15,h_y+190);
Rectangle(b_x-15,b_y-15,b_x+b_w+15,h_y+190,GUI_BLUE);//画边框
}
///////////////////////////////////////////////////////////////////////////
void Fun(void)//fun里面的Fun函数------一切动画贴图都在此
{
init1();//初始化1画面
init2();//初始化2画面
GUI_SetBkColor(WHITE);
GUI_Clear();
//GUI_DrawBitmap(&bmkk,0,0);
draw0();
while(1)
{
run1();
line_old_l=line_l;
line_l+=1;
if(line_l>160)line_l=10;
draw1();
draw2();
GUI_Delay(1);
}
}