add sfx, more ergonomic keys (in my opinion), fixed (probably) piece swapping #1

Closed
electron271 wants to merge 2 commits from electron271/just-plain-tetris:main into main
3 changed files with 79 additions and 22 deletions

View file

@ -7,12 +7,13 @@ i spent a while looking for a gimmick-free tetris implementation because i'm hoo
# controls
| | |
|---|---|
| **W** | Rotate piece |
| **A**/**D** | Move left/right |
| **S**/**Space** | Move down |
| **R** | Rotate piece |
| **←**/**→** | Move left/right |
| **S**/**** | Move down |
| **Tab** | Swap with storage |
| **P** | Pause |
| **Escape** | Quit |
| **M** | Mute SFX |
# to do
- balanced pseudorandom piece generation

BIN
beep.wav Normal file

Binary file not shown.

View file

@ -86,6 +86,7 @@ static bool pieceActive = false;
static bool detection = false;
static bool lineToDelete = false;
static bool storageInUse = false;
static bool audioMuted = false;
// Statistics
static int level = 1;
@ -102,6 +103,9 @@ static int fadeLineCounter = 0;
// Based on level
static int gravitySpeed = 30;
// Sound
static Sound beep;
//------------------------------------------------------------------------------------
// Module Functions Declaration (local)
//------------------------------------------------------------------------------------
@ -120,7 +124,6 @@ static bool ResolveTurnMovement();
static void CheckDetection(bool *detection);
static void CheckCompletion(bool *lineToDelete);
static int DeleteCompleteLines();
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
@ -129,6 +132,7 @@ int main(void)
// Initialization (Note windowTitle is unused on Android)
//---------------------------------------------------------
InitWindow(screenWidth, screenHeight, "just plain tetris");
InitAudioDevice();
InitGame();
@ -152,6 +156,7 @@ int main(void)
UnloadGame(); // Unload loaded data (textures, sounds, models...)
CloseWindow(); // Close window and OpenGL context
CloseAudioDevice();
//--------------------------------------------------------------------------------------
return 0;
@ -193,6 +198,9 @@ void InitGame(void)
fadeLineCounter = 0;
gravitySpeed = 30;
// beep
beep = LoadSound("beep.wav");
// Initialize grid matrices
for (int i = 0; i < GRID_HORIZONTAL_SIZE; i++)
{
@ -226,6 +234,21 @@ void UpdateGame(void)
{
if (IsKeyPressed('P')) pause = !pause;
// Mute audio toggle
if (IsKeyPressed('M'))
{
if (!audioMuted)
{
PauseSound(beep);
audioMuted = true;
}
else
{
ResumeSound(beep);
audioMuted = false;
}
}
if (!pause)
{
if (!lineToDelete)
@ -249,11 +272,11 @@ void UpdateGame(void)
turnMovementCounter++;
// We make sure to move if we've pressed the key this frame
if (IsKeyPressed(KEY_A) || IsKeyPressed(KEY_D)) lateralMovementCounter = LATERAL_SPEED;
if (IsKeyPressed(KEY_W)) turnMovementCounter = TURNING_SPEED;
if (IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_RIGHT)) lateralMovementCounter = LATERAL_SPEED;
if (IsKeyPressed(KEY_R)) turnMovementCounter = TURNING_SPEED;
// Fall down
if ((IsKeyDown(KEY_S) || IsKeyDown(KEY_SPACE)) && (fastFallMovementCounter >= FAST_FALL_AWAIT_COUNTER))
if ((IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) && (fastFallMovementCounter >= FAST_FALL_AWAIT_COUNTER))
{
// We make sure the piece is going to fall this frame
gravityMovementCounter += gravitySpeed;
@ -317,23 +340,52 @@ void UpdateGame(void)
pieceActive = false;
storageInUse = true;
} else {
// Swap current piece with stored piece
// Check if the stored piece can fit in the old piece's position
bool canFit = true;
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
if (piece[i][j].status == MOVING) grid[piecePositionX+i][piecePositionY+j].status = EMPTY;
tempPiece[i][j] = piece[i][j];
piece[i][j] = storage[i][j];
storage[i][j] = tempPiece[i][j];
if (piecePositionX + i >= GRID_HORIZONTAL_SIZE || piecePositionY + j >= GRID_VERTICAL_SIZE || grid[piecePositionX + i][piecePositionY + j].status == FULL){
canFit = false;
break;
}
}
if (!canFit){
break;
}
}
piecePositionX = (int)((GRID_HORIZONTAL_SIZE - 4)/2);
piecePositionY = 0;
for (int i = piecePositionX; i < piecePositionX + 4; i++){
for (int j = 0; j < 4; j++){
if (piece[i - (int)piecePositionX][j].status == MOVING){
grid[i][j].status = MOVING;
if (canFit){
// Swap current piece with stored piece
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
if (piece[i][j].status == MOVING) grid[piecePositionX+i][piecePositionY+j].status = EMPTY;
tempPiece[i][j] = piece[i][j];
piece[i][j] = storage[i][j];
storage[i][j] = tempPiece[i][j];
}
}
int oldPiecePositionX = piecePositionX;
int oldPiecePositionY = piecePositionY;
piecePositionX = oldPiecePositionX;
piecePositionY = oldPiecePositionY;
for (int i = oldPiecePositionX; i < oldPiecePositionX + 4; i++){
for (int j = oldPiecePositionY; j < oldPiecePositionY + 4; j++){
if (grid[i][j].status == MOVING){
grid[i][j].status = EMPTY; // Derender the old piece
}
}
}
for (int i = piecePositionX; i < piecePositionX + 4; i++){
for (int j = piecePositionY; j < piecePositionY + 4; j++){
if (piece[i - oldPiecePositionX][j - oldPiecePositionY].status == MOVING){
grid[i][j].status = MOVING;
}
grid[i][j].color = piece[i - oldPiecePositionX][j - oldPiecePositionY].color;
}
grid[i][j].color = piece[i - (int)piecePositionX][j].color;
}
}
}
@ -522,6 +574,7 @@ void DrawGame(void)
void UnloadGame(void)
{
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
UnloadSound(beep);
}
// Update and Draw (one frame)
@ -696,7 +749,7 @@ static bool ResolveLateralMovement()
bool collision = false;
// Piece movement
if (IsKeyDown(KEY_A) || IsGamepadButtonPressed(0, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) // Move left
if (IsKeyDown(KEY_LEFT) || IsGamepadButtonPressed(0, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) // Move left
{
// Check if is possible to move to left
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
@ -731,7 +784,7 @@ static bool ResolveLateralMovement()
piecePositionX--;
}
}
else if (IsKeyDown(KEY_D) || IsGamepadButtonPressed(0, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) // Move right
else if (IsKeyDown(KEY_RIGHT) || IsGamepadButtonPressed(0, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) // Move right
{
// Check if is possible to move to right
for (int j = GRID_VERTICAL_SIZE - 2; j >= 0; j--)
@ -778,8 +831,11 @@ static bool ResolveLateralMovement()
static bool ResolveTurnMovement()
{
// Input for turning the piece
if (IsKeyDown(KEY_W) || IsGamepadButtonPressed(0, GAMEPAD_BUTTON_RIGHT_FACE_LEFT))
if (IsKeyDown(KEY_R) || IsGamepadButtonPressed(0, GAMEPAD_BUTTON_RIGHT_FACE_LEFT))
{
// play beep.wav
if (!audioMuted) PlaySound(beep);
Cell aux;
bool checker = false;