ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-01-24 16:31:03  阅读:212  来源: 互联网

标签:练手 Angle RotPie float Pie C++ 小游戏 Updata Offset


概述

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

实现效果和涉及的知识

初步接触for循环

文件结构

在这里插入图片描述

具体代码

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);

标签:练手,Angle,RotPie,float,Pie,C++,小游戏,Updata,Offset
来源: https://blog.csdn.net/IHTY_NUI/article/details/113090939

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有