python3.3pygame1.9.2a0(64位)图形窗口显示第一个图像,但随后冻结

2024-10-01 11:34:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在教室电脑上使用Python2.7.3和Pygame,创建了一个电影报价猜谜游戏,使用命令提示窗口(与用户交互)和图形窗口(显示静态.png文件,如电影中的照片)。比赛进行得很成功。在

现在我想在我自己的Windows7 64位PC上运行和增强游戏。我下载了Python版本3.3.5和pygame-1.9.2a0.win-amd64-py3.3.exe。然后我对我的游戏代码做了两个修改,从Python2.7.3调整到Python3.3.5环境:(1)从“raw_input()”命令中删除了“raw_200;”;以及(2)删除了第1行,指导老师告诉我们使用该行,以便Python2.6的操作与以后的版本一样:“fromfuture导入除法,绝对导入,打印函数,unicode_文字”。在

现在,在我的电脑上,命令提示符窗口和音频都能正常工作。pygame图形窗口只显示first.png图像。窗口顶部(在pygame徽标旁边)立即显示“(无响应)”。没有错误消息。谢谢你的帮助。在

代码如下:

# Import common modules
import pygame, pygame.mixer, os
from pygame.locals import *

# Initialize pygame, window and sound mixer
pygame.init()
screen = pygame.display.set_mode((600,450))
pygame.display.set_caption('Greatest Movie Lines')
pygame.mouse.set_visible(0)
pygame.mixer.init()

# Create and display background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
screen.blit(background, (0,0)) 

# Initialize variables that will persist through entire game
gameRunning = True  
roundsCompleted = 0
totalRoundsAvailable = 5   
scoreSoFar = 0
quitOrContinue = 'Try another movie line?  Type y or n: '

def beginGame():
    titleDisplay = pygame.image.load('titleSlide.png')
    titleDisplay = pygame.transform.scale(titleDisplay, (600, 450))
    screen.blit(titleDisplay, (0,0)) 
    pygame.display.flip()
    sound = pygame.mixer.music.load('20fox-fanfare-w-cinemascope-ext_anewman.mp3')
    pygame.mixer.music.play()
    print('First, move the photo window rightwards and make this black window')
    print('smaller so that you can see both windows completely (no overlap).')
    print(     )
    doneFixingWindow = input('When done repositioning windows, hit enter here.')

    howToPlay = pygame.image.load('howToPlay.png')
    howToPlay = pygame.transform.scale(howToPlay, (600, 450))
    screen.blit(howToPlay, (0,0)) 
    pygame.display.flip()

    print(     )
    print('Read the instructions at right.')
    doneFixingWindow = input('Then hit enter to play!')
    print(     )

def endGame():
    endDisplay = pygame.image.load('ending.png')
    endDisplay = pygame.transform.scale(endDisplay, (600, 450))
    screen.blit(endDisplay, (0,0)) 
    pygame.display.flip()
    sound = pygame.mixer.music.load('warnerbros_fanfare.mp3')
    pygame.mixer.music.play()
    print('    ')
    print('Game over.  Thank you for playing.')
    raw_input('Hit enter to exit the game.')


