だから、私はプレイヤーのスコアを保持しているが、私は、コードを令状とき、それは動作しませんテキストをレンダリングしようとしています。
Pythonのコード
class Player(pygame.sprite.Sprite):
def __init__(self, x, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 100))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.y = HEIGHT/2 - self.rect.height/2
self.rect.centerx = x
self.displacement = 8
self.score = 0
def update(self):
# update function
self.constrain(0, HEIGHT - self.rect.height)
def constrain(self, min, max):
# constrian player to walls
if self.rect.y > max:
self.rect.y = max
if self.rect.y < min:
self.rect.y = min
class Text():
def __init__(self, x, y, color, text, font):
self.x = x
self.y = y
self.color = color
self.text = text
self.font = font
def draw(self):
text = self.font.render(self.text, False, self.color)
text.blit(text, (self.x, self.y))
class Game(pygame.sprite.Sprite):
def __init__(self):
# get sprite object
pygame.sprite.Sprite.__init__(self)
# init pygame
pygame.init()
# init pygame modules
pygame.mixer.init()
pygame.font.init()
# set window
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
# set title
pygame.display.set_caption(TITLE)
# holds whether the game is running for not
self.running = True
# get timer function
self.clock = pygame.time.Clock()
# set fonts
self.scoreText = pygame.font.Font("assets/fonts/square.TTF", 25)
self.titleFontLight = pygame.font.Font("assets/fonts/square.TTF", 50)
# group for sprites
self.allSprites = pygame.sprite.Group()
def new(self):
# creates a new game
# make players
self.player1 = Player(20, RED)
self.player2 = Player(WIDTH - 30, BLUE)
self.ball = Ball()
self.p1Score = Text(100, 100, (RED), str(self.player1.score), self.scoreText)
self.p2Score = Text(WIDTH - 10, 10, (WHITE), str(self.player2.score), self.scoreText)
# put players inside groups
self.allSprites.add(self.player1)
self.allSprites.add(self.player2)
# run game loop
self.gameLoop()
def gameLoop(self):
# main game loop
while self.running:
self.allSprites.update()
self.update()
self.clock.tick(FPS)
def update(self):
# updates game
self.draw()
pygame.display.update()
def draw(self):
# draws to screen
# set background color
self.screen.fill(BLACK)
# draw to screen
self.ball.draw(self.screen)
self.p1Score.draw()
self.allSprites.draw(self.screen)
if __name__ == '__main__':
Game().new()
私はxで、画面に表示するテキストおよびyは、私は私のコードを入れていること座標たい
を発生する欲しいです。
は、テキストが表示されていない私は現在
を取得しています、またエラーがありません。私は、フォント、元の絶対パスに入れしようとした
- 試してみましたが何
:あなたの現在のコードで
C:/name/file/font.TTF
なぜあなたの 'ゲーム'はパイゲームのスプライトですか? :P – skrx
ここにコードを投稿する前にコードをコピーして実行できることを確認してください。 https://stackoverflow.com/help/mcve – skrx