在屏幕绘制 10 个任意反弹的球

在屏幕绘制 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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值