Created
December 18, 2024 08:05
-
-
Save ProfAndreaPollini/0d1d7e36869b2e02bdbaca32bce60c86 to your computer and use it in GitHub Desktop.
game of life - C++ & raylib
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; | |
std::vector<int> trail; | |
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++; | |
} | |
if (player_x < 0) { | |
player_x += colonne; | |
} | |
player_x = player_x % colonne; | |
if(player_y < 0) { | |
player_y += righe; | |
} | |
if (player_y >= righe) { | |
player_y = player_y % righe; | |
} | |
auto tile_idx = player_y*colonne + player_x; // tilem corrente | |
if (trail.size() > 20) { | |
trail.pop_back(); | |
} | |
bool found = false; | |
for (int i = 0; i < trail.size(); i++) { | |
if (trail[i] == tile_idx) { | |
found = true; | |
break; | |
} | |
} | |
if(!found) { | |
//trail.push_back(tile_idx); | |
trail.insert(trail.begin(),tile_idx); | |
} | |
is_drawing = IsKeyDown(KEY_SPACE); | |
if (is_drawing) { | |
tiles[tile_idx] = 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); | |
// disegnola trail | |
for (int i = 0; i < trail.size(); ++i) { | |
auto idx = trail[i]; | |
int colonna = idx % colonne; | |
int riga = idx / colonne; | |
auto r = tile_size/2 -i*0.1*(tile_size/2); | |
DrawCircle(colonna*tile_size,riga*tile_size,r,GREEN); | |
} | |
DrawText(TextFormat("pos: %i %i", player_x,player_y), 20, 20, 40, BLACK); | |
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