You are on page 1of 7

//////////////////graphics.

h
#pragma once
#include<bits/stdc++.h>
#include<Windows.h>
#include<thread>
#include<mutex>
#include <conio.h>
#include<random>
#define KEY_NONE -1
using namespace std;
void PositionXY(int , int );
void phong();
void stop();
void nophao();
void hat();
void dat();
void roi();
int getx();
int gety();
void setcolor(WORD );
void showcur(bool );
int inputKey();
void textcolor(int );
void SetWindowSize(SHORT , SHORT );
int random(int ,int);
void clear();
//////////////////graphics.cpp
#include"Graphics.h"
queue<pair<int, int>> ap;
queue<pair<int, int>> an;
queue<pair<int, int>> ar;
queue<pair<int, int>> ah;
queue<int> maun;
queue<int> mauh;
queue<int> maur;
queue<int> no;
queue<int> sizer;
queue<int> sizeh;
int p = 10;
int speed = 1;

void PositionXY(int x, int y)


{
COORD coordinates;
coordinates.X = x;
coordinates.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordinates);
}

void hat()
{
while(1)
{
if(!ah.empty()&&!mauh.empty()&&!sizeh.empty())
{
pair<int, int> b = ah.front();
int size = sizeh.front();
int mau = mauh.front();
int hat = random(20, 30);
for (int i = 1; i <= hat; i++)
{
int x = random(b.first - size, b.first + size);
int y = random(b.second - size, b.second + size);
mutex c;
c.lock();
setcolor(mau);
PositionXY(x, y);
cout << "*";
Sleep(1);
PositionXY(x, y);
cout << " ";
c.unlock();
}
mauh.pop();
ah.pop();
sizeh.pop();
}
}
}

void roi()
{
while (1)
{
if (!ar.empty())
{
int mau = maur.front();
pair<int, int> b = ar.front();
int xn[8] = { -1,-1,-1,0,1,1,1,0 };
int yn[8] = { 1,0,-1,-1,-1,0,1,1 };
int size = sizer.front();
mauh.push(mau);
ah.push(b);
sizeh.push(size);
for (int i = 2; i <= size; i++)
{
Sleep(30);
for (int j = 0; j < 8; j++)
{
mutex c;
c.lock();
PositionXY(b.first + 3 * xn[j] * i-random(1,2),
b.second + yn[j] * i - random(1, 2));
cout << " ";
c.unlock();
}
}
sizer.pop();
ar.pop();
maur.pop();
}
}
}

void nophao()
{
while (1)
{
if (!an.empty())
{
pair<int, int> b = an.front();
int xn[8] = { -1,-1,-1,0,1,1,1,0 };
int yn[8] = { 1,0,-1,-1,-1,0,1,1 };
int xr1[8] = { -1,-1,1,1,0,0,0,-1 };
int yr1[8] = { 0,0,0,1,1,1,1,1 };
int xr2[8] = { -1,-1,1,0,0,0,0,0 };
int yr2[8] = { 1,1,1,1,0,0,0,1 };
int size = random(5, 9);
while (size % 2 == 0)
{
size = random(5, 9);
}
int mau = random(1, 15);
for (int i = 2; i < size; i++)
{
Sleep(50);
for (int j = 0; j < 8; j++)
{
mutex c;
c.lock();
setcolor(mau);
PositionXY((b.first + 3 * xn[j] * i)-random(1,2),
(b.second + yn[j] * i) - random(1, 2)); cout << ";";
c.unlock();
}
}
maur.push(mau);
sizer.push(size);
ar.push(an.front());
an.pop();
}
}
}

