Merge pull request #2 from DulcisL/menu-rebuild

Menu rebuild
This commit is contained in:
Lakota authored and GitHub committed 2026-05-16 21:33:39 -04:00
commit 63ca99e88d
3 files changed
+219 -138

No files matched your search

+102 -42
View File
@@ -1,48 +1,108 @@
#define SDL_MAIN_USE_CALLBACKS 1 #define SDL_MAIN_USE_CALLBACKS 1
#include <stdio.h> #include <stdio.h>
#include <SDL2/SDL.h> #include <stdlib.h>
#include <unistd.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
int main(int argc, char *argv[]){ static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
static Uint64 last_time = 0;
//Initialize Variables #define WINDOW_HEIGHT = 1080;
const SDL_VideoInfo* info = NULL; #define WINDOW_WIDTH = 1920;
SDL_Surface *SCREEN;
int DX = 1920; #define MIN_PIXELS_PER_SECOND = 10;
int DY = 1080; #define MAX_PIXELS_PER_SECOND = 20;
int PixelDepth = 3;
int Flags = 0; int main(int argc, char *argv[]) {
// Constants
printf("Initializing SDL ... \n"); int GAME_X, GAME_Y;
//error check if the video was initialized GAME_X = 1920;
if (SDL_Init(SDL_INIT_VIDEO) == -1){ GAME_Y = 1080;
printf("Failed to initialize \n", SDL_GetError()); bool RUNNING = true;
exit(-1); // Use Calloc to ensure the start is all 0's'
unsigned char *GAME_MATRIX =
(unsigned char *)calloc(GAME_X * GAME_Y, sizeof(bool));
//-----------------------------------------------------
// Var declarations
int scale, mouse_x, mouse_y;
scale = 10;
//-----------------------------------------------------
// Init
if (GAME_MATRIX == NULL) {
printf("\nCalloc failed");
return 1;
}
if (!SDL_INIT(SDL_INIT_VIDEO)) {
printf("\nCould not initiallize SDL_Window");
return 1;
}
//-----------------------------------------------------
// Logic
while (RUNNING) {
for (int y = 0; y < GAME_Y; y++) {
for (int x = 0; x < GAME_X; x++) {
// adjust for indexing
unsigned int index = y * GAME_X + x;
if (GAME_MATRIX[index]) {
int live_count = 0;
// Get neighbor indexes
unsigned int north, south, east, west, north_east, north_west,
south_east, south_west;
north = index - GAME_X;
north_east = index - GAME_X + 1;
east = index + 1;
south_west = index + GAME_X + 1;
south = index + GAME_X;
south_east = index + GAME_X - 1;
west = index - 1;
north_west = index - GAME_X - 1;
// Error check for boundary issue
// Need to find a way to have no neighbor rather than set the index to
// 0
if (north < 0) {
north = 0;
}
if (north_east < 0) {
north_east = 0;
}
if (north_west < 0) {
north_west = 0;
}
if (south > GAME_Y) {
south = 0;
}
if (south_east > GAME_Y) {
south_east = 0;
}
if (south_west > GAME_Y) {
south_west = 0;
}
// Adjust count (value will either be a 1 or a 0)
if (GAME_MATRIX[north] | GAME_MATRIX[north_west] | GAME_MATRIX[west] |
GAME_MATRIX[south_west] | GAME_MATRIX[south] | GAME_MATRIX[east] |
GAME_MATRIX[south_east] | GAME_MATRIX[north_east]) {
live_count += 1;
}
// Check if cell should live or die
if (live_count >= 4) {
GAME_MATRIX[index] = true;
} else {
GAME_MATRIX[index] = false;
}
} }
printf("SDL Initialized \n"); }
}
//Get SDL_OpenGl info and initialize }
info =SDL_GetVideoInfo(SDL_GL_RED_SIZE, 1); //-----------------------------------------------------
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 1); // Close out the game
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 1); free(GAME_MATRIX);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 3); return 0;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0); }
void SDL_AppQuit(void *appstate, SDL_AppResult result) {}
//Request initial window
Flags = SDL_OPENGL | SDL_FULLSCREEN;
//Set up the screen
SCREEN = SDL_SetVideoMode(DX, DY, PixelDepth, Flags);
//Check that screen was initialized
if (SCREEN == NULL) {
printf(stderr, "Screen could not be initialized \n", SDL_GetError());
exit(1);
}
//Quit SDL Before program close
SDL_Quit();
printf("Program out\n");
return 0;
}
Binary file not shown.
+117 -96
View File
@@ -1,6 +1,5 @@
import sys import sys
import pygame import pygame
import os
import pygame_menu import pygame_menu
import numpy as np import numpy as np
import math import math
@@ -22,122 +21,134 @@ GRID = np.zeros((GRID_H, GRID_W), dtype=bool)
MENU_THEME = pygame_menu.themes.Theme( MENU_THEME = pygame_menu.themes.Theme(
title_background_color=(0,0,0), # Black title_background_color=(0, 0, 0), # Black
border_color=(255,255,255), border_color=(255, 255, 255),
border_width=5, border_width=5,
fps=30, fps=30,
title_close_button=True, title_close_button=True,
title_close_button_background_color= (255,0,0), # Red title_close_button_background_color=(255, 0, 0), # Red
title_font=pygame_menu.font.FONT_MUNRO, title_font=pygame_menu.font.FONT_MUNRO,
title_font_color= (255,255,255), title_font_color=(255, 255, 255),
title_font_size= 40, title_font_size=40,
widget_box_background_color=(128, 128, 128), # Grey
widget_box_border_color=(30, 0, 0), # Light Red
widget_box_background_color = (128,128,128), # Grey
widget_box_border_color=(30,0,0), # Light Red
widget_box_border_width=2, widget_box_border_width=2,
widget_font=pygame_menu.font.FONT_MUNRO, widget_font=pygame_menu.font.FONT_MUNRO,
) )
#Functions # Functions
def updateGame(): def updateGame():
for x in range(GRID_W): for x in range(GRID_W):
for y in range(GRID_H): for y in range(GRID_H):
color = () color = ()
if GRID[y,x] == True: if GRID[y, x]:
color = (255,255,255) color = (255, 255, 255)
if GRID[y,x] == False: if not GRID[y, x]:
color = (0,0,0) color = (0, 0, 0)
drawRect(x * CELL_SIZE, y * CELL_SIZE, color) drawRect(x * CELL_SIZE, y * CELL_SIZE, color)
drawGrid() drawGrid()
pygame.display.flip() pygame.display.flip()
def drawRect(x,y, color):
#Draw the rectangle def drawRect(x, y, color):
square = pygame.Rect((x,y, CELL_SIZE, CELL_SIZE)) # Draw the rectangle
square = pygame.Rect((x, y, CELL_SIZE, CELL_SIZE))
pygame.draw.rect(SCREEN, color, square) pygame.draw.rect(SCREEN, color, square)
def drawGrid(): def drawGrid():
# Draw the grid lines # Draw the grid lines
gridOffset = CELL_SIZE * 10 gridOffset = CELL_SIZE * 10
for x in range(0, SCREEN.get_width(), gridOffset): for x in range(0, SCREEN.get_width(), gridOffset):
#(display,color, start, end, thickness) # (display,color, start, end, thickness)
pygame.draw.line(SCREEN, (0, 0, 60), (x, 0), (x, SCREEN.get_height()), 2) pygame.draw.line(SCREEN, (0, 0, 60), (x, 0), (x, SCREEN.get_height()), 2)
for y in range(0, SCREEN.get_height(), gridOffset): for y in range(0, SCREEN.get_height(), gridOffset):
pygame.draw.line(SCREEN, (0, 0, 60), (0, y), (SCREEN.get_width(), y), 2) pygame.draw.line(SCREEN, (0, 0, 60), (0, y), (SCREEN.get_width(), y), 2)
def displayMenu():
def displayMenu():
SCREEN.fill((0, 0, 0)) SCREEN.fill((0, 0, 0))
menu = pygame_menu.Menu('Display', MENU_X, MENU_Y, theme=MENU_THEME) menu = pygame_menu.Menu("Display", MENU_X, MENU_Y, theme=MENU_THEME)
menu.add.button('Back', settingsMenu) menu.add.button("Back", settingsMenu)
menu.mainloop(SCREEN) menu.mainloop(SCREEN)
pygame.display.flip() pygame.display.flip()
return return
def controlsMenu(): def controlsMenu():
SCREEN.fill((0, 0, 0)) SCREEN.fill((0, 0, 0))
menu = pygame_menu.Menu('Controls', MENU_X, MENU_Y, theme=MENU_THEME) menu = pygame_menu.Menu("Controls", MENU_X, MENU_Y, theme=MENU_THEME)
menu.add.label('Esc - Goes to the settings / options menu') menu.add.label("Esc - Goes to the settings / options menu")
menu.add.label('C - Clears the screen while in the game') menu.add.label("C - Clears the screen while in the game")
menu.add.label('Left mouse - Adds a cell to the screen while in the game') menu.add.label("Left mouse - Adds a cell to the screen while in the game")
menu.add.label('Right mouse - Removes a cell from the screen while in the game') menu.add.label("Right mouse - Removes a cell from the screen while in the game")
menu.add.button('Back', settingsMenu) menu.add.button("Back", settingsMenu)
menu.mainloop(SCREEN) menu.mainloop(SCREEN)
pygame.display.flip() pygame.display.flip()
return return
def settingsMenu(): def settingsMenu():
SCREEN.fill((0, 0, 0)) SCREEN.fill((0, 0, 0))
menu = pygame_menu.Menu('Display', MENU_X, MENU_Y, theme=MENU_THEME) menu = pygame_menu.Menu("Display", MENU_X, MENU_Y, theme=MENU_THEME)
menu.add.button('Display', displayMenu) menu.add.button("Display", displayMenu)
menu.add.range_slider('Sound', default=100, range_values=(0,100), increment=1) menu.add.range_slider("Sound", default=100, range_values=(0, 100), increment=1)
menu.add.button('Controls', controlsMenu) menu.add.button("Controls", controlsMenu)
menu.add.button('Main Menu', mainMenu) menu.add.button("Main Menu", mainMenu)
menu.add.button('Quit to Desktop', pygame_menu.events.EXIT) menu.add.button("Quit to Desktop", pygame_menu.events.EXIT)
menu.mainloop(SCREEN) menu.mainloop(SCREEN)
pygame.display.flip() pygame.display.flip()
return return
def optionsMenu(): def optionsMenu():
global RUNNING global RUNNING
SCREEN.fill((0, 0, 0)) SCREEN.fill((0, 0, 0))
menu = pygame_menu.Menu('Options', MENU_X, MENU_Y, theme=MENU_THEME) menu = pygame_menu.Menu("Options", MENU_X, MENU_Y, theme=MENU_THEME)
menu.add.button('Resume', game) menu.add.button("Resume", game)
menu.add.button('Settings', settingsMenu) menu.add.button("Settings", settingsMenu)
menu.add.button('Controls', controlsMenu) menu.add.button("Controls", controlsMenu)
def quit_to_menu(): def quit_to_menu():
global RUNNING global RUNNING
RUNNING = False RUNNING = False
menu.disable() menu.disable()
menu.add.button('Quit to Menu', quit_to_menu)
menu.add.button('Quit to Desktop', pygame_menu.events.EXIT) menu.add.button("Quit to Menu", quit_to_menu)
menu.add.button("Quit to Desktop", pygame_menu.events.EXIT)
menu.mainloop(SCREEN) menu.mainloop(SCREEN)
pygame.display.flip() pygame.display.flip()
return return
def mainMenu(): def mainMenu():
SCREEN.fill((0, 0, 0)) SCREEN.fill((0, 0, 0))
mainTheme = MENU_THEME.copy() mainTheme = MENU_THEME.copy()
mainTheme.title_close_button=False mainTheme.title_close_button = False
menu = pygame_menu.Menu("Welcome to a python recreation of Conways's game of Life", MENU_X, MENU_Y, theme=mainTheme) menu = pygame_menu.Menu(
"Welcome to a python recreation of Conways's game of Life",
MENU_X,
MENU_Y,
theme=mainTheme,
)
menu.add.button('Play', game) menu.add.button("Play", game)
menu.add.button('Settings', settingsMenu) menu.add.button("Settings", settingsMenu)
menu.add.button('Quit', pygame_menu.events.EXIT) menu.add.button("Quit", pygame_menu.events.EXIT)
menu.mainloop(SCREEN) menu.mainloop(SCREEN)
pygame.display.flip() pygame.display.flip()
return return
def game(): def game():
global SCREEN global SCREEN
global RUNNING global RUNNING
@@ -148,20 +159,18 @@ def game():
black = (0, 0, 0) black = (0, 0, 0)
nlimit = 3 nlimit = 3
SCREEN.fill(black) SCREEN.fill(black)
#Draw the grid # Draw the grid
drawGrid() drawGrid()
pygame.display.flip() pygame.display.flip()
clock = pygame.time.Clock() clock = pygame.time.Clock()
RUNNING = True RUNNING = True
while RUNNING: while RUNNING:
if not SIMULATE: if not SIMULATE:
clock.tick(120) clock.tick(120)
if SIMULATE: if SIMULATE:
clock.tick(10) clock.tick(10)
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
@@ -177,30 +186,28 @@ def game():
if event.key == pygame.K_ESCAPE: if event.key == pygame.K_ESCAPE:
SIMULATE = False SIMULATE = False
optionsMenu() optionsMenu()
#Get mouse input # Get mouse input
mx, my = pygame.mouse.get_pos() mx, my = pygame.mouse.get_pos()
#Snap to grid # Snap to grid
gx = (mx * SCALE_X) // CELL_SIZE gx = (mx * SCALE_X) // CELL_SIZE
gy = (my * SCALE_Y)// CELL_SIZE gy = (my * SCALE_Y) // CELL_SIZE
if 0 <= gx < GRID_W and 0 <= gy < GRID_H: if 0 <= gx < GRID_W and 0 <= gy < GRID_H:
mouseInput = pygame.mouse.get_pressed() mouseInput = pygame.mouse.get_pressed()
if not SIMULATE: if not SIMULATE:
if mouseInput[0]: # Left mouse button make a white box if mouseInput[0]: # Left mouse button make a white box
GRID[gy,gx] = True GRID[gy, gx] = True
drawRect(gx * CELL_SIZE, gy * CELL_SIZE, white) drawRect(gx * CELL_SIZE, gy * CELL_SIZE, white)
if mouseInput[2]: # Right mouse button get rid of white box if mouseInput[2]: # Right mouse button get rid of white box
GRID[gy,gx] = False GRID[gy, gx] = False
drawRect(gx * CELL_SIZE, gy * CELL_SIZE, black) drawRect(gx * CELL_SIZE, gy * CELL_SIZE, black)
if SIMULATE: if SIMULATE:
#Game Simulation logic # Game Simulation logic
#Check neighbors # Check neighbors
NEXT = GRID.copy() NEXT = GRID.copy()
for x in range(GRID_W): for x in range(GRID_W):
for y in range(GRID_H): for y in range(GRID_H):
@@ -211,32 +218,33 @@ def game():
continue continue
ny = (y + dy) % GRID_H ny = (y + dy) % GRID_H
nx = (x + dx) % GRID_W nx = (x + dx) % GRID_W
neighbors += GRID[ny,nx] neighbors += GRID[ny, nx]
#Apply rules # Apply rules
#If nlimit neighbors persist if alive # If nlimit neighbors persist if alive
if neighbors == nlimit - 1 and GRID[y,x]: if neighbors == nlimit - 1 and GRID[y, x]:
NEXT[y,x] = True NEXT[y, x] = True
#if nlimit neighbors lives if dead (repop) # if nlimit neighbors lives if dead (repop)
if neighbors == nlimit and not GRID[y,x]: if neighbors == nlimit and not GRID[y, x]:
NEXT[y,x] = True NEXT[y, x] = True
#if nlimit neighbors randomly lives or dies if alive (repop / overpop) # if nlimit neighbors randomly lives or dies if alive (repop / overpop)
if neighbors == nlimit and GRID[y,x]: if neighbors == nlimit and GRID[y, x]:
rx = (x + rand.randint(-1,1)) % GRID_W rx = (x + rand.randint(-1, 1)) % GRID_W
ry = (y + rand.randint(-1,1)) % GRID_H ry = (y + rand.randint(-1, 1)) % GRID_H
NEXT[ry,rx] = rand.randint(0, 1) NEXT[ry, rx] = rand.randint(0, 1)
#if >nlimit dies (overpop) # if >nlimit dies (overpop)
if neighbors > nlimit or neighbors < (nlimit - 1): if neighbors > nlimit or neighbors < (nlimit - 1):
NEXT[y,x] = False NEXT[y, x] = False
GRID[:] = NEXT GRID[:] = NEXT
updateGame() updateGame()
if not RUNNING: if not RUNNING:
return return
def main(): def main():
global SCREEN global SCREEN
global SCREEN_X global SCREEN_X
@@ -248,39 +256,52 @@ def main():
if sys.platform != "emscripten": if sys.platform != "emscripten":
import importlib import importlib
importlib.import_module("pygame_menu") importlib.import_module("pygame_menu")
#Initialization
# Initialization
pygame.init() pygame.init()
try: try:
#initialize the screen and set the height and widths # Initialize the screen and set the height and widths
SCREEN = pygame.display.set_mode((SCREEN_X,SCREEN_Y), pygame.RESIZABLE | pygame.SCALED) SCREEN = pygame.display.set_mode(
(SCREEN_X, SCREEN_Y), pygame.RESIZABLE | pygame.SCALED
)
SCREEN_X, SCREEN_Y = SCREEN.get_size() SCREEN_X, SCREEN_Y = SCREEN.get_size()
window_w, window_h = pygame.display.get_window_size() window_w, window_h = pygame.display.get_window_size()
if SCREEN if SCREEN:
SCALE_X = window_w / SCREEN_X | 1 SCALE_X = window_w / SCREEN_X
SCALE_Y = window_h / SCREEN_Y | 1 SCALE_Y = window_h / SCREEN_Y
# Normalize the scale
if SCALE_X > 1:
SCALE_X = 1
if SCALE_Y > 1:
SCALE_Y = 1
MENU_Y = 0.9 * SCREEN_Y * SCALE_Y
MENU_X = 0.8 * SCREEN_X * SCALE_X
MENU_Y = .9 * SCREEN_Y * SCALE_Y # Set the correct Scale for menus
MENU_X = .8 * SCREEN_X * SCALE_X MENU_THEME.title_font_size = min(50, math.floor(100 * SCALE_X)) | max(
50, math.floor(100 * SCALE_X)
)
MENU_THEME.widget_font_size = min(25, math.floor(50 * SCALE_X)) | max(
25, math.floor(100 * SCALE_X)
)
#Set the correct Scale for menus
MENU_THEME.title_font_size= min(50, math.floor(100 * SCALE_X)) | max(50, math.floor(100 * SCALE_X))
MENU_THEME.widget_font_size= min(25, math.floor(50 * SCALE_X)) | max(25, math.floor(100 * SCALE_X))
except Exception as e: except Exception as e:
print(e) print(e)
finally:
#Update screen finally:
# Update screen
pygame.display.flip() pygame.display.flip()
if(RUNNING == True): if RUNNING:
#Load menu # Load menu
mainMenu() mainMenu()
if(RUNNING == False): if not RUNNING:
pygame_menu.events.EXIT pygame_menu.events.EXIT
if __name__=="__main__":
if __name__ == "__main__":
main() main()