commit 85e278c572f21948acc542a97de4c10f0b2cd4bf Author: Matin Date: Mon Apr 20 07:59:37 2026 +0330 final diff --git a/final.cpp b/final.cpp new file mode 100644 index 0000000..680f88b --- /dev/null +++ b/final.cpp @@ -0,0 +1,863 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std ; + +struct Player{ + string name; + int score; +}; + +struct Paddle{ + int x; + int y; + int arz; +}; + +struct Ball{ + char model; + int x; + int y; + int vx; + int vy; +}; + +struct Stone{ + char model; + int x; + int y; + bool alive; +}; + +Paddle pedal; +Ball ball; +Stone enemy[10][4]; +Player player; +int heart = 1; +int menu_cursor = -1; +int gameWidth = 50; +int gameHeight = 20; +int countOfEnemy; +float score = 0 ; + +void menu(); +void makeElements(); +void printmap(); +void gameLop(); +void pedalplay(); +bool checkCollision(); +void checkDirection(); +void countAliveEnemy(); +void countscore(); +void gotoxy(int x , int y); +void hideCursor(); +void map(); +void gameOver(); +void win(); +void choose(); + + + +int main (){ + hideCursor(); + system("chcp 65001"); // تغییر کدپیج به UTF-8 + system("cls"); + + menu(); + switch (menu_cursor){ + case 0: + { + makeElements(); + system("cls"); + cout << "Please Enter your name" << endl; + cin >> player.name; + gameLop(); + break; + } + case 1: + { + + } + case 2: + { + + } + case 3: + { + + } + case 4: + { + + } + case 5: + { + return 0; + } + default: + menu(); + break; + } + +return 0 ; +} + + + +void menu(){ + string menu[6]; + menu[0] = "New Game"; + menu[1] = "Load Game"; + menu[2] = "Help"; + menu[3] = "Game History"; + menu[4] = "Setting"; + menu[5] = "Exit"; + char a; + system("cls"); + + cout << "╔═════════════════╗" << endl ; + for(int i = 0 ; i < 6 ; i++){ + cout <<" "<< menu[i] << endl; + } + cout << "╚═════════════════╝" << endl ; + + do{ + + a = getch(); + if (a == 'w' || a == 'W' ) menu_cursor--; + if (a == 's' || a == 'S' ) menu_cursor++; + while (menu_cursor <0){ + menu_cursor +=6 ; + } + if(menu_cursor >=6) menu_cursor = menu_cursor % 6; + + system("cls"); + + cout << "╔═════════════════╗" << endl ; + + for (int i = 0 ; i < 6 ; i++){ + if(i == menu_cursor){ + cout << "\033[31m" << " << " <>" << "\033[0m" << endl; + } + else { + cout << " " << menu[i] << endl ; + } + } + cout << "╚═════════════════╝" << endl ; + + + }while(a != '\r'); + +} + + +void makeElements(){ + for (int i = 0 ; i < 10 ; i++){ // enemy + for(int j = 0 ; j < 4 ; j++){ + enemy[i][j].model = 'X'; + enemy[i][j].x = i+21; + enemy[i][j].y = j+3; + enemy[i][j].alive = true; + } + } + + ball.model = 'O'; + ball.x = 25; + ball.y = 18; + int sign = rand() % 2; + if (sign == 1){ + ball.vx = -1*(rand() %3 +1); + } + else{ + ball.vx = rand() %3 +1; + } + ball.vy = -1; + + pedal.x = 25; + pedal.y = 19; + pedal.arz = 14 ; + +} + +void printmap(){ + countAliveEnemy(); + countscore(); + + cout << setw(30) << "Score: " << score << endl; + cout << setw(28)<< "\033[31m" << "Heart: " << heart << "\033[0m" << endl ; + cout << setw(35) << "Count of enemy: " << countOfEnemy << endl; + for (int j = 0 ; j <=gameHeight ; j++){ + for (int i = 0 ; i <= gameWidth ; i++){ + if (i == 0 && j == 0) cout << "┌"; + else if (j == 0 && i != 0 && i != gameWidth )cout <<"─"; + else if (j== 0 && i == gameWidth) cout << "┐" << endl; + + else if ( j == pedal.y && i < pedal.x + (pedal.arz)/2 - 2 && i > pedal.x - (pedal.y)/2 +2 ) cout << "─"; + + else if (i == 0 && j != 0 && j != gameHeight) cout <<"│"; + else if (i == gameWidth && j != 0 && j != gameHeight ) cout <<"│" << endl; + + else if ( i == 0 && j == gameHeight) cout << "└"; + else if (j == gameHeight && i != 0 && i != gameWidth) cout << "─" ; + else if (j== gameHeight && i == gameWidth) cout <<"┘"; + else if (i == ball.x && j == ball.y) cout << ball.model; + else if (i == enemy[i-21][j-3].x && j == enemy[i-21][j-3].y){ + if(enemy[i-21][j-3].alive == true) cout << enemy[i-21][j-3].model; + else cout << " "; + } + + else cout << " "; + } + } +} + +void gameLop(){ + bool gameover = false; + char act ; + + while(true) + { + gotoxy(0,0); + printmap(); + + countAliveEnemy(); + if(countOfEnemy==0){ + win(); + break; + } + + //از اینجا به بعد بررسی وضعیت بازی + if (ball.y >= gameHeight){ + if (heart == 1) + { + gameOver(); + break; + } + else { + if(heart % 2 == 0){ + ball.x = 25; + } + else{ + ball.x = 24; + } + pedal.x = 25; + + ball.y = 18; + int sign = rand() % 2; + if (sign == 1){ + ball.vx = -1*(rand() %3 +1); + } + else{ + ball.vx = rand() %3 +1; + } + + ball.vy = -1; + + heart--; + Sleep(1000); + continue; + + + } + } + + // کنترل پدال بدون توقف حرکت توپ + if ( kbhit()){ + act = getch(); + if (act == 'a' || act == 'A') + { + if (pedal.x - (pedal.arz)/2 >= 2){ + pedal.x -= 2; + } + } + if (act == 'd' || act == 'D') { + if(pedal.x + (pedal.arz)/2 <=gameWidth){ + pedal.x +=2; + } + } + + if ( act == 'q' || act == 'Q' ) break ; + + } + + + // اینجا ببینم مرحله بعدی کجاست اصلا برخورد میکنه یا نه + // اگه برخورد کرد که باید بریم یه جای دیگه + // اگه نکرد پایین + if(checkCollision()){ + ball.model = '#'; + checkDirection(); + } + + else{ + ball.model = 'O'; + ball.x += ball.vx; + ball.y += ball.vy; + } + if (ball.x >= gameWidth-1 || ball.x <= 1){ + ball.vx = -ball.vx; + } + if(ball.y <= 1 ){ + ball.vy = - ball.vy; + } + + pedalplay(); + if (countOfEnemy>30){ + Sleep(80); + } + else if(countOfEnemy>20){ + Sleep(70); + } + else if(countOfEnemy>10 && countOfEnemy<=20){ + Sleep(60); + } + else if(countOfEnemy>0 && countOfEnemy<=10){ + Sleep(50); + } + } +} + +void map(){ + system("cls"); + for(int i=0; i<=gameHeight; i++){ + for(int j=0; j<=gameWidth; j++){ + if(i==0 && j==0) cout << "┌"; + else if(i==0 && j==gameWidth) cout << "┐"; + else if(i==gameHeight && j==0) cout << "└"; + else if(i==gameHeight && j==gameWidth) cout << "┘"; + else if(i==0 || i==gameHeight) cout << "─"; + else if(j==0 || j==gameWidth) cout << "│"; + else cout << " "; + } + cout << endl; + } +} + +void gameOver(){ + map(); + string f = "==== GAME OVER ===="; + string s = "Player: " + player.name; + string t = "Score: " + to_string(score); //change int to string + + gotoxy( gameWidth/2 - f.size()/2 , gameHeight/2 -1 ); + cout << f; + gotoxy( gameWidth/2 - s.size()/2 , gameHeight/2 ); + cout << s; + gotoxy( gameWidth/2 - t.size()/2 , gameHeight/2 +1 ); + cout << t; + gotoxy( gameWidth+1 , gameHeight+1 ); + + choose(); +} + +void win(){ + map(); + string f = "*** YOU WIN! ***"; + string s = "Player: " + player.name; + string t = "Score: " + to_string(score); + + gotoxy( gameWidth/2 - f.size()/2 , gameHeight/2 -1 ); + cout << f; + gotoxy( gameWidth/2 - s.size()/2 , gameHeight/2 ); + cout << s; + gotoxy( gameWidth/2 - t.size()/2 , gameHeight/2 +1 ); + cout << t; + gotoxy( gameWidth+1 , gameHeight+1 ); + + choose(); +} + +void choose() { + char choose; + gotoxy(0, gameHeight + 2); + cout << "If you want to play a new game, press the M button, and if you want to exit, press the Q button." << endl; + + do { + choose = getch(); + if (choose == 'm' || choose == 'M') { + system("cls"); + menu_cursor = -1;//no choise in menu + return; + } + else if (choose == 'q' || choose == 'Q') { + exit(0); + } + } while (true); + +} + +void pedalplay(){ + if (ball.y == pedal.y && ball.x < pedal.x + (pedal.arz)/2 - 2 && ball.x > pedal.x - (pedal.y)/2 +2){ + ball.vy = -ball.vy; + // markaz = pedal.x -1 + if (ball.x - (pedal.x -1) == 0) ball.vx = 0; + else if (ball.x - (pedal.x -1) == 1 ||ball.x - (pedal.x -1) == -1|| ball.x - (pedal.x -1) == 2 || ball.x - (pedal.x -1) == -2 ){ + if (ball.vx > 0) ball.vx = +1; + else if (ball.vx <0) ball.vx = -1; + else{ + if(ball.x - (pedal.x -1) == 1 || ball.x - (pedal.x -1) == 2) ball.vx =1; + else ball.vx = -1; + } + } + else if (ball.x - (pedal.x -1) == 3 || ball.x - (pedal.x -1) == -3 || ball.x - (pedal.x -1) == 4 || ball.x - (pedal.x -1) == -4){ + if (ball.vx > 0) ball.vx = +2; + else if (ball.vx <0) ball.vx = -2; + else{ + if(ball.x - (pedal.x -1) == 3 || ball.x - (pedal.x-1) == 4) ball.vx =2; + else ball.vx = -2; + } + } + else { + if (ball.vx > 0) ball.vx = +3; + else if (ball.vx<0) ball.vx = -3; + else{ + if(ball.x - (pedal.x-1) > 0 ) ball.vx = 3 ; + else ball.vx = -3 ; + } + } + } + + +} + +bool checkCollision(){ + if(ball.vx == 0 && ball.vy ==-1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31; j++){ + if (ball.x == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.y += -1; + return true; + } + } + } + return false; + } + + else if(ball.vx == 0 && ball.vy == 1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.y += 1; + return true; + } + } + } + return false; + } + + else if (ball.vx == 1 && ball.vy ==-1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 1 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += 1; + ball.y += -1; + return true; + } + } + } + return false; + } + + else if (ball.vx == 2 && ball.vy ==-1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 1 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=1 ; + ball.y += -1; + return true; + } + } + } + + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 2 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=2 ; + ball.y += -1; + return true; + } + } + } + + return false; + + } + + else if (ball.vx == 3 && ball.vy ==-1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 1 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=1 ; + ball.y += -1; + return true; + } + } + } + + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 2 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=2 ; + ball.y += -1; + return true; + } + } + } + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 3 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.vx +=3 ; + ball.vy += -1; + return true; + } + } + } + + return false; + + } + + else if (ball.vx == 1 && ball.vy ==1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 1 == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += 1; + ball.y += 1; + return true; + } + } + } + return false; + } + + else if (ball.vx == 2 && ball.vy ==1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 1 == j && ball.y + 1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=1 ; + ball.y +=1; + return true; + } + } + } + + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 2 == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=2 ; + ball.y +=1; + return true; + } + } + } + + return false; + + } + + else if (ball.vx == 3 && ball.vy == 1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 1 == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=1 ; + ball.y += +1; + return true; + } + } + } + + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 2 == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=2 ; + ball.y += 1; + return true; + } + } + } + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x + 3 == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=3 ; + ball.y += 1; + return true; + } + } + } + + return false; + + } + + else if (ball.vx == -1 && ball.vy ==-1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 1 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += -1; + ball.y += -1; + return true; + } + } + } + return false; + } + + else if (ball.vx == -2 && ball.vy ==-1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 1 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=-1 ; + ball.y += -1; + return true; + } + } + } + + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 2 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += -2 ; + ball.y += -1; + return true; + } + } + } + + return false; + + } + + else if (ball.vx == -3 && ball.vy ==-1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 1 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=-1 ; + ball.y += -1; + return true; + } + } + } + + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 2 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x +=-2 ; + ball.y += -1; + return true; + } + } + } + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 3 == j && ball.y -1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += -3 ; + ball.y += -1; + return true; + } + } + } + + return false; + + } + + else if (ball.vx == -1 && ball.vy ==1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 1 == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += -1; + ball.y += 1; + return true; + } + } + } + return false; + } + + else if (ball.vx == -2 && ball.vy ==1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 1 == j && ball.y + 1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += -1 ; + ball.y +=1; + return true; + } + } + } + + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 2 == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += -2 ; + ball.y +=1; + return true; + } + } + } + + return false; + + } + + else if (ball.vx == -3 && ball.vy == 1){ + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 1 == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += -1 ; + ball.y += +1; + return true; + } + } + } + + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 2 == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += -2 ; + ball.y += 1; + return true; + } + } + } + + for(int i = 3 ; i < 7 ; i++){ + for(int j = 21 ; j < 31 ; j++){ + if (ball.x - 3 == j && ball.y +1 == i && enemy[j-21][i-3].alive == true){ + enemy[j-21][i-3].alive = false; + ball.x += -3 ; + ball.y += 1; + return true; + } + } + } + + return false; + + } + return false; +} + +void checkDirection(){ + if (ball.vx > 0 && ball.vy <0){ + if (ball.x -1 >= 21 && ball.y +1 <= 6 && enemy[(ball.x-21)-1][ball.y-3].alive == true && enemy[ball.x-21][(ball.y)-3+1].alive == true){ + ball.vx *= -1; + ball.vy *= -1; + return; + } + } + + if (ball.vx > 0 && ball.vy > 0){ + if (ball.x - 1 >= 21 && ball.y - 1 >= 3 && enemy[(ball.x-21)-1][ball.y-3].alive == true && enemy[ball.x-21][(ball.y-3)-1].alive == true){ + ball.vx *= -1; + ball.vy *= -1; + return; + } + } + + if (ball.vx < 0 && ball.vy > 0){ + if (ball.x + 1 <= 30 && ball.y - 1 >= 3 && enemy[(ball.x-21)+1][ball.y-3].alive == true && enemy[ball.x-21][(ball.y-3)-1].alive == true){ + ball.vx *= -1; + ball.vy *= -1; + return; + } + } + + if (ball.vx < 0 && ball.vy < 0){ + if (ball.x + 1 <= 30 && ball.y + 1 <= 6 && enemy[(ball.x-21)+1][ball.y-3].alive == true && enemy[ball.x-21][(ball.y-3)+1].alive == true){ + ball.vx *= -1; + ball.vy *= -1; + return; + } + } + + + + if(ball.vy > 0){ + if( ball.y-4 >=0 && enemy[ball.x-21][ball.y-4].alive == true){ + ball.vx *=-1; + } + else{ + ball.vy *=-1; + } + } + else{ + if(ball.y - 2 < 4 && enemy[ball.x-21][ball.y-2].alive == true){ + ball.vx *=-1; + } + else{ + ball.vy *=-1; + } + } +} + +void countAliveEnemy(){ + countOfEnemy = 0 ; + for (int i = 0 ; i < 4 ; i++){ + for (int j = 0 ; j< 10 ; j++){ + if (enemy[j][i].alive == true) countOfEnemy++; + } + } +} + + +void countscore(){ + score = (40 - countOfEnemy) * 2.5; +} + +//windows API +void gotoxy (int x , int y){ + COORD pos; + pos.X = x; + pos.Y = y; + SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE) , pos); +} + +//windows API +void hideCursor(){ + HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_CURSOR_INFO info; + info.dwSize = 1; // اندازه کرسر + info.bVisible = FALSE; // مخفی + SetConsoleCursorInfo(h, &info); +}