在屏幕绘制 10 个任意反弹的球
最近在看easyx 的教学,虽然之前写了个贪吃蛇小游戏,但都是用到啥查什么写什么,没系统的看。工作一如既往的忙,只能抽空学学习,心情不太美丽,我觉得还是更喜欢写代码而不是写八股文。
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include<math.h>
#define HIGH 1200
#define WIDTH 1600
#define RADIUS 20
typedef struct Ball
{
int x;
int y;
int r;
int velocity_x;
int velocity_y;
}BALL;
static BALL ball[10];
void startup()
{
int a = 0;
initgraph(WIDTH, HIGH);
srand(time(NULL));
setbkcolor(RGB(255, 255, 255));
cleardevice();//清屏
setfillcolor(RED);
for (int i = 0; i < 10; i++)
{
ball[i].x = rand() % (WIDTH - RADIUS) + RADIUS;
ball[i].y = rand() % (HIGH - RADIUS) + RADIUS;
ball[i].r = RADIUS;
a = rand() % 361;
ball[i].velocity_x = (RADIUS / 5) * cos(a);
ball[i].velocity_y = (RADIUS / 5) * sin(a);
fillcircle(ball[i].x, ball[i].y, RADIUS);
}
}
void show()
{
for (int i = 0; i < 10; i++)
{
setfillcolor(WHITE);
fillcircle(ball[i].x, ball[i].y, RADIUS);
if ((ball[i].x - RADIUS <= 0) || (ball[i].x + RADIUS >= WIDTH))
ball[i].velocity_x = - ball[i].velocity_x;
if ((ball[i].y - RADIUS <= 0) || (ball[i].y + RADIUS >= HIGH))
ball[i].velocity_y = -ball[i].velocity_y;
ball[i].x += ball[i].velocity_x;
ball[i].y += ball[i].velocity_y;
setfillcolor(RED);
fillcircle(ball[i].x, ball[i].y, RADIUS);
}
}
int main(void)
{
startup();
while (!_kbhit())
{
show();
Sleep(10);
}
closegraph();
}