我正在使用「更多Python編程初學者」一書,並從中複製代碼。嘗試在OOP類中執行某個函數時發生錯誤
我想創建一個函數,在屏幕上用print_text()
的屏幕創建文本,並帶有一些渲染文本所需的參數。
下面是函數:
def print_text(font, x, y, text, colour=(255, 255, 255), shadow=True):
if shadow:
txtimg = font.render(text, True, (0,0,0))
surface.blit(txtimg, (x-2,y-2))
txtimg = font.render(text, True, colour)
surface.blit(txtimg, (x, y))
這裏是整個程序:
# Trivia Game
#
# An editable quiz. Accumulate ALL the points.
# This has 11 questions so far.
#
# -------------------------------------------------
import pygame, sys
from pygame.locals import *
class Trivia(object):
def __init__(self, filename):
self.data = []
self.current = 0
self.total = 0
self.correct = 0
self.score = 0
self.scored = False
self.failed = False
self.wronganswer = 0
self.colour = [white, white, white, white]
txt = open("trivia.txt", "r")
tdata = txt.readlines()
txt.close()
for text_line in tdata:
self.data.append(text_line.strip())
self.total += 1
def print_text(font, x, y, text, colour=(255, 255, 255), shadow=True):
if shadow:
txtimg = font.render(text, True, (0,0,0))
surface.blit(txtimg, (x-2,y-2))
txtimg = font.render(text, True, colour)
surface.blit(txtimg, (x, y))
def show_question(self):
trivia.print_text(font1, 210, 5, "TRIVIA GAME")
print_text(font2, 192, 500-20, "press keys 1,2,3,4 to answer", blue)
print_text(font2, 530, 5, "SCORE:", blue)
print_text(font2, 550, 25, str(self.score), blue)
self.correct = int(self.data[self.current+5])
question = self.current // 6 + 1
print_text(font1, 5, 80, "QUESTION " + str(question))
print_text(font2, 20, 120, self.data[self.current], white)
if self.scored:
self.colour = [white, white, white, white]
self.colour[self.correct-1] = green
print_text(font1, 230, 380, "CORRECT!", green)
print_text(font2, 170, 420, "Press Enter For Next Question", green)
elif self.failed:
self.colour = [white, white, white, white]
self.colour[self.wronganswer-1] = red
self.colour[self.correct-1] = green
print_text(font1, 230, 380, "INCORRECT!", red)
print_text(font2, 170, 420, "Press Enter For Next Question", red)
print_text(font1, 5, 170, "ANSWERS")
print_text(font2, 20, 210, "1 - " + self.data[self.current+1], self.colour[0])
print_text(font2, 20, 240, "2 - " + self.data[self.current+2], self.colour[1])
print_text(font2, 20, 270, "3 - " + self.data[self.current+3], self.colour[2])
print_text(font2, 20, 300, "4 - " + self.data[self.current+4], self.colour[3])
def handle_input(self,number):
if not self.scored and not self.failed:
if number == self.correct:
self.scored = True
self.score += 1
else:
self.failed = True
self.wronganswer = number
def next_question(self):
if self.scored or self.failed:
self.scored = False
self.failed = False
self.correct = 0
self.colour = [white, white, white, white]
self.current += 6
if self.current >= self.total:
self.current = 0
# main program
pygame.init()
surface = pygame.display.set_mode((600, 500))
pygame.display.set_caption("School")
font1 = pygame.font.Font(None, 40)
font2 = pygame.font.Font(None, 24)
white = 255, 255, 255
blue = 0, 0, 255
green = 0, 255, 0
red = 255, 0, 0
black = 0, 0, 0
trivia = Trivia("trivia_data.txt")
# loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == KEYUP:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_1:
trivia.handle_input(1)
elif event.key == pygame.K_2:
trivia.handle_input(2)
elif event.key == pygame.K_3:
trivia.handle_input(3)
elif event.key == pygame.K_4:
trivia.handle_input(4)
elif event.key == pygame.K_RETURN:
trivia.next_question()
surface.fill((black))
trivia.show_question()
pygame.display.update()
我得到運行該程序時的錯誤是:
Traceback (most recent call last):
File "C:\Users\PylonBuffering\Desktop\Pylon Buffering\Games\Other\Trivia Game\Trivia Game.pyw", line 126, in <module>
trivia.show_question()
File "C:\Users\PylonBuffering\Desktop\Pylon Buffering\Games\Other\Trivia Game\Trivia Game.pyw", line 39, in show_question
trivia.print_text(font1, 210, 5, "TRIVIA GAME")
File "C:\Users\PylonBuffering\Desktop\Pylon Buffering\Games\Other\Trivia Game\Trivia Game.pyw", line 33, in print_text
txtimg = font.render(text, True, (0,0,0))
AttributeError: 'Trivia' object has no attribute 'render'
幫助我。如果你發現這個話題已經解決了,請把它和我聯繫起來。
print_text蟒蛇應該有第一個參數的自我。當您將方法作爲對象的屬性調用時,它會自動添加到參數列表中:trivia.print_text(...) - > Trivia.print_text(trivia,...) – mdurant 2014-09-04 18:56:52