Pygame Hangman关于wordbank和游戏逻辑的问题

时间:2016-10-31 21:04:50

标签: python pygame

开始pygame一周,我正在尝试创建一个Hangman游戏,但我正在努力从需要从多个wordbank接收单词的部分,如

Fruit = "lemon","pineapple","kiwi","banana","watermelon" Mobile = "motorola","apple","nokia","samsung","oppo" Furniture = "table","sofa","wardrobe","chair","bed"

我做了一些研究并创建了一些功能,但我不确定是否应该以这种方式创建。

这是我的全部代码:

import pygame, sys,time , random
from pygame.locals import *

black = (0,0,0)
white = (255,255,255)
grey = (127,127,127)


display_width = 1000
display_height = 430

pygame.init()

demo = pygame.mixer.Sound("Hangman/message-Party Rock Anthem.wav")
bgm = pygame.mixer.Sound("Hangman/Sad and Scary Song.wav")

bgm.play(-1)

screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('First Try')

clock = pygame.time.Clock()

#define loads of codes regarding where the image were located
hangman_p1 = pygame.image.load("Hangman/Hangman_1.png")
hangman_p2 = pygame.image.load("Hangman/Hangman_2.png")
hangman_p3 = pygame.image.load("Hangman/Hangman_3.png")
hangman_p4 = pygame.image.load("Hangman/Hangman_4.png")
hangman_p5 = pygame.image.load("Hangman/Hangman_5.png")
hangman_p6 = pygame.image.load("Hangman/Hangman_6.png")
hangman_p7 = pygame.image.load("Hangman/Hangman_7.png")
hangman_p8 = pygame.image.load("Hangman/Hangman_8.png")
hangman_p9 = pygame.image.load("Hangman/Hangman_9.png")
hangman_p10 = pygame.image.load("Hangman/Hangman_10.png")

smile_1 = pygame.image.load("Hangman/creepy_smile_1.jpg")
smile_2 = pygame.image.load("Hangman/creepy_smile_2.jpg")
smile_3 = pygame.image.load("Hangman/creepy_smile_3.jpg")
smile_4 = pygame.image.load("Hangman/creepy_smile_4.jpg")
smile_5 = pygame.image.load("Hangman/creepy_smile_5.jpg")

Fruit = "lemon","pineapple","kiwi","banana","watermelon"
Mobile = "motorola","apple","nokia","samsung","oppo"
Furniture = "table","sofa","wardrobe","chair","bed"


charlist = "a b c d e f g h i j k l m n o p q r s v w x y z".split()

def getRandomword(wordlist):
    wordindex = random.randint(0,len(wordlist)-1)
    return wordlist[wordindex]

def getFruit():
    hintSize = pygame.font.Font("Hangman/CHILLER.TTF", 50)
    hintText, hintText_Size = text_objects('Hint: It is a kind of fruit', hintSize)
    hintText_Size.center = ((display_width/2)+80,(display_height/2)-80) 
    screen.blit(hintText, hintText_Size)

    pygame.display.flip()

    return Fruit

def getMobile():
    hintSize = pygame.font.Font("Hangman/CHILLER.TTF", 50)
    hintText, hintText_Size = text_objects('Hint: Its a brand of mobile phone', hintSize)
    hintText_Size.center = ((display_width/2)+80,(display_height/2)-80) 
    screen.blit(hintText, hintText_Size)

    pygame.display.flip()

    return Mobile

def getFurniture():
    hintSize = pygame.font.Font("Hangman/CHILLER.TTF", 50)
    hintText, hintText_Size = text_objects('Hint: Its a Furniture', hintSize)
    hintText_Size.center = ((display_width/2)+80,(display_height/2)-80) 
    screen.blit(hintText, hintText_Size)

    pygame.display.flip()

    return Furniture


def getRandom():
    hintSize = pygame.font.Font("Hangman/CHILLER.TTF", 50)
    hintText, hintText_Size = text_objects('You will get a random question', hintSize)
    hintText_Size.center = ((display_width/2)+80,(display_height/2)-80) 
    screen.blit(hintText, hintText_Size)

    pygame.display.flip()

    return Fruit + Mobile + Furniture



def creep_smile():
    screen.blit(smile_1,(10,0))
    screen.blit(smile_2,(650,0))
    screen.blit(smile_3,(500,300))
    screen.blit(smile_4,(300,-5))
    screen.blit(smile_5,(50,200))