def playRound(cumScoreLastRound,roundsDone):
    # Initialize variables and constants used in the game rounds
    hintUsed = False
    guessOrHint = 'Would you like to (g)uess or get a(h)int first? Type g or h:  ' 
    requestGuess = 'Guess the movie line (no commas):  '
    noKeywordsMatched = "Sorry, your guess didn't match any keywords."
    oneKeywordMatched = 'Not bad.  You got one keyword right:'
    twoKeywordsMatched = 'Pretty good!  You got two keywords right:'
    threeKeywordsMatched = 'Great! You got all three keywords:'

    # Load variables specific to this round
    fo = open("quoteData.csv","r")
    movieData = fo.readlines()
    line = movieData[roundsDone + 1]
    movie = line.split(",")
    droodle = pygame.image.load(movie[3])
    droodle = pygame.transform.scale(droodle, (600, 450))
    hint = movie[4]
    keyword1 = movie[5]
    keyword2 = movie[6]
    keyword3 = movie[7]
    answer = pygame.image.load (movie[8])
    answer = pygame.transform.scale(answer, (600, 450))

    # Initialize counters specific to this round
    keywordMatches = 0                                                  
    keyword1Yes = ' '
    keyword2Yes = ' '
    keyword3Yes = ' '   

    # Display this round's droodle 
    screen.blit(droodle, (0, 0))
    pygame.display.flip()
    print()
    print('Here is the droodle portraying a famous movie line.')

    # Give user option of hint before guessing
    playerChoice = input(guessOrHint)

    while playerChoice != 'g' and playerChoice != 'h':   # Ensure valid selection
            print('    ')
        print('Not a valid selection')
        playerChoice = input(guessOrHint)

    if playerChoice == 'h':     # Display hint if player chooses to see one
        print('    ')
        print('Hint: ',hint)
        hintUsed = True

    # Solicit and evaluate the player's guess   
    print(   )
    guess = str.lower(input(requestGuess))              

    guessParsed = guess.split() # Determine which keywords match, if any
        if word == keyword1:
            keyword1Yes = keyword1
            keywordMatches = keywordMatches + 1
        if word == keyword2:
            keyword2Yes = keyword2
            keywordMatches = keywordMatches + 1
        if word == keyword3:
            keyword3Yes = keyword3
            keywordMatches = keywordMatches + 1

    # Display and play the correct answer
    screen.blit(answer, (0, 0))
    pygame.display.flip()
    if roundsDone == 0:
        sound = pygame.mixer.Sound('casab.wav') 
        sound.play()
    elif roundsDone == 1:
        sound = pygame.mixer.Sound('oz6.wav')   
        sound.play()
    elif roundsDone == 2:
        sound = pygame.mixer.music.load('WaterfrontClass.mp3')
        pygame.mixer.music.play()       
    elif roundsDone == 3:
        sound = pygame.mixer.Sound('offer.wav') 
        sound.play()
    else:
        sound = pygame.mixer.Sound('gwtw.wav')      
        sound.play()

    # Calculate score for this round and new total score
    if keywordMatches == 0:
        scoreThisRound = 0
    if keywordMatches == 1:
        scoreThisRound = 25
    if keywordMatches == 2:
        scoreThisRound = 50
    if keywordMatches == 3:
        scoreThisRound = 100
    if hintUsed == True:
        scoreThisRound = scoreThisRound - 20
    newCumScore = cumScoreLastRound + scoreThisRound


    # Display player's result, score for round, and cumulative score
    print('    ')
    if keywordMatches == 0:
        print(noKeywordsMatched, keyword1Yes, keyword2Yes, keyword3Yes)
    if keywordMatches == 1:
        print(oneKeywordMatched, keyword1Yes, keyword2Yes, keyword3Yes)
    if keywordMatches == 2:
        print(twoKeywordsMatched, keyword1Yes, keyword2Yes, keyword3Yes)
    if keywordMatches == 3:
        print(threeKeywordsMatched, keyword1Yes, keyword2Yes, keyword3Yes)
    print('Your score for this round is ', scoreThisRound)
    print( 'Your new total score is ', newCumScore)

    return newCumScore

while gameRunning:      

    # To begin game, display title page and instructions
    if roundsCompleted == 0:
        beginGame()

    # Play the round 
    scoreSoFar = playRound(scoreSoFar,roundsCompleted)

    # Check to see if any rounds left to be played
    roundsCompleted = roundsCompleted + 1
    if roundsCompleted == totalRoundsAvailable: 
       # End game if no rounds left to play
        print()
        input('That was our last quote.  Hit enter to exit the game.')
        endGame()
        gameRunning = False

    # Ask player whether to continue 
    else:
        print('    ')
        playerContinue = input(quitOrContinue)

        while playerContinue != 'y' and playerContinue != 'n':  # Ensure valid selection
        print('    ')
        print('Not a valid selection')
        playerContinue = input(quitOrContinue)

        if playerContinue == 'n':    # End game if player wants to quit
            endGame()
            gameRunning = False

pygame.quit()

Tags: andthetoinputplayifdisplayload
1条回答
网友
1楼 · 发布于 2024-10-01 11:34:10

PyGame是一个事件驱动的系统。如果你不使用它的内部事件循环来驱动你的游戏,你仍然需要让它偶尔花一些时间来处理内部事件,比如移动或调整窗口的大小。有一个专门用于此的函数:^{}。在

我认为,如果在代码中的几个位置(也许就在控制台上收集输入之前或之后)对该函数的调用,您可以使屏幕响应。在

相关问题 更多 >