Images not showing up when pygame program is run

Pygame is installed correctly and path is correct but I can’t get the images to appear.

import pygame
import math
import random

#setup display
pygame.init()
WIDTH, HEIGHT = 1000, 600
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Helen's Hangman Game!") #display name change

#button variables
RADIUS = 20
GAP = 15
letters = []
startx = round((WIDTH - (RADIUS ^ 2 + GAP) * 13) / 2)
starty = 400
A = 65
for i in range(26):
    x = startx + GAP * 2 + ((RADIUS * 2 + GAP) * (i % 13))
    y = starty + ((i // 13) * (GAP + RADIUS * 2))
    letters.append([x, y, chr(A + i), True])

#fonts
LETTER_FONT = pygame.font.SysFont('comicsans', 30)
WORD_FONT = pygame.font.SysFont('comicsans', 40)
TITLE_FONT = pygame.font.SysFont('comicsans', 60)
CATEGORY_FONT = pygame.font.SysFont('comicsans', 30)

#load images
images = []
for i in range(7):
    image = pygame.image.load("images/hangman" + str(i) + ".png")
    images.append(image)

#game variables
hangman_status = 0
categories = {
    'Transportation': ["automobile", "truck", "boat", "bicycle", "airplane", "train", "motorcycle"],
    'Body Parts': ["head", "mouth", "eyes", "fingers", "knees", "chest", "stomach"],
    'Zoo Animals': ["gorillas", "orangutans", "elephants", "flamingos", "bears", "snakes", "lions", "tigers"],
    'Ocean Words': ["fish", "octopus", "beach", "pier", "seagull", "shark", "anemone", "starfish", "waves", "riptide"]
    } #categories need to be all caps , have quotation marks around each word, and a comma between each word
#brackets need to be placed around each category in order for the function to work
category = random.choice(list(categories.keys()))
word = random.choice(categories[category]).upper() #choose a word from the selected category and convert to uppercase
guessed = []

#colors
WHITE = (255,255,255)
BLACK = (0,0,0)

def draw():
    win.fill(WHITE)

    #draw title
    text = TITLE_FONT.render("HELEN'S HANGMAN", 1, BLACK) #changes to the title
    win.blit(text, (WIDTH//2 - text.get_width()//2, 20))

    #draw category
    category_text = CATEGORY_FONT.render("Category: " + category, 1, BLACK) #changes to the category
    win.blit(category_text, (WIDTH//2 - category_text.get_width()//2, 100))

    #draw word
    display_word = ""
    for letter in word:
        if letter in guessed:
            display_word += letter + ""
        else:
            display_word += "_ "
    text = WORD_FONT.render(display_word, 1, BLACK)
    win.blit(text, (400, 200))

    #draw buttons
    for letter in letters:
        x, y, ltr, visible = letter
        if visible:
            pygame.draw.circle(win, BLACK, (x, y), RADIUS, 3)
            text = LETTER_FONT.render(ltr, 1, BLACK)
            win.blit(text, (x - text.get_width() / 2, y - text.get_height() / 2))

    win.blit(images[hangman_status], (150, 100))
    pygame.display.update()

    def display_message(message):
        pygame.time.delay(1000)
        win.fill(WHITE)
        text = WORD_FONT.render(message, 1, BLACK)
        win.blit(text, (WIDTH / 2 - text.get_width() / 2, HEIGHT / 2 - text.get_height() / 2))
        pygame.display.update()
        pygame.time.delay(3000)

    def main():
        global hangman_status
        global category
        global word
        global guesses
        global letters

        FPS = 60
        clock = pygame.time.Clock()
        run = True

        while run:
            clock.tick(FPS)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                if event.type == pygame.MOUSEBUTTONDOWN:
                    m_x, m_y = pygame.mouse.get_pos()
                    for letter in letters:
                        x, y, ltr, visible = letter
                        if visible:
                            dis = math.sqrt((x - m_x) ** 2 + (y - m_y) ** 2)
                            if dis < RADIUS:
                                letter[3] = False
                                guessed.append(ltr)
                                if ltr not in word:
                                    hangman_status += 1


            draw()

            won = True
            for letter in word:
                if letter not in guessed:
                    won = False
                    break

                if won:
                    display_message("Congratulations! You won!")
                    category = random.choice(list(categories.keys()))
                    word = random.choice(categories[category]).upper()
                    guessed.clear()
                    hangman_status = 0
                    letters =[] # reset the buttons and letters after winning
                    startx = round((WIDTH - (RADIUS * 2 + GAP) * 13) / 2)
                    starty = 400
                    A = 65
                    for i in range(26):
                        x = startx + GAP * 2 + ((RADIUS * 2 + GAP) * (i % 13))
                        y = starty + ((i//13) * (GAP + RADIUS * 2))
                        letters.append([x, y, chr(A + i), True])

                if hangman_status == 6:
                    display_message("Sorry! You lost!")
                    category = random.choice(list(categories.keys()))
                    word = random.choice(categories[category]).upper()
                    guessed.clear()
                    hangman_status = 0
                    letters = [] # reset the buttons and letters after losing
                    startx = round((WIDTH - (RADIUS * 2 + GAP) * 13) / 2)
                    starty = 400
                    A = 65
                    for i in range(26):
                        x = startx + GAP * 2 + ((RADIUS * 2 + GAP) * (i % 13))
                        y = starty + ((i//13) * (GAP + RADIUS * 2))
                        letters.append([x, y, chr(A + i), True])

        pygame.quit()
1 Like

not sure if this helps but if you divide everything up into related functions then call those functions in a main game loop like

main(
setup()
while( 1 )
(
input()
logic()
update()
draw()
)
)
you might find your problem
sorry that wasn’t better advice but i don’t understand python at all
good luck:)

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Looking at your code pretty closely, I’m going to assume that your images are in the “images” folder with names like “hangman0.png”.

I’d maybe try ensuring draw doesn’t contain the main. Python is very picky about indentation.

Happy coding. :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.