在教室PC上使用Python 2.7.3和Pygame,我使用命令提示符窗口(与用户交互)和图形窗口(显示仍然.png文件,如电影中的照片)。游戏顺利进行。
现在我想在自己的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)删除了第1行,教师告诉我们使用这一行,以便Python 2.6的行为类似于更高版本:“来自 future import division,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()
答案 0 :(得分:3)
PyGame是一个事件驱动的系统。如果你没有使用它的内部事件循环来驱动游戏,你仍然需要花一些时间来处理内部事件,比如移动窗口或调整窗口大小。有一个专门用于此的功能:pygame.event.pump
。
我认为如果你在几个地方的代码中调用该函数(可能就在收集控制台上的输入之前或之后),你可以让你的屏幕响应。