2011-11-28 135 views
0

我已經嘗試使用blit功能將其粘貼到表面,但沒有奏效,因爲我無法使用表面作爲分數的來源,就像我對玩家,敵人和食物變量所做的那樣。我也嘗試過使用self.score = Score((75,575)),但這並不起作用,因爲它說「自我」沒有定義。我如何在屏幕上顯示分數?如何顯示此簡單遊戲中的分數?

import os, sys 
import pygame 
from pygame.locals import * 

pygame.init() 
mainClock = pygame.time.Clock() 

WINDOWWIDTH = 1000 
WINDOWHEIGHT = 700 
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) 
pygame.display.set_caption("Avoid!") 

BLACK = (0, 0, 0) 
RED = (255, 0, 0) 
WHITE = (255, 255, 255) 
GREEN = (0, 255, 0) 

player = pygame.Rect(500, 300, 40, 40) 
playerImage = pygame.Surface((40, 40)) 

enemy = pygame.Rect(300, 400, 20, 20) 
enemyImage = pygame.Surface((20, 20)) 
enemyImage.fill((RED)) 

food = pygame.Rect(300, 500 , 20, 20) 
foodImage = pygame.Surface((20, 20)) 
foodImage.fill((GREEN)) 

moveLeft = False 
moveRight = False 
moveUp = False 
moveDown = False 

MOVESPEED = 6 

class Score(pygame.sprite.Sprite): 
    """A sprite for the score.""" 

    def __init__(self, xy): 
     pygame.sprite.Sprite.__init__(self) 
     self.xy = xy #save xy -- will center our rect on it when we change the score 
     self.font = pygame.font.Font(None, 50) # load the default font, size 50 
     self.color = (255, 165, 0)   # our font color in rgb 
     self.score = 0 # start at zero 
     self.reRender() # generate the image 

    def update(self): 
     pass 

    def add(self, points): 
     """Adds the given number of points to the score.""" 
     self.score += points 
     self.reRender() 
     if player.colliderect(food): 
      return add 

    def reset(self): 
     """Resets the scores to zero.""" 
     self.score = 0 
     self.reRender() 

    def reRender(self): 
     """Updates the score. Renders a new image and re-centers at the initial coordinates.""" 
     self.image = self.font.render("%d"%(self.score), True, self.color) 
     self.rect = self.image.get_rect() 
     self.rect.center = self.xy 

while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type == KEYDOWN: 
      if event.key == K_LEFT: 
       moveRight = False 
       moveLeft = True 
      if event.key == K_RIGHT: 
       moveLeft = False 
       moveRight = True 
      if event.key == K_UP: 
       moveDown = False 
       moveUp = True 
      if event.key == K_DOWN: 
       moveUp = False 
       moveDown = True 
     if event.type == KEYUP: 
      if event.key == K_ESCAPE: 
       pygame.quit() 
       sys.exit() 
      if event.key == K_LEFT: 
       moveRight = False 
       moveLeft = True 
      if event.key == K_RIGHT: 
       moveLeft = False 
       moveRight = True 
      if event.key == K_UP: 
       moveDown = False 
       moveUp = True 
      if event.key == K_DOWN: 
       moveUp = False 
       moveDown = True 

    windowSurface.fill(WHITE) 

    if moveDown and player.bottom < WINDOWHEIGHT: 
     player.top += MOVESPEED 
    if moveUp and player.top > 0: 
     player.top -= MOVESPEED 
    if moveLeft and player.left > 0: 
     player.left -= MOVESPEED 
    if moveRight and player.right < WINDOWWIDTH: 
     player.right +=MOVESPEED 

    if player.colliderect(enemy): 
     pygame.quit() 
     sys.exit() 

    windowSurface.blit(playerImage, player) 
    windowSurface.blit(enemyImage, enemy) 
    windowSurface.blit(foodImage, food) 

    score = Score((75, 575)) 

    pygame.display.update() 
    mainClock.tick(40) 

回答

1

print()將文本輸出到stdout(又名命令行)。我認爲你的問題只是與自我相關,並且你理解關於pygame的部分。

下面是一個代碼塊我在網上找到,做你正在嘗試做的事:

font = pygame.font.Font(None, 36) 
text = font.render("Pummel The Chimp, And Win $$$", 1, (10, 10, 10)) 
textpos = text.get_rect(centerx=background.get_width()/2) 
background.blit(text, textpos) 
+0

但是,我將如何使文本動態?這意味着如果玩家獲得了積分,我將如何改變它? – user1064913

+3

font.render(「遊戲的得分是:%d」%(my_score.score),1,(10,10,10)。以通常的方式更新任何精靈的方式。 –

+0

我不知道如何 – user1064913

2

你的問題不在於bgit或pygame的任何其他部分;它是與您使用self關鍵字。 Self是對對象屬性的引用。您不要在Score類本身之外調用self.score。

取而代之,在遊戲開始時初始化分數對象,並根據需要添加分數。這是它應該如何工作:

# init Score object 
my_score = Score((75, 575)) 
# add to score like this 
my_score.add(5) 
# access score attribute like this 
# for simplicity, I print the value to the console 
print my_score.score # not self.score! 
+0

感謝您的解釋。它仍然不打印屏幕上的分數。你確定「打印」是正確的方式來顯示它嗎? – user1064913

+0

請參閱下面的答案。 –