Completed grid and added logic, changed game rules to be semi random and added a double buffer like change over

This commit is contained in:
DulcisL committed 2025-12-18 17:16:52 -05:00
1 parent f0c385defe
commit 78493fda53
2 files changed
+101 -77

No files matched your search

Binary file not shown.
+101 -77
View File
@@ -2,18 +2,22 @@ import sys
import pygame import pygame
import pygbag import pygbag
import os import os
#import pyopengl
import pygame_menu import pygame_menu
import numpy as np import numpy as np
import random as rand
SCREEN_X = 1280 SCREEN_X = 1280
SCREEN_Y = 720 SCREEN_Y = 720
SCREEN = None SCREEN = None
MENU_X = 1000 MENU_X = 1000
MENU_Y = 700 MENU_Y = 700
RECTS = [] CELL_SIZE = 10
RUNNING = True RUNNING = True
SIMULATE = False SIMULATE = False
GRID_H = SCREEN_Y // CELL_SIZE
GRID_W = SCREEN_X // CELL_SIZE
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
@@ -34,6 +38,27 @@ MENU_THEME = pygame_menu.themes.Theme(
#Functions #Functions
def updateGame():
global GRID
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)
drawRect(x * CELL_SIZE, y * CELL_SIZE, color)
drawGrid()
pygame.display.flip()
def drawRect(x,y, color):
global CELL_SIZE
#Draw the rectangle
square = pygame.Rect((x,y, CELL_SIZE, CELL_SIZE))
pygame.draw.rect(SCREEN, color, square)
def drawGrid(): def drawGrid():
# Draw the grid lines only # Draw the grid lines only
global SCREEN global SCREEN
@@ -42,28 +67,6 @@ def drawGrid():
for y in range(0, SCREEN.get_height(), 100): for y in range(0, SCREEN.get_height(), 100):
pygame.draw.line(SCREEN, (0, 0, 60), (0, y), (SCREEN.get_width(), y)) pygame.draw.line(SCREEN, (0, 0, 60), (0, y), (SCREEN.get_width(), y))
def drawRects():
global SCREEN
global RECTS
#Clear old rectangles
SCREEN.fill((0,0,0))
drawGrid()
for rectangle in RECTS:
# (Surface, color [R,G,B,A], rect, width)
pygame.draw.rect(SCREEN, (255, 255, 255), rectangle, width=0)
pygame.display.flip()
def removeRect(pos):
global SCREEN
global RECTS
for i in range(len(RECTS) - 1, -1, -1):
if RECTS[i].collidepoint(pos):
RECTS.pop(i)
drawRects()
def displayMenu(): def displayMenu():
global SCREEN global SCREEN
global MENU_THEME global MENU_THEME
@@ -110,7 +113,7 @@ def optionsMenu():
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', settingsMenu) 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():
@@ -142,84 +145,105 @@ def mainMenu():
return return
def game(): def game():
global RECTS
global SCREEN global SCREEN
global RUNNING global RUNNING
global SIMULATE global SIMULATE
global CELL_SIZE
global GRID
white = (255, 255, 255)
black = (0, 0, 0)
nlimit = 3
SCREEN.fill((0, 0, 0))
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 == True: while RUNNING:
clock.tick(30) if not SIMULATE:
clock.tick(120)
if SIMULATE:
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
return return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
SIMULATE = not SIMULATE
if event.key == pygame.K_c and not SIMULATE:
GRID[:] = False
SCREEN.fill(black)
drawGrid()
if event.key == pygame.K_ESCAPE:
SIMULATE = False
optionsMenu()
#Get mouse input #Get mouse input
mousePos = (pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) mx, my = pygame.mouse.get_pos()
mouseInput = pygame.mouse.get_pressed() #Snap to grid
#Get keyboard input gx = mx // CELL_SIZE
keys = pygame.key.get_pressed() gy = my // CELL_SIZE
#Parse input if 0 <= gx < GRID_W and 0 <= gy < GRID_H:
if keys[pygame.K_ESCAPE]: # Open the options menu
optionsMenu()
if not RUNNING:
break
if keys[pygame.K_RETURN]:
#Reverse if the simulation is running
#NOTE: Doesn't always work as expected
SIMULATE = not SIMULATE
if not SIMULATE:
if keys[pygame.K_c]: # Clear screen when 'c' is pressed
RECTS = []
drawRects()
if mouseInput[0]: # Left mouse button make a white box mouseInput = pygame.mouse.get_pressed()
#Snap position to grid if not SIMULATE:
x,y = mousePos
if x % 10 != 0: if mouseInput[0]: # Left mouse button make a white box
x -= x % 10 GRID[gy,gx] = True
if y % 10 != 0: drawRect(gx * CELL_SIZE, gy * CELL_SIZE, white)
y -= y % 10
#Draw rect at grid location
newRect = pygame.Rect(x, y, 10, 10)
if not any(rect.colliderect(newRect) for rect in RECTS): if mouseInput[2]: # Right mouse button get rid of white box
RECTS.append(newRect) GRID[gy,gx] = False
drawRects() drawRect(gx * CELL_SIZE, gy * CELL_SIZE, black)
if mouseInput[2]: # Right mouse button get rid of white box if SIMULATE:
#Snap position to grid #Game Simulation logic
x,y = mousePos
if x % 10 != 0: #Check neighbors
x -= x % 10 NEXT = GRID.copy()
if y % 10 != 0: for x in range(GRID_W):
y -= y % 10 for y in range(GRID_H):
removeRect((x,y)) neighbors = 0
for dx in (-1, 0, 1):
for dy in (-1, 0, 1):
if dx == 0 and dy == 0:
continue
ny = (y + dy) % GRID_H
nx = (x + dx) % GRID_W
neighbors += GRID[ny,nx]
prev_mouse = mouseInput #Apply rules
if SIMULATE: #If nlimit neighbors persist
#Game Simulation logic if neighbors == nlimit - 1:
#Check Rectangles if they have neighbors NEXT[y,x] = True
#Get rectangle
#Get neighbors
#Check count
#If more than 3 neighbors rectangle "lives"
#Else rectangle "dies"
pass #if nlimit neighbors lives if dead (repop)
return if neighbors == nlimit and not GRID[y,x]:
NEXT[y,x] = True
# Random play around
#if nlimit neighbors randomly lives or dies if alive
if neighbors == nlimit and GRID[y,x]:
rand.seed(rand.random())
rx = rand.randint(-1,1)
ry = rand.randint(-1,1)
NEXT[ry,rx] = rand.randint(0,1)
#if >nlimit dies (overpop)
if neighbors > 3:
NEXT[y,x] = False
GRID[:] = NEXT
updateGame()
if not RUNNING:
return
def main(): def main():
global SCREEN global SCREEN