在教室PC上使用Python 2.7.3和Pygame,我創建了一個電影引用猜測遊戲,使用命令提示符窗口與用戶交互)和圖形窗口(以顯示靜態.png文件,例如來自電影的照片)。遊戲成功運行。Python 3.3 pygame 1.9.2a0(64位)圖形窗口顯示第一個圖像,但然後凍結
現在我想在我自己的Windows 7 64位PC上運行和增強遊戲。我下載了Python版本3.3.5和pygame-1.9.2a0.win-amd64-py3.3.exe。然後,我對遊戲代碼做了兩處更改,以便從Python 2.7.3調整到Python 3.3.5環境:(1)從「raw_input()」命令中刪除「raw_」;和(2)刪除第一行,該教師告訴我們要使用,以便Python 2.6能夠像更高版本一樣操作:「從將來導入分區,absolute_import,print_function,unicode_literals」。
現在,在我的電腦上,命令提示符窗口和音頻都正常工作。 pygame圖形窗口只顯示第一個.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()
工作。謝謝! – user3458167