概述
编译环境:Mictosoft Visual Studio 2022, EasyX_2021
作 者:代码骑士<1696297834@qq.com>
最后修改:2021-12-16
演示:
(多看一眼,图片会动哦)
思路:
更加美观。
代码:
#include<graphics.h>
#include<time.h>
#include<conio.h>
#include<iostream>
#include"EasyXPng.h"
using namespace std;
#pragma comment(lib,"Winmm.lib")
const int width = 600;
const int length = 600;
const int MAXSNOW = 200;
const int MAXLIGHT = 20;
const int BOUNDS = 20;
//const int SZ = 10;
int key = 0;//功能选择键:2下雪,3点灯
IMAGE gift, snows, star1, star2, voids, voidss, ren, ling, li, wa, llala;
struct SNOW
{
int x;
double y;//沿y轴下落
double step;//下落速度
int color;
};
SNOW snow[MAXSNOW];//结构体数组(200颗雪花)
int TREE[BOUNDS][BOUNDS] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//15-x
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//15-x
{0,0,0,0,0,0,0,7,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,7,7,7,7,7,0,0,0,0,0},
{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,7,7,7,7,7,0,0,0,0,0},
{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,7,7,7,7,7,7,7,7,7,0,0,0},
{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,7,7,7,7,7,7,7,7,7,0,0,0},
{0,0,1,1,1,1,1,1,1,1,1,1,1,0,0},
{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0}//15-y
};
class christmasTree
{
public:
void Initialize();//黑夜
//
void drawTree();//圣诞树
void drawTree2();//2tree
void drawTree3();//3tree
//
void initSnow(int i);//雪花初始化
void moveSnow(int i);//下雪
void Snow(christmasTree CT);//飞雪
//
void yellowLight(int i, int j, int SZ);
void redLight(int i, int j, int SZ);
void Light(int i, int j, int SZ);//闪灯
//
void Surprise();//惊喜
};
void christmasTree::Initialize()
{
initgraph(width, length);
//setbkcolor(WHITE);
//cleardevice();
loadimage(&gift, "gift.jpg", 300, 300);
loadimage(&snows, "snows.png", 30, 30);
loadimage(&voids, "voids.png", 20, 20);
loadimage(&voidss, "voidss.png", 20, 20);
loadimage(&star1, "star.png", 600, 600);
loadimage(&star2, "star.png", 300, 300);
loadimage(&ren, "ren.png", 500, 500);
loadimage(&ling, "ling.png", 300, 300);
loadimage(&li, "li.png", 300, 300);
loadimage(&wa, "wa.png", 300, 300);
loadimage(&llala, "llala.png", 300, 100);
putimage(150, 150, &gift);
_getch();
closegraph();
//BeginBatchDraw();
}
void christmasTree::yellowLight(int i, int j, int SZ)
{
if (TREE[i][j] == 7)
{
int x = ((j + 0) * SZ + (j + 1) * SZ) / 2;
int y = ((i + 0) * SZ + (i + 1) * SZ) / 2;
int r = SZ / 4;
setlinecolor(YELLOW);
setfillcolor(YELLOW);
fillcircle(x, y, r);//圆心公式(x1+x2)/2
//Sleep(20);
setlinecolor(RGB(9, 163, 67));
setfillcolor(RGB(9, 163, 67));
fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
}
}
void christmasTree::redLight(int i, int j, int SZ)
{
if (TREE[i][j] == 1)
{
int x = ((j + 0) * SZ + (j + 1) * SZ) / 2;
int y = ((i + 0) * SZ + (i + 1) * SZ) / 2;
int r = SZ / 4;
setlinecolor(RED);
setfillcolor(RED);
fillcircle(x, y, r);//圆心公式(x1+x2)/2
//Sleep(20);
setlinecolor(GREEN);
setfillcolor(GREEN);
fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
}
}
int color = 10;
void christmasTree::Light(int i, int j, int SZ)
{
//yellowLight(i, j, SZ);
//redLight(i, j, SZ);
int x = 20;
putimagePng(100 + x, 50, &star2);
putimagePng(50 + x, 100, &star2);
putimagePng(20 + x, 150, &star2);
putimagePng(-10 + x, 200, &star2);
putimagePng(-30 + x, 250, &star2);
putimagePng(-50 + x, 300, &star2);
putimagePng(-80 + x, 350, &star2);
putimagePng(100 + x, 50, &star2);
putimagePng(150 + x, 100, &star2);
putimagePng(180 + x, 150, &star2);
putimagePng(210 + x, 200, &star2);
putimagePng(230 + x, 250, &star2);
putimagePng(250 + x, 300, &star2);
putimagePng(280 + x, 350, &star2);
putimagePng(150 + x, 180, &ren);
putimagePng(50 + x, 200, &ling);
setbkmode(TRANSPARENT);//显示透明文字
settextcolor(RGB(255-color, 0+color,color++));//设置字体颜色
settextstyle(50, 0, _T("宋体"));//设置字体大小,字体
outtextxy(240, 130, _T("圣"));//输出字体
settextstyle(50, 0, _T("宋体"));//设置字体大小,字体
outtextxy(240, 190, _T("诞"));//输出字体
settextstyle(50, 0, _T("宋体"));//设置字体大小,字体
outtextxy(240, 250, _T("快"));//输出字体
settextstyle(50, 0, _T("宋体"));//设置字体大小,字体
outtextxy(240, 310, _T("乐"));//输出字体
}
void christmasTree::drawTree()
{
int i, j;
setlinestyle(PS_SOLID, 36);
int SZ = width / BOUNDS*1.25;
for (int i = 0; i < BOUNDS; i++)
{
for (int j = 0; j < BOUNDS; j++)
{
if (TREE[i][j] == 1)
{
setlinecolor(GREEN);
setfillcolor(GREEN);
fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
Sleep(1);
}
if (TREE[i][j] == 2)
{
setlinecolor(RGB(95, 41, 31));
setfillcolor(RGB(95,41,31));
fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
Sleep(1);
}
if (TREE[i][j] == 7)
{
setlinecolor(RGB(9, 163, 67));
setfillcolor(RGB(9, 163, 67));
fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
Sleep(1);
}
if (key == 3)
{
//BeginBatchDraw();
//drawTree();
Light(i, j, SZ);//闪灯
//FlushBatchDraw();
}
}
}
putimagePng(-25, -40, &star1);
}
void christmasTree::drawTree2()
{
int i, j;
int SZ = width / BOUNDS * 1.25;
for (int i = 0; i < BOUNDS; i++)
{
for (int j = 0; j < BOUNDS; j++)
{
if (TREE[i][j] == 1)
{
setlinecolor(GREEN);
setfillcolor(GREEN);
fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
}
if (TREE[i][j] == 2)
{
setlinecolor(RGB(95, 41, 31));
setfillcolor(RGB(95, 41, 31));
fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
}
if (TREE[i][j] == 7)
{
setlinecolor(RGB(9, 163, 67));
setfillcolor(RGB(9, 163, 67));
fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
}
if (key == 3)
{
//BeginBatchDraw();
drawTree();
putimagePng(-25, -40, &star1);
Light(i, j, SZ);//闪灯
//FlushBatchDraw();
}
}
}
putimagePng(-25, -40, &star1);
//FlushBatchDraw();
}
void christmasTree::initSnow(int i)
{
snow[i].y = 0;
snow[i].x = rand() % width;
snow[i].step = (rand() % 5000) / 10000.0 + 1;
//snow[i].color = (int)(snow[i].step * 255 / 6.0 + 0.5);//渐变的
//snow[i].color = RGB(snow[i].color, snow[i].color, snow[i].color);
cleardevice();
drawTree2();//画树
}
void christmasTree::moveSnow(int i)//下雪
{
//BeginBatchDraw();
setlinestyle(PS_SOLID, 36);
//putpixel((int)snow[i].x, snow[i].y, 0);//画雪
//cleardevice();
putimagePng(-25, -40, &star1);
putimagePng((int)snow[i].x, snow[i].y, &voids);
snow[i].y += snow[i].step;
if (snow[i].y > length) initSnow(i);//生成新雪花
//putpixel((int)snow[i].x, snow[i].y, snow[i].color);//再画雪
putimagePng((int)snow[i].x, snow[i].y, &snows);
//cleardevice();
//FlushBatchDraw();
}
void christmasTree::Snow(christmasTree CT)
{
srand((unsigned)time(NULL));
for (int i = 0; i < MAXSNOW; i++)
{
CT.initSnow(i);
snow[i].y = rand() % length;
}
while (!_kbhit())
{
drawTree2();//画树
for (int i = 0; i < MAXSNOW; i++)
CT.moveSnow(i);
Sleep(20);
}
//FlushBatchDraw();
}
int main()
{
christmasTree CT;
CT.Initialize();
initgraph(width, length);
mciSendString(_T("open 123.mp3 alias bkmusic"), NULL, 0, NULL);//播放音乐
mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);//循环播放
CT.drawTree();
while (cin>>key)
{
switch (key)
{
case 2:
CT.Snow(CT);
break;
case 3:
CT.Snow(CT);
break;
case 4:
return 0;
}
}
return 0;
}
项目资源:
https://download.csdn.net/download/qq_51701007/87320763https://download.csdn.net/download/qq_51701007/87320763
最后
以上就是坦率宝贝为你收集整理的C++游戏编程--游戏名称:闪光圣诞树2.0演示:思路:代码:项目资源:的全部内容,希望文章能够帮你解决C++游戏编程--游戏名称:闪光圣诞树2.0演示:思路:代码:项目资源:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复