Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <graphics.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #include <dos.h>
- #include <iostream.h>
- #include <time.h>
- long int score=0;
- void assigny(int k,int yb[4],int pb[4][10])
- {pb[k][1]=yb[k];
- pb[k][3]=yb[k]+20;
- pb[k][5]=yb[k]+20;
- pb[k][7]=yb[k];
- pb[k][9]=yb[k];
- };
- void assignblock(int k,int r[4],int xb[4],int yb[4],int pb[4][10])
- {
- if(r[k]==1&&random(3)>0)
- {score++;
- if(k%2==0)
- {
- if(random(2)==0)
- xb[k]=120;
- else
- xb[k]=240;
- }
- else
- {
- if(random(2)==0)
- xb[k]=390;
- else
- xb[k]=510;
- }
- /////
- if(xb[k]==xb[(k+2)%4])
- { do
- {yb[k]=random(200)+21;
- }while(abs(yb[k]-yb[(k+2)%4])<=50);
- }
- else
- { do
- {yb[k]=random(200)+21;
- }while(abs(yb[k]-yb[(k+2)%4])<=100);
- }
- //////
- pb[k][0]=xb[k];
- pb[k][2]=xb[k];
- pb[k][4]=xb[k]+20;
- pb[k][6]=xb[k]+20;
- pb[k][8]=xb[k];
- assigny(k,yb,pb);
- r[k]=0;
- }
- };
- //////
- void game()
- {
- int xb[4],x1=120,x2=390,yb[4],pb[4][10];
- int p1[10] = {120,400,120,440,140,440,140,400,120,400};
- int p2[10] = {390,400,390,440,410,440,410,400,390,400};
- int r[4] = {1,1,1,1};
- score=0;
- char key;
- cleardevice();
- settextstyle(2,HORIZ_DIR,4);
- outtextxy(1,10,"SCORE ");
- cout<<endl<<endl<<" ";
- moveto(190,0);
- lineto(190, 475);
- moveto(460,0);
- lineto(460, 475);
- moveto(70,0);
- lineto(70, 475);
- moveto(580,0);
- lineto(580, 475);
- moveto(310,0);
- lineto(310, 475);
- moveto(340,0);
- lineto(340, 475);
- setcolor(0);
- setfillstyle(1,RED);
- fillpoly(4,p1);
- setcolor(0);
- setfillstyle(1,BLUE);
- fillpoly(4,p2);
- int t=0;
- randomize();
- for(;t!=1;)
- {
- assignblock(0,r,xb,yb,pb);
- assignblock(1,r,xb,yb,pb);
- assignblock(2,r,xb,yb,pb);
- assignblock(3,r,xb,yb,pb);
- while((yb[0]<500)&&(yb[1]<500)&&(yb[2]<500)&&(yb[3]<500))
- {
- delay(3);
- setfillstyle(1,BLACK);
- fillpoly(4,pb[0]);
- fillpoly(4,pb[1]);
- fillpoly(4,pb[2]);
- fillpoly(4,pb[3]);
- yb[0]+=1;
- yb[1]+=1;
- yb[2]+=1;
- yb[3]+=1;
- assigny(0,yb,pb);
- assigny(1,yb,pb);
- assigny(2,yb,pb);
- assigny(3,yb,pb);
- setfillstyle(1,YELLOW);
- fillpoly(4,pb[0]);
- fillpoly(4,pb[1]);
- fillpoly(4,pb[2]);
- fillpoly(4,pb[3]);
- if(((xb[0]==x1)&&(yb[0]>=400)&&(yb[0]<=440))||((xb[1]==x2)&&(yb[1]>=400)&&
- (yb[1]<=440))||((xb[2]==x1)&&(yb[2]>=400)&&(yb[2]<=440))||((xb[3]==x2)&&(yb[3]>=400)&&(yb[3]<=440)))
- {
- t=1;
- break;
- }
- if(kbhit())
- {
- key=getch();
- if(key=='a')
- { if(x1==240)
- {
- setfillstyle(1,BLACK);
- fillpoly(4,p1);
- p1[0]=120;
- p1[2]=120;
- p1[4]=140;
- p1[6]=140;
- p1[8]=120;
- setfillstyle(1,RED);
- fillpoly(4,p1);
- x1=120;
- }
- else
- {
- setfillstyle(1,BLACK);
- fillpoly(4,p1);
- setfillstyle(1,RED);
- p1[0]=240;
- p1[2]=240;
- p1[4]=260;
- p1[6]=260;
- p1[8]=240;
- fillpoly(4,p1);
- x1=240;
- }
- }
- if(key=='l')
- { if(x2==390)
- {
- setfillstyle(1,BLACK);
- fillpoly(4,p2);
- setfillstyle(1,BLUE);
- p2[0]=510;
- p2[2]=510;
- p2[4]=530;
- p2[6]=530;
- p2[8]=510;
- fillpoly(4,p2);
- x2=510;
- }
- else
- {
- setfillstyle(1,BLACK);
- fillpoly(4,p2);
- setfillstyle(1,BLUE);
- p2[0]=390;
- p2[2]=390;
- p2[4]=410;
- p2[6]=410;
- p2[8]=390;
- fillpoly(4,p2);
- x2=390;
- }
- }
- }
- if(yb[0]==500)
- r[0]=1;
- if(yb[1]==500)
- r[1]=1;
- if(yb[2]==500)
- r[2]=1;
- if(yb[3]==500)
- r[3]=1;
- cout<<"\b\b\b"<<score;
- }
- }
- cleardevice();
- settextstyle(2,0,20);
- setcolor(10);
- outtextxy(222,100,"GAME OVER");
- outtextxy(210,150,"YOUR SCORE ");
- char str[5];
- outtextxy(320,200,itoa(score,str,10));
- }
- void main()
- {
- int gdriver = DETECT, gmode, errorcode,t=0;
- char key;
- initgraph(&gdriver, &gmode, "C:\\TurboC3\\BGI");
- errorcode = graphresult();
- if (errorcode != grOk)
- {
- printf("Graphics error: %s\n", grapherrormsg(errorcode));
- printf("Press any key to halt:");
- getch();
- exit(1);
- }
- settextstyle(2,HORIZ_DIR,20);
- outtextxy(220,100,"2 BLOCKS");
- settextstyle(2,HORIZ_DIR,8);
- outtextxy(190,240,"ARE YOU READY????");
- settextstyle(2,HORIZ_DIR,4);
- outtextxy(245,350,"Press space bar to play");
- settextstyle(2,HORIZ_DIR,4);
- while(getch()==32)
- {game();settextstyle(2,HORIZ_DIR,4);outtextxy(245,240,"Press space bar to play again");}
- cleardevice();
- settextstyle(4,HORIZ_DIR,4);
- outtextxy(320,400,"Quitting");exit(0);
- closegraph();
- }
Add Comment
Please, Sign In to add comment