Last active
April 17, 2018 18:37
-
-
Save Nircek/5acaed4a2e0ddbc172f996ecfea36efc to your computer and use it in GitHub Desktop.
Wycieniowany pasek zdrowia w konsoli.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <sstream> | |
| using namespace std; | |
| typedef unsigned int uint; | |
| string getPasekString(uint hp, uint maxhp, uint size=10, bool multi=true){ | |
| string ret = ""; | |
| if(maxhp<hp)hp=maxhp; | |
| uint pr;//ilosc znakow pierwszego stylu | |
| uint st;//numer drugiego stylu | |
| char cien[] = | |
| {' ', | |
| (char)0xb0, //░ in CP852 | |
| (char)0xb1, //▒ in CP852 | |
| (char)0xb2, //▓ in CP852 | |
| (char)0xdb, //█ in CP852 | |
| }; | |
| if(multi){ | |
| uint il=4;//ilosc stylow | |
| uint pmhp=maxhp/il;//maxhp w czesci | |
| if(pmhp==0)pmhp=1;//anty /0 | |
| st=hp/pmhp;//w ktorej czesci jestesmy | |
| uint php=hp%pmhp;//hp w czesci | |
| pr=size*php/pmhp;//jak daleko w tej czesci jestesmy | |
| if(st>=il){st=il-1;pr=size;}//anty-overflow (caly pasek pelny) | |
| }else{ | |
| st=0;cien[1]=(char)0xdb; | |
| pr=size*hp/maxhp; | |
| } | |
| while(pr--)ret += cien[st+1];//znaki pierwszego stylu | |
| while(size-ret.size())ret += cien[st];//padding drugiego stylu | |
| return ret; | |
| } | |
| int main() | |
| { | |
| int maxhp=4315; | |
| int hp=0; | |
| for(hp=0;hp<maxhp+1;++hp) | |
| cout<<1000*hp/maxhp<<" PASEK HP\t"<<getPasekString(hp, maxhp,22,true)<<endl; | |
| return 0; | |
| } |
Author
Author
Kod jest teraz bardziej opisowy. W miarę dobrze działa przy małych wartościach, aczkolwiek czasem zachowuje się tak jakby to nie było proporcjonalne...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nie jest to, ani najpiękniejszy pasek, ani najpiękniejszy kod, bo jest zrobiony na szybko, żeby działał, ale źle działa przy mniejszych wartościach maxhp.