level 1
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<time.h>
//画圆,遇到障碍物,按键后垂直跳起来
int main() {
/*******初始化*******/
//系统变量
float width = 800, height = 600;//主界面的宽度,高度
float g = 1;//加速度
//小球变量
float radius = 20;//小球的半径
float x = width / 4, y = height - radius;//小球的坐标位置
float vy = 0;//小球y方向的初始速度
//障碍物的变量
float rect_left_x= width/10;
float rect_bottom_y=height;
float rect_width=40;
float rect_height=60;
float rect_vx = -3;//障碍物X方向的速度
/*******绘制主界面*********/
initgraph(width, height);
/****************/
/*******主程序*********/
while (1)
{
setfillcolor(RED);
fillcircle(x, y, radius);//1.绘制小球
//2.绘制障碍物
setfillcolor(GREEN);
fillrectangle(rect_left_x, rect_bottom_y - rect_height, rect_left_x + rect_width, rect_bottom_y);
/*******碰撞检测*********/
//同时满足三个条件:1.小球右边缘x>=障碍物左边缘x
// 2.小球左边缘x<=障碍物左边缘x
//3.小球下边缘y>=障碍物上边缘y
if ((x + radius >= rect_left_x) &&
(y + radius >= rect_bottom_y - rect_height) &&
(x - radius <= rect_left_x + rect_width))
{
//printf("产生碰撞!");
Sleep(400);
}
//2.判断当发生“按键事件”时,才响应
if (_kbhit())
{
char ch = _getch();
if (ch == ' ')
{
vy = -30; //3.设置小球初速度(向上为负)
}
}
vy += g;//4.设置小球下一刻的速度,受到重力影响,
y = y + vy;//5.设置小球下一刻的位置,受速度影响
if (y >= height - radius)
{//6.判断小球位置是否在地面上(如果在vy=0;y=height-radius)
y = height - radius;
vy = 0;
}
if (y < radius)
{
y = radius;
}
rect_left_x += rect_vx;//位置更新
//当右移出屏幕后,又从左边进入屏幕
if (rect_left_x <= 0)
{
srand(time(0));
int rand_num = rand();
rect_height = rand_num % int(height / 4) + height / 4;
rect_width = rand_num % int(width / 20) + width / 40;
rect_vx = rand_num % 7 - 8;
rect_left_x = width;
}
Sleep(10);
cleardevice();
}
closegraph();
return 0;
}
请问这个障碍物为什么不停地闪烁呢?
2022年06月15日 07点06分
4
加上批量绘图试试
2023年02月01日 12点02分
level 2
安装了easyx.h,运行c++2022时编译报错,无法找到文件,怎么回事?头文件一加就出问题#include<easyx.h>
2024年03月14日 10点03分
10