I am making a 2048 game in python and I am using def functions to execute a certain chunk of my code whenever I want to. But I've run into a problem while trying to generate the random numbers that spawn at the start of the game and whenever you make a move. RecursionError: maximum recursion depth exceeded in comparison. I have no idea what this means and how it happened. This is a chunk of the code simplified:
def game():
if not 'n' in locals():
random_n()
#Generating 2/4s
def random_n():
num = randint(0,100)
global tile_number, n
if num > 10:
tile_number = 2
else:
tile_number = 4
if not 'n' in locals():
n = 0
random_n()
game()
while True:
events = pygame.event.get()
for event in events:
#WASD (only really the end matters(i'm just showing I have a loop at the bottom))
if event.type == pygame.KEYDOWN:
def keybinds():
if event.key == pygame.K_w:
print("Pressed W")
elif event.key == pygame.K_a:
print("Pressed A")
elif event.key == pygame.K_s:
print("Pressed S")
elif event.key == pygame.K_d:
print("Pressed D")
#ARROWS
elif event.key == pygame.K_UP:
print("Pressed UP")
elif event.key == pygame.K_LEFT:
print("Pressed LEFT")
elif event.key == pygame.K_DOWN:
print("Pressed DOWN")
elif event.key == pygame.K_RIGHT:
print("Pressed RIGHT")
else:
keybinds()
random_n()
keybinds()
This is not my actual code but a simplified version to get rid of things that I don't think affect the error, but if that is not true, here is my real code:
import pygame
from random import randint
#Initialising PyGame
pygame.init()
#Creating Display
w = 640
h = 780
screen = pygame.display.set_mode((w,h))
#PyGame window name and icon
pygame.display.set_caption('2048')
icon = pygame.image.load('2048 icon.png')
pygame.display.set_icon(icon)
#Variables
background_c = 236, 231, 217
font_c = 147, 121, 97
button_c = font_c
menu_restart_background_c = button_c
game_background_c = 191, 173, 155
score_background_c = game_background_c
font1 = pygame.font.Font('calibri font sv\Calibri Bold.TTF', 30)
font2 = pygame.font.Font('calibri font sv\Calibri Bold.TTF', 80)
font3 = pygame.font.Font('calibri font sv\Calibri Bold.TTF', 60)
game_slots_c = 208, 193, 176
number_text_c = {"2+4_c": (123, 110, 98), "above4_c": (255,255,255)}
#number_background_c = {"2": (238, 228, 218), "4": (237, 224, 199), "8": (242, 177, 122), "16": (245, 149, 99),
#"32": (255, 116, 85), "64": (, , ), "128": (, , ), "256": (, , ), "512": (255, 255, 255),
#"1024": (255, 255, 255), "2048": (255, 255, 255)}
play = 0
#Menu
def menu():
#Colours + Rectangles
screen.fill((249, 248, 236))
pygame.draw.rect(screen, background_c,(0,0, w, h/10))
#Title Text
title = font1.render("2048", True, font_c)
screen.blit(title, (35, 25))
pygame.display.flip()
#Button
pygame.draw.rect(screen, button_c, (49, 349, 536, 100), 0, 12)
#Button Text
pbutton_f = font2.render("Play", True, (255,255,255))
screen.blit(pbutton_f,(240, 360))
pygame.display.update()
menu()
#Game
def game():
i = 114
#Game board
screen.fill(background_c)
game_board = pygame.Surface((536, 536))
game_board.fill((background_c))
pygame.draw.rect(game_board, game_background_c,(0,0, 536, 536),0,12)
#Game slots
while i <= 2064:
if i <= 504:
pygame.draw.rect(game_board, game_slots_c,((16+i)-114, 16, 114, 114),0,6)
elif i <= 1024:
pygame.draw.rect(game_board, game_slots_c,((16+i)-634, 146, 114, 114),0,6)
elif i <= 1544:
pygame.draw.rect(game_board, game_slots_c,((16+i)-1154, 276, 114, 114),0,6)
else:
pygame.draw.rect(game_board, game_slots_c,((16+i)-1674, 406, 114, 114),0,6)
i += 130
screen.blit(game_board,(49, 200))
#Menu, Restart, Scores
pygame.draw.rect(screen, menu_restart_background_c,(49, 25, 200, 70),0,7)
pygame.draw.rect(screen, menu_restart_background_c,(49, 110, 200, 70),0,7)
pygame.draw.rect(screen, score_background_c,(284, 25, 138, 155),0,7)
pygame.draw.rect(screen, score_background_c,(437, 25, 138, 155),0,7)
mbutton_f = font3.render("Menu", True , (255,255,255))
screen.blit(mbutton_f, (75, 32))
rbutton_f = font3.render("Restart", True , (255,255,255))
screen.blit(rbutton_f, (58, 118))
if not 'n' in locals():
random_n()
#Generating 2/4s
def random_n():
num = randint(0,100)
global tile_number, n
if num > 10:
tile_number = 2
else:
tile_number = 4
if not 'n' in locals():
n = 0
random_n()
game()
pygame.display.update()
#Loop
while True:
events = pygame.event.get()
for event in events:
#Button functions
if event.type == pygame.MOUSEBUTTONDOWN:
xy = pygame.mouse.get_pos()
if play == 0:
if 49 <= xy[0] <= 536:
if 349 <= xy[1] <= 449:
game()
play = 1
if play == 1:
if 49 <= xy[0] <= 249:
if 25 <= xy[1] <= 95:
menu()
play = 0
if 110 <= xy[1] <= 180:
game()
#Making the "X" button work
if event.type == pygame.QUIT:
pygame.quit()
exit()
#WASD
if event.type == pygame.KEYDOWN:
def keybinds():
if event.key == pygame.K_w:
print("Pressed W")
elif event.key == pygame.K_a:
print("Pressed A")
elif event.key == pygame.K_s:
print("Pressed S")
elif event.key == pygame.K_d:
print("Pressed D")
#ARROWS
elif event.key == pygame.K_UP:
print("Pressed UP")
elif event.key == pygame.K_LEFT:
print("Pressed LEFT")
elif event.key == pygame.K_DOWN:
print("Pressed DOWN")
elif event.key == pygame.K_RIGHT:
print("Pressed RIGHT")
else:
keybinds()
random_n()
keybinds()