def wordlist():

    screen.fill(black)
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            choose = pygame.key.name(event.key)


            choice = {
                "1": Fruit,
                "2": Mobile,
                "3": Furniture
                }


            return choice.get(choose,Random)().split()

    msgSize = pygame.font.Font("Hangman/CHILLER.TTF", 40)
    msgText, msgText_Size = text_objects('Select one of the category', msgSize)
    msgText_Size.center = ((display_width/2)+80,(display_height/2)-50) 
    screen.blit(msgText, msgText_Size)

    msgSize_a = pygame.font.Font("Hangman/timesbd.ttf", 22)
    msgText_1, msgText_1_Size = text_objects('1. Fruits', msgSize_a)
    msgText_1_Size.center = ((display_width/2)-20,(display_height/2)-40)
    screen.blit(msgText_1, msgText_1_Size)

    msgText_2, msgText_2_Size = text_objects('2. Moblie phones Brand', msgSize_a)
    msgText_2_Size.center = ((display_width/2)+52,(display_height/2)-15)
    screen.blit(msgText_2, msgText_2_Size)

    msgText_3, msgText_3_Size = text_objects('3. Furniture', msgSize_a)
    msgText_3_Size.center = ((display_width/2)-3,(display_height/2)+10)
    screen.blit(msgText_3, msgText_3_Size)

    msgText_4, msgText_4_Size = text_objects('4. Random', msgSize_a)
    msgText_4_Size.center = ((display_width/2)-9,(display_height/2)+30)
    screen.blit(msgText_4, msgText_4_Size)

    pygame.display.flip()

    return Next == True


def display(incorrect_letter_count,correct_letters,secret):

    penalty()

    blanks = '_' * len(secret)

    for i in range(len(secret)):
        if secret[i] in correct_letters:
            blanks = blanks[:i] + secret[i] + blanks[i+1:]

    for letter in blanks:
        textSize = pygame.font.Font("freesansbold.ttf", 30)
        secretText1, secretText1_Size = text_objects(blanks, textSize)
        secretText1_Size.center = ((display_width/2)+200,(display_height/2)+60)
        screen.blit(secretText1, secretText1_Size)

        pygame.display.flip()

def getguess(alreadyguessed):

    while True:
        for event in pygame.event.get():
            guess = pygame.key.name(event.key)
            if event.type == pygame.KEYDOWN:
                if len(guess) != 1:
                    msgSize = pygame.font.Font("Hangman/CHILLER.TTF", 20)
                    msgText, msgText_Size = text_objects('Enter single letter please', msgSize)
                    msgText_Size.center = ((display_width/2)+150,(display_height/2)-40) 
                    screen.blit(msgText, msgText_Size)

                    pygame.display.flip()

                elif guess in alreadyguessed:
                    msgSize = pygame.font.Font("Hangman/CHILLER.TTF", 20)
                    msgText, msgText_Size = text_objects('This letter already guessed', msgSize)
                    msgText_Size.center = ((display_width/2)+150,(display_height/2)-40) 
                    screen.blit(msgText, msgText_Size)

                    pygame.display.fip()

                else:
                    return guess

def intro():
    textSize = pygame.font.Font("Hangman/ITCBLKAD.TTF", 120)
    introText_1, introText_Size_1 = text_objects('Are You Ready', textSize)
    introText_Size_1.center = ((display_width/2),(display_height/2)-80) 
    screen.blit(introText_1, introText_Size_1)

    introText_2, introText_Size_2 = text_objects('For the Game?', textSize)
    introText_Size_2.center = ((display_width/2),(display_height/2)+50) 
    screen.blit(introText_2, introText_Size_2)

    pygame.display.flip()

    time.sleep(2) 

    game()

def game():

    screen.fill(black)

    display(incorrect_letter_count,correctletters,secret)
    guess = getguess(missedletters + correctletters)

    click = pygame.mouse.get_pressed()

    screen.blit(hangman_p1, (-72,0))
    screen.blit(hangman_p2, (20,75))
    screen.blit(hangman_p3, (-137,-15))

    pygame.display.flip()

    gameQuit = False
    gameOver = False
    won = False 

    while not gameQuit:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                gameQuit = True

            if event.type == pygame.KEYDOWN:

                if guess in secret:
                    correct_letters = correct_letters + guess
                    found = True

                    for i in range(len(secret)):
                        if secret[i] not in correct_letters:
                            found = False
                            break

                if found == True:

                    screen.fill(black)

                    msgSize = pygame.font.Font("Hangman/ITCBLKAD.TTF", 80)
                    msgText, msgText_Size = text_objects('Congratz!! You won!', msgSize)
                    msgText_Size.center = ((display_width/2),(display_height/2)-70) 
                    screen.blit(msgText, msgText_Size)

                    msgSize1 = pygame.font.Font("Hangman/ITCBLKAD.TTF", 50)
                    msgText1, msgText1_Size = text_objects('Play again?', msgSize1)
                    msgText1_Size.center = ((display_width/2),(display_height/2)+30) 
                    screen.blit(msgText1, msgText1_Size)

                    buttonYes("Yes",350,295,80,40,black,grey)   
                    buttonNo("No",550,295,80,40,black,grey) 

                    pygame.display.flip()

                else:
                    incorrect_letters = incorrect_letters + guess

                    if len(incorrect_letters) == 7:

                        screen.fill(black)

                        display(incorrect_letters,correct_letters,secret)
                        msgSize = pygame.font.Font("Hangman/ITCBLKAD.TTF", 80)
                        msgText, msgText_Size = text_objects('You Lose! the word was: ' + secret, msgSize)
                        msgText_Size.center = ((display_width/2),(display_height/2)-70) 
                        screen.blit(msgText, msgText_Size)

                        msgSize1 = pygame.font.Font("Hangman/ITCBLKAD.TTF", 50)
                        msgText1, msgText1_Size = text_objects('Play again?', msgSize1)
                        msgText1_Size.center = ((display_width/2),(display_height/2)+30) 
                        screen.blit(msgText1, msgText1_Size)

                        buttonYes("Yes",350,295,80,40,black,grey)   
                        buttonNo("No",550,295,80,40,black,grey) 

                        pygame.display.flip()



    pygame.quit()
    quit()  


