在家太闲,编c++。
肝没了
1.MC加载动画
#include<graphics.h>
#include<conio.h>
#include<time.h>
int main(){
initgraph(500, 300);
setfillcolor(RGB(60,20,0));
setbkcolor RGB(60,20,0);
solidrectangle(0,0,1000,1000);
int x=50;
setfillcolor(RGB(0,250,0));
setlinecolor(RGB(0,150,0));
setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 2);
setbkmode(OPAQUE);
outtextxy(230,100,"Æô¶¯ÖÐ...");
while(x<=450){
BeginBatchDraw();
solidrectangle(50,155,x,165);
int xn=50;
while(xn<=x){
rectangle(50,155,xn,165);
xn+=25;
}
rectangle(xn+2,155,x,165);
EndBatchDraw();
if(rand()%10>1)Sleep(rand()%1);
else Sleep(rand()%300);
clearrectangle(50,155,x,165);
x++;
}
}
2.一堆彩色小球撞来撞去
#include <conio.h>
#include <graphics.h>
#include <time.h>
#define number 10 //СÇò¸öÊý
struct Ball{
int ball_x;
int ball_y;
int ball_vx;
int ball_vy;
int distance[2];
int coo;};
Ball balls[number];
int main(){
void get_distance(struct Ball balls[number]);
void knock(struct Ball balls[number]);
srand((unsigned)time(NULL));
for(int i=0;i<number;i++){
balls[i].ball_x=rand()%600+20;
balls[i].ball_y=rand()%360+20;
for(int j=0;j<number;j++)
if(i!=j){
//³õʼ»¯ÈÃСÇò²»»áÇáÒ×Öصþ
if((balls[i].ball_x>=balls[j].ball_x-20)&&(balls[i].ball_x<=balls[j].ball_x+20)&&
(balls[i].ball_y>=balls[j].ball_y-20)&&(balls[i].ball_y<=balls[j].ball_y+20))
{
balls[i].ball_x=rand()%600+20;
balls[i].ball_y=rand()%360+20;
}
}
balls[i].ball_vx=1;
balls[i].ball_vy=1;
balls[i].coo=RGB(rand()%255,rand()%255,rand()%255);
//printf("[%d,%d]\n",balls[i].ball_x,balls[i].ball_y);
}
for (i=0;i<number;i++)
{
balls[i].distance[0] = 99999999;
balls[i].distance[1] = -1;
}
initgraph(640,400);
BeginBatchDraw();
while(1)
{
//»æÖÆËùÓÐСÇò
for(int i=0;i<number;i++)
{
setcolor(balls[i].coo);
setfillcolor(balls[i].coo);
fillcircle(balls[i].ball_x,balls[i].ball_y,20);
}
get_distance(balls);
knock(balls);
Sleep(2);
FlushBatchDraw();
setcolor(BLACK);
setfillcolor(BLACK);
//¸Ä±äËùÓÐСÇò×ø±ê
for(i=0;i<number;i++)
{
fillcircle(balls[i].ball_x,balls[i].ball_y,20);
balls[i].ball_x=balls[i].ball_x+balls[i].ball_vx;
balls[i].ball_y=balls[i].ball_y+balls[i].ball_vy;
if(balls[i].ball_x<=20||balls[i].ball_x>=620)
balls[i].ball_vx=-balls[i].ball_vx;
if(balls[i].ball_y<=20||balls[i].ball_y>=380)
balls[i].ball_vy=-balls[i].ball_vy;
}
}
EndBatchDraw();
closegraph();
return 0;
}
int i,j;
void get_distance(struct Ball balls[number])
{
// Çó½âËùÓÐСÇòÁ½Á½Ö®¼äµÄ¾àÀëƽ·½
for (i=0;i<number;i++)
{
for (j=0;j<number;j++)
{
if (i!=j) // ×Ô¼ººÍ×Ô¼º²»ÐèÒª±È
{
int dist2;
dist2 = (balls[i].ball_x - balls[j].ball_x)*(balls[i].ball_x - balls[j].ba