0
我正在創建一個黑色方塊在屏幕上移動的基本遊戲。還有另外兩個廣場,一個綠色廣場和一個紅色廣場。當黑色方塊與紅色方塊相撞時,退出程序,當它與綠色方塊相撞時,它將添加到您的積分計數器中。我已經將紅色部分放下了,但我無法使用精靈和類創建動態文本。我之前問過的一個問題告訴我,我應該把分數放到屏幕上,然後在每次廣場與綠色廣場相撞時重新敲擊它。但我不知道如何正確地做到這一點。這裏是我的代碼:使用blit方法創建動態文本
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))
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 = (BLACK) # 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."""
if player.colliderect(food):
my_score.score += points
self.reRender()
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
my_score = Score((75, 575))
print my_score.score
font = pygame.font.Font(None, 36)
text = font.render("%d" % (my_score.score), 1, (10, 10, 10))
textpos = text.get_rect(centerx=windowSurface.get_width()/2)
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 6
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)
windowSurface.blit(text, textpos)
score = Score((75, 575))
pygame.display.update()
mainClock.tick(40)
我將windowSurface.blit(text,textpos)放入Score類中的add函數嗎?一旦大廣場接觸到綠色的小廣場,我該如何提高分數?