我正在使用 pygame 制作一个刽子手游戏。一切都已完成,只是我的图像无法正常循环。
图像与 main.py 位于同一目录中,我检查了名称以确保它们多次匹配(确实如此)。我一直无法找到解决问题的方法,所以我希望得到一些帮助?希望我在这里遗漏了一些明显的东西。
图片加载代码: `
# Load images.
images = []
for i in range(7):
image_path = os.path.join(os.path.dirname(__file__), f"Hangman{i}.png")
image = pygame.image.load("Hangman" + str(i) + ".png") # Adjusted the filenames
images.append(image)
完整代码:
import sys
import os
import math
import random
import pygame
from pygame.locals import QUIT
# Sets up display.
pygame.init()
display = pygame.display.set_mode((1600, 1000))
pygame.display.set_caption("Vancouver Canucks Hangman")
# Fonts.
letterfont = pygame.font.SysFont("arial", 32)
wordfont = pygame.font.SysFont("arial", 36)
# Game variables.
status = 0
guessed = []
words = [
"ELIAS PETTERSSON", "J.T MILLER", "QUINN HUGHES", "BROCK BOESER",
"FILIP HRONEK", "THATCHER DEMKO", "CASEY DESMITH", "SAM LAFFERTY",
"CONOR GARLAND", "DAKOTA JOSHUA", "ANDREI KUZMENKO", "IAN COLE", "TYLER MYERS",
"NIKITA ZADOROV", "ROBERTO LUONGO", "HENRIK SEDIN", "DANIEL SEDIN", "ALEX BURROWS",
"MARKUS NASLUND", "KIRK MCLEAN", "TREVOR LINDEN", "PAVEL BURE",
"GINO ODJICK", "TODD BERTUZZI", "STAN SMYL", "ORLAND KURTENBACH",
"THOMAS GRADIN", "BO HORVAT", "RYAN KESSLER"
]
word = random.choice(words).upper() # Ensure the word is in uppercase.
status = 0
# Button variables.
radius = 20
gap = 15
letters = []
startx = round((1600 - (radius * 2 + gap) * 13) / 2)
starty = 650
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]) # Puts letters on buttons.
# Colors
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)
black = (0, 0, 0)
yellow = (255, 255, 0)
# Load images.
images = []
for i in range(7):
image_path = os.path.join(os.path.dirname(__file__), f"Hangman{i}.png")
image = pygame.image.load("Hangman" + str(i) + ".png") # Adjusted the filenames
images.append(image)
# Drawing function.
def draw():
display.fill(white)
display.blit(images[status], (150, 100))
# Draw word with spaces between letters.
spaced_word = ""
for letter in word:
if letter in guessed:
spaced_word += letter + " "
elif letter == " ":
spaced_word += " "
else:
spaced_word += "_ "
text = wordfont.render(spaced_word, 1, black)
display.blit(text, (470, 500))
# Draw buttons.
for letter in letters:
x, y, ltr, visible = letter
if visible:
pygame.draw.circle(display, blue, (x, y), radius, 3) # Line of code that actually draws the buttons.
text = letterfont.render(ltr, 1, blue) # Button visuals like colour.
display.blit(text,
(x - text.get_width() / 2, y - text.get_height() / 2))
pygame.display.update() # Updates display.
# Set up game loop.
incorrectguess = 0
FPS = 60
clock = pygame.time.Clock()
# Main game loop
run = True
def display_message(message, HEIGHT): # Function to display message.
global incorrectguess
pygame.time.delay(1000)
display.fill(white)
text = wordfont.render(message, 1, black)
display.blit(text, (display.get_width()/2 - text.get_width()/2, HEIGHT/2 - text.get_height()/2))
pygame.display.update()
pygame.time.delay(3000)
# Main gameplay loop.
while run:
for event in pygame.event.get():
if event.type == QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
m_x, m_y = pygame.mouse.get_pos()
for letter in letters: # Loop that makes sure the letter buttons work.
x, y, ltr, visible = letter
if visible:
dis = math.sqrt((x - m_x) ** 2 + (y - m_y) ** 2)
if dis < radius:
letter[3] = False # Set button visibility to False when clicked
guessed.append(ltr)
if ltr not in word: # Keeps track of wrong guesses.
incorrectguess += 1
draw()
pygame.display.update()
if incorrectguess >= 6:
status = 6 # Sets the right image.
draw() # Draws the final image.
pygame.display.update() # Updates final image.
display_message("YOU LOSE!", 1000) # Displays lose message.
run = False # Ends game.
# Check win condition
won = all(letter in guessed or letter == ' ' for letter in word)
if won:
# Display win screen
display.fill(blue)
text = wordfont.render("YOU WON!", 1, white)
display.blit(text, (display.get_width() // 2 - text.get_width() // 2,
display.get_height() // 2 - text.get_height() // 2))
pygame.display.update()
# Delay the closing of the window after winning
pygame.time.delay(3000)
run = False
clock.tick(FPS)
pygame.quit()
sys.exit()
您正在创建一个
image_path
变量,但未在代码中使用它。尝试将您的代码更改为:
images = []
for i in range(7):
image_path = os.path.join(os.path.dirname(__file__), f"Hangman{i}.png")
image = pygame.image.load(image_path)
images.append(image)