Windmill Rain
#include<windows.h>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <thread>
#include <random>
#include <iostream>
#include <vector>
using namespace sf;
using namespace std;
class WindMill
{
public:
Vector2f scale;
float transparency = 255;
Vector2f position;
float x = 3;
ConvexShape con0;
ConvexShape con1;
ConvexShape con2;
ConvexShape con3;
void translate(RenderWindow &window);
WindMill(ConvexShape con[], Vector2f position);
~WindMill(){}
};
WindMill::WindMill(ConvexShape con[], Vector2f position){
scale = Vector2f(0.1, 0.1);
this->con0 = con[0];
this->con1 = con[1];
this->con2 = con[2];
this->con3 = con[3];
this->position = position;
con0.setPoint(0, Vector2f(0, 0));
con0.setPoint(1, Vector2f(200, 0));
con0.setPoint(2, Vector2f(0, 100));
con0.setOrigin(0, 100);
con1.setPoint(0, Vector2f(0, 0));
con1.setPoint(1, Vector2f(200, 0));
con1.setPoint(2, Vector2f(0, 100));
con1.setOrigin(0, 100);
con2.setPoint(0, Vector2f(0, 0));
con2.setPoint(1, Vector2f(200, 0));
con2.setPoint(2, Vector2f(0, 100));
con2.setOrigin(0, 100);
con3.setPoint(0, Vector2f(0, 0));
con3.setPoint(1, Vector2f(200, 0));
con3.setPoint(2, Vector2f(0, 100));
con3.setOrigin(0, 100);
con1.rotate(90);
con2.rotate(180);
con3.rotate(-90);
con0.setPosition(this->position);
con1.setPosition(this->position);
con2.setPosition(this->position);
con3.setPosition(this->position);
con0.setFillColor(Color(255, 0, 0, transparency));
con1.setFillColor(Color(0, 0, 255, transparency));
con2.setFillColor(Color(255, 255, 0, transparency));
con3.setFillColor(Color(0, 255, 0, transparency));
}
void WindMill::translate(RenderWindow &window) {
this->position.y = this->position.y + 1;
con0.rotate(x);
con1.rotate(x);
con2.rotate(x);
con3.rotate(x);
con0.setScale(scale);
con1.setScale(scale);
con2.setScale(scale);
con3.setScale(scale);
con0.setPosition(this->position);
con1.setPosition(this->position);
con2.setPosition(this->position);
con3.setPosition(this->position);
window.draw(con0);
window.draw(con1);
window.draw(con2);
window.draw(con3);
}
int main() {
vector<WindMill> instance;
Vector2f position[100];
RenderWindow window(VideoMode(900, 500), "My Window");
window.setFramerateLimit(60);
ConvexShape con[4];
for (int i = 0; i < 4; i++) {
con[i].setPointCount(4);
}
srand(time(0));
for (int i = 0; i < 100; i++)
{
position[i].x = rand() % 900;
position[i].y = -rand() % 3000;
instance.push_back(WindMill(con, position[i]));
}
while (true)
{
window.clear();
for (int i = 0; i < 100; i++) {
instance[i].translate(window);
}
window.display();
}
}
缩放因子
Vector2f scale
透明度
float transparency = 255
生成的风车图案的位置坐标
Vector2f position
旋转速度
float x = 3
三角形图案
ConvexShape con0
图案变换函数
void translate(RenderWindow &window)
构造函数
WindMill(ConvexShape con[], Vector2f position)
析构函数
~WindMill(){}
初始化缩放因子
scale = Vector2f(0.1, 0.1)
初始化三角形图案对象
this->con0 = con[0]
初始化位置
this->position = position
绘制三角形图案方法
con0.setPoint(0, Vector2f(0, 0))
con0.setPoint(1, Vector2f(200, 0))
con0.setPoint(2, Vector2f(0, 100))
con0.setOrigin(0, 100)
初始化图案旋转角度
con1.rotate(90)
初始化三角形位置
con0.setPosition(this->position)
填充颜色
con0.setFillColor(Color(255, 0, 0, transparency))
控制位置移动
this->position.y = this->position.y + 1
控制旋转
con0.rotate(x)
重置缩放
con0.setScale(scale)
重置位置
con0.setPosition(this->position)
绘制准备
window.draw(con0)
创建向量储存对象
vector<WindMill> instance
初始化位置数组
Vector2f position[100]
准备图版
RenderWindow window(VideoMode(900, 500), "My Window")
控制帧率
window.setFramerateLimit(60)
绘制三角形外形
ConvexShape con[4]
设置随机数种子
srand(time(0))
随机创造风车图案的位置
position[i].x = rand() % 900
position[i].y = -rand() % 3000
windmill实例化放入vector容器中
instance.push_back(WindMill(con, position[i]))
循环调用所有windmill实体的translate方法
for (int i = 0; i < 100; i++) {
instance[i].translate(window)
}