我想添加一个函数来更新self.ScoreP变量的值
我在屏幕上有一个数字,它打印出ScoreP的当前值(updateScoreBoard())。它是准确的并且可以完美运行,但是我也在打印getScoreP。无论当前分数是多少,ScoreP都会打印0。
import pygame
class ScoreBoard():
def __init__(self):
self.WIDTH = 1024
self.HEIGHT = 576
self.WHITE = (255, 255, 255)
self.BLACK = (0,0,0)
self.minFont = "font/Minecraft.ttf"
self.scoreFont = pygame.font.Font(self.minFont, 75)
self.ScoreP = 0
self.ScorePStr = str(self.ScoreP)
self.ScoreO = 0
self.ScoreOStr = str(self.ScoreO)
self.ScorePWidth, self.ScorePHeight = self.scoreFont.size(str(self.ScoreP))
self.ScoreOWidth, self.ScoreOHeight = self.scoreFont.size(str(self.ScoreO))
self.ScorePX = (self.WIDTH/2)-self.ScorePWidth*2
self.ScorePY = 10
self.ScoreOX = self.WIDTH/2 + self.ScoreOWidth
self.ScoreOY = 10
def updateScoreBoard(self, screen):
pygame.draw.rect(screen, self.BLACK, [self.ScorePX, self.ScorePY, self.ScorePWidth, self.ScorePHeight])
scorePRender = self.scoreFont.render("{}".format(self.ScoreP), False, self.WHITE)
screen.blit(scorePRender, (self.ScorePX, self.ScorePY))
pygame.draw.rect(screen, self.BLACK, [self.ScoreOX, self.ScoreOY, self.ScoreOWidth, self.ScoreOHeight])
scoreORender = self.scoreFont.render("{}".format(self.ScoreO), False, self.WHITE)
screen.blit(scoreORender, (self.ScoreOX, self.ScoreOY))
pygame.display.flip()
def updateScore(self, playerIncrease, opponentIncrease):
self.ScoreP += playerIncrease
self.ScorePStr = self.ScoreP
self.ScoreO += opponentIncrease
self.ScoreOStr = self.ScoreO
def getScoreP(self):
return self.ScoreP
但是,getScore函数会打印出0 即使如此,游戏也能正确跟踪并重绘分数
提前谢谢
答案 0 :(得分:1)
在这里,我认为我所做的更改并不多,但是可以。下次再给我们提供您的整个代码,包括如何测试它,因为可能存在问题。我在最后添加了一个可以删除的测试。
import pygame
import random
class ScoreBoard:
def __init__(self,w,h):
pygame.font.init()
self.WIDTH = w
self.HEIGHT = h
self.screen = pygame.display.set_mode((1024,576))
self.WHITE = (255, 255, 255)
self.BLACK = (0,0,0)
self.minFont = None
self.scoreFont = pygame.font.Font(self.minFont, 75)
self.ScoreP = 0
self.ScoreO = 0
self.ScorePWidth, self.ScorePHeight = self.scoreFont.size(str(self.ScoreP))
self.ScoreOWidth, self.ScoreOHeight = self.scoreFont.size(str(self.ScoreO))
self.ScorePX = (self.WIDTH/2)-self.ScorePWidth*2
self.ScorePY = 10
self.ScoreOX = self.WIDTH/2 + self.ScoreOWidth
self.ScoreOY = 10
def updateScoreBoard(self):
self.ScorePWidth, self.ScorePHeight = self.scoreFont.size(str(int(self.ScoreP)))
self.ScoreOWidth, self.ScoreOHeight = self.scoreFont.size(str(int(self.ScoreO)))
screen = self.screen
screen.fill(self.WHITE)
pygame.draw.rect(screen, self.BLACK, [self.ScorePX, self.ScorePY, self.ScorePWidth, self.ScorePHeight])
scorePRender = self.scoreFont.render("{}".format(int(self.ScoreP)), False, self.WHITE)
screen.blit(scorePRender, (self.ScorePX, self.ScorePY))
pygame.draw.rect(screen, self.BLACK, [self.ScoreOX, self.ScoreOY, self.ScoreOWidth, self.ScoreOHeight])
scoreORender = self.scoreFont.render(str(int(self.ScoreO)), False, self.WHITE)
screen.blit(scoreORender, (self.ScoreOX, self.ScoreOY))
pygame.display.flip()
def updateScore(self, playerIncrease, opponentIncrease):
self.ScoreP += playerIncrease
self.ScoreO += opponentIncrease
def getScoreP(self):
return self.ScoreP
running = True
a = ScoreBoard(1024,576)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
a.updateScore(random.random()/10,random.random()/10)
a.updateScoreBoard()