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
+164 -83

No files matched your search

+95 -35
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>
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
static Uint64 last_time = 0;
#define WINDOW_HEIGHT = 1080;
#define WINDOW_WIDTH = 1920;
#define MIN_PIXELS_PER_SECOND = 10;
#define MAX_PIXELS_PER_SECOND = 20;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// Constants
int GAME_X, GAME_Y;
GAME_X = 1920;
GAME_Y = 1080;
bool RUNNING = true;
// Use Calloc to ensure the start is all 0's'
unsigned char *GAME_MATRIX =
(unsigned char *)calloc(GAME_X * GAME_Y, sizeof(bool));
//Initialize Variables //-----------------------------------------------------
const SDL_VideoInfo* info = NULL; // Var declarations
SDL_Surface *SCREEN; int scale, mouse_x, mouse_y;
int DX = 1920; scale = 10;
int DY = 1080;
int PixelDepth = 3;
int Flags = 0;
printf("Initializing SDL ... \n"); //-----------------------------------------------------
//error check if the video was initialized // Init
if (SDL_Init(SDL_INIT_VIDEO) == -1){ if (GAME_MATRIX == NULL) {
printf("Failed to initialize \n", SDL_GetError()); printf("\nCalloc failed");
exit(-1); return 1;
} }
printf("SDL Initialized \n"); if (!SDL_INIT(SDL_INIT_VIDEO)) {
printf("\nCould not initiallize SDL_Window");
//Get SDL_OpenGl info and initialize return 1;
info =SDL_GetVideoInfo(SDL_GL_RED_SIZE, 1);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 1);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
//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(); // Logic
printf("Program out\n"); 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;
}
}
}
}
}
//-----------------------------------------------------
// Close out the game
free(GAME_MATRIX);
return 0; return 0;
} }
void SDL_AppQuit(void *appstate, SDL_AppResult result) {}
Binary file not shown.
+69 -48
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
@@ -31,8 +30,6 @@ MENU_THEME = pygame_menu.themes.Theme(
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_background_color=(128, 128, 128), # Grey
widget_box_border_color=(30, 0, 0), # Light Red widget_box_border_color=(30, 0, 0), # Light Red
widget_box_border_width=2, widget_box_border_width=2,
@@ -41,24 +38,27 @@ MENU_THEME = pygame_menu.themes.Theme(
# 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): def drawRect(x, y, color):
# Draw the rectangle # Draw the rectangle
square = pygame.Rect((x, y, CELL_SIZE, CELL_SIZE)) 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
@@ -68,76 +68,87 @@ def drawGrid():
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,7 +159,6 @@ 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()
@@ -162,7 +172,6 @@ def game():
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:
RUNNING = False RUNNING = False
@@ -185,10 +194,8 @@ def game():
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)
@@ -237,6 +244,7 @@ def game():
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_Y = .9 * SCREEN_Y * SCALE_Y MENU_X = 0.8 * SCREEN_X * SCALE_X
MENU_X = .8 * SCREEN_X * SCALE_X
# Set the correct Scale for menus # 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.title_font_size = min(50, math.floor(100 * SCALE_X)) | max(
MENU_THEME.widget_font_size= min(25, math.floor(50 * SCALE_X)) | max(25, math.floor(100 * SCALE_X)) 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:
finally:
# Update screen # 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()