Reputation: 67
I want to add a function that updates the value of the self.ScoreP variable
I have a number on the screen, which prints out the current value of the ScoreP (updateScoreBoard()). It is accurate and works perfectly, but I am also printing out getScoreP. ScoreP prints 0 no matter what the score currently is.
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
However, the getScore function prints out 0 Even though, the game properly keeps track of and redraws the score
Thank you in advance
Upvotes: 0
Views: 75
Reputation:
Here I don't think I changed much, but it works. Also next time give us your whole code including how you test it, because the problem may be there. I added a test at the end that you can delete.
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()
Upvotes: 1