2017-05-26 18 views
0

問題テキスト - パイソン(pygameのダウンロード)

だから、私はプレイヤーのスコアを保持しているが、私は、コードを令状とき、それは動作しませんテキストをレンダリングしようとしています。

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は、私は私のコードを入れていること座標たい

を発生する欲しいです。

は、テキストが表示されていない私は現在

を取得しています、またエラーがありません。私は、フォント、元の絶対パスに入れしようとした

  1. 試してみましたが何

    :あなたの現在のコードでC:/name/file/font.TTF

+0

なぜあなたの 'ゲーム'はパイゲームのスプライトですか? :P – skrx

+0

ここにコードを投稿する前にコードをコピーして実行できることを確認してください。 https://stackoverflow.com/help/mcve – skrx

答えて

2

を、この行が問題です: text.blit(text, (self.x, self.y))。このコードは、テキストをそれ自体に描画しています。テキストを画面に描画したい。その行を次の行に置き換えます。 screen.blit(text, (self.x, self.y))Textの描画機能には、screen引数が必要です。したがって、def draw(self):def draw(self,screen):である必要があります。最後に、self.screen引数を渡す必要があります。したがって、self.p1Score.draw()self.p1Score.draw(self.screen)である必要があります。最終的なコードはすべて変更されている必要があります:

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,screen): 
     text = self.font.render(self.text, False, self.color) 
     screen.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.screen) 
     self.allSprites.draw(self.screen) 

if __name__ == '__main__': 
    Game().new() 
関連する問題