Need help with shape collision in tetris clone

I need help detecting other blocks. right now the block will go through other blocks.

new code:
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525

//Dark GDK - The Game Creators - www.thegamecreators.com

//the wizard has created a very simple project that uses Dark GDK
//it contains the basic code for a GDK application

//whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
#include <cstdlib>
#include <ctime>


//Global
int Block_Size=20; 
int grid[20][10];
//colors
const DWORD Red = dbRGB(255,0,0);
const DWORD Green = dbRGB(0,255,0);
const DWORD Blue = dbRGB(0,0,255);
const DWORD Magenta = dbRGB(255,0,255);
const DWORD Black = dbRGB(0,0,0);
const DWORD White = dbRGB(255,255,255);
const DWORD Yellow = dbRGB(255,255,0);
const DWORD Cyan = dbRGB(0,255,255);
const DWORD Orange = dbRGB(255,165,0);
const DWORD Grey = dbRGB(177,177,177);
//end colors
//end global

//Classes

class Block {
private:
	DWORD color;
	//int blocks[4];
public:
	int x,y;
	Block();
	Block(int, int, DWORD);
	void draw();
	void move(int, int);
	void clear();
	void rotation(int,int);
	
};
Block::Block(){
x = 0;
y = 0;
color = White;
}
//Methods (Block)
void Block::rotation(int px , int py)
{
	int xp, yp;

	xp=px-py+y;
	yp=px+py-x;

	x=xp;
	y=yp;
	dbInk(color, Grey);
	draw();

}

Block::Block(int X, int Y, DWORD COLOR)
{
	x=X;
	y=Y;
	color=COLOR;
}

void Block::draw()
{
	int x1, y1, x2, y2;
	x1=x*Block_Size;
	y1=y*Block_Size;
	x2=x1+Block_Size;
	y2=y1+Block_Size;

//	dbInk(color, Black);
	dbBox(x1,y1,x2,y2);
}

void Block::clear()
{

	dbInk(Grey, Grey);
	//dbBox(x1,y1,x2,y2);
	draw();
}

void Block::move(int dx, int dy)
{
	x=x+dx;
	y=y+dy;
	dbInk(color, Grey);
	draw();
}

//End Method (Block)

class Shape {

private:
	Block blocks[4];
	//int pos[8];
public:
	int pos[8];
	DWORD color;
    Shape();
	void make_ishape();
	void make_oshape();
	void make_jshape();
	void make_lshape();
	void make_tshape();
	void make_zshape();
	void make_sshape();
	void move_shape(int,int);
	void draw_shape();
	bool col(int,int);
	bool colnew(int,int);
	void rotate_shape();
	//void rotate_zshape();
	//void rot(int,int);
	
};
//Methods (Shape)
Shape::Shape() {
blocks[0] = Block(0,0, White);
blocks[1] = Block(0,0, White);
blocks[2] = Block(0,0, White);
blocks[3] = Block(0,0, White);
}
bool Shape::col(int dx, int dy)
{
	for (int i=0; i<=4; i++)
	{
		if (blocks[i].y + dy > 19 || blocks[i].x + dx < 0 || blocks[i].x +dx > 9)
		{
			return false;
		}
		
	}
	return true;
}
bool Shape::colnew(int dx, int dy)
{
	for (int i=0; i<=4; i++)
	{
		if (blocks[i].y + dy > 19)
		{
			return false;
		}
		
	}
	return true;
}



