用类和easyx画一把枪
时间: 2023-11-13 13:15:39 浏览: 88
好的,以下是我为您提供的类和easyx画一把枪的代码:
```cpp
#include <graphics.h>
#include <cmath>
class Gun {
private:
int x, y, length, thickness;
COLORREF color;
public:
Gun(int x_, int y_, int length_, int thickness_, COLORREF color_)
: x(x_), y(y_), length(length_), thickness(thickness_), color(color_) { }
void draw() {
setfillcolor(color);
setlinecolor(color);
setlinestyle(PS_SOLID, thickness);
// 画枪管
int barrel_length = length / 2;
int barrel_x = x + barrel_length, barrel_y = y;
line(x, y, barrel_x, barrel_y);
// 画枪机
int receiver_length = length / 4;
int receiver_x = barrel_x + receiver_length, receiver_y = barrel_y - thickness / 2;
rectangle(barrel_x, barrel_y - thickness / 2, receiver_x, barrel_y + thickness / 2);
// 画枪托
int stock_length = length / 3;
int stock_x = receiver_x + stock_length, stock_y = y;
rectangle(receiver_x, receiver_y, stock_x, stock_y + thickness);
// 画枪口
int muzzle_length = barrel_length / 2;
int muzzle_x = barrel_x + muzzle_length * cos(30), muzzle_y = barrel_y - muzzle_length * sin(30);
line(barrel_x, barrel_y, muzzle_x, muzzle_y);
line(muzzle_x, muzzle_y, barrel_x + muzzle_length * sin(30), barrel_y + muzzle_length * cos(30));
}
};
int main() {
initgraph(640, 480);
Gun gun(320, 240, 200, 8, RGB(255, 255, 255));
gun.draw();
getch();
closegraph();
return 0;
}
```
以上代码使用了Gun类,表示一把枪,通过构造函数传入枪的位置、大小、颜色等参数,通过draw函数绘制出枪的形状。在main函数中创建了一把枪,然后调用其draw函数进行绘制。
阅读全文