I am making a word game in python with pygame, and I am struggling with generating my board. for the game, I need the board to be 8*6, filled with letters. Additionally, when a letter is clicked, I’d like it to be highlighted gray. The following is the progress I’ve made towards doing that:
Here is my current code:
import random
import string
import pygame
# defining colors
black = (0, 0, 0)
white = (255, 255, 255)
gray = (128, 128, 128)
# screen width, height, etc
screen_width = 600
screen_height = 800
board_cols = 6
board_rows = 8
font_size = 32
def generate_board():
all_letters = string.ascii_uppercase
board = []
for i in range(board_rows):
row = random.sample(all_letters, board_cols)
board.append(row)
return board
# printing the gameboard (code doesn't display anything otherwise)
def print_board(board):
for row in board:
print(" ".join(row))
board = generate_board()
print_board(board)
# function to format cells and stylize board
def draw_board(screen, board, selected_letter=None):
pygame.init()
cell_width = screen_width // board_cols
cell_height = screen_height // board_rows
font = pygame.font.SysFont('None', font_size)
# drawing letters in the correct spaces
for row_index, row in enumerate(board):
for col_index, letter in enumerate(row):
x = col_index * cell_width
y = row_index * cell_height
#drawing letters
text_surface = font.render(letter, True, white)
screen.blit(text_surface, (x, y))
#drawing outline around letter if selected
if letter == selected_letter:
pygame.draw.rect(screen, gray, (x, y, cell_width, cell_height))
def get_clicked_letter(board, position): #position = position of click
cell_width = screen_width // board_cols
cell_height = screen_height // board_rows
# if click is within board:
if 0 <= position[0] < screen_width and 0 < position[1] < screen_height:
# find in what index the click occurred
row_index = position[1] // cell_height
col_index = position[0] // cell_width
# if click is within a certain cell:
if 0 <= row_index < board_rows and 0 <= col_index < board_cols:
# find index where click is
return board[row_index][col_index]
return None
# main game board creation and instituting click function
def main_board():
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Strands")
game_board = generate_board()
selected_letter = None
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# find position of clicked letter:
clicked_letter = get_clicked_letter(game_board, pygame.mouse.get_position())
if clicked_letter:
selected_letter = clicked_letter
screen.fill(black)
draw_board(screen, game_board, selected_letter)
pygame.display.flip()
pygame.init()
main_board()
I expected the code to output a screen/pop-up with a grid of randomized letters, and for these letters to, when clicked, turn gray.
However, all I got was a matrix of letters within the terminal.
I’ve tried initializing pygame in different places, as well as playing around with my order of code, but I am at an impasse. This is my first time using pygame, so sorry if I’m missing something obvious. Thanks for your help!
baby carrots is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.