help tetris

can anyone help me put some menu here that includes *Start *name of player
and to improve the speed of the piece that falls when the block score is 20
here's my program

1st page

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <windows.h>
#include <conio.h>
#include <iostream>

using std::cout;
using std::endl;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

class Bads
{
      
public:
   Bads(int x = 0, int y = 0)
      : X(x), Y(y)
   { }

   int X, Y;

   Bads operator + (const Bads &right) const 
   {
      return Bads(X + right.X, Y + right.Y);
   }
};


const char *TITLE = "########## BADS TETRIS ##########";
const char *SCORE_FILE = "c:\\tetrisScore.txt";
const char *FAIL_STR = "GAME OVER!";

const int FIELD_WIDTH = 14, FIELD_HEIGHT = 19;
const int HORIZONTAL_DIVIDE = 4, VERTICAL_DIVIDE = 20;
const int BOARD_WIDTH = 35, BOARD_HEIGHT = FIELD_HEIGHT + HORIZONTAL_DIVIDE + 1;
const Bads FIELD_START((VERTICAL_DIVIDE - FIELD_WIDTH)/2,HORIZONTAL_DIVIDE + 1);
const Bads NEXT_PIECE_POS(VERTICAL_DIVIDE + (BOARD_WIDTH - VERTICAL_DIVIDE)/2,HORIZONTAL_DIVIDE + 4);
const Bads SCORE_POS(VERTICAL_DIVIDE + 2,NEXT_PIECE_POS.Y + 4);

char field[FIELD_HEIGHT * FIELD_WIDTH];
bool exiting = false;
bool downHeld = false;
int score = 0, highScore = 0 ;

 void Reset();

char &GetField(const Bads &pos)
{
   return field[pos.Y*FIELD_WIDTH + pos.X];
}

char &GetField(int x, int y)
{
   return field[y*FIELD_WIDTH + x];
}

void SetConsoleCursor(const Bads &pos)
{
   COORD c = {(short)pos.X, (short)pos.Y};

   HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleCursorPosition( h, c );
}

class Piece
{
public:
   Piece()
   { }

   Piece(const Bads &b1, const Bads &b2, const Bads &b3, const Bads &b4)
   { 
      Blocks[0] = b1;
      Blocks[1] = b2;
      Blocks[2] = b3;
      Blocks[3] = b4;
   }

   void SetPosition(const Bads &pos)
   {
      Position = pos;
   }

   Bads &GetPosition()
   {
      return Position;
   }

   bool Step()
   {
      ++Position.Y;
      if (Collide())
      {
         --Position.Y;
         Freeze();
         return true;
      }

      return false;
   }

   void Rotate()
   {
      for (int i = 0; i < 4; ++i)
      {
         Bads pos = Blocks[i];
         Blocks[i] = Bads(-pos.Y, pos.X);
      }

      if (Collide())
      {
         UnRotate();
      }
   }

   void UnRotate()
   {
      for (int i = 0; i < 4; ++i)
      {
         Bads pos = Blocks[i];
         Blocks[i] = Bads(-pos.Y, pos.X);
      }
   }

   void Draw(const Bads &fieldStart, bool clamp = true, char character = 'O')
   {
      for (int i = 0; i < 4; ++i)
      {
         Bads pos = Position + Blocks[i];         
         if (pos.Y >= 0 || !clamp)
         {
            SetConsoleCursor(fieldStart + pos);
            cout<<character;
         }
      }
   }

   void UnDraw(const Bads &fieldStart, bool clamp = true)
   {
      Draw(fieldStart, clamp, ' ');
   }

   bool Collide()
   {
      for (int i = 0; i < 4; ++i)
      {
         Bads pos = Position + Blocks[i];
         if (pos.X < 0 || pos.X >= FIELD_WIDTH || (pos.Y >= 0 && GetField(pos) == '#'))
            return true;
      }

      return false;
   }

private:
   void Freeze()
   {
      for (int i = 0; i < 4; ++i)
         GetField(Position + Blocks[i]) = '#';
   }

   Bads Position;
   Bads Blocks[6];
};

const int PIECE_COUNT = 7;
Piece pieces[PIECE_COUNT];
Piece activePiece, nextPiece;

