Created
December 12, 2024 12:52
-
-
Save ProfAndreaPollini/70720ef9bb7e30b0f6304be5df3f8722 to your computer and use it in GitHub Desktop.
game of life - pre
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include "raylib.h" | |
const int screen_width = 800; | |
const int screen_height = 450; | |
const int tile_size = 20; | |
//------------------------------------------------------------------------------------ | |
// Program main entry point | |
//------------------------------------------------------------------------------------ | |
int main(void) | |
{ | |
// Initialization | |
//-------------------------------------------------------------------------------------- | |
int righe = screen_height/tile_size; | |
int colonne = screen_width/tile_size; | |
std::vector<bool> tiles; | |
int player_x = 10; | |
int player_y = 20; | |
bool is_drawing = false; | |
for (int i = 0; i < righe*colonne; i++) { | |
tiles.push_back(false); | |
} | |
InitWindow(screen_width, screen_height, "raylib [core] example - basic window"); | |
SetTargetFPS(60); // Set our game to run at 60 frames-per-second | |
//-------------------------------------------------------------------------------------- | |
// Main game loop | |
while (!WindowShouldClose()) // Detect window close button or ESC key | |
{ | |
// Update | |
//---------------------------------------------------------------------------------- | |
// TODO: Update your variables here | |
//---------------------------------------------------------------------------------- | |
if(IsKeyDown(KEY_UP)) { | |
player_y--; | |
} | |
if(IsKeyDown(KEY_DOWN)) { | |
player_y++; | |
} | |
if(IsKeyDown(KEY_LEFT)) { | |
player_x--; | |
} | |
if(IsKeyDown(KEY_RIGHT)) { | |
player_x++; | |
} | |
is_drawing = IsKeyDown(KEY_SPACE); | |
if (is_drawing) { | |
tiles[player_y*colonne + player_x] = true; | |
} | |
// if (IsKeyDown(KEY_SPACE)) { | |
// is_drawing = true; | |
// } else { | |
// is_drawing = false; | |
// } | |
// Draw | |
//-------------------------------------------------------------------------------- | |
BeginDrawing(); | |
ClearBackground(RAYWHITE); | |
for (int r = 0; r < righe; ++r) { | |
for (int c = 0; c < colonne; ++c) { | |
auto color = RED; | |
if (tiles[r*colonne+c]) { | |
color = YELLOW; | |
} | |
DrawRectangle(tile_size*c,tile_size*r,tile_size-1,tile_size-1,color); | |
} | |
} | |
DrawRectangle(player_x*tile_size,player_y*tile_size,tile_size-1,tile_size-1,WHITE); | |
EndDrawing(); | |
//---------------------------------------------------------------------------------- | |
} | |
// De-Initialization | |
//-------------------------------------------------------------------------------------- | |
CloseWindow(); // Close window and OpenGL context | |
//-------------------------------------------------------------------------------------- | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment