Added menus and reworked keys to correctly work and not take repeated inputs
This commit is contained in:
1 parent
dd0ab3d64b
commit
7304469a8f
1 file changed
+199
-71
+199
-71
@@ -1,103 +1,231 @@
|
||||
import pygame
|
||||
#import pyopengl
|
||||
import pygame_menu
|
||||
|
||||
SCREEN_X = 1920
|
||||
SCREEN_Y = 1080
|
||||
SCREEN = None
|
||||
MENU_X = 1000
|
||||
MENU_Y = 800
|
||||
RECTS = []
|
||||
RUNNING = True
|
||||
SIMULATE = False
|
||||
|
||||
MENU_THEME = pygame_menu.themes.Theme(
|
||||
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_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
|
||||
widget_box_border_width=2,
|
||||
widget_font=pygame_menu.font.FONT_MUNRO,
|
||||
)
|
||||
|
||||
#Functions
|
||||
def drawRects(rects):
|
||||
#Get rid of old rectangles
|
||||
screen.fill((0, 0, 0))
|
||||
for rectangle in rects:
|
||||
#(Surface, color [A], rect, width)
|
||||
pygame.draw.rect(screen, (255, 255, 255, 1), rectangle, width=0)
|
||||
if len(rects) == 0:
|
||||
screen.fill((0, 0, 0))
|
||||
|
||||
def removeRect(rects, pos):
|
||||
print(f"I am removing rectangle at {pos}")
|
||||
for rectangle in rects:
|
||||
if rectangle.collidepoint(pos):
|
||||
rects.remove(rectangle)
|
||||
print(f"I have removed the rectangle")
|
||||
#update screen
|
||||
drawRects(rects)
|
||||
return 1
|
||||
def drawGrid():
|
||||
# Draw the grid lines only
|
||||
global SCREEN
|
||||
for x in range(0, SCREEN.get_width(), 100):
|
||||
pygame.draw.line(SCREEN, (0, 0, 60), (x, 0), (x, SCREEN.get_height()))
|
||||
for y in range(0, SCREEN.get_height(), 100):
|
||||
pygame.draw.line(SCREEN, (0, 0, 60), (0, y), (SCREEN.get_width(), y))
|
||||
|
||||
return 0
|
||||
def drawRects():
|
||||
global SCREEN
|
||||
global RECTS
|
||||
|
||||
#Clear old rectangles
|
||||
SCREEN.fill((0,0,0))
|
||||
drawGrid()
|
||||
|
||||
# Initializations
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((1920, 1080))
|
||||
clock = pygame.time.Clock()
|
||||
running = True
|
||||
rects = []
|
||||
|
||||
screen.fill((0, 0, 0)) # Clear screen with black
|
||||
|
||||
#Game begins
|
||||
while running:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
pygame.display.flip() # Update the display
|
||||
clock.tick(30) # Maintain 30 FPS
|
||||
#Intro Screen
|
||||
|
||||
#Clear screen
|
||||
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()
|
||||
|
||||
#Draw grid
|
||||
for x in range(0, screen.get_width(), 100):
|
||||
#(surface, color, start_pos, end_pos)
|
||||
pygame.draw.line(screen, (0, 0, 60), (x, 0), (x, screen.get_width()))
|
||||
for y in range (0, screen.get_height(), 100):
|
||||
pygame.draw.line(screen, (0, 0, 60), (0, y), (screen.get_width(), y))
|
||||
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():
|
||||
global SCREEN
|
||||
global MENU_THEME
|
||||
|
||||
SCREEN.fill((0, 0, 0))
|
||||
menu = pygame_menu.Menu('Display', MENU_X, MENU_Y, theme=MENU_THEME)
|
||||
menu.add.button('FPS', mainMenu)
|
||||
|
||||
menu.mainloop(SCREEN)
|
||||
pygame.display.flip()
|
||||
return
|
||||
|
||||
def controlsMenu():
|
||||
pass
|
||||
|
||||
def settingsMenu():
|
||||
global SCREEN
|
||||
global MENU_THEME
|
||||
|
||||
SCREEN.fill((0, 0, 0))
|
||||
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('Display', displayMenu)
|
||||
menu.add.button('Controls', controlsMenu)
|
||||
menu.add.button('Quit to Desktop', pygame_menu.events.EXIT)
|
||||
|
||||
menu.mainloop(SCREEN)
|
||||
pygame.display.flip()
|
||||
return
|
||||
|
||||
def optionsMenu():
|
||||
global SCREEN
|
||||
global MENU_THEME
|
||||
|
||||
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.add.button('Quit to Menu', mainMenu)
|
||||
menu.add.button('Quit to Desktop', pygame_menu.events.EXIT)
|
||||
|
||||
menu.mainloop(SCREEN)
|
||||
pygame.display.flip()
|
||||
return
|
||||
|
||||
def mainMenu():
|
||||
# NOTE: For some reason mouse must click on item 2x on program start
|
||||
global SCREEN
|
||||
global MENU_THEME
|
||||
|
||||
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)
|
||||
|
||||
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 RECTS
|
||||
global SCREEN
|
||||
global RUNNING
|
||||
global SIMULATE
|
||||
|
||||
SCREEN.fill((0, 0, 0))
|
||||
#Draw the grid
|
||||
drawGrid()
|
||||
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
RUNNING = True
|
||||
while RUNNING == True:
|
||||
clock.tick(30)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
RUNNING = False
|
||||
return
|
||||
|
||||
#Get mouse input
|
||||
mousePos = (pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
|
||||
mouseInput = pygame.mouse.get_pressed()
|
||||
#Get keyboard input
|
||||
keys = pygame.key.get_pressed()
|
||||
|
||||
#Parse input
|
||||
if keys[pygame.K_ESCAPE]: # Open the options menu
|
||||
optionsMenu()
|
||||
if keys[pygame.K_RETURN]:
|
||||
#Reverse if the simulation is running
|
||||
SIMULATE = not SIMULATE
|
||||
if not SIMULATE:
|
||||
if keys[pygame.K_c]: # Clear screen when 'c' is pressed
|
||||
RECTS = []
|
||||
drawRects()
|
||||
if keys[pygame.K_s]: # Move screen down when 's' is pressed
|
||||
SCREEN.scroll(dy=+100)
|
||||
if keys[pygame.K_w]: # Move screen up when 'w' is pressed
|
||||
SCREEN.scroll(dy=-100)
|
||||
if keys[pygame.K_a]: # Move screen left when 'a' is pressed
|
||||
SCREEN.scroll(dx=-100)
|
||||
if keys[pygame.K_d]: # Move screen right when 'd' is pressed
|
||||
SCREEN.scroll(dx=100)
|
||||
|
||||
if mouseInput[0]: # Left mouse button make a white box
|
||||
#Snap position to grid
|
||||
x,y = mousePos
|
||||
if x % 10 == 0 and y % 10 == 0:
|
||||
break
|
||||
|
||||
if x % 10 != 0:
|
||||
x -= x % 10
|
||||
if y % 10 != 0:
|
||||
y -= y % 10
|
||||
#Draw rect at grid location
|
||||
newRect = pygame.Rect(x, y, 10, 10)
|
||||
if len(rects) == 0:
|
||||
rects.append(newRect)
|
||||
if len(rects) != 0:
|
||||
for rectangle in rects:
|
||||
if rectangle.colliderect(newRect) == False:
|
||||
rects.append(newRect)
|
||||
|
||||
drawRects(rects)
|
||||
|
||||
if not any(rect.colliderect(newRect) for rect in RECTS):
|
||||
RECTS.append(newRect)
|
||||
drawRects()
|
||||
|
||||
if mouseInput[2]: # Right mouse button get rid of white box
|
||||
#remove rect
|
||||
removeRect(rects, mousePos)
|
||||
#Snap position to grid
|
||||
x,y = mousePos
|
||||
|
||||
#Get keyboard input
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_ESCAPE]:
|
||||
running = False
|
||||
if keys[pygame.K_c]: # Clear screen when 'c' is pressed
|
||||
screen.fill((0, 0, 0))
|
||||
if keys[pygame.K_s]: # Move screen down when 's' is pressed
|
||||
screen.scroll(dy=100)
|
||||
if keys[pygame.K_w]: # Move screen up when 'w' is pressed
|
||||
screen.scroll(dy=-100)
|
||||
if keys[pygame.K_a]: # Move screen left when 'a' is pressed
|
||||
screen.scroll(dx=-100)
|
||||
if keys[pygame.K_d]: # Move screen right when 'd' is pressed
|
||||
screen.scroll(dx=100)()
|
||||
if x % 10 != 0:
|
||||
x -= x % 10
|
||||
if y % 10 != 0:
|
||||
y -= y % 10
|
||||
removeRect((x,y))
|
||||
|
||||
#Game Logic
|
||||
prev_mouse = mouseInput
|
||||
if SIMULATE:
|
||||
#Game Simulation logic
|
||||
#Check Rectangles if they have neighbors
|
||||
#Get rectangle
|
||||
#Get neighbors
|
||||
#Check count
|
||||
#If more than 3 neighbors rectangle "lives"
|
||||
#Else rectangle "dies"
|
||||
|
||||
pass
|
||||
|
||||
|
||||
if RUNNING == False:
|
||||
mainMenu()
|
||||
|
||||
def main():
|
||||
global SCREEN
|
||||
|
||||
#Initialization
|
||||
pygame.init()
|
||||
SCREEN = pygame.display.set_mode((SCREEN_X, SCREEN_Y))
|
||||
|
||||
#Update screen
|
||||
pygame.display.flip()
|
||||
if(RUNNING == True):
|
||||
#Load menu
|
||||
mainMenu()
|
||||
if(RUNNING == False):
|
||||
pygame_menu.events.EXIT
|
||||
|
||||
if __name__=="__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user