Skip to content

Instantly share code, notes, and snippets.

@ProfAndreaPollini
Created December 18, 2024 10:36
Show Gist options
  • Save ProfAndreaPollini/ccc40176193e406964aa4758553aa187 to your computer and use it in GitHub Desktop.
Save ProfAndreaPollini/ccc40176193e406964aa4758553aa187 to your computer and use it in GitHub Desktop.
game of life - lezione 18 dicembre 2024
#include <vector>
#include "raylib.h"
const int screen_width = 800;
const int screen_height = 450;
const int tile_size = 20;
const int righe = screen_height/tile_size;
const int colonne = screen_width/tile_size;
int n_celle_vive(const std::vector<bool>&world,int col, int row) {
int celle_vive = 0;
for(int i = -1; i<=1; i++) {
for(int j = -1; j<=1; j++) {
auto colonna = col+i;
auto riga = row+j;
if(!(i==0 and j==0)) {
auto idx = riga* colonne + colonna;
if(world[idx] == true) {
celle_vive++;
}
//DrawCircle(colonna*tile_size+tile_size/2,riga*tile_size+tile_size/2,tile_size/2,GREEN);
}
}
}
return celle_vive;
}
void simulation_step(std::vector<bool>&world) {
for(auto col = 0; col < colonne; col++) {
for(auto row = 0; row < righe; row++) {
auto cell_state = world[col+row*colonne];
auto celle_vive = n_celle_vive(world,col,row);
//world[col+row*colonne] = false;
if (cell_state == true) {
} else {
if(celle_vive==3) {
world[col+row*colonne] = true;
}
}
}
}
}
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
std::vector<bool> tiles;
int player_x = 10;
int player_y = 20;
bool is_drawing = false;
std::vector<int> trail;
bool highlight_mouse_pos = 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++;
}
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 (IsKeyPressed(KEY_E)) {
simulation_step(tiles);
}
// 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);
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
highlight_mouse_pos = !highlight_mouse_pos;
}
if (highlight_mouse_pos) {
auto mouse_x = GetMouseX();
auto mouse_y = GetMouseY();
auto mouse_cell_x = mouse_x/tile_size;
auto mouse_cell_y = mouse_y/tile_size;
DrawRectangle(mouse_cell_x*tile_size,mouse_cell_y*tile_size,tile_size-1,tile_size-1,GREEN);
auto celle_vive = n_celle_vive(tiles,mouse_cell_x,mouse_cell_y);
DrawText(TextFormat("vive = %i",celle_vive),mouse_x+20,mouse_y+20,64,BLACK);
// for(int i = -1; i<=1; i++) {
// for(int j = -1; j<=1; j++) {
// auto colonna = mouse_cell_x+i;
// auto riga = mouse_cell_y+j;
// if(!(i==0 and j==0)) {
// DrawCircle(colonna*tile_size+tile_size/2,riga*tile_size+tile_size/2,tile_size/2,GREEN);
// }
// }
}
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