void SetupPieces()
{
   pieces[0] =  Piece(Bads(0,2), Bads(0,1), Bads(0,0), Bads(0,-1));
   pieces[1] =  Piece(Bads(0,0), Bads(0,1), Bads(1,0), Bads(1,1));
   pieces[2] =  Piece(Bads(-1,0), Bads(0,0), Bads(1,0), Bads(0,-1));
   pieces[3] =  Piece(Bads(-1,1), Bads(-1,0), Bads(0,0), Bads(0,-1));
   pieces[4] =  Piece(Bads(1,1), Bads(1,0), Bads(0,0), Bads(0,-1));
   pieces[5] =  Piece(Bads(1,1), Bads(1,0), Bads(0,0), Bads(-1,0));
   pieces[6] =  Piece(Bads(-1,1), Bads(1,0), Bads(0,0), Bads(-1,0));
}
int max(int a, int b)
{
    return a>b ? a : b;
}

int RandInt(int low, int high)
{
   int range = high - low;
   return low + rand() % range;
}

void LoadHighScore()
{
   FILE *file = fopen(SCORE_FILE, "r");

   if (file)
   {
      fscanf(file, "%i", &highScore);
      fclose(file);
   }
}

void SaveHighScore()
{
   FILE *file = fopen(SCORE_FILE, "w");

   if (file)
   {
      fprintf(file, "%i", highScore);
      fclose(file);
   }
}

void Fail()
{
   int failLen = (int)strlen(FAIL_STR);
   Bads pos = FIELD_START + Bads((FIELD_WIDTH - failLen)/2,FIELD_HEIGHT/2);
   SetConsoleCursor(pos);

   cout<<FAIL_STR;

   while (GetKeyState(VK_SPACE) > -100 && GetKeyState(VK_RETURN) > -100)
   {}

   highScore = max(highScore, score);
   SaveHighScore();

   Reset();
}

void SpawnPiece()
{
   activePiece = nextPiece;
   activePiece.SetPosition(Bads(FIELD_WIDTH/2, 0));

   if (activePiece.Collide())
   {
      Fail();
      return;
   }

   nextPiece.UnDraw(NEXT_PIECE_POS, false);
   nextPiece = pieces[RandInt(0,PIECE_COUNT)];
   nextPiece.Draw(NEXT_PIECE_POS, false);
}

void DrawStatics()
{
   for (int i = 0; i < BOARD_WIDTH; ++i)
   {
      SetConsoleCursor(Bads(i,0));
      cout<<'#';

      SetConsoleCursor(Bads(i,BOARD_HEIGHT));
      cout<<'#';

      SetConsoleCursor(Bads(i,HORIZONTAL_DIVIDE));
      cout<<'#';
   }
   
   for (int i = 1; i < BOARD_HEIGHT; ++i)
   {
      SetConsoleCursor(Bads(0,i));
      cout<<'#';
      
      SetConsoleCursor(Bads(BOARD_WIDTH-1,i));
      cout<<'#';
      
      SetConsoleCursor(Bads(VERTICAL_DIVIDE,max(i, HORIZONTAL_DIVIDE+1)));
      cout<<'#';
   }

   for (int i = 0; i < FIELD_HEIGHT; ++i)
   {
      SetConsoleCursor(FIELD_START + Bads(-1,i));
      cout<<"#";
      SetConsoleCursor(FIELD_START + Bads(FIELD_WIDTH,i));
      cout<<"#";
   }

   int len = (int)strlen(TITLE);
   Bads pos((BOARD_WIDTH - len)/2, HORIZONTAL_DIVIDE/2);

   SetConsoleCursor(pos);
   cout<<TITLE;

   SetConsoleCursor(Bads(SCORE_POS.X, NEXT_PIECE_POS.Y-2));
   cout<<"Next Piece:";

   SetConsoleCursor(SCORE_POS);
   cout<<"Score:";

   SetConsoleCursor(SCORE_POS + Bads(0,3));
   cout<<"High Score:";
}

void DrawScore()
{
   Bads pos = SCORE_POS + Bads(0,1);
   int length = BOARD_WIDTH - pos.X - 1;

   SetConsoleCursor(pos);
   for(int i = 0; i < length; ++i)
      cout<<" ";

   SetConsoleCursor(pos);
   cout<<score;

   pos.Y += 3;

   SetConsoleCursor(pos);
   for(int i = 0; i < length; ++i)
      cout<<" ";

   SetConsoleCursor(pos);
   cout<<highScore;
}