def buttonYes(msg,x,y,w,h,ic,ac):

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    pygame.draw.rect(screen, white,(x-2,y-2,w+5,h+5))

    if x+w > mouse[0] > x and y+h > mouse[1] > y and click [0] == 1: 
        pygame.draw.rect(screen, grey,(x,y,w,h)) 
        game()
    else:
        pygame.draw.rect(screen, black,(x,y,w,h))

    smallText = pygame.font.Font("freesansbold.ttf", 20) 
    textSurf,textRect = text_objects(msg, smallText) 
    textRect.center = ((x+(w/2)),(y+(h/2))) 

    screen.blit(textSurf, textRect)

    smallText = pygame.font.Font("freesansbold.ttf", 20) 
    textSurf,textRect = text_objects(msg, smallText) 
    textRect.center = ((x+(w/2)),(y+(h/2))) 

    screen.blit(textSurf, textRect)

def buttonNo(msg,x,y,w,h,ic,ac):

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    pygame.draw.rect(screen, white,(x-2,y-2,w+5,h+5))

    if x+w > mouse[0] > x and y+h > mouse[1] > y and click [0] == 1: 
        pygame.draw.rect(screen, grey,(x,y,w,h)) 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()  
    else:
        pygame.draw.rect(screen, black,(x,y,w,h))

    smallText = pygame.font.Font("freesansbold.ttf", 20) 
    textSurf,textRect = text_objects(msg, smallText) 
    textRect.center = ((x+(w/2)),(y+(h/2))) 

    screen.blit(textSurf, textRect)

    smallText = pygame.font.Font("freesansbold.ttf", 20) 
    textSurf,textRect = text_objects(msg, smallText) 
    textRect.center = ((x+(w/2)),(y+(h/2))) 

    screen.blit(textSurf, textRect)



def text_objects(text, font):
    textSurface = font.render(text, True, white)
    return textSurface, textSurface.get_rect()


def penalty():

    incorrect_letter_count =  len(incorrect_letters)

    if incorrect_letter_count > 0:
        screen.blit(hangman_p4, (-40,-28))
    if incorrect_letter_count > 1:
        screen.blit(hangman_p5, (-58,13))
    if incorrect_letter_count > 2:
        screen.blit(hangman_p6, (-46,22))
    if incorrect_letter_count > 3:
        screen.blit(hangman_p7, (-38,5))
    if incorrect_letter_count > 4:
        screen.blit(hangman_p8, (-55,18))
    if incorrect_letter_count > 5:
        screen.blit(hangman_p9, (-46,47))
    if incorrect_letter_count > 5:
        screen.blit(hangman_p10, (-37,16))

    pygame.display.flip()

words = wordlist()
secret = getRandomword(words)
correct_letters = incorrect_letters = ""

Run = True
Next = False

while Run:

    intro()

while not Run:

    screen.fill(black)

    wordlist()

while Next:

    game()

clock.tick(42)

我也对wordindex = random.randint(0,len(wordlist)-1)感到头痛,有时会崩溃并显示TypeError: object of type 'NoneType' has no len(),另一件事是我仍然不确定如何接受正确的单词,替换空白下划线和在屏幕上显示它。请帮忙!!! :(

到目前为止,TypeError: object of type 'NoneType' has no len()问题已经解决,感谢Junuxx。我做了一些改动,弹出

return choice.get(choose,getRandom).split() AttributeError: 'function' object has no attribute 'split'

所以如果这部分不能拆分,那么有没有人可以教我如何在三个单词库中找到一个随机单词?

以下是我当前代码的链接,即enter link description here

1 个答案:

答案 0 :(得分:0)

words = wordlist()

这应该是检索单词列表。

但是wordlist()的实施实际上并不是return。在Python中,如果方法没有明确的return语句,则返回None

因此words变量设置为None,然后您执行

getRandomword(None)

导致您看到TypeError,因为您无法获取len()的{​​{1}}。

通过更改None以返回完整的单词列表,或者通过为所有单词添加全局变量来解决此问题。例如:

wordlist()