3 files changed
+212
-131
No files matched your search
+100
-40
@@ -1,48 +1,108 @@
|
||||
#define SDL_MAIN_USE_CALLBACKS 1
|
||||
#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
|
||||
const SDL_VideoInfo* info = NULL;
|
||||
SDL_Surface *SCREEN;
|
||||
int DX = 1920;
|
||||
int DY = 1080;
|
||||
int PixelDepth = 3;
|
||||
int Flags = 0;
|
||||
#define WINDOW_HEIGHT = 1080;
|
||||
#define WINDOW_WIDTH = 1920;
|
||||
|
||||
printf("Initializing SDL ... \n");
|
||||
//error check if the video was initialized
|
||||
if (SDL_Init(SDL_INIT_VIDEO) == -1){
|
||||
printf("Failed to initialize \n", SDL_GetError());
|
||||
exit(-1);
|
||||
#define MIN_PIXELS_PER_SECOND = 10;
|
||||
#define MAX_PIXELS_PER_SECOND = 20;
|
||||
|
||||
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));
|
||||
|
||||
//-----------------------------------------------------
|
||||
// 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);
|
||||
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();
|
||||
printf("Program out\n");
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------
|
||||
// Close out the game
|
||||
free(GAME_MATRIX);
|
||||
return 0;
|
||||
}
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result) {}
|
||||
Binary file not shown.
+112
-91
@@ -1,6 +1,5 @@
|
||||
import sys
|
||||
import pygame
|
||||
import os
|
||||
import pygame_menu
|
||||
import numpy as np
|
||||
import math
|
||||
@@ -22,122 +21,134 @@ GRID = np.zeros((GRID_H, GRID_W), dtype=bool)
|
||||
|
||||
|
||||
MENU_THEME = pygame_menu.themes.Theme(
|
||||
title_background_color=(0,0,0), # Black
|
||||
border_color=(255,255,255),
|
||||
title_background_color=(0, 0, 0), # Black
|
||||
border_color=(255, 255, 255),
|
||||
border_width=5,
|
||||
fps=30,
|
||||
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_color= (255,255,255),
|
||||
title_font_size= 40,
|
||||
|
||||
|
||||
widget_box_background_color = (128,128,128), # Grey
|
||||
widget_box_border_color=(30,0,0), # Light Red
|
||||
title_font_color=(255, 255, 255),
|
||||
title_font_size=40,
|
||||
widget_box_background_color=(128, 128, 128), # Grey
|
||||
widget_box_border_color=(30, 0, 0), # Light Red
|
||||
widget_box_border_width=2,
|
||||
widget_font=pygame_menu.font.FONT_MUNRO,
|
||||
)
|
||||
|
||||
#Functions
|
||||
# Functions
|
||||
|
||||
|
||||
def updateGame():
|
||||
for x in range(GRID_W):
|
||||
for y in range(GRID_H):
|
||||
color = ()
|
||||
if GRID[y,x] == True:
|
||||
color = (255,255,255)
|
||||
if GRID[y,x] == False:
|
||||
color = (0,0,0)
|
||||
if GRID[y, x]:
|
||||
color = (255, 255, 255)
|
||||
if not GRID[y, x]:
|
||||
color = (0, 0, 0)
|
||||
drawRect(x * CELL_SIZE, y * CELL_SIZE, color)
|
||||
|
||||
drawGrid()
|
||||
pygame.display.flip()
|
||||
|
||||
def drawRect(x,y, color):
|
||||
#Draw the rectangle
|
||||
square = pygame.Rect((x,y, CELL_SIZE, CELL_SIZE))
|
||||
|
||||
def drawRect(x, y, color):
|
||||
# Draw the rectangle
|
||||
square = pygame.Rect((x, y, CELL_SIZE, CELL_SIZE))
|
||||
pygame.draw.rect(SCREEN, color, square)
|
||||
|
||||
|
||||
def drawGrid():
|
||||
# Draw the grid lines
|
||||
gridOffset = CELL_SIZE * 10
|
||||
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)
|
||||
for y in range(0, SCREEN.get_height(), gridOffset):
|
||||
pygame.draw.line(SCREEN, (0, 0, 60), (0, y), (SCREEN.get_width(), y), 2)
|
||||
|
||||
def displayMenu():
|
||||
|
||||
def displayMenu():
|
||||
SCREEN.fill((0, 0, 0))
|
||||
menu = pygame_menu.Menu('Display', MENU_X, MENU_Y, theme=MENU_THEME)
|
||||
menu.add.button('Back', settingsMenu)
|
||||
menu = pygame_menu.Menu("Display", MENU_X, MENU_Y, theme=MENU_THEME)
|
||||
menu.add.button("Back", settingsMenu)
|
||||
|
||||
menu.mainloop(SCREEN)
|
||||
pygame.display.flip()
|
||||
return
|
||||
|
||||
|
||||
def controlsMenu():
|
||||
SCREEN.fill((0, 0, 0))
|
||||
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('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('Right mouse - Removes a cell from the screen while in the game')
|
||||
menu.add.button('Back', settingsMenu)
|
||||
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("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("Right mouse - Removes a cell from the screen while in the game")
|
||||
menu.add.button("Back", settingsMenu)
|
||||
|
||||
menu.mainloop(SCREEN)
|
||||
pygame.display.flip()
|
||||
return
|
||||
|
||||
|
||||
def settingsMenu():
|
||||
|
||||
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.range_slider('Sound', default=100, range_values=(0,100), increment=1)
|
||||
menu.add.button('Controls', controlsMenu)
|
||||
menu.add.button('Main Menu', mainMenu)
|
||||
menu.add.button('Quit to Desktop', pygame_menu.events.EXIT)
|
||||
menu.add.button("Display", displayMenu)
|
||||
menu.add.range_slider("Sound", default=100, range_values=(0, 100), increment=1)
|
||||
menu.add.button("Controls", controlsMenu)
|
||||
menu.add.button("Main Menu", mainMenu)
|
||||
menu.add.button("Quit to Desktop", pygame_menu.events.EXIT)
|
||||
|
||||
menu.mainloop(SCREEN)
|
||||
pygame.display.flip()
|
||||
return
|
||||
|
||||
|
||||
def optionsMenu():
|
||||
global RUNNING
|
||||
|
||||
SCREEN.fill((0, 0, 0))
|
||||
menu = pygame_menu.Menu('Options', MENU_X, MENU_Y, theme=MENU_THEME)
|
||||
menu.add.button('Resume', game)
|
||||
menu.add.button('Settings', settingsMenu)
|
||||
menu.add.button('Controls', controlsMenu)
|
||||
menu = pygame_menu.Menu("Options", MENU_X, MENU_Y, theme=MENU_THEME)
|
||||
menu.add.button("Resume", game)
|
||||
menu.add.button("Settings", settingsMenu)
|
||||
menu.add.button("Controls", controlsMenu)
|
||||
|
||||
def quit_to_menu():
|
||||
global RUNNING
|
||||
RUNNING = False
|
||||
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)
|
||||
pygame.display.flip()
|
||||
return
|
||||
|
||||
|
||||
def mainMenu():
|
||||
SCREEN.fill((0, 0, 0))
|
||||
mainTheme = MENU_THEME.copy()
|
||||
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)
|
||||
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.add.button('Play', game)
|
||||
menu.add.button('Settings', settingsMenu)
|
||||
menu.add.button('Quit', pygame_menu.events.EXIT)
|
||||
menu.add.button("Play", game)
|
||||
menu.add.button("Settings", settingsMenu)
|
||||
menu.add.button("Quit", pygame_menu.events.EXIT)
|
||||
menu.mainloop(SCREEN)
|
||||
pygame.display.flip()
|
||||
return
|
||||
|
||||
|
||||
def game():
|
||||
global SCREEN
|
||||
global RUNNING
|
||||
@@ -148,9 +159,8 @@ def game():
|
||||
black = (0, 0, 0)
|
||||
nlimit = 3
|
||||
|
||||
|
||||
SCREEN.fill(black)
|
||||
#Draw the grid
|
||||
# Draw the grid
|
||||
drawGrid()
|
||||
pygame.display.flip()
|
||||
clock = pygame.time.Clock()
|
||||
@@ -162,7 +172,6 @@ def game():
|
||||
if SIMULATE:
|
||||
clock.tick(10)
|
||||
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
RUNNING = False
|
||||
@@ -178,29 +187,27 @@ def game():
|
||||
SIMULATE = False
|
||||
optionsMenu()
|
||||
|
||||
#Get mouse input
|
||||
# Get mouse input
|
||||
mx, my = pygame.mouse.get_pos()
|
||||
#Snap to grid
|
||||
# Snap to grid
|
||||
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:
|
||||
|
||||
mouseInput = pygame.mouse.get_pressed()
|
||||
if not SIMULATE:
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
if SIMULATE:
|
||||
#Game Simulation logic
|
||||
# Game Simulation logic
|
||||
|
||||
#Check neighbors
|
||||
# Check neighbors
|
||||
NEXT = GRID.copy()
|
||||
for x in range(GRID_W):
|
||||
for y in range(GRID_H):
|
||||
@@ -211,32 +218,33 @@ def game():
|
||||
continue
|
||||
ny = (y + dy) % GRID_H
|
||||
nx = (x + dx) % GRID_W
|
||||
neighbors += GRID[ny,nx]
|
||||
neighbors += GRID[ny, nx]
|
||||
|
||||
#Apply rules
|
||||
#If nlimit neighbors persist if alive
|
||||
if neighbors == nlimit - 1 and GRID[y,x]:
|
||||
NEXT[y,x] = True
|
||||
# Apply rules
|
||||
# If nlimit neighbors persist if alive
|
||||
if neighbors == nlimit - 1 and GRID[y, x]:
|
||||
NEXT[y, x] = True
|
||||
|
||||
#if nlimit neighbors lives if dead (repop)
|
||||
if neighbors == nlimit and not GRID[y,x]:
|
||||
NEXT[y,x] = True
|
||||
# if nlimit neighbors lives if dead (repop)
|
||||
if neighbors == nlimit and not GRID[y, x]:
|
||||
NEXT[y, x] = True
|
||||
|
||||
#if nlimit neighbors randomly lives or dies if alive (repop / overpop)
|
||||
if neighbors == nlimit and GRID[y,x]:
|
||||
rx = (x + rand.randint(-1,1)) % GRID_W
|
||||
ry = (y + rand.randint(-1,1)) % GRID_H
|
||||
NEXT[ry,rx] = rand.randint(0, 1)
|
||||
# if nlimit neighbors randomly lives or dies if alive (repop / overpop)
|
||||
if neighbors == nlimit and GRID[y, x]:
|
||||
rx = (x + rand.randint(-1, 1)) % GRID_W
|
||||
ry = (y + rand.randint(-1, 1)) % GRID_H
|
||||
NEXT[ry, rx] = rand.randint(0, 1)
|
||||
|
||||
#if >nlimit dies (overpop)
|
||||
# if >nlimit dies (overpop)
|
||||
if neighbors > nlimit or neighbors < (nlimit - 1):
|
||||
NEXT[y,x] = False
|
||||
NEXT[y, x] = False
|
||||
|
||||
GRID[:] = NEXT
|
||||
updateGame()
|
||||
if not RUNNING:
|
||||
return
|
||||
|
||||
|
||||
def main():
|
||||
global SCREEN
|
||||
global SCREEN_X
|
||||
@@ -248,39 +256,52 @@ def main():
|
||||
|
||||
if sys.platform != "emscripten":
|
||||
import importlib
|
||||
|
||||
importlib.import_module("pygame_menu")
|
||||
#Initialization
|
||||
|
||||
# Initialization
|
||||
pygame.init()
|
||||
try:
|
||||
#initialize the screen and set the height and widths
|
||||
SCREEN = pygame.display.set_mode((SCREEN_X,SCREEN_Y), pygame.RESIZABLE | pygame.SCALED)
|
||||
# Initialize the screen and set the height and widths
|
||||
SCREEN = pygame.display.set_mode(
|
||||
(SCREEN_X, SCREEN_Y), pygame.RESIZABLE | pygame.SCALED
|
||||
)
|
||||
SCREEN_X, SCREEN_Y = SCREEN.get_size()
|
||||
|
||||
window_w, window_h = pygame.display.get_window_size()
|
||||
|
||||
if SCREEN
|
||||
SCALE_X = window_w / SCREEN_X | 1
|
||||
SCALE_Y = window_h / SCREEN_Y | 1
|
||||
if SCREEN:
|
||||
SCALE_X = window_w / SCREEN_X
|
||||
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
|
||||
MENU_X = .8 * SCREEN_X * 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))
|
||||
# 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:
|
||||
print(e)
|
||||
finally:
|
||||
|
||||
#Update screen
|
||||
finally:
|
||||
# Update screen
|
||||
pygame.display.flip()
|
||||
if(RUNNING == True):
|
||||
#Load menu
|
||||
if RUNNING:
|
||||
# Load menu
|
||||
mainMenu()
|
||||
if(RUNNING == False):
|
||||
if not RUNNING:
|
||||
pygame_menu.events.EXIT
|
||||
|
||||
if __name__=="__main__":
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user