在播放器rect与窗口宽度发生冲突之后,我现在收到错误,表明即使已经在def message_display(text)下定义了textSurface,也没有定义它: 非常感谢帮助。
这是我的源代码:
https://www.youtube.com/watch?v=dX57H9qecCU&index=5&list=PLQVvvaa0QuDdLkP8MrOXLe_rKuf6r80KO
对比这里是我的代码:
# This just imports all the Pygame modules
import pygame
import time
pygame.init()
display_width = 800
display_height = 600
# This initates your colors
black = (0,0,0)
# You have 256 color options so you only use 255 because one of those is 0
white = (255,255,255)
red = (255,0,0)
plyrImg_width = 30
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Bit Racer')
clock = pygame.time.Clock()
plyrImg = pygame.image.load('Sprite-01 double size.png')
def player(x,y):
gameDisplay.blit(plyrImg,(x,y))
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSuface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def killed():
message_display('You Died')
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
gameExit = False
while not gameExit:
# This handles any and all events in the game
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
print(event)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
# Make sure to write the background before the player
gameDisplay.fill(white)
player(x,y)
if x > display_width - plyrImg_width or x < 0:
killed()
pygame.display.update()
clock.tick(60)
# This is how you un-initiate pygame
# This is what closes the window so always have this
# If you want to end your game loop enter this
game_loop()
pygame.quit()
quit()
答案 0 :(得分:2)
尝试正确拼写。表面,而不是表面。