151 lines
4.9 KiB
C
151 lines
4.9 KiB
C
#include "include/raylib.h"
|
|
#include <stdio.h>
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------
|
|
typedef struct Block {
|
|
Vector2 position, size;
|
|
int form, onGround;
|
|
float speed, step;
|
|
|
|
struct Block *next;
|
|
} Block;
|
|
|
|
|
|
void createBlock(Block **head) {
|
|
Block *block = MemAlloc(sizeof(Block));
|
|
block->position = (Vector2){ 400, -40 };
|
|
block->size = (Vector2){ 20, 20 };
|
|
block->form = 1;
|
|
block->onGround = 0;
|
|
block->speed = 4;
|
|
block->step = 20;
|
|
block->next = NULL;
|
|
|
|
// Falls die Liste leer ist
|
|
if (*head == NULL) {
|
|
*head = block;
|
|
return;
|
|
}
|
|
|
|
// Durch alle Blöcke iterieren und den neuesten am Schluss hinzufügen
|
|
Block *currentBlock = *head;
|
|
while (currentBlock->next != NULL) {
|
|
currentBlock = currentBlock->next;
|
|
}
|
|
|
|
currentBlock->next = block;
|
|
}
|
|
|
|
|
|
int main(void) {
|
|
|
|
// INIT
|
|
const int screenWidth = 800;
|
|
const int screenHeight = 450;
|
|
InitWindow(screenWidth, screenHeight, "Tetris-C");
|
|
|
|
Block *block = { 0 };
|
|
createBlock(&block);
|
|
int blockMoving = 1;
|
|
int blockCounter = 1;
|
|
|
|
SetTargetFPS(60);
|
|
|
|
while (!WindowShouldClose()) {
|
|
//-------------------------------------------------------------------------------------------------------------------------------------
|
|
// UPDATE GAME LOGIC
|
|
// INPUT
|
|
Block *currentBlock = block;
|
|
// Alle Blöcke fallen lassen
|
|
while (currentBlock != NULL) {
|
|
// Wenn Block noch nicht auf Boden, dann fallen
|
|
if (currentBlock->onGround == 0) {
|
|
|
|
// MOVEMENT -------------------------------------------------
|
|
if (IsKeyPressed(KEY_LEFT)) {
|
|
currentBlock->position.x -= currentBlock->step;
|
|
}
|
|
|
|
if (IsKeyPressed(KEY_RIGHT)) {
|
|
currentBlock->position.x += currentBlock->step;
|
|
}
|
|
// ----------------------------------------------------------
|
|
// Kollision gegen anderen Block checken
|
|
|
|
float nextY = currentBlock->position.y + currentBlock->speed;
|
|
|
|
if (nextY > screenHeight - currentBlock->size.y) {
|
|
currentBlock->position.y = screenHeight - currentBlock->size.y;
|
|
blockMoving = 0;
|
|
currentBlock->onGround = 1;
|
|
}
|
|
else {
|
|
Rectangle nextRect = { currentBlock->position.x, nextY, currentBlock->size.x, currentBlock->size.y };
|
|
|
|
int collided = 0;
|
|
Block *collisionCheck = block;
|
|
while (collisionCheck != NULL) {
|
|
// Nur gegen andere, bereits gelandete Blöcke prüfen (und nicht gegen sich selbst!)
|
|
if (collisionCheck != currentBlock && collisionCheck->onGround == 1) {
|
|
Rectangle otherRect = { collisionCheck->position.x, collisionCheck->position.y, collisionCheck->size.x, collisionCheck->size.y };
|
|
|
|
if (CheckCollisionRecs(nextRect, otherRect)) {
|
|
collided = 1;
|
|
break;
|
|
}
|
|
}
|
|
collisionCheck = collisionCheck->next;
|
|
}
|
|
|
|
if (collided) {
|
|
blockMoving = 0;
|
|
currentBlock->onGround = 1;
|
|
}
|
|
else {
|
|
currentBlock->position.y = nextY;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
currentBlock = currentBlock->next;
|
|
}
|
|
|
|
// Wenn sich kein Block mehr bewegt, einen neuen erstellen
|
|
if (blockMoving == 0) {
|
|
createBlock(&block);
|
|
blockCounter++;
|
|
blockMoving = 1;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------
|
|
// ZEICHNEN
|
|
BeginDrawing();
|
|
ClearBackground(RAYWHITE);
|
|
|
|
DrawFPS(20, 20);
|
|
DrawText(TextFormat("Blocks: %i", blockCounter), 20, 40, 20, GREEN);
|
|
|
|
// BLOCK ZEICHNEN
|
|
currentBlock = block;
|
|
while (currentBlock != NULL) {
|
|
DrawRectangleV(currentBlock->position, currentBlock->size, BLUE);
|
|
currentBlock = currentBlock->next;
|
|
}
|
|
|
|
EndDrawing();
|
|
//-------------------------------------------------------------------------------------------------------------------------------------
|
|
}
|
|
|
|
|
|
// Alle Blöcke im Speicher freigeben
|
|
Block *currentBlock = block;
|
|
while (currentBlock != NULL) {
|
|
Block *nextBlock = currentBlock->next;
|
|
MemFree(currentBlock);
|
|
currentBlock = nextBlock;
|
|
}
|
|
|
|
CloseWindow();
|
|
return 0;
|
|
} |