Pygame Hangman游戏逻辑问题

时间:2016-11-02 12:59:56

标签: python pygame

我的游戏有问题,

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"

Dict = Fruit + Mobile + Furniture

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, gameList == False, gameQuit == False

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, gameList == False, gameQuit == False

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, gameList == False, gameQuit == False


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 random.choice(Dict), gameList == False, gameQuit == False



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": getFruit(),
                "2": getMobile(),
                "3": getFurniture()
                }


            return choice.get(choose,getRandom())

    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) 

    wordlist()

    return gameList == True

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 = ""

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()  

现在,当我运行我的代码时,我选择任何一个选择,它会弹出所有三个选项注释并跳出游戏。知道我做错了吗?

0 个答案:

没有答案