#include<graphics.h> #include<conio.h> #include<iostream> using namespace std; class CObject { public: virtual void Draw() = 0; virtual void Update(float t) = 0; }; class Cball :public CObject { private: float m_x; float m_y; int m_r; int m_color; float m_Vy; public: Cball(float x, float y, int r, int color) { m_x = x; m_y = y; m_r = r; m_color = color; m_Vy = 0; } void Draw() { COLORREF color = getfillcolor(); setfillcolor(m_color); solidcircle(m_x, m_y, m_r); } void Update(float t) { m_Vy += -9.8 * t; m_y += m_Vy * t + 0.5 * (-9.8) * t * t; if (m_y < m_r - 200) { m_Vy = -0.8 * m_Vy; m_y = m_r - 200; } } }; int main() { initgraph(800, 600); setorigin(400, 300); setaspectratio(1, -1); cleardevice(); static const COLORREF groundColor = RGB(100, 100, 100); // 绘制地面直线 line(-400, -180, 400, -180); for (int i = -400; i < 400; i += 10) { moveto(i, -181); lineto(i, -179); } // 设置地面颜色 setcolor(groundColor); bar(-400, -180, 400, -179); Cball ball(20, 200, 20, RED); //BeginBatchDraw(); for (int i = 0; i < 1000; ++i) { BeginBatchDraw(); cleardevice(); ball.Draw(); ball.Update(0.05); Sleep(30); EndBatchDraw(); } //EndBatchDraw(); _getch(); closegraph(); return 0; }还是不能显示大地
时间: 2024-04-12 13:30:14 浏览: 264
我注意到你的代码中使用了 EasyX 图形库来进行绘图。如果你的代码无法显示大地,那么可能是因为没有正确配置 EasyX 图形库。请确保你已经正确安装了 EasyX 图形库,并且头文件和库文件的路径设置正确。
另外,你可以尝试将绘制地面的部分代码放在 `cleardevice()` 之后,并将绘制地面的代码块放在 `BeginBatchDraw()` 和 `EndBatchDraw()` 之间。这样可以确保在每次绘制之前清空屏幕,并在绘制完成后进行显示。
以下是修改后的代码示例:
```cpp
#include <graphics.h>
#include <conio.h>
#include <iostream>
using namespace std;
class CObject
{
public:
virtual void Draw() = 0;
virtual void Update(float t) = 0;
};
class Cball : public CObject
{
private:
float m_x;
float m_y;
int m_r;
int m_color;
float m_Vy;
public:
Cball(float x, float y, int r, int color)
{
m_x = x;
m_y = y;
m_r = r;
m_color = color;
m_Vy = 0;
}
void Draw()
{
COLORREF color = getfillcolor();
setfillcolor(m_color);
solidcircle(m_x, m_y, m_r);
}
void Update(float t)
{
m_Vy += -9.8 * t;
m_y += m_Vy * t + 0.5 * (-9.8) * t * t;
if (m_y < m_r - 200)
{
m_Vy = -0.8 * m_Vy;
m_y = m_r - 200;
}
}
};
int main()
{
initgraph(800, 600);
setorigin(400, 300);
setaspectratio(1, -1);
static const COLORREF groundColor = RGB(100, 100, 100);
Cball ball(20, 200, 20, RED);
for (int i = -400; i < 400; i += 10)
{
moveto(i, -181);
lineto(i, -179);
}
while (!kbhit())
{
cleardevice();
BeginBatchDraw();
// 绘制地面
setcolor(groundColor);
bar(-400, -180, 400, -179);
ball.Draw();
ball.Update(0.05);
EndBatchDraw();
Sleep(30);
}
closegraph();
return 0;
}
```
请注意,此代码仅供参考,你可能需要根据你的开发环境和需求进行适当的调整。
阅读全文
相关推荐
![application/x-gzip](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![.rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)