Discuz! Board

 找回密码
 立即注册
查看: 354|回复: 1

122

[复制链接]

17

主题

17

帖子

637

积分

高级会员

Rank: 4

积分
637
发表于 2022-11-12 14:00:19 | 显示全部楼层 |阅读模式
#include <graphics.h>                 //包含EGE的头文件
#include<time.h> //包含随机数,时间函数
#include<bits/stdc++.h>
using namespace std;
int width =800;
int height =600;
int num;
struct ballon
{
    float x;
    float y;
    float vx;
    float vy;
    float radius;
        int health;//健康=1,不健康=0
}ball[100000];

void start()//初始化
{
        cout<<"请输入球的数量:";
        cin>>num;
        srand(time(0));//随机种子函数      
    for(int i=0;i<num;i++)
    {
        ball[i].x=rand()%400+10;//ball[i].x坐标
        ball[i].y=rand()%400+10;//ball[i].y坐标
        ball[i].vx=rand()%3+1;//ball[i].x向速度
        ball[i].vy=rand()%5+1;//ball[si].y向速度
        ball[i].radius =rand()%3+10;//半径
        ball[i].health=1;
        }
    ball[0].health=0;
   
    float g=0;
    initgraph(width, height);               //初始化图形界面
    setcolor(GREEN);  //设置画图线为红色
    setbkcolor(WHITE);//设置背景颜色为白色
    setfillcolor(GREEN);//设置填充颜色为红色
}

void update()//数据更新
{
       
        for(int i=0;i<num;i++)
    {
        ball[i].x=ball[i].x+ball[i].vx;
        ball[i].y=ball[i].y+ball[i].vy;
        //ball[i].vy=ball[i].vy+g;
        if(ball[i].y>=height-ball[i].radius)
        {
            ball[i].vy=-ball[i].vy;
        }
        if(ball[i].y<=ball[i].radius)
        {
            ball[i].vy=-ball[i].vy;
        }
        if(ball[i].x>=width-ball[i].radius)
        {
            ball[i].vx=-ball[i].vx;
        }
        if(ball[i].x<=ball[i].radius)
        {
            ball[i].vx=-ball[i].vx;
        }
    }
}

void drawball()//画球
{
        for(int i=0;i<num;i++)
        {
                fillellipse(ball[i].x, ball[i].y, ball[i].radius, ball[i].radius); //画一个实心圆
        }
    Sleep(10);
    cleardevice();         
}

int main()
{
          start();
    while(1)
    {
        update();
        drawball();
    }
    return 0;
}
回复

使用道具 举报

20

主题

33

帖子

3458

积分

论坛元老

Rank: 8Rank: 8

积分
3458

小火马 Lv:11
发表于 2022-12-10 13:38:03 | 显示全部楼层
穷缝纫机
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-2-13 05:21 , Processed in 0.049397 second(s), 28 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表