Discuz! Board

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

1.8 躲避球

[复制链接]

54

主题

66

帖子

574

积分

高级会员

Rank: 4

积分
574
发表于 2023-1-8 17:40:03 | 显示全部楼层 |阅读模式
[C++] 纯文本查看 复制代码
#include <graphics.h> //包含EGE的头文件
#include <time.h> //包含时间、随机数的头文件 
#include <bits/stdc++.h>
using namespace std;
int mousePosX = -1, mousePosY = -1;//鼠标位置
mouse_msg msgRecord = { 0 };//鼠标结构体初始化
int n,count2=0,num=0;
int temp=0xffff;
float t=0;
int height=600,width=400;
float rnx(int min,int max)  //随机数函数 
{
    float t;
    t=rand()%(max-min)+min;
    return t;
}
struct ballon
{
    int r;
    double g,y,x,vy,vx;
}ball[1000];
void csh()  //初始化函数 
{
    initgraph(width, height);   //初始化图形界面
    setcolor(BLACK);    //设置画图线为黑色   
    setbkcolor(YELLOW);     //设置背景颜色为黄色
    srand(time(0));     //随机种子函数  
    setfont(25,0,"宋体");//(字体高度,字体宽度(为0即为自适应),字形)设置字体为25,宋体
    setbkmode(TRANSPARENT); //设置文字背景色为透明(默认为有背景色)
}
void new_ball()
{
    num++;
    ball[num].x = rnx(30,width-30);
    ball[num].y = 5;
    ball[num].vx = rnx(-2,2);
    ball[num].vy = rnx(1,3);
    ball[num].r = 5; 
}
void update()
{
    for(int i =0;i<num;i++)
    {
        ball[i].x += ball[i].vx;
        ball[i].y += ball[i].vy;
        if(ball[i].x<=ball[i].r || ball[i].x>=width-ball[i].r)ball[i].vx = -ball[i].vx;
        if(ball[i].y<=ball[i].r || ball[i].y>=height-ball[i].r)ball[i].vy = -ball[i].vy;
    }
    while (mousemsg())
    {  
        msgRecord = getmouse();//getmouse 获取鼠标消息
        mousePosX = msgRecord.x;
        mousePosY = msgRecord.y;
    }
    if(count2%2000==0) new_ball();
}
void draw_ball()    //画球函数 
{
    cleardevice(); 
    for(int i=0;i<num;i++)
    {
        setfillcolor(RED);
        fillellipse(ball[i].x, ball[i].y, ball[i].r, ball[i].r);
    } 
    
    setfillcolor(GREEN);
    fillellipse(mousePosX, mousePosY,  7,  7);
    xyprintf(20,20,"计时:%d",count2/1000);
    count2=count2+10;
    Sleep(10); 
}
bool game_over()
{
	 for(int i=0;i<num;i++)       
        {
        	if((abs(mousePosX-ball[i].x)+abs(mousePosY-ball[i].y))<=(7+ball[i].r))       //距离够近
            { 
            	return true;
			}
        }
        return false;
}
int main()
{
    csh();
    while(1)
    {
        update();
 		draw_ball();
        if(game_over()) break;
    }
	xyprintf(10,200,"GAMEOVER");
    getch();
    return 0;
}
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-7-18 08:30 , Processed in 0.073986 second(s), 26 queries .

Powered by Discuz! X3.4

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

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