void Shape::make_ishape()
{
	blocks[0] = Block(pos[0],pos[1],Blue);
	blocks[1] = Block(pos[2],pos[3],Blue);
	blocks[2] = Block(pos[4],pos[5],Blue);
	blocks[3] = Block(pos[6],pos[7],Blue);
	
}
void Shape::make_oshape()
{
	blocks[0] = Block(pos[0],pos[1],Green);
	blocks[1] = Block(pos[2],pos[3],Green);
	blocks[2] = Block(pos[4],pos[5],Green);
	blocks[3] = Block(pos[6],pos[7],Green);
	
}
void Shape::make_jshape()
{
	blocks[0] = Block(pos[0],pos[1],Yellow);
	blocks[1] = Block(pos[2],pos[3],Yellow);
	blocks[2] = Block(pos[4],pos[5],Yellow);
	blocks[3] = Block(pos[6],pos[7],Yellow);
	
}
void Shape::make_lshape()
{
	blocks[0] = Block(pos[0],pos[1],Orange);
	blocks[1] = Block(pos[2],pos[3],Orange);
	blocks[2] = Block(pos[4],pos[5],Orange);
	blocks[3] = Block(pos[6],pos[7],Orange);
	
}
void Shape::make_tshape()
{
	blocks[0] = Block(pos[0],pos[1],Cyan);
	blocks[1] = Block(pos[2],pos[3],Cyan);
	blocks[2] = Block(pos[4],pos[5],Cyan);
	blocks[3] = Block(pos[6],pos[7],Cyan);
	
}
void Shape::make_zshape()
{
	blocks[0] = Block(pos[0],pos[1],Red);
	blocks[1] = Block(pos[2],pos[3],Red);
	blocks[2] = Block(pos[4],pos[5],Red);
	blocks[3] = Block(pos[6],pos[7],Red);
	
}
/*/void Shape::rotate_zshape()
{
	blocks[0] = Block(pos[0],pos[1],Red);
	blocks[1] = Block(pos[2],pos[3],Red);
	blocks[2] = Block(pos[4],pos[5],Red);
	blocks[3] = Block(pos[6],pos[7],Red);
}
	/*/
void Shape::make_sshape()
{
	blocks[0] = Block(pos[0],pos[1],Magenta);
	blocks[1] = Block(pos[2],pos[3],Magenta);
	blocks[2] = Block(pos[4],pos[5],Magenta);
	blocks[3] = Block(pos[6],pos[7],Magenta);
	
}
void Shape::draw_shape()
{
	for (int i=0; i<4; i++)
	{
		blocks[i].draw();
	}
}

void Shape::move_shape(int dx, int dy)
{
	for (int i=0; i<4; i++)
	{
		blocks[i].clear();
	}
	for (int i=0; i<4; i++)
	{
		blocks[i].move(dx,dy);
	}
}

void Shape::rotate_shape()
{
	for (int i=0; i<4; i++)
	{
		blocks[i].clear();
	}
	for (int i=0; i<4; i++)
	{
		blocks[i].rotation(blocks[1].x,blocks[1].y);

	}
	
}

class I_Shape: public Shape{
public:
	I_Shape(int,int);
	
private:
	DWORD color;
	//int pos[8];
};

//Methods (I_Shape)


I_Shape::I_Shape(int x, int y):Shape()
{

	color=Blue;
	pos[0]=x-1;
	pos[1]=y;
	pos[2]=x;
	pos[3]=y;
	pos[4]=x+1;
	pos[5]=y;
	pos[6]=x+2;
	pos[7]=y;
	make_ishape(); //makes I_Shape
}


class O_Shape: public Shape{
public:
	O_Shape(int,int);
private:
	DWORD color;
};
O_Shape::O_Shape(int x, int y):Shape()
{
	color=Red;
	pos[0]=x;
	pos[1]=y;
	pos[2]=x;
	pos[3]=y-1;
	pos[4]=x+1;
	pos[5]=y-1;
	pos[6]=x+1;
	pos[7]=y;
	make_oshape(); //makes O_Shape

}

class J_Shape: public Shape{
public:
	J_Shape(int,int);
private:
	DWORD color;
};
J_Shape::J_Shape(int x, int y):Shape()
{
	color=Yellow;
	pos[0]=x-1;
	pos[1]=y;
	pos[2]=x;
	pos[3]=y;
	pos[4]=x+1;
	pos[5]=y;
	pos[6]=x+1;
	pos[7]=y-1;
	make_jshape(); //makes I_Shape
}

class L_Shape: public Shape{
public:
	L_Shape(int,int);
private:
	DWORD color;
};
L_Shape::L_Shape(int x, int y):Shape()
{
	color=Orange;
	pos[0]=x+1;
	pos[1]=y;
	pos[2]=x;
	pos[3]=y;
	pos[4]=x-1;
	pos[5]=y;
	pos[6]=x-1;
	pos[7]=y-1;
	make_lshape(); //makes L_Shape
}

