Hey! Been awhile since I've been here, and I felt like making a Tetris clone, just for fun. So I'm about an hour into it (almost got collision, but I've hit this roadblock and decided not to continue until it's fixed), and I've spent the last hour trying to debug this issue, but it's just not working! Basically, the way the code works is that every second (60 frames), it updates where the players block is. So it'll check for collision, update the blocks placing on the screen, etc. Now, every time this happens, it increases the y value by 1, so it moves down 1 block. But, here's where my issue comes in. While debugging it, one of my x values will just shoot up to 16777228, and since that's much too big for an int, it just crashes the program. I have nowhere in the code that should do anything even remotely close to this, but yet it happens. I was wondering if someone could take a look at it and see if there's anything obviously wrong. So here's the code:
#include "DarkGDK.h"
// Pre-define
void L_Piece(void);
void UpdatePosition(void);
void UpdateMovement(void);
void PickPiece(void);
void SetupBoard(void);
bool CheckCollision(void);
bool CheckBottom(void);
void CheckKeys(void);
bool CheckLCol(void);
bool CheckRCol(void);
// Global Variables
bool Board[10][20];
int piece1_x = 0;
int piece1_y = 0;
int piece2_x = 0;
int piece2_y = 0;
int piece3_x = 0;
int piece3_y = 0;
int piece4_x = 0;
int piece4_y = 0;
int piece_num = 1;
int piece_type = 0;
int rotate = 0;
int timer = 0;
bool left_key = false;
bool right_key = false;
bool down_key = false;
void DarkGDK ( void )
{
// Init stuff
dbSyncOn ( );
dbSyncRate ( 60 );
dbDisableEscapeKey ( );
dbRandomize ( dbTimer ( ) );
dbLoadImage ( "MainBox.bmp", 1 );
dbSetImageColorKey ( 255, 0, 255 );
SetupBoard();
PickPiece();
dbSprite(piece_num, piece1_x * 16, piece1_y * 16, 1);
dbSprite(piece_num + 1, piece2_x * 16, piece2_y * 16, 1);
dbSprite(piece_num + 2, piece3_x * 16, piece3_y * 16, 1);
dbSprite(piece_num + 3, piece4_x * 16, piece4_y * 16, 1);
while ( LoopGDK ( ) )
{
CheckKeys();
if (timer < 60)
{
timer = timer + 1;
}
else
{
timer = 0;
UpdatePosition();
UpdateMovement();
}
dbSync ( );
}
return;
}
void SetupBoard(void)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 20; j++)
{
Board[i][j] = false;
}
}
}
void PickPiece(void)
{
L_Piece();
}
void L_Piece(void)
{
piece1_x = 5;
piece1_y = 1;
piece2_x = 5;
piece2_y = 0;
piece3_x = 6;
piece3_y = 0;
piece4_x = 7;
piece4_y = 0;
}
void UpdateMovement(void)
{
piece1_y++;
piece2_y++;
piece3_y++;
piece4_y++;
}
void UpdatePosition(void)
{
if ((CheckCollision() == true) || (CheckBottom() == true))
{
Board[piece1_x][piece1_y] = true;
Board[piece2_x][piece2_y] = true;
Board[piece3_x][piece3_y] = true;
Board[piece4_x][piece4_y] = true;
piece_num = piece_num + 4;
PickPiece();
}
else
{
dbSprite(piece_num, piece1_x * 16, piece1_y * 16, 1);
dbSprite(piece_num + 1, piece2_x * 16, piece2_y * 16, 1);
dbSprite(piece_num + 2, piece3_x * 16, piece3_y * 16, 1);
dbSprite(piece_num + 3, piece4_x * 16, piece4_y * 16, 1);
}
}
bool CheckCollision(void)
{
if ((Board[piece1_x][piece1_y + 1] == true) || (Board[piece2_x][piece2_y + 1] == true) || (Board[piece3_x][piece3_y + 1] == true) || (Board[piece4_x][piece4_y + 1] == true))
{
return true;
}
else
{
return false;
}
}
bool CheckBottom(void)
{
if (piece1_y == 20)
{
Board[piece1_x][piece1_y] = true;
return true;
}
else if (piece2_y == 20)
{
Board[piece2_x][piece2_y] = true;
return true;
}
else if (piece3_y == 20)
{
Board[piece3_x][piece3_y] = true;
return true;
}
else if (piece4_y == 20)
{
Board[piece4_x][piece4_y] = true;
return true;
}
else
{
return false;
}
}
void CheckKeys(void)
{
if ((dbDownKey() == 1) && (down_key == false))
{
down_key = true;
piece1_y += 1;
piece2_y += 1;
piece3_y += 1;
piece4_y += 1;
}
if (dbDownKey() == 0)
{
down_key = false;
}
if ((dbLeftKey() == 1) && (left_key == false) && (CheckLCol != false))
{
left_key = true;
piece1_x -= 1;
piece2_x -= 1;
piece3_x -= 1;
piece4_x -= 1;
}
if (dbLeftKey() == 0)
{
left_key = false;
}
if ((dbRightKey() == 1) && (right_key == false) && (CheckRCol != false))
{
right_key = true;
piece1_x += 1;
piece2_x += 1;
piece3_x += 1;
piece4_x += 1;
}
if (dbRightKey() == 0)
{
right_key = false;
}
UpdatePosition();
}
bool CheckLCol(void)
{
if ((Board[piece1_x - 1][piece1_y] == true) || (Board[piece2_x - 1][piece2_y] == true) || (Board[piece3_x - 1][piece3_y] == true) || (Board[piece4_x - 1][piece4_y] == true))
{
return true;
}
else
{
return false;
}
}
bool CheckRCol(void)
{
if ((Board[piece1_x + 1][piece1_y] == true) || (Board[piece2_x + 1][piece2_y] == true) || (Board[piece3_x + 1][piece3_y] == true) || (Board[piece4_x + 1][piece4_y] == true))
{
return true;
}
else
{
return false;
}
}
And don't mind my global variables please, this is a rush job.
Media required is attached below.