UPDATE:2024.8.6 minor upgrades
This is a 2Dgame to test how water flows. It tuns out that waterflowing is a quiet complicated subjuect, so this code may occurs a lot of BUGs.
But anyway,I’m not updating this project anymore
KEY INSTRUCTIONS
-
press 1,2 or 3 to change blocks you’re going to place.
-
press ‘space’ to place blocks
-
use WASD to move the block you chose
-
press E to start the water flowing
-
press 0 to erse the whole map**
Special thanks:
@James_L
^CHECK HIS COOL BLOG WITH LOT’S OF FUN GAMES INSIDE!
CODE
#include<bits/stdc++.h>
#include<windows.h>
//#include<cmd.h>
#include<conio.h>
#include<bitset>
#include<winuser.h>
//#include<thread>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
//#define SHAKE 5
using namespace std;
extern const int
BLACK=0,
DARKBLUE=1,DBLUE=1,
GREEN=2,
BLUE=3,
RED=4,
PURPLE=5,
DIRT=6,YELLOWGREY=6,
LIGHTGREY=7,LGREY=7,
GREY=8,DGREY=8,DARKGREY=8,
SKYBLUE=9,SBLUE=9,
LGREEN=10,LIGHTGREEN=10,
CANYON=11,
LRED=12,LIGHTRED=12,BRED=12,BRIGHTRED=12,
LPURPLE=13,LIGHTPURPLE=13,
LYELLOW=14,LIGHTYELLOW=14,
WHITE=15
;
typedef BOOL (WINAPI *PROCSETCONSOLEFONT)(HANDLE, DWORD);
PROCSETCONSOLEFONT SetConsoleFont;
void ResizeFont(int a)
{
HMODULE hKernel32=GetModuleHandle("kernel32");
SetConsoleFont=(PROCSETCONSOLEFONT)GetProcAddress(hKernel32,"SetConsoleFont");
SetConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void Color(int text,int background)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),text+background*16);
}
void BColor(int background)
{
if(background==15)Color(0,background);
else Color(15,background);
}
void BColor(string background)
{
int len=background.length();
for(int i=0;i<len;i++)
{
if(background[i]>96)background[i]-=32;
}
if(background=="BLACK")Color(15,0);
if(background=="DARKBLUE"||background=="DBLUE")Color(15,1);
if(background=="GREEN")Color(15,2);
if(background=="BLUE")Color(15,3);
if(background=="RED")Color(15,4);
if(background=="PURPLE")Color(15,5);
if(background=="DIRT"||background=="YELLOWGREY")Color(15,6);
if(background=="LIGHTGREY"||background=="LGREY")Color(15,7);
if(background=="GREY"||background=="DGREY"||background=="DARKGREY")Color(15,8);
if(background=="SKYBLUE"||background=="SBLUE")Color(15,9);
if(background=="LGREEN"||background=="LIGHTGREEN")Color(15,10);
if(background=="CANYON")Color(15,11);
if(background=="LRED"||background=="LIGHTRED"||background=="BRED"||background=="BRIGHTRED")Color(15,12);
if(background=="LPURPLE"||background=="LIGHTPURPLE")Color(15,13);
if(background=="LYELLOW"||background=="LIGHTYELLOW")Color(15,14);
if(background=="WHITE")Color(0,15);
}
void cls()
{
BColor(BLACK);
system("cls");
}
struct CTRL
{
void SetPointerVisual(bool visual)
{
//1=show 0=hide
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=visual;
SetConsoleCursorInfo(hOut,&cci);
}
};
extern CTRL Ctrl;
struct Bas
{
void Hi()
{
cout<<"Hello world!";
}
};
extern Bas Basic;
void PrintByChar(string str,int t)
{
int len=str.length();
for(int i=0;i<len;i++)
{
Sleep(double(t/len));
cout<<str[i];
}
cout<<endl;
}
struct BigNum
{
string num;
int length()
{
return num.length();
}
void Print()
{
bool m=0;
for(int i=0;i<length();i++)
{
if(num[i]!='0')m=1;
if(m)cout<<num;
}
}
string operator+(const BigNum& B)
{
//string a=this->num;
string b=B.num;
return "1";
};
};
struct TO
{
template < typename T >
string String( T& n )
{
std::ostringstream stm ;
stm<<fixed<<setprecision(16)<<n;
return stm.str() ;
}
};
extern TO To;
int GetWindowPosX()
{
RECT rect;
HWND hwnd=GetForegroundWindow();
GetWindowRect(hwnd,&rect);
return rect.left;
}
int GetWindowPosY()
{
RECT rect;
HWND hwnd=GetForegroundWindow();
GetWindowRect(hwnd,&rect);
return rect.top;
}
int GetCursorPosX()
{
POINT pt={0,0};
GetCursorPos(&pt);
return pt.x;
}
int GetCursorPosY()
{
POINT pt={0,0};
GetCursorPos(&pt);
return pt.y;
}
int GetSubX()
{
return GetCursorPosX()-GetWindowPosX();
}
int GetSubY()
{
return GetCursorPosY()-GetWindowPosY();
}
void gotoxy(int x,int y)
{
int xx=0x0b;
HANDLE hOutput;
COORD loc;
loc.X=x;
loc.Y=y;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,loc);
}
const char air=0,barrier=1,spring=2,water=3;
char world[55][55];
int mouseX=1,mouseY=1,gamemode=0;
unsigned long long frame=1;
string blocktype="barrier";
int newc[55][55];
void clearmap(){
for(int i=1;i<=50;i++){
for(int j=1;j<=50;j++){
world[i][j]=air;
}
}
}
void clearwater(){
for(int i=1;i<=50;i++){
for(int j=1;j<=50;j++){
if(world[i][j]==water)world[i][j]=air;
}
}
}
void screenupdate(){
gotoxy(0,0);
for(int i=1;i<=50;i++){
for(int j=1;j<=50;j++){
if(i==mouseX && j==mouseY && gamemode==0){
BColor(WHITE);
cout << " ";
}else if(world[i][j]==air){
BColor(SBLUE);
cout << " ";
}else if(world[i][j]==barrier){
BColor(DIRT);
cout << " ";
}else if(world[i][j]==spring){
BColor(DBLUE);
cout << "~~";
}else if(world[i][j]==water){
BColor(BLUE);
cout << "~~";
}
}
BColor(BLACK);
putchar('\n');
}
cout<<"X: "<<mouseX<<" Y: "<<mouseY<<" blocktype: "<< blocktype << " gamemode: " << gamemode+1 << " test.frame: "<< frame << endl;
}
void dectect(){
if(GetKeyState('W')<0 && mouseX>1)mouseX--;
if(GetKeyState('S')<0 && mouseX<150)mouseX++;
if(GetKeyState('A')<0 && mouseY>1)mouseY--;
if(GetKeyState('D')<0 && mouseY<150)mouseY++;
if(GetKeyState('E')<0){
if(gamemode==0)gamemode=1;
else gamemode=0;
if(gamemode==0){
clearwater();memset(newc,false,sizeof(newc));
}
}
if(GetKeyState('1')<0 && gamemode==0)blocktype="barrier";
if(GetKeyState('2')<0 && gamemode==0)blocktype="spring";
if(GetKeyState('3')<0 && gamemode==0)blocktype="air";
if(GetKeyState('0')<0 && gamemode==0)clearmap();
if(GetKeyState(' ')<0 && gamemode==0){
if(blocktype=="air")world[mouseX][mouseY]=air;
else if(blocktype=="barrier")world[mouseX][mouseY]=barrier;
else if(blocktype=="spring")world[mouseX][mouseY]=spring;
}
}
void tryflow(){//PROBLEM
for(int i=1;i<=50;i++){
for(int j=1;j<=50;j++){
if(newc[i][j]==0 && (world[i][j]==water || world[i][j]==spring)){
newc[i][j]=3;
if(i<50 && (world[i+1][j]==air || world[i+1][j]==water))world[i+1][j]=water,newc[i+1][j]=1;
else{
if(j<50 && world[i][j+1]==air)world[i][j+1]=water,newc[i][j+1]=1;
if(j>1 && world[i][j-1]==air)world[i][j-1]=water,newc[i][j-1]=1;
}
}
}
}
for(int i=1;i<=50;i++){
for(int j=1;j<=50;j++){
if(newc[i][j]==1){
newc[i][j]--;
}
}
}
//Sleep(50);
}
void game(){
cls();
clearmap();
while(1){
frame++;
screenupdate();
dectect();
if(/*frame%2==0 &&*/ gamemode==1){
tryflow();
}
//Sleep(100);
}
}
int main(){
system("mode con cols=100 lines=55");
system("title Waterflow simulater beta version v0.02 By sky_of_stars");
cout << " WFS ";
Color(BLACK,GREY);
getch();
game();
}