Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File POLLGAMES.C in c:\Ptikva14\Polling 27-4-2014
- // A witty polling swap gamer ( state machine)
- #include <conio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <stdio.h>
- #define UP 72
- #define DOWN 80
- #define LEFT 75
- #define RIGHT 77
- void showMenu (void);
- int game1 (void);
- int game2 (void);
- int game3 (void);
- int selectGame (int choice);
- void main (void)
- {
- char key='1';
- showMenu();
- while ( key!='4')
- {
- key= selectGame(key);
- }
- }
- void showMenu (void)
- {
- clrscr();
- _setcursortype(_NOCURSOR);
- gotoxy(55,12);
- textcolor(WHITE);
- cprintf("MENU");
- textcolor(WHITE);
- gotoxy(55,13);
- cprintf("1.GAME1");
- gotoxy(55,14);
- cprintf("2.GAME2");
- gotoxy(55,15);
- cprintf("3.GAME3");
- gotoxy(55,16);
- cprintf("4.EXIT");
- }
- int game1 (void)
- {
- char key;
- int x =58;
- int y =35;
- int ballx=50;
- int bally= 15;
- int dirx=1;
- int diry=1;
- int i;
- int counter =0;
- while(1)
- {
- gotoxy(55,12);
- textcolor(WHITE);
- cprintf("MENU");
- textcolor(WHITE);
- gotoxy(55,13);
- cprintf("1.GAME1");
- gotoxy(55,14);
- cprintf("2.GAME2");
- gotoxy(55,15);
- cprintf("3.GAME3");
- gotoxy(55,16);
- cprintf("4.EXIT");
- gotoxy(100,10);
- Sleep(50);
- textcolor(random(15)+1);
- for (i=0; i< 14; i++)
- {
- gotoxy(x+i,y);
- cprintf("-");
- }
- if(ballx ==1)
- dirx=1;
- if(ballx==118)
- dirx=-1;
- if(bally==7)
- diry=1;
- if(bally==35)
- diry=-1;
- gotoxy(ballx,bally);
- cprintf(" ");
- ballx = ballx+ dirx;
- bally = bally +diry;
- gotoxy(ballx,bally);
- cprintf("*");
- if(bally==35 &&(ballx-14>x||ballx<x))
- {
- Sleep(3000);
- counter++;
- }
- if( kbhit())
- {
- key=getch();
- for (i=0; i<3; i++)
- {
- if(GetAsyncKeyState(VK_RIGHT)<0)
- {
- if (x+14!=120)
- {
- gotoxy(x,y);
- cprintf(" ");
- x++;
- }
- }
- if(GetAsyncKeyState(VK_LEFT)<0)
- {
- if (x!=0)
- {
- gotoxy(x+13,y);
- cprintf(" ");
- x--;
- }
- }
- if( key>='1' && key<='4')
- {
- clrscr();
- showMenu();
- return(key);
- }
- }
- }
- }
- }
- int game2 (void)
- {
- char key;
- int x =58;
- int y =35;
- int ballx=50;
- int bally= 15;
- int dirx=1;
- int diry=1;
- int x1=58;
- int y1= 7;
- int i;
- int counter =0;
- while(1)
- {
- gotoxy(55,12);
- textcolor(WHITE);
- cprintf("MENU");
- textcolor(WHITE);
- gotoxy(55,13);
- cprintf("1.GAME1");
- gotoxy(55,14);
- cprintf("2.GAME2");
- gotoxy(55,15);
- cprintf("3.GAME3");
- gotoxy(55,16);
- cprintf("4.EXIT");
- gotoxy(100,10);
- Sleep(50);
- textcolor(WHITE);
- for (i=0; i< 14; i++)
- {
- gotoxy(x+i,y);
- cprintf("-");
- }
- for (i=0; i<14; i++)
- {
- gotoxy(x1+i,y1);
- cprintf("-");
- }
- if(ballx ==1)
- dirx=1;
- if(ballx==118)
- dirx*=-1;
- if(bally==7)
- {
- diry=1;
- dirx= rand() % (2+ 1 - 1) + 1 ;
- }
- if(bally==35)
- {
- diry=-1;
- dirx = rand() % (2+1-1)+1;
- }
- gotoxy(ballx,bally);
- cprintf(" ");
- ballx = ballx+ dirx;
- bally = bally +diry;
- gotoxy(ballx,bally);
- cprintf("*");
- if(bally==35 &&(ballx-14>x||ballx<x))
- {
- Sleep(3000);
- counter++;
- }
- if(bally==7 &&(ballx-14>x1||ballx<x1))
- {
- Sleep(3000);
- counter++;
- }
- // if( kbhit())
- //{
- //key=getch();
- for (i=0; i<3; i++)
- {
- //if(GetKeyState(RIGHT) & 0x8000/*check if high-order bit is set (1 << 15)*/)
- //if(key ==RIGHT)
- if(GetAsyncKeyState(VK_RIGHT)<0)
- {
- if (x+14!=120)
- {
- gotoxy(x,y);
- cprintf(" ");
- x++;
- }
- }
- if(GetAsyncKeyState(VK_LEFT)<0)
- {
- if (x!=0)
- {
- gotoxy(x+13,y);
- cprintf(" ");
- x--;
- }
- }
- //if (key=='q')
- //if(GetKeyState('e') & 0x8000/*check if high-order bit is set (1 << 15)*/)
- if (GetKeyState(0x51) <0)
- {
- if(x1!=0)
- {
- gotoxy(x1+13,y1);
- cprintf(" ");
- x1--;
- }
- }
- if (GetKeyState(0x45) <0)
- // if (key=='e')
- {
- if(x1+14!=120)
- {
- gotoxy(x1,y1);
- cprintf(" ");
- x1++;
- }
- }
- if( GetKeyState(0x31)<0)
- {
- clrscr();
- showMenu();
- return(0x31);
- }
- if( GetKeyState(0x32)<0)
- {
- clrscr();
- showMenu();
- return(0x32);
- }
- if( GetKeyState(0x33)<0)
- {
- clrscr();
- showMenu();
- return(0x33);
- }
- if( GetKeyState(0x34)<0)
- {
- clrscr();
- showMenu();
- return(0x34);
- }
- }
- // }
- }
- }
- int game3 (void)
- {
- char key;
- while(1)
- {
- gotoxy(18,20);
- textcolor(random(15)+1);
- cprintf("GAME3");
- Sleep(100);
- if( kbhit())
- {
- key=getch();
- if( key>='1' && key<='4')
- {
- clrscr();
- showMenu();
- return(key);
- }
- }
- }
- }
- int selectGame(int choice)
- {
- switch(choice)
- {
- case '1':
- choice=game1();
- break;
- case '2':
- choice=game2();
- break;
- case '3':
- choice=game3();
- break;
- default:
- break;
- }
- return(choice);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement