在python中从文件中随机选择单词



在我的代码中,我有一个文本文件,我必须从中随机读取单词来玩hangman,但它不能正常工作,因为它会使我的游戏崩溃,因为有时它会生成一个空单词,而不是选择一个,所以当我运行它时,有时它只会说你是赢家并完成游戏。如果你能告诉我我做错了什么,那就太好了。

我的代码:

import random
import pygame
import math
pygame.init()
WIDTH , HIGHT =800,500
win=pygame.display.set_mode((WIDTH,HIGHT))
pygame.display.set_caption("hangman")
#button variables
RADIUS =20
GAP=15
letters=[]
startx=round((WIDTH-(RADIUS*2+GAP)*13)/2)
starty=400
A=97
for i in range(26):
x=startx + GAP* 2+ ((RADIUS*2+GAP) * (i % 13))
y=starty +((i // 13) * (RADIUS*2 + GAP))
letters.append([x,y,chr(A+i),True])

#Fonts
LETTER_FONT=pygame.font.SysFont('comicsans',40)
WORD_FONT=pygame.font.SysFont('comicsans',50)
TITLE_FONT=pygame.font.SysFont('comicsans',60)
#loading images
image={0:pygame.image.load(r'C:UserskimiaPycharmProjectspythonProject1imageshangman0.png'),1:pygame.image.load(r'C:UserskimiaPycharmProjectspythonProject1imageshangman1.png'),
2:pygame.image.load(r'C:UserskimiaPycharmProjectspythonProject1imageshangman2.png'),3:pygame.image.load(r'C:UserskimiaPycharmProjectspythonProject1imageshangman3.png'),
4:pygame.image.load(r'C:UserskimiaPycharmProjectspythonProject1imageshangman4.png'),5:pygame.image.load(r'C:UserskimiaPycharmProjectspythonProject1imageshangman5.png'),
6:pygame.image.load(r'C:UserskimiaPycharmProjectspythonProject1imageshangman6.png')}
hangman_status=0
for i in image:
if i ==hangman_status:
images=image.get(i)

#game variables
WHITE=(255,255,255)
BLACK=(0,0,0)
file=open('example.txt')
contents = file.read().splitlines()
word=random.choice(contents)
guessed =[]

#set up of loop
FPS=60
clock=pygame.time.Clock()
run=True

#draw
def draw():
win.fill(WHITE)
text=TITLE_FONT.render("Let's hang the man bud!",1,BLACK)
win.blit(text, (WIDTH/2 - text.get_width()/2, 20))
#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))

#buttions
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,(100,100))
pygame.display.update()
#making a def since it is drier
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, HIGHT / 2 - text.get_height() / 2))
pygame.display.update()
pygame.time.delay(3000)

def hangman():
global hangman_status
global images
# set up of loop
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
if hangman_status in image:
images = image.get(hangman_status)
draw()
won=True
for letter in word:
if letter not in guessed:
won=False
break
if won:
display_message("YOUUU WON!")
break
if hangman_status==6:
display_message("YOU ARE A LOSER!")
break



hangman()
pygame.quit()

我找到了答案,我只需要用这种方式更改我的代码来消除那些空代码:

''

file=open('example.txt')
contents = file.read().splitlines()
contents = [i for i in contents if i != '' and i != ', ']
word=random.choice(contents)

''

最新更新