Discuz! Board

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

fly bird(2)

[复制链接]

49

主题

70

帖子

23万

积分

论坛元老

Rank: 8Rank: 8

积分
232976

大王燕 Lv:51
发表于 2022-12-9 19:47:55 | 显示全部楼层 |阅读模式
[C++] 纯文本查看 复制代码
#include<bits/stdc++.h>
#include <graphics.h>             //包含EGE的头文件
#include<time.h>
using namespace std;
int width=300;
int hight=600;
int number;
int count1=1,count2;
char ch;
int wall_w = 50,wall_h = 50;//障碍物宽和高
int wall_pass = 200;//障碍物间的缺口长度
int score,score_flag;
struct ballon
{
    int r;
    float vx;
    float vy;
    float x;
    float y;
    float g;
    COLORS color;
}ball[1];
struct wall{
    int xr;//左上x坐标
    int yr;//左上y坐标
    int xl;//右下x坐标
    int yl;//右下y坐标 y
    int vx;//x向速度
}w,wall_t;

float inx(int min,int max)
{
    float t=rand()%(max-min)+min;
    return t;
}

void start()
{
    ball[0].x = inx(ball[0].r*2+1,width-ball[0].r);
    ball[0].y = inx(ball[0].r*2+1,hight-ball[0].r);
    ball[0].vx = rand()%10;
    ball[0].vy = rand()%10;
    ball[0].r = 10+rand()%10;
    ball[0].g = 0.25;
    ball[0].color=RED;
    w.xr = width;
    w.yr = inx(100,300);//150;
    w.xl = width-wall_w;
    w.yl = 0;
    w.vx = -2   ;
    initgraph(width, hight);             //初始化图形界面
    setcolor(RED);  //设置画图线为红色
    setbkcolor(WHITE);//设置背景颜色为白色
    setfillcolor(RED);//设置填充颜色为红色
}

void update()
{
	ball[0].vy=ball[0].vy+ball[0].g;
    ball[0].y=ball[0].vy+ball[0].y;
    if(ball[0].y>=hight-ball[0].r)
    {
        ball[0].y=hight-ball[0].r;
        ball[0].vy=-ball[0].vy;
    }
    if(ball[0].y<=ball[0].r)
    {
        ball[0].y=ball[0].r;
        ball[0].vy=-ball[0].vy;
    }//球体的运动
    if(kbhit())
    {
        ch = getch();
        if(ch==' ')
        {
            ball[0].vy=-10;
        } 
    }//按键响应
	//w.xr=w.vx+ball[0].g;
    w.xl=w.vx+w.xl;
    w.xr=w.xl+50;
    //w.xl+=w.vx;
	//墙体的运动
	for(int i=0;i<20;i++ )
	if(w.xr==0)
	{
  		w.xr = width;
    	w.yr = inx(100,300);//150;
    	w.xl = width-wall_w;
    	w.yl = 0;
	}
	
}

void  draw()
{
  	 setfillcolor(ball[0].color);	
	fillellipse(ball[0].x, ball[0].y, ball[0].r, ball[0].r); //画一个实心圆 
    //floodfillsurface(400, 300, RED);
 
  	bar(w.xl,w.yl,w.xr,w.yr);  
  	bar(w.xl,w.yr+wall_pass,w.xl+50,hight);
	Sleep(10);
    cleardevice();//绘制球体

	//绘制墙体           
}

bool gameover()
{
	if((ball[0].x>w.xl)&&(ball[0].x<w.xr))//w.xl<w.xr+ball[0].r&&w.yr+ball[0].r<w.yr
	{
		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()
{
   start();
   while(1)
   {
   		update();
   		draw();
   		if(gameover()) break;
   }
    getch();                            //暂停,等待键盘按键
    closegraph();        //关闭图形界面
        
    
       
    return 0;
}

回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

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

Powered by Discuz! X3.4

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

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