C++练手小游戏3——万花筒

概述

原项目是旋转蛇错觉图案,但我这里修改了一下,只随机生成同心圆及其颜色。
在这里插入图片描述

实现效果和涉及的知识

初步接触for循环。,另外我想在这里初步探讨下什么时候用类,什么时候用函数:
一般情况下,类和函数一样,声明放在.h中,定义放在.c中。那么在应用时如何取舍呢?我认为基本上没啥区别,类能实现的函数基本也都能实现,不过类把状态和修改状态的代码通过语法层面组织到一起,语法上更讨喜一点。
水平有限,这个以后还需进一步研究探讨。

文件结构

在这里插入图片描述

具体代码

FQF_RotCir.cpp

// FQF_RotCir.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream> // C++ STL 输入输出库
#include <graphics.h> // 绘图函数库,非cpp STL,是TurboC扩展库(EasyX)
#include <conio.h> // console input/output的简写,非cpp STL,定义了通过控制台进行输入输出的函数,如getch()
#include "data.h"
#include "RotPie.h"

using namespace std;

int main()
{    
    RotPie rotpie;
    initgraph(GraWid, GraHei);
    setbkcolor(BkColor);
    cleardevice();
    
    for (int n = 0; n < 4; n++)
    {
        for (int m = 0; m < 6; m++)
        {
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 9; j++)
                { 
                    rotpie.Cal_RotPie();
                    rotpie.Plot_RotPie();
                    rotpie.Updata_Ang_1();
                }
                rotpie.Updata_Circle_R();
                rotpie.Updata_Ang();
                int a = 1;
            }
            rotpie.Updata_Circle_X();
            rotpie.Updata_Color();
            Sleep(500);
        }
        rotpie.Updata_Circle_Y();
    }

    _getch();
    closegraph();
    
    return 0;
}

RotPie.h

#pragma once

class RotPie
{
private:
	float Pie_left, Pie_up, Pie_right, Pie_down;

public:
	RotPie();
	~RotPie();
	void Cal_RotPie();
	void Plot_RotPie();
	void Updata_Ang();
	void Updata_Ang_1();
	void Updata_Circle_R();
	void Updata_Circle_X();
	void Updata_Circle_Y();
	void Updata_Color();
};


RotPie.cpp

#include "RotPie.h"
#include "data.h"
#include <graphics.h>
#include <ctime> // time()
#include <stdlib.h> // rand()和srand()

RotPie::RotPie()
{
	Pie_left = 0;
	Pie_up = 0;
	Pie_right = GraWid;
	Pie_down = GraHei;
}

RotPie::~RotPie()
{
}

void RotPie::Cal_RotPie()
{
	Pie_left = CircleCenterX- CircleR;
	Pie_up = CircleCenterY - CircleR;
	Pie_right = CircleCenterX + CircleR;
	Pie_down = CircleCenterY + CircleR;
}

void RotPie::Plot_RotPie()
{
	setfillcolor(PieColor1);
	solidpie(Pie_left, Pie_up, Pie_right, Pie_down, Angle+Angle_1, Angle + Angle_1 +Angle_Offset_1);
	setfillcolor(PieColor2);
	solidpie(Pie_left, Pie_up, Pie_right, Pie_down, Angle + Angle_1 + Angle_Offset_1, Angle + Angle_1 + 2*Angle_Offset_1);
	setfillcolor(PieColor3);
	solidpie(Pie_left, Pie_up, Pie_right, Pie_down, Angle + Angle_1 + 2*Angle_Offset_1, Angle + Angle_1 + 3*Angle_Offset_1);
}

void RotPie::Updata_Ang()
{
	Angle = Angle + Angle_Offset;
	if (Angle >= 2 * pi)
	{
		Angle = 0;
	}
}

void RotPie::Updata_Ang_1()
{
	Angle_1 = Angle_1 + Angle_Offset_1 * 3;
	if (Angle_1 >= 2 * pi)
	{
		Angle_1 = 0;
	}
}

void RotPie::Updata_Circle_R()
{
	CircleR = CircleR - R_Offset;
	if (CircleR <= 0)
	{
		CircleR = 50;
	}
}

void RotPie::Updata_Circle_X()
{
	CircleCenterX = CircleCenterX + CircleCenterX_Offset;
	if (CircleCenterX >= GraWid)
	{
		CircleCenterX = 50;
	}
}

void RotPie::Updata_Circle_Y()
{
	CircleCenterY = CircleCenterY + CircleCenterY_Offset;
	if (CircleCenterY >= GraHei)
	{
		CircleCenterY = 50;
	}
}

void RotPie::Updata_Color()
{
	srand(time(0));
	BaseColor = rand() % 3 * 60 + 50;
	srand(time(0));
	int gap= rand() % 5 * 50;
	PieColor1 = RGB(BaseColor, BaseColor, 0 + gap);
	PieColor2 = RGB(0 + gap, BaseColor -50, BaseColor - 50);
	PieColor3 = RGB(BaseColor + 50, 0 + gap, BaseColor + 50);
}

data.h

#pragma once

#include <graphics.h>

extern float GraWid, GraHei;
extern float pi;
extern float CircleCenterX, CircleCenterY;
extern float CircleR;
extern float Angle, Angle_1;

extern float CircleCenterX_Offset, CircleCenterY_Offset;
extern float R_Offset;
extern float Angle_Offset, Angle_Offset_1;

extern int BaseColor;
extern COLORREF BkColor, PieColor1, PieColor2, PieColor3;

data.cpp

#include "data.h"

float GraWid = 600, GraHei = 400;
float pi = 3.1415;
float CircleCenterX = 50, CircleCenterY = 50;
float CircleR = 50;
float Angle = 0, Angle_1 = 0;

float CircleCenterX_Offset = 100, CircleCenterY_Offset = 100;
float R_Offset = 10;
float Angle_Offset = pi / 20, Angle_Offset_1 = pi / 9;

int BaseColor = 0;
COLORREF BkColor = RGB(0, 0, 0) , PieColor1 = RGB(0, BaseColor+100, BaseColor + 100), PieColor2 = RGB(BaseColor + 170, 0, BaseColor + 170), PieColor3 = RGB(BaseColor + 240, BaseColor + 240, 0);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值