diff --git a/practices/c/level1/p01_runningLetter/lvl1_1_Running_letter.c b/practices/c/level1/p01_runningLetter/lvl1_1_Running_letter.c new file mode 100644 index 00000000..54334540 --- /dev/null +++ b/practices/c/level1/p01_runningLetter/lvl1_1_Running_letter.c @@ -0,0 +1,46 @@ +/* code: Running Letter + * author: Kyrios0 + * date: 2017.02.26 + * state: finished + * version: 1.0.0 + */ + +#include +#include + +void gotoxy(int x, int y) +{ + HANDLE hOutput; + COORD coo; + coo.X = x; + coo.Y = y; + hOutput = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleCursorPosition(hOutput, coo); +} + +int main(int argc, char** argv) +{ + system("chcp 437"); + system("color 0f"); + system("mode con cols=63 lines=32"); + + int x = 0, y = 0, round = 0; + + while(1) + { + system("cls"); + + gotoxy(x, y); + putchar('#'); + gotoxy(x, y); + + (round % 2 == 0) ? x++ : x--; + if((x == 0) || (x == 63)) + { + round++; + } + + Sleep(25); + } + return 0; +} \ No newline at end of file diff --git a/practices/c/level1/p02_isPrime/lvl1_2_isPrime.c b/practices/c/level1/p02_isPrime/lvl1_2_isPrime.c new file mode 100644 index 00000000..eaa02357 --- /dev/null +++ b/practices/c/level1/p02_isPrime/lvl1_2_isPrime.c @@ -0,0 +1,58 @@ +// isPrime.c +// + + +/* code: Step0 for Eula Prime (change from lvl1_5) +* author: Kyrios0 +* date: 2017.02.21 +* state: finished +* version: 2.0.2 +*/ + +#include +#include//malloc() +#include +#include + + + +int main(int argc, char** argv) +{ + long max; + puts("Prime Number Detection System"); + puts("Please enter the number you want to detect(It should be no less than 2)"); + scanf("%ld", &max); + + long ct = 0; + int *judge; + judge = (int*)malloc((max + 1) * sizeof(int)); + long *prime; + prime = (long*)malloc((max + 1) * sizeof(long)); + memset(prime, 0, (max + 1) * sizeof(prime)); + memset(judge, 0, (max + 1) * sizeof(judge)); + judge[0] = judge[1] = 1; + for (long i = 2; i < max; i++) + { + if (!judge[i]) + { + prime[ct] = i; + ct++; + } + + for (int j = 0; j < ct && i * prime[j] <= max; j++) + { + judge[i * prime[j]] = 1; + if (!(i%prime[j])) + { + break; + } + + } + } + + printf((!judge[max]) ? ("%d is a prime number\n") : ("%d is a composite number\n"), max); + + system("pause"); + + return 0; +} diff --git a/practices/c/level1/p03_Diophantus/README.md b/practices/c/level1/p03_Diophantus/README.md index 021fd647..cba12675 100755 --- a/practices/c/level1/p03_Diophantus/README.md +++ b/practices/c/level1/p03_Diophantus/README.md @@ -14,4 +14,6 @@ 年级是他的一半。 -问儿子死时丢番图多大? \ No newline at end of file +问儿子死时丢番图多大? + +/* *just for test* */ \ No newline at end of file diff --git a/practices/c/level1/p03_Diophantus/lvl1_3_Diophantus.c b/practices/c/level1/p03_Diophantus/lvl1_3_Diophantus.c new file mode 100644 index 00000000..9ae9625b --- /dev/null +++ b/practices/c/level1/p03_Diophantus/lvl1_3_Diophantus.c @@ -0,0 +1,27 @@ +/* author: Kyrios0 + * date: 2017.02.23 + * version: 1.0.0 + * state: finished + */ + +#include +#include +#include + +int main(int argc, char *argv[]) { + system("chcp 437"); + system("cls"); + double age = 0.0, ans = 0.0; + while(1) + { + ans = (1.0/6.0 + 1.0/12.0 + 1.0/7.0 + 1.0/2.0) * age + 9.0; + if(ans - age < 0.1) + { + printf("He was %.0f years old at that time. \n", ans - 4); + system("pause"); + return 0; + } + age += 1.0; + } + return 0; +} diff --git a/practices/c/level1/p04_ narcissus/README.md b/practices/c/level1/p04_ narcissus/README.md index c7fc78e3..49096e29 100755 --- a/practices/c/level1/p04_ narcissus/README.md +++ b/practices/c/level1/p04_ narcissus/README.md @@ -4,4 +4,4 @@ 水仙花数:n位数的每个数位的n次方之和等于该n位数本身 -例如:153=1^3+5^3+3^3 \ No newline at end of file +例如:153=1^3 + 5^3 + 3^3 \ No newline at end of file diff --git a/practices/c/level1/p04_ narcissus/lvl1_4_narcissus.c b/practices/c/level1/p04_ narcissus/lvl1_4_narcissus.c new file mode 100644 index 00000000..261e8ea7 --- /dev/null +++ b/practices/c/level1/p04_ narcissus/lvl1_4_narcissus.c @@ -0,0 +1,36 @@ +/* code: Narcissus + * author: Kyrios0 + * date: 2017.02.26 + * state: finished + * version: 1.0.0 + */ + +#include "stdafx.h" +#include +#include +#include + +int main(int argc, char** argv) +{ + system("chcp 437"); + system("cls"); + int hun, ten, one, sum = 100;//hun = 100 + puts("The following is the number of narcissus in 1000"); + + while (sum < 1000) + { + one = sum % 10; + ten = sum / 10 % 10; + hun = sum / 100; + + if ((int)(pow(double(one), 3.0) + pow(double(ten), 3.0) + pow(double(hun), 3.0)) == sum) + { + printf("%d\n", sum); + } + + sum++; + } + + system("pause"); + return 0; +} \ No newline at end of file diff --git a/practices/c/level1/p05_allPrimes/lvl1_5_allPrimes.c b/practices/c/level1/p05_allPrimes/lvl1_5_allPrimes.c new file mode 100644 index 00000000..99a037a5 --- /dev/null +++ b/practices/c/level1/p05_allPrimes/lvl1_5_allPrimes.c @@ -0,0 +1,68 @@ +// allPrime.c +// + +/* code: Step0 for Eula + * author: Kyrios0 + * date: 2017.02.21 + * state: finished + * version: 2.0.2 + */ +#define MAX 200000000 +#include +#include//malloc() +#include +#include + +int judge[MAX]; +long prime[MAX]; + + + +int main(int argc, char** argv) +{ + long ct = 0; + judge[0] = judge[1] = 1; + + for (long i = 2; i < MAX; i++) + { + if (!judge[i]) + { + prime[ct] = i; + ct++; + } + + for (int j = 0; j < ct && i * prime[j] < MAX; j++) + { + judge[i * prime[j]] = 1; + if (!(i%prime[j])) + { + break; + } + + } + } + + FILE *fpt; + fpt = fopen("PrimeList.txt", "w"); + + puts("Start writing... Please wait a moment."); + + for (long i = 0; ; i++)//output + { + if (!prime[i]) + { + break; + } + fprintf(fpt, "%d, ", prime[i]); + if ((i % 100000 == 0) && (i != 0)) + { + printf("%d numbers has been writen: %d \n", i, prime[i]); + } + } + + puts("DONE!"); + + system("pause"); + + return 0; +} \ No newline at end of file diff --git a/practices/c/level1/p06_Goldbach/lvl1_6_Goldbach.c b/practices/c/level1/p06_Goldbach/lvl1_6_Goldbach.c new file mode 100644 index 00000000..b85d6e93 --- /dev/null +++ b/practices/c/level1/p06_Goldbach/lvl1_6_Goldbach.c @@ -0,0 +1,83 @@ +// Goldbach.c +// + +/* code: Goldbach (change from lvl1_5) +* author: Kyrios0 +* date: 2017.02.26 +* state: finished +* version: 2.0.1 +*/ + +#include +#include +#include +int prime[1000000]; +int judge[80000]; + +int main(int argc, char** argv) +{ + long MAX = 1000000;//about 8min + long ct = 0; + judge[0] = judge[1] = 1; + + for (long i = 2; i < MAX; i++) + { + if (!judge[i]) + { + prime[ct] = i; + ct++; + } + + for (int j = 0; j < ct && i * prime[j] < MAX; j++) + { + judge[i * prime[j]] = 1; + if (!(i%prime[j])) + { + break; + } + + } + + } + + FILE *fpt; + fpt = fopen("Goldbach.txt", "w"); + puts("Start writing... Please wait a moment."); + + int x = 0, y = 0; + + for (int i = 4; i <= 1000000;) + { + if (prime[x] < i - 300)//The speed can be increased by 40% if i == 100,000 + { + x++; + } + + if (i - prime[x] != prime[y]) + { + x++; + if (prime[x] > 1000000) + { + x = 0; + y++; + } + } + else + { + fprintf(fpt, "%d = %d + %d\n", i, prime[x], prime[y]); + if (i % 20000 == 0) + { + printf("%d lines has been writen.\n", i / 2); + } + x = 0; y = 0, i += 2; + continue; + } + + } + + puts("DONE!"); + + system("pause"); + + return 0; +} \ No newline at end of file diff --git a/practices/c/level1/p07_encrypt_decrypt/README.md b/practices/c/level1/p07_encrypt_decrypt/README.md index efd67472..b708a320 100755 --- a/practices/c/level1/p07_encrypt_decrypt/README.md +++ b/practices/c/level1/p07_encrypt_decrypt/README.md @@ -1,3 +1,14 @@ ### 功能要求: -1. 分别编写“加密”、“解密”函数,输入为任意长度的字符串 \ No newline at end of file +<<<<<<< HEAD +1. 分别编写“加密”、“解密”函数,输入为任意长度的字符串 + +### Writeup + +> 想起寒假学密码学看到的Feistel结构加解密于是产生了用C语言实现的想法。 然后我大概不会再用C语言写加解密了...//IDE:VS2017 RC +> +> P.S.:程序执行加密的时候如果明文过短可能会出BUG,具体原理不详,断在了system语句. +> P.P.S.:附上的文件中,一份源代码,一份程序 +======= +1. 分别编写“加密”、“解密”函数,输入为任意长度的字符串 +>>>>>>> parent of ba96cb9... lvl1_7 diff --git a/practices/c/level1/p07_encrypt_decrypt/lvl1_7_Feisitel_Encrypt_and_Decrypt_System.c b/practices/c/level1/p07_encrypt_decrypt/lvl1_7_Feisitel_Encrypt_and_Decrypt_System.c new file mode 100644 index 00000000..cbf64e5b --- /dev/null +++ b/practices/c/level1/p07_encrypt_decrypt/lvl1_7_Feisitel_Encrypt_and_Decrypt_System.c @@ -0,0 +1,384 @@ +// Feistel.cpp +// + +/* code: Feistel-construction Encrypt/Decrypt System + * author: Kyrios0 + * date: 2017.02.26 + * state: finished + * version: 1.1.1 + */ + +#define _CRT_SECURE_NO_WARNINGS +#include "stdafx.h" +#include +#include +#include +#include +#include +#include + +char* XOR(char *a, char *b); +unsigned int SDBMHash(char *str); +unsigned int RSHash(char *str); +int stringCopy(char *a, char *b, unsigned c); +char* HASH(char *str); +char* key_schedule(char *key); +int encrypt(char *data); +int decrypt(char *cipher); +int outputTrans(char *str); +int inputTrans(char *userPlain); +char* stringInput(char *buf); +int EncryptMode(); +int DecryptMode(); + +char hstr[17], hstrsub[9], subKeys[16 * 16 + 1], cipher[33], plain[33]; +char *userPlain = NULL, *userCipher = NULL; +//Just xor +char* XOR(char *a, char *b) +{ + for (int i = 0; i < 16; i++) + { + a[i] = a[i] ^ b[i]; + } + + a[16] = '\0'; + b[16] = '\0'; + + return a; +} +//Hash1 +unsigned int SDBMHash(char *str) +{ + unsigned int hash = 0; + + while (*str) + { + hash = (*str++) + (hash << 6) + (hash << 16) - hash; + } + + return (hash & 0x7FFFFFFF); +} +//Hash2 +unsigned int RSHash(char *str) +{ + unsigned int b = 378551; + unsigned int a = 63689; + unsigned int hash = 0; + + while (*str) + { + hash = hash * a + (*str++); + a *= b; + } + + return (hash & 0x7FFFFFFF); +} +//Instead strncopy, this won't stop at '0x00' +int stringCopy(char *a, char *b, unsigned c) +{ + for (int i = 0; i < c; i++) + { + a[i] = b[i]; + } + + return 0; +} +//Hash1 and hash2 +char* HASH(char *str) +{ + unsigned hash, hashsub; + + hash = SDBMHash(str); + hashsub = RSHash(str); + _itoa_s(hash, hstr, 16); + _itoa_s(hashsub, hstrsub, 16); + strcat_s(hstr, hstrsub); + + if (strlen(hstr) < 16) + { + char subKey[] = { '0', 0x00 }; + + for (int i = (16 - strlen(hstr)); i > 0; i--) + { + strcat_s(hstr, subKey); + } + + } + + return hstr; +} +//Generate a key schedule +char* key_schedule(char *key) +{ + char *subKey = key; + + for (int i = 0; i < 16; i++) + { + subKey = HASH(subKey); + strcat_s(subKeys, subKey); + } + + return subKeys; +} +//Basic encrypt +int encrypt(char *data) +{ + *cipher = { 0x00 }; + char d1[17] = { 0x00 }, d2[17] = { 0x00 }, currentkey[17], temp[17]; + char done[17]; + done[16] = '\0'; + + strncpy_s(d1, data, 16); + strncpy_s(d2, data + 16, strlen(data) - 16); + + for (int i = 0; i < 16; i++) + { + strncpy_s(currentkey, subKeys + (16 * i), 16); + stringCopy(done, XOR(XOR(HASH(d2), currentkey), d1), 16); + stringCopy(d1, done, 16); + stringCopy(temp, d1, 16); + stringCopy(d1, d2, 16); + stringCopy(d2, temp, 16); + } + + d2[16] = '\0'; + d1[16] = '\0'; + strcat_s(cipher, d2); + strcat_s(cipher, d1); + + return 0; +} +//Basic decrypt +int decrypt(char *cipher) +{ + *plain = { 0x00 }; + char d1[17] = { 0x00 }, d2[17] = { 0x00 }, currentkey[17], temp[17]; + char done[17]; + done[16] = '\0'; + + stringCopy(d2, cipher, 16); + stringCopy(d1, cipher + 16, 16); + + for (int i = 0; i < 16; i++) + { + strncpy_s(currentkey, subKeys + (16 * (15 - i)), 16); + stringCopy(temp, d1, 16); + stringCopy(d1, d2, 16); + stringCopy(d2, temp, 16); + stringCopy(done, XOR(XOR(HASH(d2), currentkey), d1), 16); + stringCopy(d1, done, 16); + } + + strcat_s(plain, d1); + strcat_s(plain, d2); + + return 0; +} +//Enhanced ascii to hex +int outputTrans(char *str) +{ + for (int i = 0; i < 32; i++) + { + if (((unsigned char)str[i] < 0x10) && ((unsigned char)str[i] > 0x00)) + { + putchar('0'); + } + printf("%x", (unsigned char)str[i]); + } + + return 0; +} +//Enhanced hex to ascii +int inputTrans(char *userPlain) +{ + for (int i = 0; i < 64; i += 2) + { + if ((userPlain[i] >= '0') && (userPlain[i] <= '9')) + { + userPlain[i] -= '0'; + } + else + { + userPlain[i] -= ('a' - 10); + } + if ((userPlain[i + 1] >= '0') && (userPlain[i + 1] <= '9')) + { + userPlain[i + 1] -= '0'; + } + else + { + userPlain[i + 1] -= ('a' - 10); + } + cipher[i / 2] = userPlain[i] * 0x10 + userPlain[i + 1]; + } + + return 0; +} +//Arbitrary length +char* stringInput(char *buf) +{ + int len = 0; + char *str = (char *)malloc(4 * sizeof(char)); + if (str == NULL) + { + exit(1); + } + + while (1) + { + fgets(str, 5, stdin); + if (str[3] == '\n') + { + str[3] = '\0'; + } + if (str[0] == '\n') + { + str[0] = '\0'; + break; + } + len += 4; + char * buff = (char *)malloc(len * sizeof(char)); + *buff = { 0x00 }; + if (len != 4) + { + strcpy(buff, buf); + *buf = { 0x00 }; + } + strcat(buff, str); + buf = buff; + buff = NULL; + if (strlen(str) != 4) + { + break; + } + } + + if (buf[strlen(buf) - 1] == '\n') + { + buf[strlen(buf) - 1] = '\0'; + } + + return buf; +} + +int EncryptMode() +{ + puts("Please enter plain text:"); + userPlain = stringInput(userPlain); + + unsigned int fill, block, length, exLength; + char currentEncryptBlock[33]; + length = strlen(userPlain); + block = (length - 1) / 32 + 1; + fill = (32 - (length % 32)) % 32; + exLength = block * 32; + + for (int i = fill; i > 0; i--) + { + userPlain[exLength - i] = ' '; + userPlain[exLength - i + 1] = 0x00; + } + + printf("cipher:\t"); + + for (int i = 0; i < block; i++) + { + stringCopy(currentEncryptBlock, userPlain + (32 * i), 32); + currentEncryptBlock[32] = '\0'; + encrypt(currentEncryptBlock);//Loop encryption + outputTrans(cipher); + } + + putchar('\n'); + + return 0; +} + +int DecryptMode() +{ + puts("Please enter cipher text:"); + unsigned int fill, block, length; + userCipher = stringInput(userCipher); + + char currentDecryptBlock[65]; + length = strlen(userCipher); + block = (length - 1) / 64 + 1; + fill = (64 - (length % 64)) % 64; + if ((length % 64) != 0) + { + puts("WARNING!"); + puts("INVAILD CIPHER!!!"); + return -1; + } + + printf("plain:\t"); + + for (int i = 0; i < block; i++) + { + stringCopy(currentDecryptBlock, userCipher + (64 * i), 64); + currentDecryptBlock[64] = '\0'; + inputTrans(currentDecryptBlock); + decrypt(cipher); //Loop encryption + printf("%s", plain); + } + + putchar('\n'); + + return 0; +} + +int main(int argc, char** argv) +{ + char key[] = "Kyrios0";//char Test_data[] = "hashtestmiaomiaomiaowhathappened" + key_schedule(key); + + system("chcp 437"); + system("mode con cols=65 lines=32"); + + while (1) + { + *hstr = { 0x00 }; *hstrsub = { 0x00 }; *cipher = { 0x00 }; *plain = { 0x00 }; + + system("cls"); + printf("\n\n\n\n\n\n\n"); + puts(" Feistel Structure Encryption and Decryption System"); + printf("\n\n\n\n"); + puts(" Press E to enter the Encryption mode"); + printf("\n\n\n"); + puts(" Press D to enter the Decryption mode"); + printf("\n\n\n"); + puts(" Press Q to Quit"); + + switch (_getch()) + { + case 'e': + system("cls"); + puts("EncryptMode"); + EncryptMode(); + break; + + case 'd': + system("cls"); + puts("DecryptMode"); + DecryptMode(); + break; + + case 'q': + system("cls"); + puts("Kyrios hope that you will have a nice day"); + puts("Bye"); + system("pause"); + exit(0); + break; + + default: + continue; + + } + + system("pause"); + } + + return 0; +} + diff --git a/practices/c/level1/p08_hanoi/lvl1_8_hanoi.c b/practices/c/level1/p08_hanoi/lvl1_8_hanoi.c new file mode 100644 index 00000000..7ac18f3b --- /dev/null +++ b/practices/c/level1/p08_hanoi/lvl1_8_hanoi.c @@ -0,0 +1,34 @@ +/* code: Hanoi + * author: Kyrios0 + * date: 2016.10 (Maybe this time) + * state: finished + * version: 1.0.0 + */ + +#include +#include +#include + +void hanoi(int n, char a, char b, char c) +{ + if(n > 0) + { + hanoi(n - 1, a, c, b); + printf("From %c to %c \n", a, c);//it's too slow...With fprintf to replace printf will be a lot faster + hanoi(n - 1, b, a, c); + } +} + +int main(int argc, char** argv) +{ + int n; char a = 'A', b = 'B', c = 'C'; + + puts("input the number of discs"); + scanf("%d", &n); + + hanoi(n, a, b, c); + + system("pause"); + return 0; +} + diff --git a/practices/c/level1/p09_maze/lvl1_9_maze.c b/practices/c/level1/p09_maze/lvl1_9_maze.c new file mode 100644 index 00000000..e0ba1686 --- /dev/null +++ b/practices/c/level1/p09_maze/lvl1_9_maze.c @@ -0,0 +1,278 @@ +// Puzzle.c +// + +/* code: Puzzle Game for Kira + * author: Kyrios0 + * date: 2017.03.03 + * state: finished + * version: 1.0.5 + */ + +#include "stdafx.h" +#include +#include +#include +#include +#include +#include + +char *getWall(int x, int y); +int sr(int x, int y); +void Make_Maze(int x, int y); +void gotoxy(int x, int y); +void ctrl(int x, int y); +void modeSelection(); +DWORD WINAPI clocks(LPVOID lpParameter); + +char cw[][4] = { " ","┃","━","┗","┃","┃","┏","┣","━","┛","━","┻","┓","┫","┳" ,"╋" };//map +char m[50][50], cm1, cm2, level; +//Automatically generate walls +char *getWall(int x, int y) +{ + int a = (m[x][y - 1] ? 0 : 1) | (m[x + 1][y] ? 0 : 2) | (m[x][y + 1] ? 0 : 4) | (m[x - 1][y] ? 0 : 8); + return cw[(m[x][y - 1] ? 0 : 1) | (m[x + 1][y] ? 0 : 2) | (m[x][y + 1] ? 0 : 4) | (m[x - 1][y] ? 0 : 8)];//CORE! +} +//Determine the path +int sr(int x, int y)//CORE algorithm! +{ + static int d[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } }; + int zx = x * 2, zy = y * 2, nx, tn = rand() % 2 ? 1 : 3, i; + m[zx][zy] = 1; + + for (i = 0, nx = rand() % 4; i < 4; i++, nx = (nx + tn) % 4) + { + if (m[zx + 2 * d[nx][0]][zy + 2 * d[nx][1]] == 0) + { + m[zx + d[nx][0]][zy + d[nx][1]] = 1, sr(x + d[nx][0], y + d[nx][1]); + } + } + + return 0; +} + +void Make_Maze(int x, int y) +{ + int z1, z2; + + for (z1 = 0, z2 = 2 * y + 2; z1 <= 2 * x + 2; z1++) + { + m[z1][0] = 1, m[z1][z2] = 1; + } + + for (z1 = 0, z2 = 2 * x + 2; z1 <= 2 * y + 2; z1++) + { + m[0][z1] = 1, m[z2][z1] = 1; + } + + m[1][2] = 1; m[2 * x + 1][2 * y] = 1; + srand((unsigned)time(NULL)); + sr(rand() % x + 1, rand() % y + 1); +} + +void gotoxy(int x, int y) +{ + HANDLE hOutput; + COORD coo; + coo.X = x; + coo.Y = y; + hOutput = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleCursorPosition(hOutput, coo); +} + +void ctrl(int x, int y) +{ + while (1) + { + char cm2 = _getch(); + if ((cm2 == -32) && (cm2 != 'w') && (cm2 != 'a') && (cm2 != 's') && (cm2 != 'd')) + { + switch (cm2 = _getch()) + { + case 72: cm2 = 'w'; break; + case 80: cm2 = 's'; break; + case 75: cm2 = 'a'; break; + case 77: cm2 = 'd'; break; + } + } + if ((( ((m[x][y + 1] == 0) && (cm2 == 's')) + || ((m[x + 1][y] == 0) && (cm2 == 'd')) + || ((m[x][y - 1] == 0) && (cm2 == 'w')) + || (((x - 1 == 0) || (m[x - 1][y] == 0)) && (cm2 == 'a')) ) == 0))//if it's vaild + { + cm1 = cm2;//Accept + break; + } + } +} + +void modeSelection() +{ + system("mode con cols=63 lines=32"); + gotoxy(26, 13); + printf("CHOOSE MODE"); + gotoxy(28, 14); + printf("E-easy"); + gotoxy(28, 15); + printf("N-normal"); + gotoxy(28, 16); + printf("H-hard"); + gotoxy(28, 17); + printf("L-lunatic"); + level = _getch(); + system("cls"); +} + +DWORD WINAPI clocks(LPVOID lpParameter) +{ + int x = *(int*)lpParameter; + int i = 0; + while (1) + { + Sleep(500); + gotoxy(4 * x + 5, 3); + printf("Time: %ds", i++); + Sleep(500); + } +} + +int main(int argc, char** argv) +{ + system("color 0f"); + while (1) + { + modeSelection(); + int x, y; + switch (level) + { + case 'e': + x = 5, y = 5; + system("mode con cols=50 lines=13"); + break; + case 'n': + x = 10, y = 10; + system("mode con cols=62 lines=23"); + break; + case 'h': + x = 15, y = 15; + system("mode con cols=82 lines=33"); + break; + case 'l': + x = 29, y = 22; + system("mode con cols=138 lines=47"); + break; + default: + gotoxy(15, 13); + puts("Because recently someone is lazy"); + gotoxy(12, 15); + puts("so there is no hidden difficulty(~ ̄▽ ̄)~"); + gotoxy(23, 17); + system("pause"); + exit(0); + break; + } + + int z1, z2, nx = 1, ny = 2; + + Make_Maze(x, y); + for (z2 = 1; z2 <= y * 2 + 1; z2++) + { + for (z1 = 1; z1 <= x * 2 + 1; z1++) + printf(m[z1][z2] ? " " : getWall(z1, z2)); + if (z2 <= y * 2) + { + putchar(10); + } + } + + switch (level) + { + case 'e': + gotoxy(25, 1); + puts("Mode: Easy"); + gotoxy(27, 5); + puts("Just"); + gotoxy(28, 6); + puts("For"); + gotoxy(27, 7); + puts("Kids"); + break; + case 'n': + gotoxy(45, 1); + puts("Mode: Normal"); + gotoxy(45, 6); + puts("Relax~~"); + break; + case 'h': + gotoxy(65, 1); + puts("Mode: Hard"); + gotoxy(65, 6); + puts("Enjoy it!"); + break; + case 'l': + gotoxy(121, 1); + puts("Mode: Lunatic"); + gotoxy(121, 6); + puts("Be patient"); + break; + default:; + } + gotoxy(4 * x - 1, 2 * y - 1); + puts("End"); + HANDLE hThread_1 = CreateThread(NULL, 0, clocks, &x, 0, NULL); + cm1 = 'd'; + + while (1) + { + + gotoxy(2 * nx - 2, ny - 1); + putchar('█'); + gotoxy(2 * nx - 2, ny - 1); + ctrl(nx, ny); + gotoxy(2 * nx - 2, ny - 1); + putchar(' '); putchar(' '); + switch (cm1) + { + case 'w': + ny--; break; + case 'd': + nx++; break; + case 's': + ny++; break; + case 'a': + nx--; break; + } + if ((2 * nx - 1 == 4 * x - 1) && (ny - 1 == 2 * y - 1)) + { + gotoxy(0, 2 * y + 1); + puts("Congratulations!!! Press Q to quit"); + printf(" Press other key to restart"); + TerminateThread(hThread_1, 1); + CloseHandle(hThread_1); + cm2 = _getch(); + (cm2 == 'q') ? exit(0) : cm2++; + break; + } + } + + } + + return 0; +}// Puzzle Game +/* This algorithm is a randomized version of Prim's algorithm. + 1.Start with a grid full of walls. + 2.Pick a cell, mark it as part of the maze. Add the walls of the cell to the wall list. + 3.While there are walls in the list: + 3.1.Pick a random wall from the list. If the cell on the opposite side isn't in the maze yet: + 3.1.1.Make the wall a passage and mark the cell on the opposite side as part of the maze. + 3.1.2.Add the neighboring walls of the cell to the wall list. + 3.2.Remove the wall from the list. + + It will usually be relatively easy to find the way to the starting cell, but hard to find the way + anywhere else. + + Note that simply running classical Prim's on a graph with random edge weights would create mazes + stylistically identical to Kruskal's, because they are both minimal spanning tree algorithms. + Instead, this algorithm introduces stylistic variation because the edges closer to the starting + point have a lower effective weight. + */ + diff --git a/practices/c/level1/p10_pushBoxes/lvl1_10_pushBoxes.c b/practices/c/level1/p10_pushBoxes/lvl1_10_pushBoxes.c new file mode 100644 index 00000000..1d8d62d2 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/lvl1_10_pushBoxes.c @@ -0,0 +1,390 @@ +// Box.c +// + +/* code: Box for Kira + * author: Kyrios0 + * date: 2017.03.04 + * state: finished + * version: 1.1.0 + * when you run it first, please creat s01.txt s02.txt s03.txt. + */ + +#include "stdafx.h" +#include +#include +#include +#include +#include +#include + +void gotoxy(int x, int y); +int ctrl(int x, int y); +void writeScreen(LPCWSTR str, int x, int color); +void go(char cm1); +void push(char cm1); +unsigned int SDBMHash(char *str); +void loadMap(int lvl); +void loadBox(int lvl); +void makeMap(); +void loadBox(int lvl); +void makeMap(); +int loadScore(int lvl); +int saveScore(int lvl, int score); +int loadScore(int lvl); +int saveScore(int lvl, int step); + +char m[50][50]; +char cm1, cm2; +int nx = 3, ny = 2; +int x[6], y[6]; + +void gotoxy(int x, int y) +{ + HANDLE hOutput; + COORD coo; + coo.X = x; + coo.Y = y; + hOutput = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleCursorPosition(hOutput, coo); +} +//a simple hash function +unsigned int SDBMHash(char *str) +{ + unsigned int hash = 0; + + while (*str) + { + hash = (*str++) + (hash << 6) + (hash << 16) - hash; + } + + return (hash & 0x7FFFFFFF); +} +//enhanced gotoxy, it could wiriting something on the screen with different colors +void writeScreen(LPCWSTR str, int x, int y, int color) +{ + COORD coord; + DWORD buf[128]; + HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); + coord.Y = y; + coord.X = x; + FillConsoleOutputAttribute(hConsoleOutput, color, 2, coord, buf); + WriteConsoleOutputCharacter(hConsoleOutput, str, 1, coord, buf); +} + +void go(char cm1) +{ + switch (cm1) + { + case 'w': + ny--; break; + case 'd': + nx++; break; + case 's': + ny++; break; + case 'a': + nx--; break; + } +} + +void push(char cm1) +{ + switch (cm1) + { + case 'w': + ny--; + writeScreen(L"█", 2 * nx - 2, ny - 2, FOREGROUND_GREEN); + m[nx][ny - 1] = 2; + break; + case 'd': + nx++; + writeScreen(L"█", 2 * nx, ny - 1, FOREGROUND_GREEN); + m[nx + 1][ny] = 2; + break; + case 's': + ny++; + writeScreen(L"█", 2 * nx - 2, ny, FOREGROUND_GREEN); + m[nx][ny + 1] = 2; + break; + case 'a': + nx--; + writeScreen(L"█", 2 * nx - 4, ny - 1, FOREGROUND_GREEN); + m[nx - 1][ny] = 2; + break; + } + m[nx][ny] = 1; +} + +int ctrl(int x, int y) +{ + while (1) + { + char cm2 = _getch(); + + if ((cm2 == -32) && (cm2 != 'w') && (cm2 != 'a') && (cm2 != 's') && (cm2 != 'd')) + { + switch (cm2 = _getch()) + { + case 72: cm2 = 'w'; break; + case 80: cm2 = 's'; break; + case 75: cm2 = 'a'; break; + case 77: cm2 = 'd'; break; + } + }//arrow key to wasd + + if (( ((m[x][y + 1] == 2) && (cm2 == 's')) + || ((m[x + 1][y] == 2) && (cm2 == 'd')) + || ((m[x][y - 1] == 2) && (cm2 == 'w')) + || (((x - 1 == 0) || (m[x - 1][y] == 2)) && (cm2 == 'a'))) == 0)//if no box + { + if (( ((m[x][y + 1] == 0) && (cm2 == 's')) + || ((m[x + 1][y] == 0) && (cm2 == 'd')) + || ((m[x][y - 1] == 0) && (cm2 == 'w')) + || (((x - 1 == 0) || (m[x - 1][y] == 0)) && (cm2 == 'a'))) == 0)//if it's vaild + { + cm1 = cm2;//Accept + return 0; + } + } + else if (((((m[x][y + 2] == 0) && (cm2 == 's')) + || ((m[x + 2][y] == 0) && (cm2 == 'd')) + || ((m[x][y - 2] == 0) && (cm2 == 'w')) + || (((x - 1 == 0) || (m[x - 2][y] == 0)) && (cm2 == 'a'))) == 0) + && ((((m[x][y + 2] == 2) && (cm2 == 's')) + || ((m[x + 2][y] == 2) && (cm2 == 'd')) + || ((m[x][y - 2] == 2) && (cm2 == 'w')) + || (((x - 1 == 0) || (m[x - 2][y] == 2)) && (cm2 == 'a'))) == 0) )//if it's vaild + { + cm1 = cm2;//Accept + return 1; + } + } +} + +void loadMap(int lvl) +{ + char buf[50][50]; + FILE *fptr; + switch (lvl) + { + case 1: + fptr = fopen("lvl01.txt", "r"); + break; + case 2: + fptr = fopen("lvl02.txt", "r"); + break; + case 3: + fptr = fopen("lvl03.txt", "r"); + break; + default: + break; + + } + for (int i = 0; i < 11; i++)//input + { + for (int j = 0; j < 1; j++) + { + fgets(&buf[i][j], 256, fptr); + if (buf[i][j] == -52) + { + break; + } + for (int k = 0; buf[i][k] != 0 && buf[i][k] != 10; k++) + { + m[i][k] = (int)(buf[i][k] - 48); + } + } + } +} + +void loadBox(int lvl) +{ + char buf[50][50]; + FILE *fptr; + switch (lvl) + { + case 1: + fptr = fopen("bag01.txt", "r"); + break; + case 2: + fptr = fopen("bag02.txt", "r"); + break; + case 3: + fptr = fopen("bag03.txt", "r"); + break; + default: + break; + } + for (int i = 0; i < 2; i++)//input + { + fgets(&buf[0][0], 128, fptr); + for (int j = 0; j < 12; j += 4) + { + x[j / 4] = (int)(buf[0][j] - 48); + y[j / 4] = (int)(buf[0][j + 1] - 48); + x[j/4 + 3] = (int)(buf[0][j + 2] - 48); + y[j/4 + 3] = (int)(buf[0][j + 3] - 48); + } + } +} + +void makeMap() +{ + int z1, z2, x = 4, y = 4; + + for (z2 = 1; z2 <= y * 2 + 1; z2++) + { + for (z1 = 1; z1 <= x * 2 + 1; z1++) + printf(m[z1][z2] ? " " : "█"); + if (z2 <= y * 2) + { + putchar(10); + } + } +} + +int loadScore(int lvl) +{ + int score = 0; + char *charScore; + charScore = (char*)malloc(8 * sizeof(char)); + char *state; + char hash[8]; + FILE *fpsr; + switch (lvl) + { + case 1: + fpsr = fopen("s01.txt", "r"); + break; + case 2: + fpsr = fopen("s02.txt", "r"); + break; + case 3: + fpsr = fopen("s03.txt", "r"); + break; + default: + break; + } + state = fgets(hash, 128, fpsr); + if (state == NULL) + { + return 999; + } + hash[7] = '\0'; + char *currentHash; + currentHash = (char*)malloc(8 * sizeof(char)); + while (1) + { + itoa(score, charScore, 10); + itoa(SDBMHash(charScore), currentHash, 16); + if (!strcmp(hash, currentHash)) + { + break; + } + score++; + } + + return score; + +} + +int saveScore(int lvl, int step) +{ + FILE *fps; + char *newScore; + newScore = (char*)malloc(8 * sizeof(char)); + switch (lvl) + { + case 1: + fps = fopen("s01.txt", "w+"); + break; + case 2: + fps = fopen("s02.txt", "w+"); + break; + case 3: + fps = fopen("s03.txt", "w+"); + break; + default: + break; + } + itoa(step, newScore, 10); + fprintf(fps, "%x", SDBMHash(newScore)); + return 0; +} + +int main() +{ + system("color 0f"); + int level = 1; + + while (1) + { + loadMap(level); + loadBox(level); + int best = loadScore(level); + + nx = 3; ny = 2; + int step = 0, PoG; + system("mode con cols=32 lines=11"); + + makeMap(); + + writeScreen(L"█", x[0], y[0], FOREGROUND_GREEN); + writeScreen(L"█", x[1], y[1], FOREGROUND_GREEN); + writeScreen(L"█", x[2], y[2], FOREGROUND_GREEN); + writeScreen(L"▒", x[3]*2 - 2, y[3], FOREGROUND_RED | FOREGROUND_INTENSITY); + writeScreen(L"▒", x[4]*2 - 2, y[4], FOREGROUND_RED | FOREGROUND_INTENSITY); + writeScreen(L"▒", x[5]*2 - 2, y[5], FOREGROUND_RED | FOREGROUND_INTENSITY); + + gotoxy(21, 3); + puts("Step: 0"); + gotoxy(21, 4); + printf("Best: %d", best); + cm1 = 'd'; + + while (1) + { + gotoxy(2 * nx - 2, ny - 1); + printf("♂"); + gotoxy(2 * nx - 2, ny - 1); + PoG = ctrl(nx, ny); + gotoxy(2 * nx - 2, ny - 1); + putchar(' '); putchar(' '); + + PoG ? push(cm1) : go(cm1); + gotoxy(21, 3); + printf("Step: %d", ++step); + + writeScreen((m[x[3]][y[3]+1] == 2) ? L"█" : L"▒", x[3] * 2 - 2, y[3], (m[x[3]][y[3] + 1] == 2) ? FOREGROUND_GREEN | FOREGROUND_INTENSITY : FOREGROUND_RED | FOREGROUND_INTENSITY); + writeScreen((m[x[4]][y[4]+1] == 2) ? L"█" : L"▒", x[4] * 2 - 2, y[4], (m[x[4]][y[4] + 1] == 2) ? FOREGROUND_GREEN | FOREGROUND_INTENSITY : FOREGROUND_RED | FOREGROUND_INTENSITY); + writeScreen((m[x[5]][y[5]+1] == 2) ? L"█" : L"▒", x[5] * 2 - 2, y[5], (m[x[5]][y[5] + 1] == 2) ? FOREGROUND_GREEN | FOREGROUND_INTENSITY : FOREGROUND_RED | FOREGROUND_INTENSITY); + + if (m[x[3]][y[3] + 1] == 2 && m[x[4]][y[4] + 1] == 2 && m[x[5]][y[5] + 1] == 2) + { + gotoxy(0, 9); + puts("Congratulations!!!"); + if (step < best) + { + saveScore(level, step); + } + Sleep(1000); + system("cls"); + + if (level < 3) + { + level++; + break; + } + + system("mode con cols=54 lines=3"); + puts(" C language is the best language in the world!!"); + printf(" Press any key to quit"); + cm2 = _getch(); + exit(0); + } + } + + } + + return 0; +} + \ No newline at end of file diff --git a/practices/c/level1/p11_linkedList/lvl1_11_LinkedList.c b/practices/c/level1/p11_linkedList/lvl1_11_LinkedList.c new file mode 100644 index 00000000..4ccd8d80 --- /dev/null +++ b/practices/c/level1/p11_linkedList/lvl1_11_LinkedList.c @@ -0,0 +1,114 @@ +/* code: Linked list Step1 + * author: Kyrios0 + * date: 2017.03.09 + * state: unfinished + * version: 1.0.2 + */ + +#include "stdafx.h" +#include +#include +#include +#include +#include + +typedef struct linked { + int data; + linked *next; +}linked; + +linked *head, *tail; + +linked* creat(int length) +{ + linked *p; + int x; + head = tail = NULL; + + for (x = 0; x < length; x++) + { + p = (linked *)malloc(sizeof(linked)); + p->data = x + 2; + p->next = NULL; + + if (head == NULL) + { + head = tail = p; + } + else + { + tail = tail->next = p; + } + } + return head; +} + +int reverseL() +{ + linked *currentHead = head, *temp = head->next, *currentTail; + currentHead->next = NULL; currentTail = temp->next; + + while (currentTail->next != NULL) + { + if (temp != tail) + { + currentTail = temp->next; + } + temp->next = currentHead; + currentHead = temp; + temp = currentTail; + } + temp->next = currentHead; + currentHead = temp; + temp = currentTail; + + temp = head; + head = tail; + tail = head; + return 0; +} + +int main(int argc, char** argv) +{ + int len = 10; + creat(len); + linked *currentHead = head, *currentTail = tail; + + printf("Before:\t"); + for (int i = 0; i < len; i++)//output + { + printf("%d, ", currentHead->data); + currentHead = currentHead->next; + } + printf("\n"); + + reverseL(); + + currentHead = head, currentTail = tail; + printf("After:\t"); + for (int i = 0; i < len; i++)//output + { + printf("%d, ", currentHead->data); + currentHead = currentHead->next; + } + printf("\n"); + + currentHead = head, currentTail = tail; + int state = 0; + for (int i = 0; i < len; i++) + { + if (currentHead->data == 5) + { + printf("Node%d : 5\n", i+1); + state = 1; + } + currentHead = currentHead->next; + } + if (state == 0) + { + puts("5 not find: -1"); + } + _getch(); + return 0; +} + diff --git a/practices/c/level1/p12_warehouse/lvl1_12_warehouse.c b/practices/c/level1/p12_warehouse/lvl1_12_warehouse.c new file mode 100644 index 00000000..bcce70e3 --- /dev/null +++ b/practices/c/level1/p12_warehouse/lvl1_12_warehouse.c @@ -0,0 +1,258 @@ +// house.c +// +/* author: Kyrios0 + * date: 2017.03.23 + * version:1.0.6 + * state: list unfinished//strlen * ' ' + * pop unfinished//number + */ +//#include "stdafx.h" +#include +#include +#include +#include +#include +FILE* fp = fopen("house.txt", "r+"); + +typedef struct list { + int data; + char name[128]; + list *next; +}list; + +list *head = NULL, *currentTail = NULL; + +list* getList() +{ + list *tail, *p; + char *state; + + p = (list *)malloc(sizeof(list)); + while (!feof(fp)) + { + state = fgets(p->name, 128, fp); + if (state == NULL)//for empty list + { + return head; + } + p->name[strlen(p->name) - 1] = '\0'; + fscanf(fp, "%d", &p->data); + fgetc(fp); + p->next = NULL; + tail = p; + if (head->next == NULL) + { + head->next = tail; + } + p = (list *)malloc(sizeof(list)); + tail->next = p; + } + free(p); + p = NULL; + tail->next = NULL; + return tail; +} + +void help() +{ + puts("\n\tGNU bash, Version 4.3.42(1)-release (x86_64-pc-kyrinux-gnu)\n\n\ + \tCommand\t\tInfo\n\ + \tlist\t\tView\n\ + \tpush\t\tWrite\n\ + \tpop\t\tDelete\n\ + \tquit\t\tQuit\n"); +} + +int pop() +{ + list *p = head->next, *prev = NULL; + if (p == NULL)//for empty list + { + puts("ERROR: The list is empty!"); + return -1; + } + puts("Which one do you want to remove?"); + int i = 1, j = 1, popN; + while (p->next != NULL) + { + printf("%d:%s\n", i, p->name); + p = p->next; + i++; + } + /*if (i == 1) + { + strcpy(p->name, "Empty");//p->name = "Empty"; + }*/ + printf("%d:%s\n", i, p->name); + while (1)//filter + { + printf("Please enter the serial number(1-%d)(Enter 0 to exit pop):", i); + scanf("%d", &popN); + getchar(); + if (popN == 0) + { + return 1; + } + if (popN >= 1 && popN <= i) + { + break; + } + puts("Invaild serial number"); + } + + p = head->next; + while (j < popN) + { + prev = p; + p = p->next; + j++; + } + printf("%d:%s\n", j, p->name); + if (popN == 1) + { + head = head->next; + } + else + { + if (popN == i) + { + currentTail = prev; + currentTail->next = NULL; + } + else + { + prev->next = p->next; + } + } + puts("You poped it!"); + return 0; + +} + +int allList() +{ + list *p = head->next; + if (p == NULL)//for empty list + { + puts("\n\tEMPTY\n"); + return -1; + } + printf("\n\tName\t\tAmount\n"); + while (p->next != NULL) + { + printf("\t%s\t\t%5d\n", p->name, p->data); + p = p->next; + } + printf("\t%s\t\t%5d\n\n", p->name, p->data); + return 0; +} + +void invalid(char *cm) +{ + cm[strlen(cm) - 1] = '\0'; + printf("\"%s\" isn't a valid command, Type \"help\" for more information.\n", cm); +} + +int push() +{ + list *p; + char yn; + while (1) + { + p = (list *)malloc(sizeof(list)); + p->next = NULL; + printf("Please enter name: "); + fgets(p->name, 128, stdin); + p->name[strlen(p->name) - 1] = '\0'; + printf("Please enter amount:"); + scanf_s("%d", &p->data); + getchar(); + if (p->data <= 0) + { + puts("ERROR: The number of items must be greater than zero."); + free(p); + return -1; + } + printf("\"%s\" has been added. Would you like to continue to push?(y/n)", p->name); + currentTail->next = p; + currentTail = p; + yn = getchar(); + getchar(); + while (yn != 'n' && yn != 'y') + { + printf("\"%s\" has been added. Would you like to continue to push?(y/n)", p->name); + yn = getchar(); + getchar(); + } + if (yn == 'n') + { + break; + } + } + return 0; +} + +int saveData() +{ + FILE* fpw = fopen("house.txt", "w+"); + list *p = head->next; + if (p == NULL)//for empty list + { + return -1; + } + while (p->next != NULL) + { + fprintf(fpw, "%s\n%d\n", p->name, p->data); + p = p->next; + } + + fprintf(fpw, "%s\n%d", p->name, p->data); + return 0; +} + +int main(int argc, char **argv) +{ + char cm[128] = "Kyrios"; + puts("Kyrinux 1.0.0 (default, Mar 10 2017, 11:07:07) [MSC v.1500 64 bit (AMD64)] on win64"); + puts("Type \"help\" for more information."); + + head = (list *)malloc(sizeof(list)); + head->next = NULL; + currentTail = getList(); + while (1) + { + printf("root@user: "); + fgets(cm, 128, stdin); + if (!strcmp(cm, "help\n")) + { + help(); + } + else if (!strcmp(cm, "push\n")) + { + push(); + } + else if (!strcmp(cm, "pop\n")) + { + pop(); + } + else if (!strcmp(cm, "list\n")) + { + allList(); + } + else if (!strcmp(cm, "quit\n")) + { + break; + } + else + { + invalid(cm); + } + + } + saveData(); + puts("Kyrios hope you'll have a nice day, bye."); + puts("Press any key to quit..."); + _getch(); + return 0; +} + diff --git a/practices/cpp/level1/p01_Queue/main.cpp b/practices/cpp/level1/p01_Queue/main.cpp new file mode 100644 index 00000000..e56c2c5d --- /dev/null +++ b/practices/cpp/level1/p01_Queue/main.cpp @@ -0,0 +1,32 @@ +/* + * Author: Kyrios0 + * Date: 2016.04 + * To-Do List: + * ~~isEmpty~~ + * ~~isFull~~ + * ~~append~~ + * ~~pop~~ + * ~~Optimization~~ + * and more? + * State: Completed + */ +#include +#include "queue.h" + +int main(int argc, char** argv) +{ + Queue queue; + + if(queue.isEmpty()) + { + puts("Status: Empty"); + } + if(queue.isFull()) + { + puts("Status: Full"); + } + queue.append(1); + queue.pop(); + + return 0; +} diff --git a/practices/cpp/level1/p01_Queue/queue.cpp b/practices/cpp/level1/p01_Queue/queue.cpp new file mode 100644 index 00000000..fa755391 --- /dev/null +++ b/practices/cpp/level1/p01_Queue/queue.cpp @@ -0,0 +1,48 @@ +#include + +#include "queue.h" + + + +bool Queue::isEmpty() +{ + if(tail == head) + { + return true; + } + return false; +} + +bool Queue::isFull() +{ + if((tail + 1) % 100 == head) + { + return true; + } + return false; +} + +int Queue::append(int x) +{ + if(isFull()) + { + puts("Error: List is full, appending failed."); + return -1; + } + data[tail++] = x; + tail = tail % 100; + return 0; +} +int Queue::pop() +{ + int ans = data[head]; + if(isEmpty()) + { + puts("Error: List is empty, poping failed."); + return -1; + } + data[head++] = 0; + head = head % 100; + return ans; +} + diff --git a/practices/cpp/level1/p01_Queue/queue.h b/practices/cpp/level1/p01_Queue/queue.h new file mode 100644 index 00000000..a748e57b --- /dev/null +++ b/practices/cpp/level1/p01_Queue/queue.h @@ -0,0 +1,14 @@ + +class Queue +{ + private: + int head = 0; + int tail = 0; + int data[100]; + public: + bool isEmpty(); + bool isFull(); + int append(int x); + int pop(); +}; + diff --git a/practices/cpp/level1/p02_Stack/main.cpp b/practices/cpp/level1/p02_Stack/main.cpp new file mode 100644 index 00000000..72c743de --- /dev/null +++ b/practices/cpp/level1/p02_Stack/main.cpp @@ -0,0 +1,32 @@ +/* + * Author: Kyrios0 + * Date: 2016.04 + * To-Do List: + * ~~isEmpty~~ + * ~~isFull~~ + * ~~append~~ + * ~~pop~~ + * ~~Optimization~~ + * and more? + * State: Completed + */ +#include +#include "stack.h" + +int main(int argc, char** argv) +{ + Stack stack; + + if(stack.isEmpty()) + { + puts("Status: Empty"); + } + if(stack.isFull()) + { + puts("Status: Full"); + } + stack.append(1); + stack.pop(); + + return 0; +} diff --git a/practices/cpp/level1/p02_Stack/stack.cpp b/practices/cpp/level1/p02_Stack/stack.cpp new file mode 100644 index 00000000..e558e435 --- /dev/null +++ b/practices/cpp/level1/p02_Stack/stack.cpp @@ -0,0 +1,47 @@ +#include + +#include "stack.h" + + + +bool Stack::isEmpty() +{ + if(tail == head) + { + return true; + } + return false; +} + +bool Stack::isFull() +{ + if((tail == 100) + { + return true; + } + return false; +} + +int Stack::append(int x) +{ + if(isFull()) + { + puts("Error: List is full, appending failed."); + return -1; + } + data[tail++] = x; + return 0; +} + +int Stack::pop() +{ + if(isEmpty()) + { + puts("Error: List is empty, poping failed."); + return -1; + } + int ans = data[--tail]; + data[tail] = 0; + return ans; +} + diff --git a/practices/cpp/level1/p02_Stack/stack.h b/practices/cpp/level1/p02_Stack/stack.h new file mode 100644 index 00000000..cf567830 --- /dev/null +++ b/practices/cpp/level1/p02_Stack/stack.h @@ -0,0 +1,14 @@ + +class Stack +{ + private: + int head = 0; + int tail = 0; + int data[100]; + public: + bool isEmpty(); + bool isFull(); + int append(int x); + int pop(); +}; + diff --git a/practices/cpp/level1/p02_Stack/stack.h.gch b/practices/cpp/level1/p02_Stack/stack.h.gch new file mode 100644 index 00000000..377d041b Binary files /dev/null and b/practices/cpp/level1/p02_Stack/stack.h.gch differ diff --git a/practices/cpp/level1/p03_SafeArray/SafeArray.cpp b/practices/cpp/level1/p03_SafeArray/SafeArray.cpp new file mode 100644 index 00000000..c8d4bd4c --- /dev/null +++ b/practices/cpp/level1/p03_SafeArray/SafeArray.cpp @@ -0,0 +1,28 @@ +#include "SafeArray.h" +#include + +int capbility; + +SafeArray::SafeArray(int cap) +{ + data = new int [cap + 1];//extra space for saving error data + capbility = cap; +} + +SafeArray::~SafeArray() +{ + delete data; +} + +int& SafeArray::operator[] (int n) +{ + if(n >= 0 && n < capbility) + { + return data[n]; + } + else + { + printf("Error: Out of limit"); + return data[capbility]; + } +} diff --git a/practices/cpp/level1/p03_SafeArray/SafeArray.h b/practices/cpp/level1/p03_SafeArray/SafeArray.h new file mode 100644 index 00000000..935c4990 --- /dev/null +++ b/practices/cpp/level1/p03_SafeArray/SafeArray.h @@ -0,0 +1,12 @@ + +class SafeArray +{ + + private: + int *data; + int capbility; + public: + SafeArray(int cap); + ~SafeArray(); + int& operator[] (int n); +}; diff --git a/practices/cpp/level1/p03_SafeArray/main.cpp b/practices/cpp/level1/p03_SafeArray/main.cpp new file mode 100644 index 00000000..3ae2a0d7 --- /dev/null +++ b/practices/cpp/level1/p03_SafeArray/main.cpp @@ -0,0 +1,9 @@ +#include +#include "SafeArray.h" + +int main(int argc, char** argv) { + SafeArray a(100); + a[0] = 0; + a[200] = 1; + return 0; +} diff --git a/practices/cpp/level1/p05_Canvas/canvas.cpp b/practices/cpp/level1/p05_Canvas/canvas.cpp new file mode 100644 index 00000000..93352125 --- /dev/null +++ b/practices/cpp/level1/p05_Canvas/canvas.cpp @@ -0,0 +1,173 @@ +#include +#include +#include "canvas.h" +using namespace std; + +Point::Point(double nx, double ny) +{ + this->x = nx; + this->y = ny; +} + +Circle::Circle(double r, double x, double y) +{ + Point p(x, y); + this->radius = r; + this->center = p; +} + +Circle::Circle(double r, Point &p) +{ + this->radius = r; + this->center = p; +} + +Rectan::Rectan(double l, double h, double x, double y, double the) +{ + Point p(x, y); + this->len = l; + this->hei = h; + this->theta = the; + this->center = p; +} + +Rectan::Rectan(double l, double h, Point &p, double the) +{ + this->len = l; + this->hei = h; + this->theta = the; + this->center = p; +} + +void Canvas::paint() +{ + puts("\nCircles:"); + if (circles.size() == 0) + { + puts("None"); + } + for (int i = 0; i < circles.size(); i++) + { + if (circles[i].radius != 0) + { + printf("%d.Circle: center:(%.2lf, %.2lf), radius: %.2lf\n", i+1, circles[i].center.x, circles[i].center.y, circles[i].radius); + } + } + + puts("\nRectangles:"); + if (rectans.size() == 0) + { + puts("None"); + } + for (int i = 0; i < rectans.size(); i++) + { + if (rectans[i].len != 0) + { + printf("%d.Rectanle: center:(%.2lf, %.2lf), length: %.2lf, height: %.2lf, theta: %.2lf\n", i+1, rectans[i].center.x, rectans[i].center.y, rectans[i].len, rectans[i].hei, rectans[i].theta); + } + } + puts(""); +} + +void Canvas::add() +{ + int mod = 0; + Circle newCir; + Rectan newRec; + printf("Please confirm the type of item you will add\n1.Circle\n2.Rectangle\n:"); + cin >> mod; + getchar(); + switch (mod) + { + case 1: + printf("Please enter radius: "); + std::cin >> newCir.radius; + printf("Please enter x and y of center point: "); + std::cin >> newCir.center.x >> newCir.center.y; + getchar(); + circles.push_back(newCir); + puts("Add success!"); + break; + case 2: + printf("Please enter length and height: "); + std::cin >> newRec.len >> newRec.hei; + printf("Please enter x and y of center point: "); + std::cin >> newRec.center.x >> newRec.center.y; + printf("Please enter theta: "); + std::cin >> newRec.theta; + getchar(); + rectans.push_back(newRec); + puts("Add success!"); + break; + default: + puts("Error: Invalid mode!"); + } + +} + +void Canvas::del() +{ + int mod = 0, serial = 0; + vector::iterator itc; + vector::iterator itr; + printf("Please confirm the type of item you will delete\n1.Circle\n2.Rectangle\n:"); + cin >> mod; + getchar(); + switch (mod) + { + case 1: + if (circles.size() == 0) + { + puts("Error: no circle!"); + break; + } + puts("\nLists: "); + for (int i = 0; i < circles.size(); i++) + { + if (circles[i].radius != 0) + { + printf("%d.Circle: center:(%.2lf, %.2lf), radius: %.2lf\n", i + 1, circles[i].center.x, circles[i].center.y, circles[i].radius); + } + } + puts("\nWhich one do you want to remove?"); + cin >> serial; + getchar(); + if (serial < 1 || serial > circles.size()) + { + puts("Error: Wrong serial number!"); + break; + } + itc = circles.begin() + serial - 1; + itc = circles.erase(itc); + puts("Delete success!"); + break; + case 2: + if (rectans.size() == 0) + { + puts("Error: no rectangle!"); + break; + } + puts("\nLists: "); + for (int i = 0; i < rectans.size(); i++) + { + if (rectans[i].len != 0) + { + printf("%d.Rectanle: center:(%.2lf, %.2lf), length: %.2lf, height: %.2lf, theta: %.2lf\n", i + 1, rectans[i].center.x, rectans[i].center.y, rectans[i].len, rectans[i].hei, rectans[i].theta); + } + } + puts("\nWhich one do you want to remove?"); + cin >> serial; + getchar(); + if (serial < 1 || serial > rectans.size()) + { + puts("Error: Wrong serial number!"); + break; + } + itr = rectans.begin() + serial - 1; + itr = rectans.erase(itr); + puts("Delete success!"); + break; + default: + puts("Error: Invalid mode!"); + } +} \ No newline at end of file diff --git a/practices/cpp/level1/p05_Canvas/canvas.h b/practices/cpp/level1/p05_Canvas/canvas.h new file mode 100644 index 00000000..4f3b6326 --- /dev/null +++ b/practices/cpp/level1/p05_Canvas/canvas.h @@ -0,0 +1,57 @@ +#include +using namespace std; + +class Point; +class Circle; +class Rectan; +class Canvas; + +class Point +{ +private: + double x; + double y; + +public: + Point() {}; + Point(double nx, double ny); + friend Canvas; +}; + +class Circle +{ +private: + Point center; + double radius; +public: + Circle() {}; + Circle(double r, double x, double y); + Circle(double r, Point &p); + friend Canvas; +}; + +class Rectan +{ +private: + Point center; + double len; + double hei; + double theta; +public: + Rectan() {}; + Rectan(double l, double h, double x, double y, double the); + Rectan(double l, double h, Point &p, double the = 0); + friend Canvas; +}; + +class Canvas +{ +private: + vector circles; + vector rectans; +public: + Canvas() {}; + void paint(); + void add(); + void del(); +}; \ No newline at end of file diff --git a/practices/cpp/level1/p05_Canvas/main.cpp b/practices/cpp/level1/p05_Canvas/main.cpp new file mode 100644 index 00000000..af854484 --- /dev/null +++ b/practices/cpp/level1/p05_Canvas/main.cpp @@ -0,0 +1,78 @@ +/* +* Author: Kyrios0 +* Date: 2016.04 +* To-Do List: +* ~~Circle&Rectan~~ +* ~~paint~~ +* ~~add~~ + * addCir + * addRec +* ~~del~~ + * delCir + * delRec +* ~~help~~ +* Optimization +* and more? +* State: Processing +*/ +#include +#include +#include +#include "canvas.h" + +void invalid(char *cm) +{ + cm[strlen(cm) - 1] = '\0'; + printf("\"%s\" isn't a valid command, Type \"help\" for more information.\n", cm); +} + +void help() +{ + puts("\n\tGNU bash, Version 4.3.42(1)-release (x86_64-pc-kyanvas-gnu)\n\n\ + \tCommand\t\tInfo\n\ + \tadd\t\tAdd a circle or rectangle\n\ + \tdel\t\tDelete an item\n\ + \tpaint\t\tDisplay all items\n\ + \tquit\t\tQuit\n"); +} + +int main(int argc, char** argv) { + char cm[128] = "Kyrios"; + puts("Kyanvas 1.0.0 (default, Apr 25 2017, 09:18:07) [MSC v.1500 64 bit (AMD64)] on win64"); + puts("Type \"help\" for more information."); + Canvas canvas; + while (1) + { + printf("root@user: "); + fgets(cm, 128, stdin); + if (!strcmp(cm, "help\n")) + { + help(); + } + else if (!strcmp(cm, "paint\n")) + { + canvas.paint(); + } + else if (!strcmp(cm, "add\n")) + { + canvas.add(); + } + else if (!strcmp(cm, "del\n")) + { + canvas.del(); + } + else if (!strcmp(cm, "quit\n")) + { + break; + } + else + { + invalid(cm); + } + + } + puts("Kyrios hope you'll have a nice day, bye."); + puts("Press any key to quit..."); + _getch(); + return 0; +} \ No newline at end of file diff --git a/practices/cpp/level1/p06_CircleAndPoint/circle.cpp b/practices/cpp/level1/p06_CircleAndPoint/circle.cpp new file mode 100644 index 00000000..a4af9d84 --- /dev/null +++ b/practices/cpp/level1/p06_CircleAndPoint/circle.cpp @@ -0,0 +1,42 @@ +#include "circle.h" + +Point::Point(double nx, double ny) +{ + this->x = nx; + this->y = ny; +} + +void Point::move(double nx, double ny) +{ + this->x += nx; + this->y += ny; +} + +void Point::reset(double nx, double ny) +{ + this->x = nx; + this->y = ny; +} + +Circle::Circle(double r, double x, double y) +{ + Point p(x, y); + this->radius = r; + this->center = p; +} + +Circle::Circle(double r, Point &p) +{ + this->radius = r; + this->center = p; +} + +void Circle::move(double cx, double cy) +{ + (this->center).move(cx, cy); +} + +void Circle::moveTo(double nx, double ny) +{ + (this->center).reset(nx, ny); +} diff --git a/practices/cpp/level1/p06_CircleAndPoint/circle.h b/practices/cpp/level1/p06_CircleAndPoint/circle.h new file mode 100644 index 00000000..5fd6a05e --- /dev/null +++ b/practices/cpp/level1/p06_CircleAndPoint/circle.h @@ -0,0 +1,26 @@ + +class Point +{ + private: + double x; + double y; + + public: + Point(){}; + Point(double nx, double ny); + void move(double nx, double ny); + void reset(double nx, double ny); + +}; + +class Circle +{ + private: + Point center; + double radius; + public: + Circle(double r, double x, double y); + Circle(double r, Point &p); + void move(double cx, double cy); + void moveTo(double nx, double ny); +}; diff --git a/practices/cpp/level1/p06_CircleAndPoint/main.cpp b/practices/cpp/level1/p06_CircleAndPoint/main.cpp new file mode 100644 index 00000000..cba2999e --- /dev/null +++ b/practices/cpp/level1/p06_CircleAndPoint/main.cpp @@ -0,0 +1,20 @@ +/* + * Author: Kyrios0 + * Date: 2016.04 + * To-Do List: + * Circle + * Point + * State: Processing + */ +#include +#include "circle.h" +using namespace std; + + +int main(int argc, char** argv) { + Point p1(2.0, 2.0); + Circle ra(5.0, p1); + ra.move(1.0, 2.0); + ra.moveTo(3.0, 3.0); + return 0; +} diff --git a/practices/cpp/level1/p08_EmployeeAndSales/employee.cpp b/practices/cpp/level1/p08_EmployeeAndSales/employee.cpp new file mode 100644 index 00000000..de6eca72 --- /dev/null +++ b/practices/cpp/level1/p08_EmployeeAndSales/employee.cpp @@ -0,0 +1,27 @@ +#include "employee.h" +#include +Employee::Employee(char *employeeName, int employeeLevel) +{ + strncpy(name, employeeName); + level = employeeLevel; +} + +virtual int Employee::getSalary() +{ + return level*1000; +} + +char* Employee::getName() +{ + return name; +} + +int Employee::getLevel() + { + return level; + } + +void setLevel(int employeeLevel) + { + level = employeeLevel; + } diff --git a/practices/cpp/level1/p08_EmployeeAndSales/employee.h b/practices/cpp/level1/p08_EmployeeAndSales/employee.h new file mode 100644 index 00000000..6e0b789f --- /dev/null +++ b/practices/cpp/level1/p08_EmployeeAndSales/employee.h @@ -0,0 +1,15 @@ +class Employee +{ + public: + Employee(){ }; + Employee(char *employeeName, int employeeLevel); + virtual ~Employee(); + virtual int getSalary(); + char* getName(); + int getLevel(); + void setLevel(int employeeLevel); + protected: + private: + char* name = new char[32]; + int level = 0; +}; diff --git a/practices/cpp/level1/p08_EmployeeAndSales/main.cpp b/practices/cpp/level1/p08_EmployeeAndSales/main.cpp new file mode 100644 index 00000000..ecc7d2e5 --- /dev/null +++ b/practices/cpp/level1/p08_EmployeeAndSales/main.cpp @@ -0,0 +1,26 @@ + +#include +#include "employee.h" +#include "sales.h" + +using namespace std; + +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char** argv) { + + Employee woman("Alice", 1); + char* name = woman.getName(); + int salary = woman.gatSalary(); + cout << name << endl; + cout << salary << endl; + + Sales sale("Bob", 2); + name = sale.getName(); + sale.setSaleCount(2000); + salary = sale.calcSalary(); + cout << name << endl; + cout << salary << endl; + return 0; +} + diff --git a/practices/cpp/level1/p08_EmployeeAndSales/sales.cpp b/practices/cpp/level1/p08_EmployeeAndSales/sales.cpp new file mode 100644 index 00000000..4db871f5 --- /dev/null +++ b/practices/cpp/level1/p08_EmployeeAndSales/sales.cpp @@ -0,0 +1,17 @@ +#include "sales.h" +#include +Sales::Sales(char* salesName, int salesLevel) +{ + strncpy(name, salesName); + level = employeeLevel; +} + +void Sales::setSaleCount(int Count) +{ + saleCount = Count; +} + +virtual int Sales::getSalary() +{ + return level*1000 + saleCount/5; +} diff --git a/practices/cpp/level1/p08_EmployeeAndSales/sales.h b/practices/cpp/level1/p08_EmployeeAndSales/sales.h new file mode 100644 index 00000000..85253e33 --- /dev/null +++ b/practices/cpp/level1/p08_EmployeeAndSales/sales.h @@ -0,0 +1,11 @@ +#include "employee.h" +class Sales : public Employee { + public: + Sales(char* salesName, int salesLevel); + virtual ~Sales(); + void setSaleCount(int Count); + virtual int getSalary(); + protected: + private: + int saleCount; +}; diff --git a/practices/cpp/level1/p09_Tree/main.cpp b/practices/cpp/level1/p09_Tree/main.cpp new file mode 100644 index 00000000..e4baeb32 --- /dev/null +++ b/practices/cpp/level1/p09_Tree/main.cpp @@ -0,0 +1,12 @@ +#include +#include "tree.h" +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char** argv) { + Node* root = new Node(1); + Node* leftChild = new Node(2); + Node* rightChild = new Node(3); + root->append(leftChild); + root->append(rightChild); + return 0; +} diff --git a/practices/cpp/level1/p09_Tree/tree.cpp b/practices/cpp/level1/p09_Tree/tree.cpp new file mode 100644 index 00000000..0485280f --- /dev/null +++ b/practices/cpp/level1/p09_Tree/tree.cpp @@ -0,0 +1,27 @@ +#include "tree.h" +Node::Node(int value) +{ + data = value; +} + +void Node::append(Node *node) +{ + node->parent = this; + childrenNodes.push_back(node); + childCount ++; +} + +int Node::count() +{ + return count; +} + +Node* Node::getParent() +{ + return parent; +} + +int Node::getValue() +{ + return data; +} diff --git a/practices/cpp/level1/p09_Tree/tree.h b/practices/cpp/level1/p09_Tree/tree.h new file mode 100644 index 00000000..ec1ae14a --- /dev/null +++ b/practices/cpp/level1/p09_Tree/tree.h @@ -0,0 +1,17 @@ +#include + +class Node { + public: + Node(int value); + virtual ~Node(); + void append(Node *node); + int count(); + Node* getParent(); + int getValue(); + protected: + private: + int data; + int childCount = 0; + std::vector childrenNodes; + Node *parent; +}; diff --git a/practices/cpp/level1/p11_Fighters/FO.cpp b/practices/cpp/level1/p11_Fighters/FO.cpp new file mode 100644 index 00000000..4780c6c6 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/FO.cpp @@ -0,0 +1,189 @@ +#include "FO.h" +#include +#include +#include +#include + +FO::FO() +{ + phase = 1; + bounds = 0; + staticFrame = 0; + dynamicFrame = 0; + width = 0; + height = 0; + score = 0; + damage = 1.0; + isSym = false; + HealthPoint = 1; +} + +FO::FO(int flag) +{ + phase = 1; + bounds = 0; + staticFrame = 0; + dynamicFrame = 0; + width = 0; + height = 0; + score = 0; + damage = 1.0; + isSym = false; + HealthPoint = 1; + + switch (flag) + { + case 0://title + type = 0; + width = 512; + height = 256; + speed = 0.0; + theta = 0.0; + velocity = sf::Vector2f(0.0, 0.0); + case 1://player + type = 1; + if (!Reimu.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\player\\pl00\\pl00.png")) + { + puts("Error: Load Reimu failed!"); + } + if (!Marisa.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\player\\pl01\\pl01.png")) + { + puts("Error: Load Marisa failed!"); + } + if (!Sanae.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\player\\pl02\\pl02.png")) + { + puts("Error: Load Marisa failed!"); + } + HealthPoint = 1; + damage = 1.0; + width = 48; + height = 72; + speed = 10.0; + hero.setTexture(Reimu); + hero.setTextureRect(sf::IntRect(0, 0, width, height)); + hero.setPosition(sf::Vector2f(430, 820)); + HSAmmo.setTexture(Reimu); + HSAmmo.setTextureRect(sf::IntRect(0, 3 * height + 0, 25, 24)); + HSAmmo.setRotation(270); + LSAmmo.setTexture(Reimu); + LSAmmo.setTextureRect(sf::IntRect(4 * width, 3 * height + 48, 2 * width, 24)); + LSAmmo.setRotation(270); + break; + case 2://sButterfly + type = 2; + // Load an enemy to display + /*if (!Enemy1.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\enemy\\enemy.png")) + { + puts("Error: Load enemy1 failed!"); + } + if (!Enemy2.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\enemy\\enemy.png")) + { + puts("Error: Load enemy2 failed!"); + }*/ + HealthPoint = 5; + width = 32; + height = 32; + speed = 2; + score = 50; + //hero.setTexture(Enemy1); + //hero.setTextureRect(sf::IntRect(0, 64, width, height)); + //hero.setScale(sf::Vector2f(1.5, 1.5)); + //printf("%x\n", hero); + break; + case 3://sFairy + type = 3; + width = 48; + height = 32; + score = 200; + break; + case 4://mFairy + type = 4; + width = 48; + height = 48; + score = 1000; + break; + case 5://mButterfly + HealthPoint = 100; + type = 5; + width = 64; + height = 64; + score = 1000; + break; + case 6://Ghost + HealthPoint = 1340; + type = 6; + width = 32; + height = 32; + score = 10000; + phase = 2; + break; + } +} + +/*void FO::setSButterfly(double x, double y) +{ + if (!Enemy1.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\enemy\\enemy.png")) + { + puts("Error: Load enemy1 failed!"); + } + if (!Enemy2.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\enemy\\enemy.png")) + { + puts("Error: Load enemy2 failed!"); + } + hero.setTexture(Enemy1); + hero.setTextureRect(sf::IntRect(0, 320, width, height)); + hero.setScale(sf::Vector2f(1.5, 1.5)); + hero.setPosition(sf::Vector2f(x, y)); +}*/ + + +sf::Vector2f FO::getJulgeArea() +{ + float x, y; + if (type == 1) + { + x = hero.getPosition().x - 24; + y = hero.getPosition().y - 8; + } + else + { + x = hero.getPosition().x; + y = hero.getPosition().y; + } + return sf::Vector2f(x, y); +} + +FO::~FO() +{ +} + +/*Player::Player() +{ + staticFrame = 0; + dynamicFrame = 0; + // Load a player to display + if (!Reimu.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\player\\pl00\\pl00.png")) + { + puts("Error: Load Reimu failed!"); + } + if (!Marisa.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\player\\pl01\\pl01.png")) + { + puts("Error: Load Marisa failed!"); + } + if (!Sanae.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\player\\pl02\\pl02.png")) + { + puts("Error: Load Marisa failed!"); + } + width = 48; + height = 72; + speed = 10.0; + hero.setTexture(Reimu); + hero.setTextureRect(sf::IntRect(0, 0, width, height)); + hero.setPosition(sf::Vector2f(400, 820)); + HSAmmo.setTexture(Reimu); + HSAmmo.setTextureRect(sf::IntRect(0, 3 * height + 0, 2.0 / 3.0 * width, 24)); + HSAmmo.setRotation(270); + LSAmmo.setTexture(Reimu); + LSAmmo.setTextureRect(sf::IntRect(4 * width, 3 * height + 48, 2 * width, 24)); + LSAmmo.setRotation(270); +}*/ diff --git a/practices/cpp/level1/p11_Fighters/FO.h b/practices/cpp/level1/p11_Fighters/FO.h new file mode 100644 index 00000000..5c7c30bf --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/FO.h @@ -0,0 +1,25 @@ +#pragma once +#include +#include +#include +#include +class FO +{ +public: + FO(); + FO(int flag); + void setSButterfly(double x, double y); + sf::Vector2f getJulgeArea(); + ~FO(); + + +public: + sf::Vector2f velocity; + sf::Sprite hero, HSAmmo, LSAmmo; + sf::Texture Reimu, Marisa, Sanae, Enemy1, Enemy2; + int HealthPoint, Score, width, height, staticFrame, dynamicFrame, phase; + int type, gap, born, score, bounds; + double damage, speed, theta; + bool isSym; +}; + diff --git a/practices/cpp/level1/p11_Fighters/Game.cpp b/practices/cpp/level1/p11_Fighters/Game.cpp new file mode 100644 index 00000000..939d27bf --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Game.cpp @@ -0,0 +1,3274 @@ +#include "Game.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define PI 3.1415926 +#define EPS 0.00001 +using namespace std; + +Game::Game() + :mWindow(sf::VideoMode(1280, 960), "TouHou20.0-chs") + , font() + , player(1) +{ + NowLoading(); + + loadBackgrounds(); + + srand((unsigned)time(NULL)); + remnant = 3; + score = 0; + mIsMovingUp = false; + mIsMovingDown = false; + mIsMovingLeft = false; + mIsMovingRight = false; + mIsGrazing = false; + mIsFire = false; + + loadPrimeFrame(); + + loadPointsAndEffs(); + + loadEnemy(); + + // Create a graphical text to display + if (!font.loadFromFile("C:\\Users\\Administrator\\Documents\\Rainmeter\\Skins\\Ultralight\\@Resources\\Fonts\\RobotoCondensed-Regular.ttf")) + { + puts("Error: Load RobotoCondensed-Regular.ttf failed!"); + } + text.setString("Testing..."); + text.setFont(font); + text.setCharacterSize(50); + + loadMusicAndSounds(); + +} +// +void Game::NowLoading() +{ + // Create the loading window + if (!loading.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\loading\\sig.png")) + { + puts("Error: Load loading failed!"); + } + if (!nowLoading.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\ascii\\loading.png", sf::IntRect(0, 0, 128, 64))) + { + puts("Error: Load nowloading failed!"); + } + loadingUI.setTexture(loading); + loadingUISub.setTexture(nowLoading); + loadingUISub.setScale(1.5, 1.5); + loadingUISub.setPosition(sf::Vector2f(1000, 800)); + mWindow.clear(); + mWindow.draw(loadingUI); + mWindow.draw(loadingUISub); + mWindow.display(); +} +// +void Game::loadBackgrounds() +{ + if (!bg1.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\background\\stage02\\stage02a.png")) + { + puts("Error: Load stage02a failed!"); + } + if (!bgEff1.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\background\\stage02\\stage02b.png")) + { + puts("Error: Load stage02b failed!"); + } + if (!bg2.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\background\\stage07\\stage02a.png")) + { + puts("Error: Load stage3bg failed!"); + } + if (!bgEff2.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\background\\stage07\\stage02b.png")) + { + puts("Error: Load stage3bg failed!"); + } + for (int i = 0; i < 6; i++) + { + back[i].setTexture(bg1); + back[i].setScale(sf::Vector2f(1.5, 1.5)); + back[i].setPosition((float)65.0, (float)(i - 1)*192.0 + 35.0); + backEff[i].setTexture(bgEff1); + backEff[i].setScale(sf::Vector2f(1.5, 1.5)); + backEff[i].setPosition(65.0, (i - 1)*384.0 + 35.0); + } + + /*for (int i = 0; i < 6; i++) + { + back[i].setTexture(bg2); + back[i].setScale(sf::Vector2f(3.0, 3.0)); + back[i].setPosition(65.0, (i - 1)*4*192.0 + 35.0); + backEff[i].setTexture(bgEff2); + backEff[i].setScale(sf::Vector2f(6.0, 6.0)); + backEff[i].setPosition(65.0, (i - 1)*384.0/2.0 + 35.0); + }*/ + + if (!lifePieces.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\front\\lifePieces.png")) + { + puts("Error: Load lifePieces failed!"); + } + lifeBoard.setTexture(lifePieces); + + if (!Title1.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\front\\logo\\stg01logo.png")) + { + puts("Error: Load stg01logo failed!"); + } + if (!Title2.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\front\\front01.png")) + { + puts("Error: Load clear failed!"); + } + if (!whiteSpark.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\front\\White.png")) + { + puts("Error: Load White failed!"); + } +} +// +void Game::loadPrimeFrame() +{ + // Load a windowsframe to display + if (!front00.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\front\\front00.png")) + { + puts("Error: Load front failed!"); + } + front01.setTexture(front00); + front01.setTextureRect(sf::IntRect(65, 0, 490, 1280)); + front01.setPosition(sf::Vector2f(795, 0)); + front02.setTexture(front00); + front02.setTextureRect(sf::IntRect(69, 1030, 730, 39)); + front02.setPosition(sf::Vector2f(65, 0)); + front03.setTexture(front00); + front03.setTextureRect(sf::IntRect(69, 1062, 739, 38)); + front03.setPosition(sf::Vector2f(65, 922)); + front04.setTexture(front00); + front04.setTextureRect(sf::IntRect(0, 0, 70, 1280)); + front04.setPosition(sf::Vector2f(0, 0)); +} +// +void Game::loadPointsAndEffs() +{ + if (!julgePointArray.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\bullet\\etama2.png"))//into player + { + puts("Error: Load julgePointArray failed!"); + } + + julgePoint.setTexture(julgePointArray); + julgePoint.setTextureRect(sf::IntRect(0, 16, 64, 64)); + julgePoint.setScale(1.5, 1.5); + + if (!bullets.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex\\bullet\\etama.png")) + { + puts("Error: Load bullets failed!"); + } + if (!buffetsEff.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\enemy\\enemy_aura.png")) + { + puts("Error: Load buffetsEff failed!"); + } + if (!deathCircle.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\effect\\eff_deadcircle.png")) + { + puts("Error: Load deathCircle failed!"); + } + if (!allBullets1.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\bullet\\bullet1.png")) + { + puts("Error: Load bullet1 failed!"); + } + if (!allBullets2.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\bullet\\bullet2.png")) + { + puts("Error: Load bullet2 failed!"); + } + if (!magicSquare.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\effect\\eff_magicsquare.png")) + { + puts("Error: Load magicsquare failed!"); + } +} +// +void Game::loadEnemy() +{ + // Load a enemy to display + if (!Enemy1.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\enemy\\enemy.png")) + { + puts("Error: Load enemy1 failed!"); + } + if (!Enemy2.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\enemy\\enemy2.png")) + { + puts("Error: Load enemy2 failed!"); + } + if (!Enemy3.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.15\\enemy\\enemy5.png")) + { + puts("Error: Load enemy3 failed!"); + } +} +// +void Game::loadMusicAndSounds() +{ + if (!menuMusic.openFromFile("C:\\Users\\Administrator\\Documents\\Visual Studio 2017\\Projects\\Canvas\\x64\\Debug\\ϺꥹØ - ˼hʤ𤤰.wav")) + { + puts("Error: Open ϺꥹØ - ˼hʤ𤤰.wav failed!"); + } + if (!stage1BGM.openFromFile("C:\\Users\\Administrator\\Documents\\Visual Studio 2017\\Projects\\Canvas\\x64\\Debug\\ϺꥹØ - δդ.wav")) + { + puts("Error: Open ϺꥹØ - δդ.wav failed!"); + } + if (!stage2BGM.openFromFile("C:\\Users\\Administrator\\Documents\\Visual Studio 2017\\Projects\\Canvas\\x64\\Debug\\ϺꥹØ - xᘤС Little Princess.wav")) + { + puts("Error: Open ϺꥹØ - xᘤС Little Princess Dark Road.wav failed!"); + } + if (!stage3BGM.openFromFile("C:\\Users\\Administrator\\Documents\\Visual Studio 2017\\Projects\\Canvas\\x64\\Debug\\ϺꥹØ - 񡩤_.wav")) + { + puts("Error: Open ϺꥹØ - 񡩤_.wav failed!"); + } + if (!playerBulletSoundBuffer.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\origin.15\\se_damage00.wav")) + { + puts("Error: Open se_damage00.wav failed!"); + } + playerBulletSound.setBuffer(playerBulletSoundBuffer); + playerBulletSound.setVolume(50); + if (!enemyBulletSoundBuffer.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\origin.12\\se_tan00.wav")) + { + puts("Error: Open se_tan00.wav failed!"); + } + enemyBulletSound.setBuffer(enemyBulletSoundBuffer); + enemyBulletSound.setVolume(15); + if (!breakSoundBuffer.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\origin.12\\se_enep00.wav")) + { + puts("Error: Open se_enep00.wav failed!"); + } + breakSound.setBuffer(breakSoundBuffer); + if (!playerDeadSoundBuffer.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\origin.15\\se_pldead00.wav")) + { + puts("Error: Open se_pldead00.wav failed!"); + } + playerDeadSound.setBuffer(playerDeadSoundBuffer); + if (!SCAnounceBuffer.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.143\\se_cat00.wav")) + { + puts("Error: Open se_cat00.wav failed!"); + } + SCAnounce.setBuffer(SCAnounceBuffer); + if (!cardGetBuffer.loadFromFile("E:\\Media\\Sources\\Jpg&Png\\TH\\Ex.143\\se_cardget.wav")) + { + puts("Error: Open se_cardget.wav failed!"); + } + cardGet.setBuffer(cardGetBuffer); +} + +void Game::run() +{ + // Play the music + //menuMusic.play(); + //menuMusic.setLoop(true); + //Menu(); + int level = 1; + switch (level) + { + case 1: + stage1BGM.play(); + stage1BGM.setLoop(true); + break; + } + // Start the game loop + mWindow.setFramerateLimit(60); + mWindow.draw(player.hero);//updating + frameDisplay(); + /*HANDLE hThread_1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)BGMPlay, self, 0, NULL);*/ + while (mWindow.isOpen()) + { + mWindow.clear(); + + switch (level) + { + case 1: + Stage1(); + break; + } + processTaps(); + mainProcessing(); + + frameDisplay(); + } +} + +void Game::Stage1() +{ + static sf::Time elapsed1 = clock.restart(); + elapsed1 = clock.getElapsedTime(); + + static int evts[20] = { 0 }; + + static int curTime = 1; + if (curTime < elapsed1.asSeconds()) + { + printf("%.0f\n", elapsed1.asSeconds()); + curTime++; + } + + switch ((int)elapsed1.asSeconds() + 0)//for testing + { + case 1: + //pre + evts[1] = 1; + break; + case 12: + //title + evts[2] = 1; + break; + case 17: + evts[3] = 1; + break; + case 23: + evts[4] = 1; + break; + case 29: + evts[5] = 1; + break; + case 36: + //wave + evts[6] = 1; + break; + case 42: + evts[7] = 1; + break; + case 49: + //middle + evts[8] = 1; + break; + case 75: + //reinforce + evts[9] = 1; + break; + case 81: + evts[10] = 1; + break; + case 90: + evts[11] = 1; + break; + case 100: + //Scene switching + evts[12] = 1; + break; + case 105: + //boss + evts[13] = 1; + break; + } + + if (evts[1]) + { + if (S1E1()) + { + evts[1] = 0; + } + } + if (evts[2]) + { + if (S1E2()) + { + evts[2] = 0; + } + } + if (evts[3]) + { + if (S1E3()) + { + evts[3] = 0; + } + } + if (evts[4]) + { + if (S1E4()) + { + evts[4] = 0; + } + } + if (evts[5]) + { + if (S1E5()) + { + evts[5] = 0; + } + } + if (evts[6]) + { + if (S1E6()) + { + evts[6] = 0; + } + } + if (evts[7]) + { + if (S1E7()) + { + evts[7] = 0; + } + } + if (evts[8]) + { + if (S1E8()) + { + evts[8] = 0; + } + } + if (evts[9] && !evts[8]) + { + if (S1E9()) + { + evts[9] = 0; + } + } + if (evts[10]) + { + if (S1E10()) + { + evts[10] = 0; + } + } + if (evts[11]) + { + if (S1E11()) + { + evts[11] = 0; + } + } + if (evts[12]) + { + if (S1E12()) + { + evts[12] = 0; + } + } + if (evts[13]) + { + if (S1E13()) + { + evts[13] = 0; + evts[14] = 1; + } + } + if (evts[14]) + { + if (S1E14()) + { + evts[14] = 0; + } + } +} + +int Game::S1E1() +{ + static int i1 = 0; + i1++; + static list wave1, wave2; + double gapTime = 0.4; + int gapFrame = gapTime * 60; + static int gap = 0, temp = 0; + + if (i1 % gapFrame == 1 && i1 < 15 * gapFrame) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320, sButterfly.width, sButterfly.height)); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(400 + pow(-1.0, i1 / gapFrame) * 0.8 * i1, 20.0)); + //sButterfly.setSButterfly(400 + pow(-1.0, i1 / gapFrame) * i1, 20.0); + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + //printf("Now %x\n", sButterfly.hero); + wave1.push_back(sButterfly); + } + if (i1 == 270) + { + FO mButterfly(5); + mButterfly.hero.setTexture(Enemy1); + mButterfly.hero.setTextureRect(sf::IntRect(0, 448, mButterfly.width, mButterfly.height)); + mButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + mButterfly.hero.setOrigin(mButterfly.width*0.5, mButterfly.height*0.5 - 24); + mButterfly.hero.setPosition(sf::Vector2f(400 + 200, -20.0)); + mButterfly.born = i1; + wave2.push_back(mButterfly); + mButterfly.hero.setPosition(sf::Vector2f(400 - 200, -20.0)); + wave2.push_back(mButterfly); + } + + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + + temp = i1 - it->gap * gapFrame; + if (temp < 200)//phase1 + { + if (rand() % 20 == 0) + { + setMultiRoundSnipe(it, 5.0, 5); + } + it->speed = 50.0 / (temp + 1.0); + it->theta = 0.5*PI; + it->hero.setTextureRect(sf::IntRect(i1 % 35 / 7 * it->width, 320, it->width, it->height)); + } + else//phase2 + { + it->speed = (temp - 200) / 10.0; + it->theta = 0.5 * PI + pow(-1.0, it->gap + 1.0) * 10.0*PI / 360.0; + + if (it->speed <= 11) + { + it->hero.setTextureRect(sf::IntRect((int)it->speed * it->width, 320, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect((int)11 * it->width, 320, it->width, it->height)); + } + if (it->theta > PI / 2.0) + { + it->hero.setScale(-1.5, 1.5); + + if (!it->isSym) + { + it->hero.move(1.5*it->width, 0); + it->isSym = true; + } + } + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + wave2.remove_if(isFOOutOfBoard); + for (list::iterator it = wave2.begin(); it != wave2.end(); it++) + { + it->hero.setTextureRect(sf::IntRect(i1 % 50 / 10 * it->width, 448, it->width, it->height)); + if (i1 < 400) + { + it->speed = (650.0 - i1) / 240; + it->theta = 0.5 * PI; + } + else if (i1 >= 400 && i1 < 500) + { + it->speed = 0.0; + + setSharpRandom(it, 3.0); + setSharpRandom(it, 3.0); + + } + else + { + it->speed = (i1 - 500) / 60.0; + it->theta = 1.5 * PI; + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + if (i1 > 15 * 60) + { + wave1.clear();//Final clear for accident + wave2.clear(); + return 1; + } + return 0; +} + +int Game::S1E2() +{ + static int i1 = 0; + i1++; + static list wave1; + + if (i1 == 1) + { + FO mainTitle(0); + mainTitle.hero.setTexture(Title1); + mainTitle.hero.setTextureRect(sf::IntRect(0, 0, 512, 256)); + //mainTitle.hero.setScale(sf::Vector2f(1.5, 1.5)); + mainTitle.speed = 0.0; + mainTitle.hero.setPosition(sf::Vector2f(200.0, 150.0)); + mainTitle.hero.setColor(sf::Color(255, 255, 255, 0)); + + wave1.push_back(mainTitle); + } + + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + it->hero.setColor(sf::Color(255, 255, 255, -i1*(i1-301) / (151.0*150.0) * 255)); + enemiesPushToDraw(it); + } + if (i1 > 5 * 60) + { + wave1.clear();//Final clear for accident + return 1; + } + return 0; +} + +int Game::S1E3() +{ + static int i1 = 0; + i1++; + static list wave1, wave2; + double gapTime = 0.2; + int gapFrame = gapTime * 60; + static int gap = 0, temp = 0; + if (i1 % gapFrame == 1 && i1 < 15 * gapFrame) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320, sButterfly.width, sButterfly.height)); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(600.0, 20.0)); + sButterfly.speed = 3.0; + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + + wave1.push_back(sButterfly); + } + if (i1 == 21) + { + FO mButterfly(5); + mButterfly.hero.setTexture(Enemy1); + mButterfly.hero.setTextureRect(sf::IntRect(0, 448, mButterfly.width, mButterfly.height)); + mButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + mButterfly.hero.setOrigin(mButterfly.width*0.5, mButterfly.height*0.5 - 24); + mButterfly.born = i1; + mButterfly.hero.setPosition(sf::Vector2f(200.0, -20.0)); + wave2.push_back(mButterfly); + } + + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + + temp = i1 - it->gap * gapFrame; + + if (rand() % 5 == 0) + { + setRoundRandom(it, 4.0, 5, 60, 90);//setMultiRoundSnipe(it, 8.0, 5); + } + it->theta = 0.5*PI + temp * temp / 20000.0 * 0.2 * PI; + if (it->theta >= 0.9 * PI) + { + it->theta = 0.9 * PI; + } + if (it->theta <= 0.7 * PI) + { + it->hero.setTextureRect(sf::IntRect((int)(it->theta * 10.0 / PI) * it->width, 320, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect((int)(temp % 4 + 8) * it->width, 320, it->width, it->height)); + } + + if (it->theta > PI / 2.0) + { + it->hero.setScale(-1.5, 1.5); + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + wave2.remove_if(isFOOutOfBoard); + for (list::iterator it = wave2.begin(); it != wave2.end(); it++) + { + it->hero.setTextureRect(sf::IntRect(i1 % 50 / 10 * it->width, 448, it->width, it->height)); + if (i1 < 150) + { + it->speed = (400.0 - i1) / 240; + it->theta = 0.5 * PI; + } + else if (i1 >= 150 && i1 < 250) + { + it->speed = 0.0; + for (int i = 0; i < 4; i++) + { + setSharpRandom(it, 3.0); + } + } + else + { + it->speed = (i1 - 250) / 60.0; + it->theta = 1.5 * PI; + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + if (i1 > 10 * 60) + { + wave1.clear();//Final clear for accident + wave2.clear(); + return 1; + } + return 0; +} + +int Game::S1E4() +{ + static int i1 = 0; + i1++; + static list wave1, wave2, wave3; + double gapTime = 0.2; + int gapFrame = gapTime * 60; + static int gap = 0, temp = 0; + + if (i1 % gapFrame == 1 && i1 < 10 * gapFrame) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320, sButterfly.width, sButterfly.height)); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(700.0, 960.0)); + sButterfly.speed = 3.0; + //sButterfly.setSButterfly(400 + pow(-1.0, i1 / gapFrame) * i1, 20.0); + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + //printf("Now %x\n", sButterfly.hero); + + wave1.push_back(sButterfly); + } + if (i1 % gapFrame == 1 && (i1 - 3 * 60) < 10 * gapFrame && (i1 - 3 * 60) > 0) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320 + 3 * sButterfly.height, sButterfly.width, sButterfly.height)); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(200.0, 20.0)); + sButterfly.speed = 3.0; + //sButterfly.setSButterfly(400 + pow(-1.0, i1 / gapFrame) * i1, 20.0); + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + //printf("Now %x\n", sButterfly.hero); + + wave2.push_back(sButterfly); + } + if (i1 % gapFrame == 1 && (i1 - 3 * 60) < 10 * gapFrame && (i1 - 3 * 60) > 0) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320, sButterfly.width, sButterfly.height)); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(200.0, 960.0)); + sButterfly.speed = 3.0; + //sButterfly.setSButterfly(400 + pow(-1.0, i1 / gapFrame) * i1, 20.0); + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + //printf("Now %x\n", sButterfly.hero); + + wave3.push_back(sButterfly); + } + + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + + temp = i1 - it->gap * gapFrame; + + if (rand() % 10 == 0 && it->hero.getPosition().y < 400) + { + setRandom(it, 4.0, 1, 5, 60, 90); //setRoundRandom(it, 4.0, 5, 60, 90);//setMultiRoundSnipe(it, 8.0, 5); + } + + if (it->hero.getPosition().y >= 400) + { + it->theta = -0.5*PI; + } + else if (it->hero.getPosition().y < 400) + { + it->theta -= (0.5 * PI / 180.0); + } + + if (it->theta >= -0.7 * PI && it->hero.getPosition().y >= 400) + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 8 % 4) * it->width, 320, it->width, it->height)); + } + else if (it->theta >= -0.7 * PI && it->hero.getPosition().y < 400) + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 32 % 4 + 3) * it->width, 320, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 8 % 4 + 8) * it->width, 320, it->width, it->height)); + } + + if (it->theta < PI / 2.0) + { + it->hero.setScale(-1.5, 1.5); + } + //it->theta = -it->theta; + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + wave2.remove_if(isFOOutOfBoard); + for (list::iterator it = wave2.begin(); it != wave2.end(); it++) + { + + temp = i1 - it->gap * gapFrame; + + if (rand() % 20 == 0) + { + setRandom(it, 4.0, 1, 5, 60, 90); //setRoundRandom(it, 4.0, 5, 60, 90);//setMultiRoundSnipe(it, 8.0, 5); + } + it->theta = 0.5*PI - temp * temp / 20000.0 * 0.2 * PI; + if (it->theta <= 0.1 * PI) + { + it->theta = 0.1 * PI; + } + if (it->theta >= 0.3 * PI) + { + it->hero.setTextureRect(sf::IntRect((int)(it->theta * 10.0 / PI) * it->width, 320 + 3 * it->height, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect((int)(temp % 4 + 8) * it->width, 320 + 3 * it->height, it->width, it->height)); + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + wave3.remove_if(isFOOutOfBoard); + for (list::iterator it = wave3.begin(); it != wave3.end(); it++) + { + + temp = i1 - it->gap * gapFrame; + + if (rand() % 10 == 0 && it->hero.getPosition().y < 400) + { + setRandom(it, 4.0, 1, 5, 60, 90); //setRoundRandom(it, 4.0, 5, 60, 90);//setMultiRoundSnipe(it, 8.0, 5); + } + + if (it->hero.getPosition().y >= 400) + { + it->theta = -0.5*PI; + } + else if (it->hero.getPosition().y < 400) + { + it->theta += (0.5 * PI / 180.0); + } + + if (it->theta >= -0.3 * PI && it->hero.getPosition().y >= 400) + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 8 % 4) * it->width, 320 + 2 * it->height, it->width, it->height)); + } + else if (it->theta >= -0.3 * PI && it->hero.getPosition().y < 400) + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 8 % 4 + 8) * it->width, 320 + 2 * it->height, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 32 % 4 + 3) * it->width, 320 + 2 * it->height, it->width, it->height)); + } + + + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + if (i1 > 15 * 60) + { + wave1.clear();//Final clear for accident + wave2.clear(); + wave3.clear(); + return 1; + } + return 0; +} + +int Game::S1E5()//mButter quit anime dierction +{ + static int i1 = 0; + i1++; + static list wave1, wave2, wave3, wave4; + double gapTime = 0.2; + int gapFrame = gapTime * 60; + static int gap = 0, temp = 0; + + if (i1 == 1) + { + FO mButterfly(5); + mButterfly.hero.setTexture(Enemy1); + mButterfly.hero.setTextureRect(sf::IntRect(0, 448, mButterfly.width, mButterfly.height)); + mButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + mButterfly.hero.setOrigin(mButterfly.width*0.5, mButterfly.height*0.5 - 24); + mButterfly.born = i1; + mButterfly.hero.setPosition(sf::Vector2f(250.0, -20.0)); + wave1.push_back(mButterfly); + } + if (i1 == 41) + { + FO mButterfly(5); + mButterfly.hero.setTexture(Enemy1); + mButterfly.hero.setTextureRect(sf::IntRect(0, 448, mButterfly.width, mButterfly.height)); + mButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + mButterfly.hero.setOrigin(mButterfly.width*0.5, mButterfly.height*0.5 - 24); + mButterfly.born = i1; + mButterfly.hero.setPosition(sf::Vector2f(600.0, -20.0)); + mButterfly.HealthPoint = 60; + wave2.push_back(mButterfly); + } + if (i1 % gapFrame == 1 && i1 < 10 * gapFrame) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320, sButterfly.width, sButterfly.height)); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(700.0, 960.0)); + sButterfly.speed = 3.0; + //sButterfly.setSButterfly(400 + pow(-1.0, i1 / gapFrame) * i1, 20.0); + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + //printf("Now %x\n", sButterfly.hero); + + wave3.push_back(sButterfly); + } + if (i1 % gapFrame == 1 && i1 < 10 * gapFrame) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320 + sButterfly.height, sButterfly.width, sButterfly.height)); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(150.0, 960.0)); + sButterfly.speed = 3.0; + //sButterfly.setSButterfly(400 + pow(-1.0, i1 / gapFrame) * i1, 20.0); + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + //printf("Now %x\n", sButterfly.hero); + + wave4.push_back(sButterfly); + } + + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + it->hero.setTextureRect(sf::IntRect(i1 % 50 / 10 * it->width, 448, it->width, it->height)); + if (i1 < 80) + { + it->speed = (80 - i1) / 8.0; + it->theta = 0.5 * PI; + } + else if (i1 >= 80 && i1 < 250) + { + it->speed = 0.0; + for (int i = 0; i < 1; i++) + { + setSharpRandom(it, 3.0); + } + } + else + { + it->speed = (i1 - 250) / 60.0; + it->theta = 1.5 * PI; + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + wave2.remove_if(isFOOutOfBoard); + for (list::iterator it = wave2.begin(); it != wave2.end(); it++) + { + it->hero.setTextureRect(sf::IntRect(i1 % 50 / 10 * it->width, 448, it->width, it->height)); + if (i1 < 120) + { + it->speed = (120 - i1) / 8.0; + it->theta = 0.5 * PI; + } + else if (i1 >= 120 && i1 < 290) + { + it->speed = 0.0; + for (int i = 0; i < 1; i++) + { + setSharpRandom(it, 3.0); + } + } + else + { + it->speed = (i1 - 290) / 60.0; + it->theta = 1.5 * PI; + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + wave3.remove_if(isFOOutOfBoard); + for (list::iterator it = wave3.begin(); it != wave3.end(); it++) + { + + temp = i1 - it->gap * gapFrame; + + if (rand() % 10 == 0 && it->hero.getPosition().y < 400) + { + setRandom(it, 4.0, 1, 5, 60, 90); //setRoundRandom(it, 4.0, 5, 60, 90);//setMultiRoundSnipe(it, 8.0, 5); + } + + if (it->hero.getPosition().y >= 400) + { + it->theta = -0.5*PI; + } + else if (it->hero.getPosition().y < 400) + { + it->theta -= (0.5 * PI / 180.0); + } + + if (it->theta >= -0.7 * PI && it->hero.getPosition().y >= 400) + { + it->hero.setTextureRect(sf::IntRect((int)fabs(temp / 8 % 4 - 1) * it->width, 320, it->width, it->height)); + } + else if (it->theta >= -0.7 * PI && it->hero.getPosition().y < 400) + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 32 % 4 + 3) * it->width, 320, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 8 % 4 + 8) * it->width, 320, it->width, it->height)); + } + + if (it->theta < PI / 2.0) + { + it->hero.setScale(-1.5, 1.5); + } + //it->theta = -it->theta; + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + wave4.remove_if(isFOOutOfBoard); + for (list::iterator it = wave4.begin(); it != wave4.end(); it++) + { + + temp = i1 - it->gap * gapFrame + 32; + + if (rand() % 30 == 0 && it->hero.getPosition().y < 400) + { + setRandom(it, 8.0, 1, 5, 60, 90); //setRoundRandom(it, 4.0, 5, 60, 90);//setMultiRoundSnipe(it, 8.0, 5); + } + + if (it->hero.getPosition().y >= 400) + { + it->theta = -0.5*PI; + } + else if (it->hero.getPosition().y < 400) + { + it->theta += (0.5 * PI / 180.0); + } + + if (it->theta >= -0.3 * PI && it->hero.getPosition().y >= 400) + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 8 % 4) * it->width, 320 + 2 * it->height, it->width, it->height)); + } + else if (it->theta >= -0.3 * PI && it->hero.getPosition().y < 400) + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 8 % 4 + 8) * it->width, 320 + 2 * it->height, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 32 % 4 + 3) * it->width, 320 + 2 * it->height, it->width, it->height)); + } + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + if (i1 > 15 * 60) + { + wave1.clear();//Final clear for accident + wave2.clear(); + return 1; + } + return 0; +} + +int Game::S1E6() +{ + static int i1 = 0; + i1++; + static list wave1, wave2, wave3, wave4; + double gapTime = 0.2; + int gapFrame = gapTime * 60; + static int gap = 0, temp = 0; + + if (i1 == 41) + { + FO mButterfly(5); + mButterfly.hero.setTexture(Enemy1); + mButterfly.hero.setTextureRect(sf::IntRect(0, 448, mButterfly.width, mButterfly.height)); + mButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + mButterfly.hero.setOrigin(mButterfly.width*0.5, mButterfly.height*0.5 - 24); + mButterfly.born = i1; + mButterfly.hero.setPosition(sf::Vector2f(250.0, -20.0)); + wave1.push_back(mButterfly); + } + if (i1 == 1) + { + FO mButterfly(5); + mButterfly.hero.setTexture(Enemy1); + mButterfly.hero.setTextureRect(sf::IntRect(0, 448, mButterfly.width, mButterfly.height)); + mButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + mButterfly.hero.setOrigin(mButterfly.width*0.5, mButterfly.height*0.5 - 24); + mButterfly.born = i1; + mButterfly.hero.setPosition(sf::Vector2f(600.0, -20.0)); + wave2.push_back(mButterfly); + } + if (i1 % gapFrame == 1 && i1 < 7 * gapFrame) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320, sButterfly.width, sButterfly.height)); + sButterfly.hero.setOrigin(sButterfly.width / 2, sButterfly.height / 2); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(800.0, 660.0)); + sButterfly.speed = 3.0; + //sButterfly.setSButterfly(400 + pow(-1.0, i1 / gapFrame) * i1, 20.0); + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + //printf("Now %x\n", sButterfly.hero); + + wave3.push_back(sButterfly); + } + if (i1 % gapFrame == 1 && i1 < 7 * gapFrame) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320 + sButterfly.height, sButterfly.width, sButterfly.height)); + sButterfly.hero.setOrigin(sButterfly.width / 2, sButterfly.height / 2); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(0.0, 660.0)); + sButterfly.speed = 3.0; + //sButterfly.setSButterfly(400 + pow(-1.0, i1 / gapFrame) * i1, 20.0); + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + //printf("Now %x\n", sButterfly.hero); + + wave4.push_back(sButterfly); + } + + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + it->hero.setTextureRect(sf::IntRect(i1 % 50 / 10 * it->width, 448, it->width, it->height)); + if (i1 < 120) + { + it->speed = (120 - i1) / 8.0; + it->theta = 0.5 * PI; + } + else if (i1 >= 120 && i1 < 290) + { + it->speed = 0.0; + for (int i = 0; i < 1; i++) + { + setSharpRandom(it, 3.0); + } + } + else + { + it->speed = (i1 - 290) / 60.0; + it->theta = 1.5 * PI; + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + wave2.remove_if(isFOOutOfBoard); + for (list::iterator it = wave2.begin(); it != wave2.end(); it++) + { + it->hero.setTextureRect(sf::IntRect(i1 % 50 / 10 * it->width, 448, it->width, it->height)); + if (i1 < 80) + { + it->speed = (80 - i1) / 8.0; + it->theta = 0.5 * PI; + } + else if (i1 >= 80 && i1 < 250) + { + it->speed = 0.0; + for (int i = 0; i < 1; i++) + { + setSharpRandom(it, 3.0); + } + } + else + { + it->speed = (i1 - 250) / 60.0; + it->theta = 1.5 * PI; + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + wave3.remove_if(isFOOutOfBoard); + for (list::iterator it = wave3.begin(); it != wave3.end(); it++) + { + temp = i1 - it->gap * gapFrame; + + if (rand() % 25 == 0 && it->hero.getPosition().y < 400) + { + setMultiRoundSnipe(it, 4.0, 5); + } + + if (it->hero.getPosition().x >= 650 && it->hero.getPosition().y < 500) + { + it->theta = 0; + } + else if (it->hero.getPosition().x >= 650 && it->hero.getPosition().y > 500) + { + it->theta = PI; + } + else + { + it->theta += (0.5 * PI / 100.0); + } + + standardSButterflyFrame(it, temp); + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + wave4.remove_if(isFOOutOfBoard); + for (list::iterator it = wave4.begin(); it != wave4.end(); it++) + { + + temp = i1 - it->gap * gapFrame; + + if (rand() % 25 == 0 && it->hero.getPosition().y < 400) + { + setMultiRoundSnipe(it, 4.0, 5); + } + + if (it->hero.getPosition().x <= 150 && it->hero.getPosition().y > 500) + { + it->theta = 0; + } + else if (it->hero.getPosition().x <= 150 && it->hero.getPosition().y < 500) + { + it->theta = PI; + } + else + { + it->theta -= (0.5 * PI / 100.0); + } + + standardSButterflyFrame(it, temp); + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + if (i1 > 15 * 60) + { + wave1.clear();//Final clear for accident + wave2.clear(); + wave3.clear(); + wave4.clear(); + return 1; + } + return 0; +} + +int Game::S1E7() +{ + static int i1 = 0; + i1++; + static list wave1; + + if (i1 == 1) + { + FO mButterflt(5); + mButterflt.hero.setTexture(Enemy1); + mButterflt.hero.setTextureRect(sf::IntRect(0, 448, mButterflt.width, mButterflt.height)); + mButterflt.hero.setScale(sf::Vector2f(1.5, 1.5)); + mButterflt.hero.setOrigin(mButterflt.width*0.5, mButterflt.height*0.5 - 24); + mButterflt.born = i1; + mButterflt.hero.setPosition(sf::Vector2f(450.0, -20.0)); + mButterflt.HealthPoint *= 4; + mButterflt.score *= 4; + wave1.push_back(mButterflt); + } + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + it->hero.setTextureRect(sf::IntRect(i1 % 50 / 10 * it->width, 448, it->width, it->height)); + if (i1 < 80) + { + it->speed = (80 - i1) / 16.0; + it->theta = 0.5 * PI; + } + else if (i1 >= 80 && i1 < 400) + { + it->speed = 0.0; + + setSharpFlower1(it, 4.0, 4, 0); + + } + else + { + it->speed = (i1 - 400) / 60.0; + it->theta = 1.5 * PI; + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + if (i1 > 10 * 60) + { + wave1.clear();//Final clear for accident + return 1; + } + return 0; +} + +int Game::S1E8() +{ + static int i1 = 0, stp = 0; + i1++; + static list wave1, wave2; + + if (i1 == 1) + { + FO ghost(6); + ghost.hero.setTexture(Enemy2); + ghost.hero.setTextureRect(sf::IntRect(0, 64, ghost.width, ghost.height)); + ghost.hero.setScale(sf::Vector2f(1.5, 1.5)); + ghost.hero.setOrigin(ghost.width*0.5, ghost.height*0.5 - 24); + ghost.born = i1; + ghost.hero.setPosition(sf::Vector2f(450.0, -20.0)); + FO spellBoard; + spellBoard.hero.setTexture(magicSquare); + spellBoard.hero.setOrigin(128, 128); + spellBoard.hero.setPosition(ghost.hero.getPosition()); + spellBoard.theta = 0; + spellBoard.hero.setColor(sf::Color(255, 255, 255, 127)); + wave1.push_back(ghost); + wave2.push_back(spellBoard); + } + if (wave1.size() == 0) + { + wave2.clear(); + return 1; + } + wave2.remove_if(isFOOutOfBoard); + for (list::iterator it = wave2.begin(); it != wave2.end(); it++) + { + it->hero.setPosition(wave1.begin()->hero.getPosition().x, wave1.begin()->hero.getPosition().y + 32); + it->theta += PI / 100.0; + it->hero.setRotation(it->theta / PI * 180.0 + 90); + backEsPushToDraw(it); + } + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + if (it->phase == 1 && stp == 0) + { + stp = 1; + i1 = 80; + } + it->hero.setTextureRect(sf::IntRect(i1 % 80 / 10 * it->width, 64, it->width, it->height)); + if (i1 < 80) + { + it->speed = (80 - i1) / 16.0; + it->theta = 0.5 * PI; + } + else if (i1 >= 80 && i1 < 1200) + { + it->speed = 0.0; + switch (it->phase) + { + case 2: + nonSpellCard1(it); + break; + case 1: + if (i1 == 141) + { + SCAnounce.play(); + } + if (i1 > 3 * 60) + { + spellCard1(it); + } + break; + } + + + } + else + { + it->speed = (i1 - 1200) / 60.0; + it->theta = 1.5 * PI; + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + if (i1 > 60 * 60) + { + wave1.clear();//Final clear for accident + return 1; + } + return 0; +} + +int Game::S1E9() +{ + static int i1 = 0; + i1++; + static list wave1, wave2; + double gapTime = 0.2; + int gapFrame = gapTime * 60; + static int gap = 0, temp = 0; + + if (i1 % gapFrame == 1 && i1 < 30 * gapFrame) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320, sButterfly.width, sButterfly.height)); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(rand() * 30 % 650 + 100, 20.0)); + //sButterfly.setSButterfly(400 + pow(-1.0, i1 / gapFrame) * i1, 20.0); + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + //printf("Now %x\n", sButterfly.hero); + + wave1.push_back(sButterfly); + } + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + + temp = i1 - it->gap * gapFrame; + if (temp < 200)//phase1 + { + if (rand() % 45 == 0) + { + setMultiRoundSnipe(it, 5.0, 5); + } + it->speed = 50.0 / (temp + 1.0); + it->theta = 0.5*PI; + it->hero.setTextureRect(sf::IntRect(i1 % 35 / 7 * it->width, 320, it->width, it->height)); + } + else//phase2 + { + it->speed = (temp - 200) / 10.0; + it->theta = 0.5 * PI + pow(-1.0, it->gap + 1.0) * 10.0*PI / 360.0; + + if (it->speed <= 11) + { + it->hero.setTextureRect(sf::IntRect((int)it->speed * it->width, 320, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect((int)11 * it->width, 320, it->width, it->height)); + } + if (it->theta > PI / 2.0) + { + it->hero.setScale(-1.5, 1.5); + + if (!it->isSym) + { + it->hero.move(1.5*it->width, 0); + it->isSym = true; + } + } + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + if (i1 > 10 * 60) + { + wave1.clear();//Final clear for accident + wave2.clear(); + return 1; + } + return 0; +} + +int Game::S1E10() +{ + static int i1 = 0; + i1++; + static list wave1, wave2; + double gapTime = 1.8; + int gapFrame = gapTime * 60; + static int gap = 0, temp = 0; + + if (i1 % gapFrame == 1 && i1 < 5 * gapFrame) + { + FO mButterfly(5); + mButterfly.hero.setTexture(Enemy1); + mButterfly.hero.setTextureRect(sf::IntRect(0, 448, mButterfly.width, mButterfly.height)); + mButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + mButterfly.hero.setOrigin(mButterfly.width*0.5, mButterfly.height*0.5 - 24); + mButterfly.hero.setPosition(sf::Vector2f(450 + pow(-1, gap)*200, -20.0)); + mButterfly.born = i1; + mButterfly.gap = gap; + wave1.push_back(mButterfly); + gap++; + } + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + + temp = i1 - it->gap * gapFrame; + if (temp < 120)//phase1 + { + it->speed = 70.0 / (temp + 1.0); + it->theta = 0.5*PI; + } + else//phase2 + { + if (temp < 220) + { + it->speed = (temp - 120) / 20.0; + } + else + { + it->speed = 5; + } + if (it->gap % 2 == 0) + { + it->theta = PI; + } + else + { + it->theta = 0; + } + if (temp % 6 == 0) + { + setSharpLine(it, 5.0); + } + } + standardMButterflyFrame(it, temp); + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + if (i1 > 20 * 60) + { + wave1.clear();//Final clear for accident + wave2.clear(); + return 1; + } + return 0; +} + +int Game::S1E11() +{ + static int i1 = 0; + i1++; + static list wave1, wave2; + double gapTime = 0.1; + int gapFrame = gapTime * 60; + static int gap = 0, temp = 0; + + if (i1 % gapFrame == 1 && i1 < 60 * gapFrame) + { + FO sButterfly(2); + sButterfly.hero.setTexture(Enemy1); + sButterfly.hero.setTextureRect(sf::IntRect(0, 320, sButterfly.width, sButterfly.height)); + sButterfly.hero.setScale(sf::Vector2f(1.5, 1.5)); + sButterfly.hero.setPosition(sf::Vector2f(rand() * 30 % 650 + 100, 20.0)); + sButterfly.born = i1; + sButterfly.gap = gap; + gap++; + + wave1.push_back(sButterfly); + } + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + + temp = i1 - it->gap * gapFrame; + if (temp < 200)//phase1 + { + if (rand() % 90 == 0) + { + setRoundRandom(it, 6.0, 5, 0, PI); + } + it->speed = 50.0 / (temp + 1.0); + it->theta = 0.5*PI; + it->hero.setTextureRect(sf::IntRect(i1 % 35 / 7 * it->width, 320, it->width, it->height)); + } + else//phase2 + { + it->speed = (temp - 200) / 10.0; + it->theta = 0.5 * PI + pow(-1.0, it->gap + 1.0) * 10.0*PI / 360.0; + + if (it->speed <= 11) + { + it->hero.setTextureRect(sf::IntRect((int)it->speed * it->width, 320, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect((int)11 * it->width, 320, it->width, it->height)); + } + if (it->theta > PI / 2.0) + { + it->hero.setScale(-1.5, 1.5); + + if (!it->isSym) + { + it->hero.move(1.5*it->width, 0); + it->isSym = true; + } + } + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + } + + if (i1 > 15 * 60) + { + wave1.clear();//Final clear for accident + wave2.clear(); + return 1; + } + return 0; +} + +int Game::S1E12() +{ + static int i1 = 0; + i1++; + static list wave1; + + if (i1 == 1) + { + FO mainTitle(0); + mainTitle.hero.setTexture(whiteSpark); + mainTitle.hero.setTextureRect(sf::IntRect(0, 0, 1280, 960)); + mainTitle.speed = 0.0; + mainTitle.hero.setPosition(sf::Vector2f(0, 0)); + mainTitle.hero.setColor(sf::Color(255, 255, 255, 0)); + + wave1.push_back(mainTitle); + } + + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + double alpha = -i1*(i1 - 301) / (151.0*150.0) * 255; + it->hero.setColor(sf::Color(255, 255, 255, alpha)); + if (i1 < 151) + { + stage1BGM.setVolume((255 - alpha) / 255.0*100.0); + if (alpha > 240) + { + stage1BGM.pause(); + for (int i = 0; i < 6; i++) + { + back[i].setTexture(bg2); + back[i].setScale(sf::Vector2f(1.5, 1.5)); + back[i].setPosition((float)65.0, (float)(i - 1)*192.0 + 35.0); + backEff[i].setTexture(bgEff2); + backEff[i].setScale(sf::Vector2f(1.5, 1.5)); + backEff[i].setPosition(65.0, (i - 1)*384.0 + 35.0); + } + } + } + enemiesPushToDraw(it); + } + if (i1 == 200) + { + stage2BGM.play(); + stage2BGM.setLoop(true); + } + if (i1 > 5 * 60) + { + wave1.clear();//Final clear for accident + return 1; + } + return 0; +} + +int Game::S1E13() +{ + static int i1 = 0, stp = 0; + i1++; + static list wave1, wave2; + + if (i1 == 1) + { + FO ghost(6); + ghost.hero.setTexture(Enemy2); + ghost.hero.setTextureRect(sf::IntRect(0, 64, ghost.width, ghost.height)); + ghost.hero.setScale(sf::Vector2f(1.5, 1.5)); + ghost.hero.setOrigin(ghost.width*0.5, ghost.height*0.5 - 24); + ghost.born = i1; + ghost.hero.setPosition(sf::Vector2f(450.0, -20.0)); + FO spellBoard; + spellBoard.hero.setTexture(magicSquare); + spellBoard.hero.setOrigin(128, 128); + spellBoard.hero.setPosition(ghost.hero.getPosition()); + spellBoard.theta = 0; + spellBoard.hero.setColor(sf::Color(255, 255, 255, 127)); + ghost.phase = 10; + wave1.push_back(ghost); + wave2.push_back(spellBoard); + } + if (wave1.size() == 0) + { + wave2.clear(); + return 1; + } + wave2.remove_if(isFOOutOfBoard); + for (list::iterator it = wave2.begin(); it != wave2.end(); it++) + { + it->hero.setPosition(wave1.begin()->hero.getPosition().x, wave1.begin()->hero.getPosition().y + 32); + it->theta += PI / 100.0; + it->hero.setScale(1.0 + 0.1*sin(i1 / 11.0), 1.0 + 0.1*sin(i1 / 11.0)); + it->hero.setRotation(it->theta / PI * 180.0 + 90); + backEsPushToDraw(it); + } + wave1.remove_if(isFOOutOfBoard); + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + if (it->phase + stp == 9) + { + stp++; + i1 = 80; + } + it->hero.setTextureRect(sf::IntRect(i1 % 80 / 10 * it->width, 64, it->width, it->height)); + if (i1 < 80) + { + it->speed = (80 - i1) / 16.0; + it->theta = 0.5 * PI; + } + else if (i1 >= 80 && i1 < 12000) + { + it->speed = 0.0; + switch (it->phase) + { + case 10: + nonSpellCard2(it); + break; + case 9: + if (i1 == 241) + { + SCAnounce.play(); + } + if (i1 > 6 * 60) + { + spellCard2(it); + } + break; + case 8: + if (i1 == 241) + { + SCAnounce.play(); + } + if (i1 > 6 * 60) + { + nonSpellCard3(it); + } + break; + case 7: + if (i1 == 241) + { + SCAnounce.play(); + } + if (i1 > 6 * 60) + { + spellCard3(it); + } + break; + case 6: + if (i1 == 241) + { + SCAnounce.play(); + } + if (i1 > 6 * 60) + { + nonSpellCard4(it); + } + break; + case 5: + if (i1 == 241) + { + SCAnounce.play(); + } + if (i1 > 6 * 60) + { + spellCard4(it); + } + break; + case 4: + if (i1 == 241) + { + SCAnounce.play(); + } + if (i1 > 6 * 60) + { + nonSpellCard5(it); + } + break; + case 3: + if (i1 == 241) + { + SCAnounce.play(); + } + if (i1 > 6 * 60) + { + spellCard5(it); + } + break; + case 2: + if (i1 == 241) + { + SCAnounce.play(); + } + if (i1 > 6 * 60) + { + nonSpellCard6(it); + } + break; + case 1: + if (i1 == 241) + { + SCAnounce.play(); + } + if (i1 > 6 * 60) + { + spellCard6(it); + } + break; + } + } + else + { + it->speed = (i1 - 12000) / 60.0; + it->theta = 1.5 * PI; + } + + enemyCollisionProcessing(it); + + enemiesPushToDraw(it); + + if (i1 > 120 * 60) + { + if (it->phase >= 2) + { + cardGet.play(); + breakSound.play(); + score += it->score; + deathEff.setTexture(deathCircle); + deathEff.setTextureRect(sf::IntRect(64, 0, 64, 64)); + deathEff.setOrigin(32, 32); + deathEff.setPosition(it->hero.getPosition().x + it->width * 0.25, it->hero.getPosition().y + it->height * 0.25); + deathEff.setScale(0.1, 0.1); + deathEffs.push_back(deathEff); + deathEff.setScale(0.3, 0.06); + deathEff.setRotation(rand() % 360); + deathEffs.push_back(deathEff); + it->HealthPoint += 1500; + } + else + { + wave1.clear();//Final clear for accident + } + + return 1; + } + } + return 0; +} + +int Game::S1E14() +{ + static int i1 = 0; + i1++; + static list wave1; + + if (i1 == 1) + { + FO mainTitle(0); + mainTitle.hero.setTexture(Title2); + mainTitle.hero.setTextureRect(sf::IntRect(0, 0, 512, 256)); + mainTitle.speed = 0.0; + mainTitle.hero.setPosition(sf::Vector2f(200.0, 150.0)); + mainTitle.hero.setColor(sf::Color(255, 255, 255, 0)); + + wave1.push_back(mainTitle); + } + + for (list::iterator it = wave1.begin(); it != wave1.end(); it++) + { + it->hero.setColor(sf::Color(255, 255, 255, -i1*(i1 - 301) / (151.0*150.0) * 255)); + enemiesPushToDraw(it); + } + if (i1 > 5 * 60) + { + wave1.clear();//Final clear for accident + return 1; + } + return 0; +} + +void Game::enemiesPushToDraw(list::iterator it) +{ + switch (it->type) + { + case 101://bound + if (it->hero.getPosition().y < 60 || it->hero.getPosition().y > 900) + { + if (it->bounds < 3) + { + it->theta += PI; + it->bounds++; + } + } + break; + case 103: + it->HealthPoint++; + if (it->HealthPoint > 60 && it->phase > 0) + { + enemyBulletSound.play(); + FO nonSpell1; + nonSpell1.speed = it->speed; + nonSpell1.width = 16; + nonSpell1.height = 16; + nonSpell1.hero.setTexture(allBullets1); + switch (it->phase) + { + case 4: + nonSpell1.hero.setTextureRect(sf::IntRect(32, 16, 16, 16)); + break; + case 3: + nonSpell1.hero.setTextureRect(sf::IntRect(32, 16, 16, 16)); + break; + case 2: + nonSpell1.hero.setTextureRect(sf::IntRect(240, 16, 16, 16)); + break; + case 1: + nonSpell1.hero.setTextureRect(sf::IntRect(160, 16, 16, 16)); + break; + } + nonSpell1.hero.setOrigin(8, 8); + nonSpell1.hero.setScale(1.5, 1.5); + nonSpell1.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y); + nonSpell1.type = 103; + nonSpell1.phase = it->phase-1; + for (int i = 0; i < 5; i++) + { + nonSpell1.speed += i / 10.0; + nonSpell1.theta = it->theta + PI - 18 / 180.0*PI + i*18.0 / 180.0*PI; + nonSpell1.hero.setRotation(nonSpell1.theta / PI * 180.0 + 90); + enemyBullets.push_back(nonSpell1); + } + it->hero.setPosition(-100, -100); + return; + } + break; + case 104: + it->HealthPoint++; + if (it->speed > EPS) + { + it->velocity.x = it->speed * cos(it->theta); + it->velocity.y = it->speed * sin(it->theta) + 4.9*it->HealthPoint*it->HealthPoint/7200.0; + it->hero.setRotation(atan2(it->velocity.y, it->velocity.x) / PI * 180.0 + 90); + if (it->velocity.y >= 8) + { + it->velocity.y = 8; + } + it->hero.move(it->velocity); + } + enemies.push_back(it->hero); + return; + break; + } + if (it->speed > EPS) + { + it->velocity.x = it->speed * cos(it->theta); + it->velocity.y = it->speed * sin(it->theta); + it->hero.move(it->velocity); + } + + enemies.push_back(it->hero); +} + +void Game::backEsPushToDraw(list::iterator it) +{ + if (it->speed > EPS) + { + it->velocity.x = it->speed * cos(it->theta); + it->velocity.y = it->speed * sin(it->theta); + it->hero.move(it->velocity); + } + + backgroundEffs.push_back(it->hero); +} + +void Game::frameDisplay()//ammo->front->player->jpoint +{ + + player.staticFrame = player.staticFrame % 56; + player.staticFrame++; + mWindow.clear(); + + backgroundDisplay(); + + playerAmmoDisplay(); + + effsDisplay(); + + playerDisplay(); + + enemiesDisplay(); + + enemyBulletsDisplay(); + + boardDisplay(); + + // Draw the string + mWindow.draw(text); + // Update the window + mWindow.display(); +} +// +void Game::backgroundDisplay() +{ + for (int i = 0; i < 6; i++) + { + if (back[i].getPosition().y >= 5 * 192 + 35) + { + back[i].setPosition(65.0, 35 - 192); + } + back[i].move(0.0, 3.0); + mWindow.draw(back[i]); + + } + for (int i = 0; i < 4; i++) + { + if (backEff[i].getPosition().y >= 3 * 384 + 35) + { + backEff[i].setPosition(65.0, 35 - 384); + } + backEff[i].move(0.0, 5.0); + mWindow.draw(backEff[i]); + } + for (list::iterator it = backgroundEffs.begin(); it != backgroundEffs.end(); it++) + { + mWindow.draw(*it); + } + backgroundEffs.clear(); +} +// +void Game::playerAmmoDisplay() +{ + //Ammo + if (mIsFire) + { + //playerAmmo = (mIsGrazing) ? player.LSAmmo : player.HSAmmo; + if (player.staticFrame % 2 == 1) + { + player.LSAmmo.setPosition(sf::Vector2f(player.hero.getPosition().x + 4, player.hero.getPosition().y + 80)); + playerBullets.push_back(player.LSAmmo); + player.LSAmmo.setPosition(sf::Vector2f(player.hero.getPosition().x + 20, player.hero.getPosition().y + 80)); + playerBullets.push_back(player.LSAmmo); + player.HSAmmo.setPosition(sf::Vector2f(player.hero.getPosition().x + 4, player.hero.getPosition().y + 110)); + playerBullets.push_back(player.HSAmmo); + player.HSAmmo.setPosition(sf::Vector2f(player.hero.getPosition().x + 20, player.hero.getPosition().y + 110)); + playerBullets.push_back(player.HSAmmo); + } + } + playerBullets.remove_if(isOutOfBoard); + for (list::iterator it = playerBullets.begin(); it != playerBullets.end(); it++) + { + it->move(0.0, - 60.0); + mWindow.draw(*it); + } +} +// +void Game::enemiesDisplay() +{ + for (list::iterator it = enemies.begin(); it != enemies.end(); it++) + { + mWindow.draw(*it); + } + enemies.clear(); +} +// +/*void Game::enemyBulletsPreDisplay() +{ + enemyBulletsPre.remove_if([](FO obj) { if (obj.hero.getScale().x <= 1.0) return true; else return false; }); + for (list::iterator it = enemyBulletsPre.begin(); it != enemyBulletsPre.end(); it++) + { + it->hero.setScale(it->hero.getScale().x - 0.2, it->hero.getScale().y - 0.2); + it->hero.setColor(sf::Color(255, 255, 255, it->hero.getColor().White + 255 / 5)); + if (obj.hero.getScale().x <= 1.0) + { + enemyBullets.push_back(*it); + } + } +}*/ +// +void Game::enemyBulletsDisplay() +{ + enemyBullets.remove_if(isFOOutOfBoard); + for (list::iterator it = enemyBullets.begin(); it != enemyBullets.end(); it++) + { + enemiesPushToDraw(it); + } +} +// +void Game::playerDisplay() +{ + //player + static sf::Time elapsed1 = clock.getElapsedTime(); + if (checkPlayerCollision() && (clock.getElapsedTime().asSeconds() - elapsed1.asSeconds() > 1.0)) + { + for (list::iterator it = enemyBullets.begin(); it != enemyBullets.end(); it++) + { + enemyCrash(it); + } + elapsed1 = clock.getElapsedTime(); + playerDeadSound.play(); + if (remnant > 1) + { + remnant--; + } + else + { + GameOver();//printf("Game Over!\n"); + } + + } + if (mIsMovingLeft) + { + if (player.dynamicFrame < 14) + { + player.dynamicFrame++; + } + player.hero.setTextureRect(sf::IntRect(player.width * (player.dynamicFrame / 2), player.height, player.width, player.height)); + } + else if (mIsMovingRight) + { + if (player.dynamicFrame < 14) + { + player.dynamicFrame++; + } + player.hero.setTextureRect(sf::IntRect(player.width * (player.dynamicFrame / 2), 2 * player.height, player.width, player.height)); + } + else + { + player.hero.setTextureRect(sf::IntRect(player.width * (player.staticFrame / 8), 0, player.width, player.height)); + player.dynamicFrame = 0; + } + + mWindow.draw(player.hero);//updating + + static int julgeRotate = 0; + julgeRotate++; + julgeRotate %= 360; + julgePoint.setOrigin(32, 32); + julgePoint.setRotation(julgeRotate); + julgePoint.setPosition(sf::Vector2f(player.hero.getPosition().x - 24 + 48, player.hero.getPosition().y - 8 + 48)); + if (mIsGrazing) + { + mWindow.draw(julgePoint); + } +} +// +void Game::effsDisplay() +{ + for (list::iterator it = playerBulletsEffs.begin(); it != playerBulletsEffs.end(); it++) + { + mWindow.draw(*it); + } + playerBulletsEffs.clear(); + for (list::iterator it = deathEffs.begin(); it != deathEffs.end(); it++) + { + double i = it->getScale().x, j = it->getScale().y / it->getScale().x; + i += 0.1; + it->setScale(i, i*j); + it->setColor(sf::Color(255, 255, 255, 255 * (1.2 - 0.5*i))); + + mWindow.draw(*it); + } + deathEffs.remove_if([](sf::Sprite obj) { if (obj.getScale().x > 2.3 || (obj.getRotation() < EPS && obj.getScale().x > 2.0)) return true; else return false; }); +} +// +void Game::boardDisplay() +{ + mWindow.draw(front01); + mWindow.draw(front02); + mWindow.draw(front03); + mWindow.draw(front04); + + switch (remnant) + { + case 3: + lifeBoard.setTextureRect(sf::IntRect(0, 0, 272, 36)); + break; + case 2: + lifeBoard.setTextureRect(sf::IntRect(0, 44, 272, 36)); + break; + case 1: + lifeBoard.setTextureRect(sf::IntRect(0, 90, 272, 36)); + break; + default: + ; + } + + lifeBoard.setScale(1.5, 1.5); + lifeBoard.setPosition(830, 300); + mWindow.draw(lifeBoard); + + static string scoreStr; + scoreStr = "Score: "; + scoreStr += to_string(score); + tempScore.setString(scoreStr); + tempScore.setStyle(sf::Text::Italic); + tempScore.setFont(font); + tempScore.setCharacterSize(50); + tempScore.setPosition(840, 50); + mWindow.draw(tempScore); +} + +void Game::enemyCollisionProcessing(list::iterator it) +{ + for (list::iterator itAmmo = playerBullets.begin(); itAmmo != playerBullets.end(); itAmmo++) + { + if (checkCollision(it->hero, *itAmmo)) + { + enemyUnderAttack(it, itAmmo); + + if (it->HealthPoint <= 0) + { + it->phase--; + if (it->phase <= 0) + { + enemyCrash(it); + } + else + { + cardGet.play(); + breakSound.play(); + score += it->score; + deathEff.setTexture(deathCircle); + deathEff.setTextureRect(sf::IntRect(64, 0, 64, 64)); + deathEff.setOrigin(32, 32); + deathEff.setPosition(it->hero.getPosition().x + it->width * 0.25, it->hero.getPosition().y + it->height * 0.25); + deathEff.setScale(0.1, 0.1); + deathEffs.push_back(deathEff); + deathEff.setScale(0.3, 0.06); + deathEff.setRotation(rand() % 360); + deathEffs.push_back(deathEff); + it->HealthPoint += 1500; + } + } + } + } +} + +void Game::enemyUnderAttack(list::iterator it, list::iterator itAmmo) +{ + score++; + it->HealthPoint -= player.damage; + AmmoEff.setTexture(buffetsEff); + AmmoEff.setTextureRect(sf::IntRect(player.staticFrame % 7 * 48, 0, 48, 48)); + AmmoEff.setPosition(itAmmo->getPosition().x - 10, it->hero.getPosition().y + it->height - 10); + playerBulletsEffs.push_back(AmmoEff); + itAmmo->setPosition(-100, -100); +} + +void Game::enemyCrash(list::iterator it) +{ + breakSound.play(); + score += it->score; + deathEff.setTexture(deathCircle); + deathEff.setTextureRect(sf::IntRect(64, 0, 64, 64)); + deathEff.setOrigin(32, 32); + deathEff.setPosition(it->hero.getPosition().x + it->width * 0.25, it->hero.getPosition().y + it->height * 0.25); + deathEff.setScale(0.1, 0.1); + deathEffs.push_back(deathEff); + deathEff.setScale(0.3, 0.06); + deathEff.setRotation(rand() % 360); + deathEffs.push_back(deathEff); + it->hero.setPosition(-100, -100); +} + +void Game::standardSButterflyFrame(list::iterator it, int temp) +{ + double t1 = it->theta - 0.5 * PI; + double t2 = it->theta - 1.5 * PI; + int he = 320; + if (fabs(t1) < EPS || fabs(t2) * PI < EPS) + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 8 % 5) * it->width, he, it->width, it->height)); + } + else if ((fabs(t1) > EPS && fabs(t1) < PI / 8.0) || (fabs(t2) > EPS && fabs(t2) < PI / 8.0)) + { + it->hero.setTextureRect(sf::IntRect(5 * it->width, he, it->width, it->height)); + } + else if ((fabs(t1) > PI / 8.0 && fabs(t1) < PI / 4.0) || (fabs(t2) > PI / 8.0 && fabs(t2) < PI / 4.0)) + { + it->hero.setTextureRect(sf::IntRect(6 * it->width, he, it->width, it->height)); + } + else if ((fabs(t1) > PI / 4.0 && fabs(t1) < PI / 2.0) || (fabs(t2) > PI / 4.0 && fabs(t2) < PI / 2.0)) + { + it->hero.setTextureRect(sf::IntRect(7 * it->width, he, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 8 % 4 + 8) * it->width, he, it->width, it->height)); + } + + if ((it->theta > 0.5 * PI && it->theta < 1.5 * PI) || (it->theta < -0.5 * PI && it->theta > -1.5 * PI)) + { + it->hero.setScale(-1.5, 1.5); + } + else + { + it->hero.setScale(1.5, 1.5); + } +} + +void Game::standardMButterflyFrame(list::iterator it, int temp) +{ + double t1 = it->theta - 0.5 * PI; + double t2 = it->theta - 1.5 * PI; + int he = 448; + if (fabs(t1) < EPS || fabs(t2) * PI < EPS) + { + it->hero.setTextureRect(sf::IntRect((int)(temp / 8 % 5) * it->width, he, it->width, it->height)); + } + else if ((fabs(t1) > EPS && fabs(t1) < PI / 8.0) || (fabs(t2) > EPS && fabs(t2) < PI / 8.0)) + { + it->hero.setTextureRect(sf::IntRect(5 * it->width, he, it->width, it->height)); + } + else if ((fabs(t1) > PI / 8.0 && fabs(t1) < PI / 4.0) || (fabs(t2) > PI / 8.0 && fabs(t2) < PI / 4.0)) + { + it->hero.setTextureRect(sf::IntRect(6 * it->width, he, it->width, it->height)); + } + else + { + it->hero.setTextureRect(sf::IntRect(7 * it->width, he + (temp / 8 % 3 - 2) * it->height, it->width, it->height)); + } + + if ((it->theta > 0.5 * PI && it->theta < 1.5 * PI) || (it->theta < -0.5 * PI && it->theta > -1.5 * PI)) + { + it->hero.setScale(-1.5, 1.5); + } + else + { + it->hero.setScale(1.5, 1.5); + } +} + +void Game::setSnipe(list::iterator it, double speed, int type, int color) +{ + enemyBulletSound.play(); + FO Snipe; + Snipe.speed = speed; + Snipe.width = 16; + Snipe.height = 16; + Snipe.hero.setTexture(allBullets1); + Snipe.hero.setTextureRect(sf::IntRect(color * 16, type * 16, 16, 16)); + Snipe.hero.setOrigin(8, 8); + Snipe.hero.setScale(1.5, 1.5); + Snipe.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + Snipe.theta = atan2(julgePoint.getPosition().y - Snipe.hero.getPosition().y, julgePoint.getPosition().x - Snipe.hero.getPosition().x); + + enemyBullets.push_back(Snipe); +} + +void Game::setRoundSnipe(list::iterator it, double speed) +{ + enemyBulletSound.play(); + FO RoundSnipe; + RoundSnipe.speed = speed; + RoundSnipe.width = 16; + RoundSnipe.height = 16; + RoundSnipe.hero.setTexture(allBullets1); + RoundSnipe.hero.setTextureRect(sf::IntRect(80, 32, 16, 16)); + RoundSnipe.hero.setOrigin(8, 8); + RoundSnipe.hero.setScale(1.5, 1.5); + RoundSnipe.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + RoundSnipe.theta = atan2(julgePoint.getPosition().y - RoundSnipe.hero.getPosition().y, julgePoint.getPosition().x - RoundSnipe.hero.getPosition().x); + + enemyBullets.push_back(RoundSnipe); +} + +void Game::setMultiRoundSnipe(list::iterator it, double speed, int color) +{ + enemyBulletSound.play(); + FO MultiRoundSnipe; + MultiRoundSnipe.speed = speed; + MultiRoundSnipe.width = 16; + MultiRoundSnipe.height = 16; + MultiRoundSnipe.hero.setTexture(allBullets1); + MultiRoundSnipe.hero.setTextureRect(sf::IntRect(color * 16, 48, 16, 16)); + MultiRoundSnipe.hero.setOrigin(8, 8); + MultiRoundSnipe.hero.setScale(1.5, 1.5); + MultiRoundSnipe.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + MultiRoundSnipe.theta = atan2(julgePoint.getPosition().y - MultiRoundSnipe.hero.getPosition().y, julgePoint.getPosition().x - MultiRoundSnipe.hero.getPosition().x); + enemyBullets.push_back(MultiRoundSnipe); + MultiRoundSnipe.theta += 0.4 * PI; + enemyBullets.push_back(MultiRoundSnipe); + MultiRoundSnipe.theta -= 0.8 * PI; + enemyBullets.push_back(MultiRoundSnipe); +} + +void Game::setGeneralMultiSnipe(list::iterator it, double speed, int type, int color, double angle) +{ + enemyBulletSound.play(); + FO GeneralMultiSnipe; + GeneralMultiSnipe.speed = speed; + GeneralMultiSnipe.width = 16; + GeneralMultiSnipe.height = 16; + GeneralMultiSnipe.hero.setTexture(allBullets1); + GeneralMultiSnipe.hero.setTextureRect(sf::IntRect(color * 16, type * 16, 16, 16)); + GeneralMultiSnipe.hero.setOrigin(8, 8); + GeneralMultiSnipe.hero.setScale(1.5, 1.5); + GeneralMultiSnipe.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + GeneralMultiSnipe.theta = atan2(julgePoint.getPosition().y - GeneralMultiSnipe.hero.getPosition().y, julgePoint.getPosition().x - GeneralMultiSnipe.hero.getPosition().x) + (rand() % 5 * 0.01 - 0.02) * PI; + enemyBullets.push_back(GeneralMultiSnipe); + GeneralMultiSnipe.theta += angle * PI; + enemyBullets.push_back(GeneralMultiSnipe); + GeneralMultiSnipe.theta -= angle * 2 * PI; + enemyBullets.push_back(GeneralMultiSnipe); +} + +void Game::setRandom(list::iterator it, double speed, int type, int color, double leftBoarder, double range) +{ + enemyBulletSound.play(); + FO Random; + Random.speed = speed; + Random.theta = rand() % (int)range + leftBoarder; + Random.width = 16; + Random.height = 16; + Random.hero.setTexture(allBullets1); + Random.hero.setTextureRect(sf::IntRect(color * 16, type * 16, 16, 16)); + Random.hero.setOrigin(8, 8); + Random.hero.setScale(1.5, 1.5); + Random.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + Random.hero.setRotation(Random.theta / PI * 180.0 + 90); + enemyBullets.push_back(Random); +} + +void Game::setRoundRandom(list::iterator it, double speed, int color, double leftBoarder, double range) +{ + enemyBulletSound.play(); + FO RoundRandom; + RoundRandom.speed = speed; + RoundRandom.theta = rand() % (int)range + leftBoarder; + RoundRandom.width = 16; + RoundRandom.height = 16; + RoundRandom.hero.setTexture(allBullets1); + RoundRandom.hero.setTextureRect(sf::IntRect(color * 16, 48, 16, 16)); + RoundRandom.hero.setOrigin(8, 8); + RoundRandom.hero.setScale(1.5, 1.5); + RoundRandom.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + RoundRandom.hero.setRotation(RoundRandom.theta / PI * 180.0 + 90); + enemyBullets.push_back(RoundRandom); +} + +void Game::setSharpRandom(list::iterator it, double speed) +{ + enemyBulletSound.play(); + FO SharpRandom; + SharpRandom.speed = speed; + SharpRandom.theta = rand()%360; + SharpRandom.width = 16; + SharpRandom.height = 16; + SharpRandom.hero.setTexture(allBullets1); + SharpRandom.hero.setTextureRect(sf::IntRect(64, 64, 16, 16));//80 96 + SharpRandom.hero.setOrigin(8, 8); + SharpRandom.hero.setScale(1.5, 1.5); + SharpRandom.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + SharpRandom.hero.setRotation(SharpRandom.theta / PI * 180.0 + 90); + enemyBullets.push_back(SharpRandom); +} + +void Game::setSharpLine(list::iterator it, double speed) +{ + enemyBulletSound.play(); + FO SharpLine; + SharpLine.speed = speed; + SharpLine.theta = 0.5 * PI; + if (it->theta > 0.5 * PI) + { + SharpLine.theta += it->speed * 0.04 * PI; + } + else + { + SharpLine.theta -= it->speed * 0.04 * PI; + } + SharpLine.width = 16; + SharpLine.height = 16; + SharpLine.hero.setTexture(allBullets1); + SharpLine.hero.setTextureRect(sf::IntRect(64, 64, 16, 16));//80 96 + SharpLine.hero.setOrigin(8, 8); + SharpLine.hero.setScale(1.5, 1.5); + SharpLine.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + SharpLine.hero.setRotation(SharpLine.theta / PI * 180.0 + 90); + enemyBullets.push_back(SharpLine); +} + +void Game::setSharpFlower1(list::iterator it, double speed, int type, int color)//sound Reform +{ + static double theta1 = PI / 6.0; + static double theta2 = PI / 2.0; + static double temp = 0.0; + static int cts = 0; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp -= 2 * PI; + } + enemyBulletSound.play(); + FO SharpFlower1; + SharpFlower1.speed = speed; + SharpFlower1.width = 16; + SharpFlower1.height = 16; + SharpFlower1.hero.setTexture(allBullets1); + SharpFlower1.hero.setTextureRect(sf::IntRect(0, 64, 16, 16)); + SharpFlower1.hero.setOrigin(8, 8); + SharpFlower1.hero.setScale(1.5, 1.5); + SharpFlower1.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + if (cts % 2 == 0) + { + for (int i = 0; i < 3; i++) + { + SharpFlower1.theta = theta1 + PI * 2 * i / 3.0; + SharpFlower1.hero.setRotation(SharpFlower1.theta / PI * 180.0 + 90); + enemyBullets.push_back(SharpFlower1); + } + } + SharpFlower1.hero.setTextureRect(sf::IntRect(176, 64, 16, 16)); + SharpFlower1.hero.setOrigin(8, 8); + SharpFlower1.hero.setScale(1.5, 1.5); + SharpFlower1.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + if (temp >= 0 && temp <= PI) + { + SharpFlower1.speed /= 2.0; + SharpFlower1.speed += 9.0 / (temp + 1.5); + for (int i = 0; i < 3; i++) + { + SharpFlower1.theta = theta2 + PI * 2 * i / 3.0 + temp; + SharpFlower1.hero.setRotation(SharpFlower1.theta / PI * 180.0 + 90); + enemyBullets.push_back(SharpFlower1); + SharpFlower1.theta = theta2 + PI * 2 * i / 3.0 - temp; + SharpFlower1.hero.setRotation(SharpFlower1.theta / PI * 180.0 + 90); + enemyBullets.push_back(SharpFlower1); + } + } + + theta1 += 0.02 * PI; + temp += PI / 24.0; + if (sin(temp) < EPS) + { + theta2 = theta1 + PI / 6.0; + } +} + +void Game::nonSpellCard1(list::iterator it) +{ + static double theta1 = PI / 6.0; + static double theta2 = PI / 2.0; + static double temp = 0.0; + static int cts = 0; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp -= 2 * PI; + } + enemyBulletSound.play(); + FO nonSpell1; + nonSpell1.speed = 8.0; + nonSpell1.width = 16; + nonSpell1.height = 16; + nonSpell1.hero.setTexture(allBullets1); + nonSpell1.hero.setTextureRect(sf::IntRect(80, 64, 16, 16)); + nonSpell1.hero.setOrigin(8, 8); + nonSpell1.hero.setScale(1.5, 1.5); + nonSpell1.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + + for (int i = 0; i < 5; i++) + { + nonSpell1.theta = theta1 + PI * 2 * i / 5.0; + nonSpell1.hero.setRotation(nonSpell1.theta / PI * 180.0 + 90); + enemyBullets.push_back(nonSpell1); + } + + theta1 += 0.04 * PI * sin(temp); + temp += PI / 120.0; +} + +void Game::nonSpellCard2(list::iterator it) +{ + static double theta1 = PI / 6.0; + static double theta2 = PI / 2.0; + static double temp = 0.0; + static int cts = 0; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp = 0.0; + } + + FO nonSpell2; + nonSpell2.speed = 4.0; + nonSpell2.width = 16; + nonSpell2.height = 16; + nonSpell2.hero.setTexture(allBullets1); + nonSpell2.hero.setTextureRect(sf::IntRect(80, 128, 16, 16)); + nonSpell2.hero.setOrigin(8, 8); + nonSpell2.hero.setScale(1.5, 1.5); + nonSpell2.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + if (cts % 360 <= 120) + { + if (cts % 24 == 0) + { + enemyBulletSound.play(); + nonSpell2.hero.setPosition((rand() % 300) * 2 + 100, (rand() % 100) + 60); + for (int i = 0; i < 72; i++) + { + nonSpell2.theta = theta1 + PI * 2 * i / 72.0; + nonSpell2.hero.setRotation(nonSpell2.theta / PI * 180.0 + 90); + enemyBullets.push_back(nonSpell2); + } + } + } + theta1 += 0.04 * PI * sin(temp); + temp += PI / 240.0; +} + +void Game::nonSpellCard3(list::iterator it) +{ + static double theta1 = PI / 6.0; + static double theta2 = PI / 2.0; + static double temp = 0.0; + static int cts = 0; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp = 0.0; + } + enemyBulletSound.play(); + FO nonSpell3; + nonSpell3.speed = 6.0; + nonSpell3.width = 16; + nonSpell3.height = 16; + nonSpell3.hero.setTexture(allBullets1); + nonSpell3.hero.setTextureRect(sf::IntRect(80, 128, 16, 16)); + nonSpell3.hero.setOrigin(8, 8); + nonSpell3.hero.setScale(1.5, 1.5); + nonSpell3.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + if (cts % 2 == 0) + { + for (int i = 0; i < 10; i++) + { + nonSpell3.theta = theta1 + PI * 2 * i / 10.0; + nonSpell3.hero.setRotation(nonSpell3.theta / PI * 180.0 + 90); + enemyBullets.push_back(nonSpell3); + } + } + theta1 += 0.04 * PI * sin(temp); + temp += PI / 240.0; +} +//waiting for redoing +void Game::nonSpellCard4(list::iterator it) +{ + static double theta1 = PI / 6.0; + static double theta2 = PI / 2.0; + static double temp = 0.0; + static int cts = 0; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp = 0.0; + } + enemyBulletSound.play(); + FO nonSpell4; + nonSpell4.speed = 6.0; + nonSpell4.width = 16; + nonSpell4.height = 16; + nonSpell4.hero.setTexture(allBullets1); + nonSpell4.hero.setTextureRect(sf::IntRect(80, 128, 16, 16)); + nonSpell4.hero.setOrigin(8, 8); + nonSpell4.hero.setScale(1.5, 1.5); + if (cts % 2 == 0) + { + for (int i = 0; i < 3; i++) + { + nonSpell4.theta = theta1 + PI * 2 * i / 3.0; + nonSpell4.hero.setRotation(nonSpell4.theta / PI * 180.0 + 90); + nonSpell4.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + enemyBullets.push_back(nonSpell4); + if (cts % 7 == 0) + { + nonSpell4.hero.setTextureRect(sf::IntRect(160, 128, 16, 16)); + nonSpell4.hero.setPosition(it->hero.getPosition().x + 200, it->hero.getPosition().y + it->height + 100); + enemyBullets.push_back(nonSpell4); + nonSpell4.hero.setPosition(it->hero.getPosition().x - 200, it->hero.getPosition().y + it->height + 100); + enemyBullets.push_back(nonSpell4); + } + } + } + theta1 += 0.04 * PI * sin(temp); + temp += PI / 240.0; +} + +void Game::nonSpellCard5(list::iterator it) +{ + static double theta1 = PI / 6.0; + static double theta2 = PI / 2.0; + static double temp = 0.0; + static int cts = 0; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp = 0.0; + } + + FO nonSpell5; + nonSpell5.speed = 4.0; + nonSpell5.width = 16; + nonSpell5.height = 16; + nonSpell5.hero.setTexture(allBullets1); + nonSpell5.hero.setTextureRect(sf::IntRect(80, 128, 16, 16)); + nonSpell5.hero.setOrigin(8, 8); + nonSpell5.hero.setScale(1.5, 1.5); + nonSpell5.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + if (cts % 360 <= 240) + { + if (cts % 8 == 0) + { + enemyBulletSound.play(); + nonSpell5.hero.setPosition(it->hero.getPosition().x + 100*sin(cts/20.0), it->hero.getPosition().y + it->height); + for (int i = 0; i < 36; i++) + { + nonSpell5.theta = theta1 + PI * 2 * i / 36.0; + nonSpell5.hero.setRotation(nonSpell5.theta / PI * 180.0 + 90); + + enemyBullets.push_back(nonSpell5); + } + } + } + theta1 += 0.04 * PI * sin(temp); + temp += PI / 240.0; +} + +void Game::nonSpellCard6(list::iterator it) +{ + static double theta1 = PI / 6.0; + static double theta2 = -PI / 6.0; + static double temp = 0.0; + static int cts = 0; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp = 0.0; + } + + FO nonSpell6; + nonSpell6.speed = 4.0; + nonSpell6.width = 16; + nonSpell6.height = 16; + nonSpell6.hero.setTexture(allBullets1); + nonSpell6.hero.setTextureRect(sf::IntRect(32, 16, 16, 16)); + nonSpell6.hero.setOrigin(8, 8); + nonSpell6.hero.setScale(1.5, 1.5); + nonSpell6.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + if (cts % 48 == 0) + { + enemyBulletSound.play(); + for (int i = 0; i < 48; i++) + { + nonSpell6.theta = theta1 + PI * 2 * i / 48.0; + nonSpell6.hero.setRotation(nonSpell6.theta / PI * 180.0 + 90); + enemyBullets.push_back(nonSpell6); + } + } + if (cts % 2 == 0) + { + for (int i = 0; i < 5; i++) + { + nonSpell6.hero.setTextureRect(sf::IntRect(128, 16, 16, 16)); + nonSpell6.theta = theta2 + PI * 2 * i / 5.0; + nonSpell6.hero.setRotation(nonSpell6.theta / PI * 180.0 + 90); + enemyBullets.push_back(nonSpell6); + } + for (int i = 0; i < 5; i++) + { + nonSpell6.hero.setTextureRect(sf::IntRect(208, 16, 16, 16)); + nonSpell6.theta = theta1 + PI * 2 * i / 5.0; + nonSpell6.hero.setRotation(nonSpell6.theta / PI * 180.0 + 90); + enemyBullets.push_back(nonSpell6); + } + } + theta1 += 0.02 * PI; + theta2 -= 0.02 * PI; + temp += PI / 240.0; +} + +void Game::spellCard1(list::iterator it) +{ + static double range = 0, x1 = rand() % 200 + 250; + static int ct = 0, temp = 0; + + FO Card1; + Card1.speed = 8.0; + Card1.width = 16; + Card1.height = 16; + Card1.hero.setTexture(allBullets1); + Card1.hero.setTextureRect(sf::IntRect(16, 7 * 16, 16, 16)); + Card1.hero.setOrigin(8, 8); + Card1.hero.setScale(1.5, 1.5); + Card1.theta = 1.5 * PI; + Card1.type = 101; + if (temp % 10 == 0) + { + enemyBulletSound.play(); + Card1.hero.setPosition(it->hero.getPosition().x + range, it->hero.getPosition().y + it->height); + enemyBullets.push_back(Card1); + Card1.hero.setPosition(it->hero.getPosition().x - range, it->hero.getPosition().y + it->height); + enemyBullets.push_back(Card1); + Card1.hero.setPosition(x1 + range, it->hero.getPosition().y + it->height); + enemyBullets.push_back(Card1); + Card1.hero.setPosition(x1 - range, it->hero.getPosition().y + it->height); + enemyBullets.push_back(Card1); + } + + (ct % 2) ? range -= 16 : range += 16; + temp++; + if (range >= 350 || range <= 0) + { + ct++; + } +} + +void Game::spellCard2(list::iterator it) +{ + double speed = 4; + int type = 4, color = 0; + static double theta1 = PI / 6.0; + static double theta2 = PI / 2.0; + static double temp = 0.0; + static int cts = 0; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp = 0.0; + } + enemyBulletSound.play(); + FO Card2; + Card2.speed = speed; + Card2.width = 16; + Card2.height = 16; + Card2.hero.setTexture(allBullets1); + Card2.hero.setTextureRect(sf::IntRect(0, 64, 16, 16)); + Card2.hero.setOrigin(8, 8); + Card2.hero.setScale(1.5, 1.5); + Card2.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + + for (int i = 0; i < 3; i++) + { + Card2.theta = theta1 + PI * 2 * i / 3.0; + Card2.hero.setRotation(Card2.theta / PI * 180.0 + 90); + enemyBullets.push_back(Card2); + } + + Card2.hero.setTextureRect(sf::IntRect(176, 64, 16, 16)); + Card2.hero.setOrigin(8, 8); + Card2.hero.setScale(1.5, 1.5); + Card2.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + if (temp >= 0 && temp <= PI) + { + Card2.speed /= 2.0; + Card2.speed += 9.0 / (temp + 1.5); + for (int i = 0; i < 3; i++) + { + Card2.theta = theta2 + PI * 2 * i / 3.0 + temp; + Card2.hero.setRotation(Card2.theta / PI * 180.0 + 90); + enemyBullets.push_back(Card2); + Card2.theta = theta2 + PI * 2 * i / 3.0 - temp; + Card2.hero.setRotation(Card2.theta / PI * 180.0 + 90); + enemyBullets.push_back(Card2); + } + } + + theta1 += 0.02 * PI; + temp += PI / 24.0; + if (sin(temp) < EPS) + { + theta2 = theta1 + PI / 6.0; + } +} + +void Game::spellCard3(list::iterator it) +{ + static double theta1 = PI / 6.0; + static double theta2 = PI / 2.0; + static double temp = 0.0; + static int cts = 0; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp = 0.0; + } + + FO Card3; + Card3.speed = 2.0; + Card3.width = 16; + Card3.height = 16; + Card3.hero.setTexture(allBullets1); + Card3.hero.setTextureRect(sf::IntRect(80, 16, 16, 16)); + Card3.hero.setOrigin(8, 8); + Card3.hero.setScale(1.5, 1.5); + Card3.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + Card3.type = 103; + Card3.phase = 3; + if (cts % 360 == 1) + { + enemyBulletSound.play(); + for (int i = 0; i < 6; i++) + { + Card3.theta = theta1 + PI * 2 * i / 6.0; + Card3.hero.setRotation(Card3.theta / PI * 180.0 + 90); + enemyBullets.push_back(Card3); + } + } + theta1 += 0.04 * PI * sin(temp); + temp += PI / 240.0; +} + +void Game::spellCard4(list::iterator it) +{ + enemyBulletSound.play(); + FO Card4; + Card4.speed = 4.0; + Card4.theta = rand() % 360; + Card4.width = 16; + Card4.height = 16; + Card4.hero.setTexture(allBullets1); + Card4.hero.setTextureRect(sf::IntRect(rand()%15 * 16, 4 * 16, 16, 16)); + Card4.hero.setOrigin(8, 8); + Card4.hero.setScale(1.5, 1.5); + Card4.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + Card4.hero.setRotation(Card4.theta / PI * 180.0 + 90); + Card4.type = 104; + enemyBullets.push_back(Card4); + Card4.theta = rand() % 360; + enemyBullets.push_back(Card4); +} + +void Game::spellCard5(list::iterator it) +{ + static double theta1 = PI / 2.0; + static double theta2 = PI / 2.0; + static double temp = 0.0; + static int cts = 0; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp = 0.0; + } + + FO Card5; + Card5.speed = 4.0; + Card5.width = 16; + Card5.height = 16; + Card5.hero.setTexture(allBullets1); + Card5.hero.setTextureRect(sf::IntRect(32, 16, 16, 16)); + Card5.hero.setOrigin(8, 8); + Card5.hero.setScale(1.5, 1.5); + Card5.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + if (cts % 72 == 0) + { + enemyBulletSound.play(); + for (int i = 0; i < 72; i++) + { + Card5.theta = theta1 + PI * 2 * i / 72.0; + Card5.hero.setRotation(Card5.theta / PI * 180.0 + 90); + enemyBullets.push_back(Card5); + } + } + if (cts % 8 == 0) + { + Card5.speed = 7.0; + for (int i = 0; i < 8; i++) + { + Card5.hero.setTextureRect(sf::IntRect(128, 16, 16, 16)); + Card5.theta = theta2 + PI * 2 * i / 8.0; + Card5.hero.setRotation(Card5.theta / PI * 180.0 + 90); + enemyBullets.push_back(Card5); + } + for (int i = 0; i < 8; i++) + { + Card5.hero.setTextureRect(sf::IntRect(208, 16, 16, 16)); + Card5.theta = theta1 + PI * 2 * i / 8.0; + Card5.hero.setRotation(Card5.theta / PI * 180.0 + 90); + enemyBullets.push_back(Card5); + } + } + theta1 += 0.003 * PI; + theta2 -= 0.003 * PI; + temp += PI / 240.0; +} + +void Game::spellCard6(list::iterator it) +{ + static double theta1 = PI / 6.0; + static double theta2 = PI / 2.0; + static double temp = 0.0; + static int cts = 0, cc = 40; + cts++; + if (fabs(temp - 2 * PI) < EPS) + { + temp = 0.0; + } + + FO Card6; + Card6.speed = 4.0; + Card6.width = 32; + Card6.height = 32; + Card6.hero.setTexture(allBullets2); + Card6.hero.setTextureRect(sf::IntRect(64, 96, 32, 32)); + Card6.hero.setOrigin(16, 16); + Card6.hero.setScale(1.5, 1.5); + Card6.hero.setPosition(it->hero.getPosition().x, it->hero.getPosition().y + it->height); + + if (cts % 60 == 0) + { + enemyBulletSound.play(); + for (int i = 0; i < 36; i++)//(rand() % 500)+ 100 + { + Card6.theta = theta1 + PI * 2 * i / 36.0; + Card6.hero.setRotation(Card6.theta / PI * 180.0 + 90); + enemyBullets.push_back(Card6); + } + } + if ((cts % (2*cc)) < cc) + { + if (cts % 19 == 0) + { + Card6.hero.setTextureRect(sf::IntRect(224, 96, 32, 32)); + Card6.speed = 8.0; + enemyBulletSound.play(); + Card6.hero.setPosition(it->hero.getPosition().x+100, it->hero.getPosition().y + it->height); + Card6.theta = atan2(julgePoint.getPosition().y - Card6.hero.getPosition().y, julgePoint.getPosition().x - Card6.hero.getPosition().x); + Card6.hero.setRotation(Card6.theta / PI * 180.0 + 90); + enemyBullets.push_back(Card6); + Card6.hero.setPosition(it->hero.getPosition().x-100, it->hero.getPosition().y + it->height); + Card6.theta = atan2(julgePoint.getPosition().y - Card6.hero.getPosition().y, julgePoint.getPosition().x - Card6.hero.getPosition().x); + Card6.hero.setRotation(Card6.theta / PI * 180.0 + 90); + enemyBullets.push_back(Card6); + if (cc <= 300) + { + cc++; + } + } + } + + theta1 += 0.04 * PI * sin(temp); + temp += PI / 240.0; +} + +void Game::processTaps() +{ + sf::Event event; + while (mWindow.pollEvent(event)) + { + switch (event.type) + { + case sf::Event::KeyPressed: + playerInput(event.key.code, true); + break; + case sf::Event::KeyReleased: + playerInput(event.key.code, false); + break; + case sf::Event::Closed: + mWindow.close(); + break; + default: + break; + } + } +} + +void Game::playerInput(sf::Keyboard::Key key, bool isPressed) +{ + if (key == sf::Keyboard::Up) + mIsMovingUp = isPressed; + else if (key == sf::Keyboard::Down) + mIsMovingDown = isPressed; + else if (key == sf::Keyboard::Left) + mIsMovingLeft = isPressed; + else if (key == sf::Keyboard::Right) + mIsMovingRight = isPressed; + else if (key == sf::Keyboard::Z) + mIsFire = isPressed; + else if (key == sf::Keyboard::LShift) + mIsGrazing = isPressed; + player.speed = (mIsGrazing) ? 3.0 : 10.0; +} + +bool isOutOfBoard(sf::Sprite value) +{ + if (value.getPosition().y >= 0 && value.getPosition().y <= 960 && value.getPosition().x >= 0 && value.getPosition().x <= 960) + { + return false; + } + return true; +} + +bool isFOOutOfBoard(const FO value) +{ + if (value.hero.getPosition().y >= -100 && value.hero.getPosition().y <= 960 && value.hero.getPosition().x >= 0 && value.hero.getPosition().x <= 960) + { + return false; + } + return true; +} + +void Game::mainProcessing() +{ + if (mIsMovingUp == true && player.hero.getPosition().y > 40) + { + //player.theta = PI*0.5; + player.hero.move(0.0, float(-player.speed)); + } + if (mIsMovingDown == true && player.hero.getPosition().y < 850) + { + //player.theta = PI*1.5; + player.hero.move(0.0, float(player.speed)); + } + if (mIsMovingLeft == true && player.hero.getPosition().x > 69) + { + //player.theta = PI; + player.hero.move(float(-player.speed), 0.0); + } + if (mIsMovingRight == true && player.hero.getPosition().x < 751) + { + //player.theta = 0.0; + player.hero.move(float(player.speed), 0.0); + } + + /*if ((mIsMovingDown || mIsMovingLeft || mIsMovingRight || mIsMovingUp) && !isOutOfBoard(player.hero)) + { + player.velocity.x = player.speed * cos(player.theta); + player.velocity.y = player.speed * sin(player.theta); + player.hero.move(player.velocity); + }*/ + + if (mIsFire) + { + if (playerBulletSound.getStatus() != playerBulletSound.Playing) + { + playerBulletSound.play(); + } + } +} + +bool Game::checkCollision(sf::Sprite obj1, sf::Sprite obj2) +{ + sf::FloatRect f1 = obj1.getGlobalBounds(); + sf::FloatRect f2 = obj2.getGlobalBounds(); + if (f1.intersects(f2)) + { + return true; + } + return false; +} + +bool Game::checkPlayerCollision() +{ + sf::Vector2f JP = julgePoint.getPosition(); + JP.x -= 8; + JP.y -= 8; + /*for (list::iterator it = enemies.begin(); it != enemies.end(); it++) + { + sf::FloatRect f = it->getGlobalBounds(); + + f.width /= 2.0; + f.height /= 2.0; + if (f.contains(JP)) + { + return true; + } + }*/ + for (list::iterator it = enemyBullets.begin(); it != enemyBullets.end(); it++) + { + sf::FloatRect f = it->hero.getGlobalBounds(); + + f.width /= 2.0; + f.height /= 2.0; + if (f.contains(JP)) + { + return true; + } + } + return false; +} + +void Game::GameOver() +{ + //ϣ򵥴ֱGameOverϷ飨 + exit(0); +} + +/*DWORD WINAPI BGMPlay(LPVOID lpParameter) +{ + Game* game = (Game*)lpParameter; + int i = 0; + while (1) + { + i %= 10; + i++; + if (game->mIsFire && i == 1) + { + if (game->playerBulletSound.getStatus() != game->playerBulletSound.Playing) + { + game->playerBulletSound.play(); + } + + // + } + } + return 0; +}*/ + +Game::~Game() +{ + ; +} diff --git a/practices/cpp/level1/p11_Fighters/Game.h b/practices/cpp/level1/p11_Fighters/Game.h new file mode 100644 index 00000000..952a3ba7 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/Game.h @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include "FO.h" +#include +using namespace std; + +class Game +{ +public: + Game(); + void NowLoading(); + void loadBackgrounds(); + void loadPrimeFrame(); + void loadPointsAndEffs(); + void loadEnemy(); + void loadMusicAndSounds(); + void run(); + void menu(); + void Stage1(); + int S1E1(); + int S1E2(); + int S1E3(); + int S1E4(); + int S1E5(); + int S1E6(); + int S1E7(); + + int S1E8(); + + int S1E9(); + int S1E10(); + int S1E11(); + + int S1E12(); + + int S1E13(); + + int S1E14(); + void enemiesPushToDraw(list::iterator it); + void backEsPushToDraw(list::iterator it); + void frameDisplay(); + void backgroundDisplay(); + void playerAmmoDisplay(); + void enemiesDisplay(); + //void enemyBulletsPreDisplay(); + void enemyBulletsDisplay(); + void playerDisplay(); + void effsDisplay(); + void boardDisplay(); + void enemyCollisionProcessing(list::iterator it); + void enemyUnderAttack(list::iterator it, list::iterator itAmmo); + void enemyCrash(list::iterator it); + void standardSButterflyFrame(list::iterator it, int temp); + void standardMButterflyFrame(list::iterator it, int temp); + + void setSnipe(list::iterator it, double speed, int type, int color); + // + void setRoundSnipe(list::iterator it, double speed); + // + void setMultiRoundSnipe(list::iterator it, double speed, int color); + + void setGeneralMultiSnipe(list::iterator it, double speed, int type, int color, double angle); + + void setRandom(list::iterator it, double speed, int type, int color, double leftBoarder, double range); + // + void setRoundRandom(list::iterator it, double speed, int color, double leftBoarder, double range); + // + void setSharpRandom(list::iterator it, double speed); + + void setSharpLine(list::iterator it, double speed); + + void setSharpFlower1(list::iterator it, double speed, int type, int color); + + void nonSpellCard1(list::iterator it); + void nonSpellCard2(list::iterator it); + void nonSpellCard3(list::iterator it); + void nonSpellCard4(list::iterator it); + void nonSpellCard5(list::iterator it); + void nonSpellCard6(list::iterator it); + void spellCard1(list::iterator it); + void spellCard2(list::iterator it); + void spellCard3(list::iterator it); + void spellCard4(list::iterator it); + void spellCard5(list::iterator it); + void spellCard6(list::iterator it); + + void processTaps(); + void playerInput(sf::Keyboard::Key key, bool isPressed); + friend bool isOutOfBoard(sf::Sprite value);//wait for updating + friend bool isFOOutOfBoard(FO value);//wait for updating + void mainProcessing(); + bool checkCollision(sf::Sprite obj1, sf::Sprite obj2); + bool checkPlayerCollision(); + void GameOver(); + /*friend DWORD WINAPI BGMPlay(LPVOID lpParameter);*/ + ~Game(); + +private: + Game* self = this; + bool mIsMovingUp, mIsMovingDown, mIsMovingLeft, mIsMovingRight, mIsGrazing, mIsFire; + sf::RenderWindow mWindow; + sf::Font font; + sf::Text text, tempScore; + sf::Texture loading, nowLoading, stageSelect, front00, julgePointArray, Title1, Title2, allBullets1, allBullets2, whiteSpark; + sf::Texture bullets, buffetsEff, deathCircle, bg1, bgEff1, bg2, bgEff2, bg3, bgEff3, Enemy1, Enemy2, Enemy3, lifePieces, magicSquare; + sf::Sprite loadingUI, loadingUISub, back[6], backEff[6], front01, front02, front03, front04; + sf::Sprite julgePoint, playerAmmo, AmmoEff, deathEff, lifeBoard; + sf::Music menuMusic, stage1BGM, stage2BGM, stage3BGM; + sf::SoundBuffer playerBulletSoundBuffer, playerBulletSoundBuffer1, enemyBulletSoundBuffer, collisionSoundBuffer, spellCardSoundBuffer, buttomSoundBuffer; + sf::SoundBuffer breakSoundBuffer, playerDeadSoundBuffer, SCAnounceBuffer, cardGetBuffer; + sf::Sound playerBulletSound, playerBulletSound1, enemyBulletSound, collisionSound, spellCardSound, buttomSound, SCAnounce, cardGet; + sf::Sound breakSound, playerDeadSound; + sf::Clock clock; + FO player; + list enemyBullets, enemyBulletsPre; + list playerBullets, deathEffs; + list enemies, playerBulletsEffs, backgroundEffs; + long long remnant, score; +}; + diff --git a/practices/cpp/level1/p11_Fighters/main.cpp b/practices/cpp/level1/p11_Fighters/main.cpp new file mode 100644 index 00000000..f9ec5fdf --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/main.cpp @@ -0,0 +1,12 @@ +#include +#include +#include +#include +#include "Game.h" + +int main(int argc, char* argv[]) +{ + Game game; + game.run(); + return 0; +} \ No newline at end of file