void Reset()
{
   memset(field, 0, sizeof(field));
   memset(field + FIELD_WIDTH * (FIELD_HEIGHT-1), '#', FIELD_WIDTH);

   score = 0;

   DrawStatics();
   DrawScore();

   srand((unsigned int)time(0));
   SpawnPiece();
   SpawnPiece();
}

void ProcessInput()
{
   static bool leftState = false;
   static bool rightState = false;
   static bool upState = false;
   static bool downState = false;
   
   Bads &pos = activePiece.GetPosition();

   if (GetKeyState(VK_LEFT) < -20)
   {
      if (!leftState)
      {
         --pos.X;
         if (activePiece.Collide())
            ++pos.X;
      }
      leftState = true;
   }
   else
   {
Last edited on
part 2

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
165
166
167
168
169
170
171
172
173
174
leftState = false;
   }

   if (GetKeyState(VK_RIGHT) < -20)
   {
      if (!rightState)
      {
         ++pos.X;
         if (activePiece.Collide())
            --pos.X;
      }
      rightState = true;
   }
   else
   {
      rightState = false;
   }

   if (GetKeyState(VK_UP) < -20)
   {
      if (!upState)
         activePiece.Rotate();

      upState = true;
   }
   else
   {
      upState = false;
   }

   if (GetKeyState(VK_DOWN) < -20)
   {
      if (!downState)
         downHeld = true;

      downState = true;
   }
   else
   {
      downState = false;
      downHeld = false;
   }

   if (GetKeyState(VK_ESCAPE) < -100)
   {
      exiting = true;
   }
}

void StepPiece()
{
   if (activePiece.Step())
   {
      SpawnPiece();
      downHeld = false;
   }
}

void CollapseBoard(int rowStart, int rowCnt)
{
   int start = rowStart * FIELD_WIDTH;
   int amount = FIELD_WIDTH * rowCnt;

   memset(field + start, 0, amount);
   memmove(field + amount, field, start);

   score += rowCnt * rowCnt;
   DrawScore();
}

void TestBoard()
{
   int rowStart = 0, rowCnt = 0;

   for (int y = 0; y < FIELD_HEIGHT-1; ++y)
   {
      bool rowFilled = true;

      for (int x = 0; x < FIELD_WIDTH; ++x)
      {
         if (GetField(x, y) != '#')
         {
            rowFilled = false;
            break;
         }
      }

      if (rowFilled)
      {
         ++rowCnt;
      }
      else
      {
         if (rowCnt > 0)
         {
            CollapseBoard(rowStart, rowCnt);
            rowStart += rowCnt;
            rowCnt = 0;
         }
         else
         {
            ++rowStart;
         }
      }
   }

   if (rowCnt > 0)
      CollapseBoard(rowStart, rowCnt);
}

void DoLogic(int dt)
{
     
   static int time = 0;
   time += dt;

  int stepRate = 300;
   
   if (time > stepRate )
   {
      StepPiece();
      time -= stepRate;
      
      }

   if (downHeld)
      StepPiece();

   TestBoard();
}

void Draw()
{
   for(int y = 0; y < FIELD_HEIGHT; ++y)
   {
      SetConsoleCursor(FIELD_START + Bads(0,y));
      for (int x = 0; x < FIELD_WIDTH; ++x)
         cout<<GetField(x,y);
   }
   activePiece.Draw(FIELD_START);
}

void Delay(unsigned long ms)
{
   unsigned long outTime = GetTickCount() + ms;
   while (outTime > GetTickCount())
      ProcessInput();
}

int main()
{
HANDLE hConsole;
int k;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for(k = 1; k < 12; k++)
{
SetConsoleTextAttribute(hConsole, k);
			
}	
   LoadHighScore();
   SetupPieces();
   Reset();

   while(!exiting)
   {
      int dt = 50;
      Delay(dt);
      ProcessInput();
      DoLogic(dt);
      Draw();
   }
}
[code]
[/code]
Last edited on
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/jEywvCM9/
You may also want to move this to the windows section instead of general by editing your first post.
thanks
Topic archived. No new replies allowed.