-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame1.h
164 lines (137 loc) · 3.26 KB
/
Game1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<windows.h>
#include<time.h>
#define SCREEN_WIDTH 90
#define SCREEN_HEIGHT 26
#define WIN_WIDTH 70
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
char keys[10];
int keyPos[10][2];
int score = 0;
class game
{
public:
int score;
void gotoxy(int x, int y){
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console, CursorPosition);
}
void setcursor(bool visible, DWORD size) {
if(size == 0) size = 20;
CONSOLE_CURSOR_INFO Cursor;
Cursor.bVisible = visible;
Cursor.dwSize = size;
SetConsoleCursorInfo(console,&Cursor);
}
void drawBorder(){
for(int i=0; i<SCREEN_WIDTH; i++){
gotoxy(i,SCREEN_HEIGHT); printf("\xB2");
}
for(int i=0; i<=SCREEN_HEIGHT; i++){
gotoxy(0,i);
printf("\xB2");
gotoxy(SCREEN_WIDTH,i);
printf("\xB2");
}
for(int i=0; i<SCREEN_HEIGHT; i++){
gotoxy(WIN_WIDTH,i);
printf("\xB2");
}
}
void genAlphabet(int x){
keys[x] = 65+rand()%25;
keyPos[x][0] = 2 + rand()%(WIN_WIDTH-2);
keyPos[x][1] = 1;
}
void drawAlphabet(int x){
if(keyPos[x][0] != 0){
gotoxy(keyPos[x][0], keyPos[x][1]);
cout<<keys[x];
}
}
void eraseAlphabet(int x){
if(keyPos[x][0] != 0){
gotoxy(keyPos[x][0], keyPos[x][1]);
cout<<" ";
}
}
void resetAlphabet(int x){
eraseAlphabet(x);
genAlphabet(x);
}
void gameover(){
system("cls");
cout<<endl;
cout<<"\t\t--------------------------"<<endl;
cout<<"\t\t-------- Game Over -------"<<endl;
cout<<"\t\t--------------------------"<<endl<<endl;
cout <<"\t\t Final Score :: " << score;
cout<<"\n\t\tPress any key to go back to menu.";
getch();
}
void updateScore(){
gotoxy(WIN_WIDTH + 6, 5);
cout<<"Score: "<<score<<endl;
}
void instructions(){
system("cls");
cout<<"Instructions";
cout<<"\n----------------";
cout<<"\n On Left side you will see falling characters ";
cout<<"\n You have to keep them away from touching floor";
cout<<"\n Press respective key from keyboard to keep playing";
cout<<"\n\n Press 'escape' to exit";
cout<<"\n\nPress any key to go back to menu";
getch();
}
void play(){
score = 0;
for(int i=0; i<10; i++){
keyPos[i][0] = keyPos[i][1] = 1;
}
system("cls");
drawBorder();
updateScore();
for(int i=0; i<10; i++)
genAlphabet(i);
gotoxy(WIN_WIDTH + 5, 2);cout<<" Falling";
gotoxy(WIN_WIDTH + 5, 3);cout<<"Alphabets";
gotoxy(WIN_WIDTH + 5, 4);cout<<"----------";
gotoxy(WIN_WIDTH + 5, 6);cout<<"----------";
gotoxy(18, 5);cout<<"Press any key to start";
getch();
gotoxy(18, 5);cout<<" ";
while(1){
if(kbhit()){
char ch = getch();
for(int i=0; i<10; i++){
if( ch == keys[i] || ch-32 == keys[i] ){
resetAlphabet(i);
score++;
updateScore();
}
}
if(ch==27){
break;
}
}
for(int i=0; i<10; i++)
drawAlphabet(i);
Sleep(700);
for(int i=0; i<10; i++){
eraseAlphabet(i);
keyPos[i][1] += 1;
if( keyPos[i][1] > SCREEN_HEIGHT ){
gameover();
return;
}
}
}
}
};