21xrx.com
2025-03-26 23:15:22 Wednesday
文章检索 我的文章 写文章
C++编写的圣诞树炫彩代码
2023-06-26 21:54:03 深夜i     --     --
C++ 编写 圣诞树 炫彩 代码

圣诞节是全球共同庆祝的重要节日之一,无论是家庭聚会还是商店装饰、庆典活动,都充满了节日气氛。为了让您的编程世界也充满圣诞气息,我们为您准备了一份 C++ 编写的圣诞树炫彩代码。

首先,我们需要使用 C++ 的图形库创建一个圣诞树的图形界面。在此我们使用了 EasyX 图形库,代码如下:

#include<graphics.h>
#include<cmath>
#include<conio.h>
#include<ctime>
#include<iostream>
using namespace std;
const float PI=3.14159;
// 圆形波浪运动
void Circle(float cx, float cy, float r, int color)
{
  for(float i=0; i<2*PI; i=i+0.1)
  {
    float x=cx+r*cos(i*10);
    float y=cy+r*sin(i*10);
    putpixel(x, y, color);
  }
}
// 绘制圣诞树
void DrawTree(int x, int y, int h, int angle, COLORREF color)
{
  int width=2*h*tan(angle*PI/180);// 根据角度和树高计算树干宽度
  setfillcolor(BROWN);// 树干颜色
  solidrectangle(x-width/2, y, x+width/2, y+h);
  setfillcolor(color);
  fillcircle(x-width/2, y+h-h/3, h/4);
  fillcircle(x+width/2, y+h-h/3, h/4);
  fillcircle(x, y+h-2*h/3, h/3);
}
// 按键音乐
void Sound(int n)
{
  switch(n)
  {
    case 1:
      Beep(2093, 300);
      break;
    case 2:
      Beep(2349, 300);
      break;
    case 3:
      Beep(2637, 300);
      break;
    case 4:
      Beep(3520, 300);
      break;
  }
}
// 炫彩模式
void Colorful(int width, int height)
{
  int i, j;
  int r, g, b;
  int x, y;
  bool s;
  DWORD rcolor;
  srand(time(NULL));
  for(i=0; i<100; i++)
  {
    r=rand()%255;
    g=rand()%255;
    b=rand()%255;
    setfillcolor(RGB(r, g, b));
    fillrectangle(0,0, width, height);
    Sleep(30);
  }
  for(i=0; i<300; i++)
  {
    r=rand()%255;
    g=rand()%255;
    b=rand()%255;
    rcolor=RGB(r,g,b);
    for(j=0; j<5; j++)
    {
      x=rand()%width;
      y=rand()%height;
      if(x>5&&x<width-5&&y>5&&y<height-5)
      {
        setfillcolor(rcolor);
        solidcircle(x,y, 5);
      }
    }
    Sleep(50);
  }
  for(i=0; i<200; i++)
  {
    r=rand()%255;
    g=rand()%255;
    b=rand()%255;
    setfillcolor(RGB(r, g, b));
    solidrectangle(0, 0, width, height);
    for(j=0; j<20; j++)
    {
      x=rand()%width;
      y=rand()%height;
      if(x>10&&x<width-10&&y>10&&y<height-10)
      {
        s=rand()%2;
        if(s==1) setfillcolor(WHITE);
          else setfillcolor(BLACK);
        fillcircle(x,y, rand()%10);
      }
    }
    Sleep(50);
  }
}
// 主函数
int main()
{
  initgraph(800, 600);// 界面大小
  int i, h;// h为树高
  int angle=20;
  int w=800;
  int key;
  int x=w/2;
  int y=450;
  COLORREF color;
  setbkcolor(WHITE);// 背景色
  setlinecolor(BLACK);// 线条颜色
  while(true)
  {
    cleardevice();
    DrawTree(x, y, 250, angle, RGB(0, 255, 0));
    Circle(x, y+260, 150, RGB(255, 0, 0));
    for(i=0; i<3; i++)
    {
      DrawTree(rand()%800, rand()%450+300, rand()%200+100, rand()%45+15, RGB(rand()%256, rand()%256, rand()%256));
    }
    if(kbhit())
    {
      key=getch();
      if(key=='s')
      {
        Colorful(w, 600);
      }
      else
      {
        Sound(key-'0');
      }
    }
    Sleep(100);
  }
  return 0;
}

这份代码主要实现了以下四个功能:

1. 绘制一个绿色的圣诞树,树干颜色为棕色,同时在树干下方绘制两个红色圆形;

2. 在树的下方绘制一个红色圆形波浪图案,使整个界面更加充满节日气氛;

3. 在程序运行时,根据用户的按键操作,可以发出不同的音效,从而增强用户的体验感;

4. 在程序运行时,通过按下 "s" 键,可以触发一个圣诞炫彩模式,将界面变成炫彩的玩具城。

最后,您只需要在电脑上安装好 EasyX 图形库,便可以通过此份代码创建一个充满圣诞气息的编程界面,与您的编程小伙伴一起共同庆祝这个美好的节日吧!

  
  

评论区

    相似文章