Discuz! Board

 找回密码
 立即注册
查看: 426|回复: 0

12.9

[复制链接]

51

主题

54

帖子

8724

积分

论坛元老

Rank: 8Rank: 8

积分
8724

大牙狸 Lv:12
发表于 2022-12-9 19:47:16 | 显示全部楼层 |阅读模式
#include <graphics.h>
#include<time.h> //包含随机数,时间函数
#include<bits/stdc++.h>
using namespace std;
int high=600,width=800,num=1,count1,time_count1;
char ch;
int wall_w,wall_h;//障碍物宽和高
int wall_pass=300;//障碍物间的缺口长度
struct ballon
{
        float x;
        float y;
        int r;
        float vx;
        float vy;
        float g;
        unsigned int R_color,G_color,B_color;
        COLORS color;
        //int health;
}ball[1000];

struct wall
{
    int xr;//左上x坐标
    int yr;//左上y坐标
    int xl;//右下x坐标
    int yl;//右下y坐标 y
    int vx;//x向速度
}w,wall_t;
float ranx(int min,int max)
{
        float h;
        h=rand()%(max-min)+min;
        return h;
}

/*void is_ill()
{
        for(int i=0;i<num;i++)
        {
                for(int j=0;j<num;j++)
                {

                                //if(ball[i].health!=ball[j].health)
                                //{
                                        //if((abs(ball[i].x-ball[j].x)<=ball[i].r+ball[j].r)&&
                                        //(abs(ball[i].y-ball[j].y)<=ball[i].r+ball[j].r))
                                //        {
                                                //float l=ranx(0,100);
                                                //if(l<30)
                                                //{
                                                        //ball[i].health=0;
                                                        //ball[j].health=0;
                                                        //count1++;
                                                //}
                                //        }
                        //        }
                }
        }
}*/

void startup()
{
        srand(time(0));//随机种子函数        
    for(int i =0;i<num;i++)
    {
        ball[i].x = ranx(ball[i].r+1,width-ball[i].r-1);
        ball[i].y = ranx(ball[i].r+1,high-ball[i].r-1);
        ball[i].vx = 0;
        ball[i].vy = 0;
        ball[i].g = 0.1;
        ball[i].r = 10+rand()%10;
                ball[i].R_color = ranx(10,255);
        ball[i].G_color = ranx(10,255);
        ball[i].B_color = ranx(10,255);
        ball[i].color=GREEN;
                //ball[i].health=1;
        }
        w.vx = 2;
        w.xr = 750;
        w.yr = ranx(50,300);
        w.xl = w.xr-50;
        w.yl = 0;
        ball[0].color=RED;
        count1=1;
        initgraph(800, 600);
        setcolor(BLACK);
    setbkcolor(WHITE);
    setfillcolor(GREEN);
        setfont(25,0,"幼圆");//(字体高度,字体宽度(为0即为自适应),字形)设置字体为25,幼圆字体
    setbkmode(TRANSPARENT); //设置文字背景色为透明(默认为有背景色)
}

void update()
{
        if(kbhit())
            {
                ch=getch();
                if(ch==' ')
                {
                    ball[0].vy=-5;
                }
            }
        for(int i=0;i<num;i++)
        {
                ball[i].vy=ball[i].vy+ball[i].g;
                        ball[i].y+=ball[i].vy;
                        ball[i].x+=ball[i].vx;
                        if(ball[i].y>=high-ball[i].r)
                        {
                                ball[i].y=high-ball[i].r;
                                ball[i].vy=-ball[i].vy;
                        }
                        if(ball[i].y<=ball[i].r)
                        {
                                ball[i].y=ball[i].r;
                                ball[i].vy=-ball[i].vy;
                        }
                        if(ball[i].x<=ball[i].r)
                        {
                                ball[i].x=ball[i].r;
                                ball[i].vx=-ball[i].vx;
                        }
                        if(ball[i].x>=width-ball[i].r)
                        {
                                ball[i].x=width-ball[i].r;
                                ball[i].vx=-ball[i].vx;
                        }
        }
        if(w.xr==0)
        {
                w.xr = 750;
                w.yr = ranx(50,300);
                w.xl = w.xr-50;
                w.yl = 0;
                ball[0].g=ball[0].g+0.5;  
        }
        w.xl = w.xl -w.vx;
}

void draw()
{
        w.xr = w.xl+50;
        bar(w.xl,w.yl,w.xr,w.yr);
        bar(w.xl,w.yr+wall_pass,w.xr,high);
        for(int i=0;i<num;i++)
        {
                setfillcolor(ball[i].color);
                fillellipse(ball[i].x,ball[i].y,ball[i].r,ball[i].r);
        }
        Sleep(10);
        time_count1+=10;
        cleardevice();
}

bool gameover()
{
        if(ball[0].x>=w.xl&&ball[0].x<=w.xr)
        {
                if((ball[0].y-ball[0].r<w.yr)||(ball[0].y+ball[0].r>w.yr+wall_pass))
                {
                        return 1;
                }
        }
        return 0;
       
}
int main()
{
        startup();
        while(1)
        {
                draw();
                 update();
                if(gameover())
                {
                        break;
                }
        }
        getch();                               
        closegraph();      
        return 0;
}
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-7-18 11:26 , Processed in 0.057493 second(s), 22 queries .

Powered by Discuz! X3.4

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

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