class T_Shape: public Shape{
public:
	T_Shape(int,int);
private:
	DWORD color;
};
T_Shape::T_Shape(int x, int y):Shape()
{
	color=Cyan;
	pos[0]=x-1;
	pos[1]=y;
	pos[2]=x;
	pos[3]=y;
	pos[4]=x;
	pos[5]=y-1;
	pos[6]=x+1;
	pos[7]=y;
	make_tshape(); //makes T_Shape
}

class Z_Shape: public Shape{
public:
	Z_Shape(int,int);
	//Rotate_Z(int,int);
private:
	DWORD color;
};
Z_Shape::Z_Shape(int x, int y):Shape()
{
	color=Red;
	pos[0]=x-1;
	pos[1]=y;
	pos[2]=x;
	pos[3]=y;
	pos[4]=x;
	pos[5]=y+1;
	pos[6]=x+1;
	pos[7]=y+1;
	make_zshape(); //makes Z_Shape
	
}
//new code 

Shape* random()
{
	int x=3;
	int y=1;
    Shape *currentshape;
	unsigned seed = time(0);

	srand(seed);

	int random = 1 + rand() % 6;
	switch (random)
	{
	case 1:
		currentshape = new I_Shape(x,y);
		break;
	case 2:
		currentshape = new L_Shape(x,y);
		break;
	case 3:
		currentshape = new J_Shape(x,y);
		break;
	case 4:
		currentshape = new Z_Shape(x,y);
		break;
	case 5:
		currentshape = new T_Shape(x,y);
		break;
	case 6:
		currentshape = new O_Shape(x,y);
		break;
	}
		return currentshape;
}


/*/class S_Shape: public Shape{
public:
	S_Shape(int,int);
private:
	DWORD color;
};
S_Shape::S_Shape(int x, int y):Shape()
{
	color=Magenta;
	pos[0]=x-1;
	pos[1]=y;
	pos[2]=x;
	pos[3]=y;
	pos[4]=x;
	pos[5]=y+1;
	pos[6]=x-1;
	pos[7]=y+1;
	make_sshape(); //makes S_Shape
}/*/

// the main entry point for the application is this function 
//(MAIN)
void DarkGDK ( void )
{
	dbInk(Grey,Grey);
	dbBox(0,0,200,400);
	
	//I_Shape First(3,1);
	//O_Shape First(3,1);
	//J_Shape First(3,1);
	//L_Shape First(3,1);
	//T_Shape First(3,1);
	//Z_Shape First(3,1);
	Shape *currentshape;
	currentshape=random();
	currentshape->draw_shape();
	
	// turn on sync rate and set maximum rate to 1 fps
	dbSyncOn   ( );
	dbSyncRate (1);
	//First.draw_shape();
	//First.move_shape(5,4);
	

	// our main loop
	while ( LoopGDK ( ) )
	{ 
		
		if (grid[20][10]!=Grey)
		{
			if (dbLeftKey())
			{
				if (currentshape->col(-1,0))
				{	
				currentshape->move_shape(-1,0);
				}
			}
			if (dbRightKey())
			{
				if (currentshape->col(1,0))
				{
				currentshape->move_shape(1,0);
				}
			}	

		
			if (dbDownKey())
			{
				if (currentshape->col(0,1))
				{
				currentshape->move_shape(0,1);		
				}
			}
			else if (currentshape->col(0,1))
				{
				currentshape->move_shape(0,1);		
				}
			else
			{
				currentshape=random();
			}
			if (dbUpKey())
			{
			//	if (First.col(0,1))
			//	{
				currentshape->rotate_shape();		
			//	}
			}
		}
		
		// update the screen
		dbSync ( );
		//dbWaitKey();
	}
	
	
	// return back to windows
	return;
}
closed account (o3hC5Di1)
Hi there,

I don't see any "board" matrix in your code which stores the position of blocks.

Take a look at following tutorials, which will explain it to you in detail.
They don't use darkGDK, but the concepts should still apply.

http://javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/
http://www.gamedev.net/topic/192483-tetris-clone-in-an-hour-with-c/

Hope that helps.

All the best,

NwN
Topic archived. No new replies allowed.