0

I've made this game using the pygame, when i run the game the screen only stay black and when I close the game i can see all the numbers while a brief moment. The game name is "The 15 puzzle".

The comments are in portuguese because I need to show to my teacher after everything is done, if you need to know something just say.

While the game is runing When I click to close the game

# importa as bibliotecas necessárias
import pygame
import random
import os
# inicializa o Pygame
pygame.init()
pygame.display.set_mode()
# define as cores usadas no jogo
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# define o tamanho da tela do jogo
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

# centraliza a janela do jogo
os.environ['SDL_VIDEO_CENTERED'] = '1'

# define o tamanho e a posição das peças do jogo
TILE_SIZE = 64
TILE_SPACING = 4
BOARD_X = (SCREEN_WIDTH - 4 * TILE_SIZE - 3 * TILE_SPACING) // 2
BOARD_Y = (SCREEN_HEIGHT - 4 * TILE_SIZE - 3 * TILE_SPACING) // 2

# define o estado inicial do jogo
board = [[15, 14, 13, 12], [11, 10, 9, 8], [7, 6, 5, 4], [3, 2, 1, None]]
selected_tile = None

# define a função que encontra a posição da peça vazia
def find_empty():
    for row in range(4):
        for col in range(4):
            if board[row][col] is None:
                return (row, col)

# define a função que gera as peças do jogo
def generate_tiles():
    global board
    global empty_pos

    # cria uma lista com os números de 1 a 15
    numbers = list(range(1, 16))

    # embaralha a lista de números aleatoriamente
    random.shuffle(numbers)

    # adiciona os números embaralhados no tabuleiro
    for row in range(4):
        for col in range(4):
            if row == 3 and col == 3:
                board[row][col] = None
                empty_pos = (row, col)
            else:
                number = None
                # verifica se ainda há números na lista
                while not number:
                    if numbers:
                        number = numbers.pop()
                    else:
                        # se a lista estiver vazia, gera uma nova lista de números aleatórios
                        numbers = list(range(1, 16))
                        random.shuffle(numbers)

                board[row][col] = number

# gera as peças do jogo
generate_tiles()

# cria a janela do jogo
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Jogo dos 15")

# define a fonte usada no jogo
font = pygame.font.SysFont(None, 48)

# loop principal do jogo
running = True
while running:
    # processa os eventos do jogo
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # obtém a posição do clique do mouse
            mouse_x, mouse_y = pygame.mouse.get_pos()
            # verifica se uma peça foi clicada
            for row in range(4):
                for col in range(4):
                    tile_x = BOARD_X + col * (TILE_SIZE + TILE_SPACING)
                    tile_y = BOARD_Y + row * (TILE_SIZE + TILE_SPACING)
                    tile_rect = pygame.Rect(tile_x, tile_y, TILE_SIZE, TILE_SIZE)
                    if tile_rect.collidepoint(mouse_x, mouse_y):
                        selected_tile = (row, col)
                        break
        elif event.type == pygame.MOUSEBUTTONUP:
            # verifica se a peça selecionada pode ser movida
            if selected_tile is not None:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                selected_row, selected_col = selected_tile
                empty_row, empty_col = find_empty()
                if selected_row == empty_row and abs(selected_col - empty_col) == 1:
                    board[selected_row][empty_col] = board[selected_row][selected_col]
                    board[selected_row][selected_col] = None
                elif selected_col == empty_col and abs(selected_row - empty_row) == 1:
                    board[empty_row][selected_col] = board[selected_row][selected_col]
                    board[selected_row][selected_col] = None
        # redefine a peça selecionada como None
        selected_tile = None
# limpa a tela do jogo
screen.fill(WHITE)
# desenha as peças do jogo
for row in range(4):
    for col in range(4):
        tile_x = BOARD_X + col * (TILE_SIZE + TILE_SPACING)
        tile_y = BOARD_Y + row * (TILE_SIZE + TILE_SPACING)
        if board[row][col] is not None:
            tile_text = font.render(str(board[row][col]), True, BLACK)
            tile_rect = tile_text.get_rect(center=(tile_x + TILE_SIZE // 2, tile_y + TILE_SIZE // 2))
            pygame.draw.rect(screen, WHITE, (tile_x, tile_y, TILE_SIZE, TILE_SIZE))
            screen.blit(tile_text, tile_rect)
# atualiza a tela do jogo
    pygame.display.flip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    It seems like an indentation problem. The bottom part, where you draw, should be inside the `while running` loop. – Emanuel P Feb 25 '23 at 18:43
  • lol, it was an indentation problem, thank you. Now i need to know why the tiles aren't moving when i click xD – BernardoGN Feb 25 '23 at 18:54
  • `find_empty` just searches for the first empty tile in the grid. Thus, a tile is moved only if it can be moved to the first empty tile in the grid. You have to test if a tile beside the selected tile is empty. – Rabbid76 Feb 25 '23 at 20:05
  • i got it at the moment, and i forget to say here, now is all right :D – BernardoGN Feb 25 '23 at 21:15

0 Answers0