void phong()
{
while (1)
{
if (!ap.empty())
{
mutex c;
Sleep(p);
pair<int, int> b = ap.front();
int e = no.front();
ap.pop();
no.pop();
c.lock();
setcolor(14);
PositionXY(b.first, b.second); cout << "^";
PositionXY(b.first, b.second + 1); cout << "*";
PositionXY(b.first, b.second + 2); cout << char(32);
PositionXY(b.first, b.second + 3); cout << char(32);
c.unlock();
b.second -= speed;
if (b.second == e)
{
c.lock();
PositionXY(b.first, b.second +1); cout << char(32);
PositionXY(b.first, b.second + 2); cout << char(32);
PositionXY(b.first, b.second + 3); cout << char(32);
c.unlock();
an.push(b);
continue;
}
else
{
ap.push(b);
no.push(e);
}
}
}
}

void dat()
{
int s = 3;
int time =200 ;
while(1)
{
if (s==1)
{
p = 5;
for (int i = 1; i <= 10; i++)
{
Sleep(time);
no.push(random(11, 30));
ap.push(make_pair(random(40, 170), 44));
}
time -= 500;
for (int i = 1; i <= 4; i++)
{
for(int o=1;o<=30;o++)
{
Sleep(time);
no.push(random(11, 30));
ap.push(make_pair(random(40, 170), 44));
}
time -= 200;
}
}
else if (s==2)
{
p = 1;
for (int i = 1; i <= 50; i++)
{
for (int o = 1; o <= 2; o++)
{
Sleep(time);
no.push(random(11, 30));
ap.push(make_pair(random(40, 170), 44));
}
Sleep(500);
}
}
else if (s==3)
{
p = 1;
for (int i = 1; i <= 15; i++)
{
for (int o = 1; o <= 3; o++)
{
Sleep(time+200);
no.push(random(11, 30));
ap.push(make_pair(random(40, 170), 44));
}
Sleep(300);
}
time -= 200;
if (time == 0)
{
for (int o = 1; o <= 50; o++)
{
Sleep(500);
no.push(random(11, 30));
ap.push(make_pair(random(80, 120), 44));
}
s = 0;
time = 1500;
continue;
}
}
s += 1;
}
}

void clear()
{
while (1)
{
Sleep(3000);
system("cls");
}
}

void stop()
{
while (1)
{
char c = _getch();
if (c == 'p')
exit(0);
if (c == 't')
system("pause");
}
}

int getx()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
return csbi.dwCursorPosition.X;
return -1;
}
int gety()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
return csbi.dwCursorPosition.Y;
return -1;
}

void setcolor(WORD color)


{
HANDLE hConsoleOutput;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);

CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
GetConsoleScreenBufferInfo(hConsoleOutput, &screen_buffer_info);

WORD wAttributes = screen_buffer_info.wAttributes;


color &= 0x000f;
wAttributes &= 0xfff0;
wAttributes |= color;

SetConsoleTextAttribute(hConsoleOutput, wAttributes);
}
void showcur(bool CursorVisibility)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor = { 1, CursorVisibility };
SetConsoleCursorInfo(handle, &cursor);
}
int inputKey()
{
if (_kbhit())
{
int key = _getch();

if (key == 224)
{
key = _getch();
return key + 1000;
}

return key;
}
else
{
return KEY_NONE;
}
return KEY_NONE;
}
void textcolor(int x)
{
HANDLE mau;
mau = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(mau, x);
}
void SetWindowSize(SHORT width, SHORT height)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

SMALL_RECT WindowSize;
WindowSize.Top = 0;
WindowSize.Left = 0;
WindowSize.Right = width;
WindowSize.Bottom = height;

SetConsoleWindowInfo(hStdout, 1, &WindowSize);
}

int random(int l,int r)


{
random_device rd;
std::mt19937 generator(rd());
uniform_int_distribution<int> distribution(l, r);
int random_number = distribution(generator);
return random_number;
}
/////////////////main.cpp
#include"Graphics.h";
int main()
{

showcur(0);
thread t1(dat);
thread t2(phong);
thread t3(nophao);
thread t4(roi);
thread t5(clear);
thread t6(stop);
thread t7(hat);
system("pause");
}

You might also like