ParsPatogh
Registered User
درود
بلاخره بازی رو نوشتم تموم شد :دی
بازی امکاناتی چون امتیاز دادن و سیو و لود بازی رو داره.
برای توقف بازی هم از کلید space استفاده کنید.
فایل اجراییش رو هم ضمیمه کردم.
ممنون میشم نظرتون رو در موردش بدونم :wub:
با تشکر از همه
بلاخره بازی رو نوشتم تموم شد :دی
بازی امکاناتی چون امتیاز دادن و سیو و لود بازی رو داره.
برای توقف بازی هم از کلید space استفاده کنید.
فایل اجراییش رو هم ضمیمه کردم.
ممنون میشم نظرتون رو در موردش بدونم :wub:
با تشکر از همه
کد:
#include <conio.h>
#include <Windows.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <fstream>
using namespace std;
//****************************
int screen[30][25]={0};
int num_1 = rand()%7;
int num = 0;
int tedadyekha=0;
bool space=false;
int emtiaz=0;
//*********************************************
int shapeTable[7][4][2]={
{{0,-1},{0,0},{-1,0},{-1,1}},
{{0,-1},{0,0},{1,0},{1,1}},
{{0,-1},{0,0},{0,1},{0,2}},
{{-1,0},{0,0},{1,0},{0,1}},
{{0,0},{1,0},{0,1},{1,1}},
{{-1,-1},{0,-1},{0,0},{0,1}},
{{1,-1},{0,-1},{0,0},{0,1}}
};
//***********************************************
int x=17,y=0;
int shape[4][2];
void showShape(char);
void keyF(char);
void save_screen();
void menu();
void game_start();
void about();
void pause_menu();
//*********************************************************//
void gotoxy(int x, int y){
HANDLE stdOutput;
COORD pos;
stdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(stdOutput, pos);
return;
}
//******************************************************//
bool noCollision(){
for(int i=0;i<4;i++)
if(screen[x+shape[i][0]][y+shape[i][1]+1]==1)
return false;
return true;
}
//******************************************************//
bool noCollisionPrim(){
for(int i=0;i<4;i++)
if(screen[x+shape[i][0]][y+shape[i][1]+2]==1)
return false;
return true;
}
//*********************************************************//
bool rightCollision(){
for(int i=0;i<4;i++)
if(screen[x+shape[i][0]+1][y+shape[i][1]]==1)
return false;
return true;
}
//********************************************************//
bool leftCollision(){
for(int i=0;i<4;i++)
if(screen[x+shape[i][0]-1][y+shape[i][1]]==1)
return false;
return true;
}
//***********************************************************//
bool rotateCollision(){
int z;
int shapeprim[4][2];
for (int i=0;i<4;i++){
for (int j=0;j<2;j++){
shapeprim[i][j]=shape[i][j];
}
}
for(int i=0;i<4;i++){
z=shapeprim[i][1];
shapeprim[i][1]=-shapeprim[i][0];
shapeprim[i][0]=z;
}
for(int i=0;i<4;i++)
if(screen[x+shapeprim[i][0]][y+shapeprim[i][1]]==true)
return false;
return true;
}
//**************************************************************//
void rotateHigh(void){
int z;
for(int i=0;i<4;i++)
{
z=shape[i][1];
shape[i][1]=-shape[i][0];
shape[i][0]=z;
}
}
//***************************************************************//
void table(){
char ch1=219;
char ch2=219;
for(int i=1;i<=24;i++){
gotoxy(5,i);
printf("%c",ch1);
gotoxy(29,i);
printf("%c",ch1);
}
for(int j=5;j<29;j++){
gotoxy(j,24);
printf("%c",ch2);
}
}
//***************************************************************//
void keyF(char c){
if(c<0)
c=getch();
switch(c)
{
case 80:
if(noCollisionPrim()){
showShape(' ');
y++;
}
break;
case 75:
if(leftCollision()){showShape(' ');x--;}
break;
case 77:
if(rightCollision()){
showShape(' ');
x++;
}
break;
case 72:
if (rotateCollision()){
showShape(' ');
rotateHigh();
}
break;
case 32:
space=true;
while(space)
{
pause_menu();
}
space=false;
break;
}
}
//*****************************************//
void updating(){
for(int i=0;i<4;i++)
screen[x+shape[i][0]][y+shape[i][1]]=1;
}
//**************************************************//
void showShape(char c)
{
for(int i=0;i<4;i++)
{
gotoxy(x+shape[i][0],y+shape[i][1]);
printf("%c",c);
}
}
//****************************************************//
void selectShape(int num)
{
for(int i=0;i<4;i++)
for(int j=0;j<2;j++)
shape[i][j]=shapeTable[num][i][j];
}
//******************************************************//
void print_what_update(){
gotoxy(6,3);
for(int i=6;i<30;i++){
for(int j=0;j<25;j++){
gotoxy(i,j);
if(screen[i][j]==1)
printf("%c",219);
else
printf(" ");
}
}
}
//*****************************************************//
void checkkardaneKhat(){
int emtiazkomaki=1;
for (int i=23;i>1;i--){
for (int j=6;j<29;j++){
if(screen[j][i]==true)
tedadyekha++;
}
if (tedadyekha==23){
for (int n=i;n>1;n--){
for (int t=1;t<=29;t++){
screen[t][n]=screen[t][n-1];
}
}
emtiaz=emtiaz+10*emtiazkomaki;
emtiazkomaki++;
i++;
}
tedadyekha=0;
}
}
//**********************************************************//
void endgame(){
system("cls");
gotoxy(35,10);
printf("GAME OVER");
Sleep(5000);
system("cls");
menu();
}
//***************************************************//
void save_screen()
{
ofstream my_save_screen;
my_save_screen.open("Screen_save.exe");
for(int i=0;i<30;i++)
{
for(int j=0;j<25;j++)
{
if(screen[i][j]==true)
my_save_screen<<"A";
else
my_save_screen<<"B";
}
}
my_save_screen.close();;
ofstream my_save_variable;
my_save_variable.open("Variable_save.exe");
my_save_variable<<emtiaz;
my_save_variable.close();;
ofstream my_save;
my_save.open("_save.exe");
my_save<<num;
my_save.close();;
}
void load_screen()
{
char ch;
ifstream MY_save_screen;
MY_save_screen.open("Screen_save.exe");
for(int i=0;i<30;i++)
{
for(int j=0;j<25;j++)
{
MY_save_screen>>ch;
if(ch=='A')
screen[i][j]=true;
else
screen[i][j]=false;
}
}
MY_save_screen.close();;
ifstream MY_save_variable;
MY_save_variable.open("Variable_save.exe");
MY_save_variable>>emtiaz;
MY_save_variable.close();;
ifstream MY_save;
MY_save.open("_save.exe");
MY_save>>num;
MY_save.close();;
print_what_update();
game_start();
}
void menu()
{
char c=26;
char d=27;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
cout<<"\n ** ** *** ****** **** ** ** ** ***** **** ** ** **\n";
Sleep(500);
cout<<" *** *** ** ** ** **** ** *** *** * **** ** ** **\n";
Sleep(500);
cout<<" ** * * ** ** *** ** ** ** ** ** ** * * ** ***** ** ** ** ** **\n";
Sleep(500);
cout<<" ** * ** ** ** ** ** **** ** * ** * ** **** ** **\n";
Sleep(500);
cout<<" ** ** ** ** ****** ** *** ** ** ***** ** *** *******\n";
for(int i=1;i<=39;i+=2)
{
gotoxy(i,7);
Sleep(100);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12);
cout<<c;
}
for(int i=77;i>=40;i-=2)
{
gotoxy(i,7);
Sleep(100);
cout<<d;
}
char a;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
gotoxy(10,9);
Sleep(500);
cout<<"1) Play Game";
gotoxy(10,11);
Sleep(500);
cout<<"2) Load Game";
gotoxy(10,13);
Sleep(500);
cout<<"3) Creators";
gotoxy(10,15);
Sleep(500);
cout<<"4) Exit";
gotoxy(10,17);
Sleep(500);
cout<<"Select YOUR MOOD: ";
first:;
a=getch();
switch (a)
{
case '1' : system("cls");
game_start();
break;
case '2' : system("cls");
load_screen();
break;
case '3' : system("cls");
about();
case '4' : char ext;
system("cls");
gotoxy(30,10);
cout<<"Are you sure? (y/n) ";
ext=getch();
if(ext=='y')
{
system("cls");
exit(0);
}
else if(ext=='n')
{
system("cls");
menu();
}
default: goto first;
}
}
void about(void)
{
char esc;
system("cls");
gotoxy(28,10);
cout<<"This game producted by:";
gotoxy(32,14);
cout<<"BEHZAD GOTBIFAR";
gotoxy(25,19);
cout<<"Press Esc to back to main menu.";
int i,j;
char d=177,c=178;
for(j=1;j<=2;j++)
{
for(i=24;i<=54;i++)
{
gotoxy(i,j*8);
cout<<d;
}
}
for(i=4;i<=10;i+=5)
{
for(j=8;j<=16;j++)
{
gotoxy(i*6,j);
cout<<c;
}
}
esc=getch();
if(esc==27)
{
system("cls");
menu();
}
else
about();
}
void kadr()
{
for(int i=38;i<=46;i++)
{
gotoxy(i,7);
printf("%c",1);
gotoxy(i,13);
printf("%c",1);
}
for(int j=8;j<=12;j++)
{
gotoxy(38,j);
printf("%c",1);
gotoxy(46,j);
printf("%c",1);
}
}
void game_start()
{
int k;
for(k=5;k<29;k++)
screen[k][24]=1;
for(k=1;k<25;k++){
screen[5][k]=1;
screen[29][k]=1;
}
srand(time(0));
table();
char key;
selectShape(num);
showShape(219);
gotoxy(38,5);
printf("YOUR SCORE: %d",emtiaz);
while(true){
if(noCollision()){
showShape(' ');
y++;
showShape(219);
}
else
{
updating();
checkkardaneKhat();
kadr();
gotoxy(38,5);
printf("YOUR SCORE: %d",emtiaz);
print_what_update();
num = num_1;
num_1 = rand() % 7;
gotoxy(39,9);
printf(" ");
gotoxy(39,10);
printf(" ");
gotoxy(39,11);
printf(" ");
gotoxy(39,12);
printf(" ");
y=10;
x=42;
selectShape(num_1);
showShape(219);
selectShape(num);
y = 0;
x = 17;
}
if(emtiaz>50)
Sleep(200);
else
Sleep(300);
if(kbhit()){
key=getch();
keyF(key);
}//end if
for(int i=6;i<28;i++)
if(screen[i][1]==1)
endgame();
}//end while
}
void pause_menu()
{
gotoxy(60,3);
cout<<"1) Continue";
gotoxy(60,5);
cout<<"2) Save";
gotoxy(60,7);
cout<<"3) Main Menu";
gotoxy(60,9);
cout<<"4) Exit";
char a=getch();
switch(a)
{
case '1': for(int i=55;i<80;i++)
{
for(int j=0;j<25;j++)
{
gotoxy(i,j);
cout<<" ";
}
}
space=false;
break;
case '2': save_screen();
gotoxy(60,13);
cout<<"Game Saved.";
break;
case '3': system("cls");
for(int i=0;i<30;i++)
{
for(int j=0;j<25;j++)
screen[i][j]={0};
}
emtiaz=0;
menu();
break;
case '4': system("cls");
exit(0);
break;
}
}
int main(){
HANDLE hOut;
CONSOLE_CURSOR_INFO ConCurInf;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
ConCurInf.dwSize = 10;
ConCurInf.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &ConCurInf);
menu();
}