# score display module import pygame from pygame.locals import * import game, gfx from fastdigits import FastDigits WHITE = [255, 255, 255] GREEN = [123, 204, 83] FONT = None FONTSIZE = 30 SCORE_DISPLAY_RECT = [0,0,800,34] SCORE_LEGEND_POS = [0,0] SCORE_POS = [100,0] FUEL_LEGEND_POS = [220,0] FUEL_POS = [290,0] BOMBS_LEGEND_POS = [420,0] BOMBS_POS = [520,0] LIVES_LEGEND_POS = [680,0] LIVES_POS = [760,0] def load_game_resources(): pass class Scoredisplay(FastDigits): def __init__(self,surface): # build digit table FastDigits.__init__(self) self.lastscore = -1 self.lastfuel = -1 self.lastbombs = -1 self.lastlives = -1 # make subsurface for us to draw on self.surface = surface.subsurface(SCORE_DISPLAY_RECT) self.font = pygame.font.Font(FONT,FONTSIZE) fontcolor = GREEN self.scoreimg, self.scoreimgrect = gfx.text(self.font,fontcolor,"Score:") self.scoreimgrect.topleft = SCORE_LEGEND_POS self.fuelimg, self.fuelimgrect = gfx.text(self.font,fontcolor,"Fuel:") self.fuelimgrect.topleft = FUEL_LEGEND_POS self.bombsimg, self.bombsimgrect = gfx.text(self.font,fontcolor,"Bombs:") self.bombsimgrect.topleft = BOMBS_LEGEND_POS self.livesimg, self.livesimgrect = gfx.text(self.font,fontcolor,"Ships:") self.livesimgrect.topleft = LIVES_LEGEND_POS s_img,self.last_s_rect = gfx.text(self.font,WHITE,"0000000") self.last_s_rect.topleft = SCORE_POS f_img,self.last_f_rect = gfx.text(self.font,WHITE,"00000") self.last_f_rect.topleft = FUEL_POS b_img,self.last_b_rect = gfx.text(self.font,WHITE,"000000") self.last_b_rect.topleft = BOMBS_POS l_img,self.last_l_rect = gfx.text(self.font,WHITE,"00") self.last_l_rect.topleft = LIVES_POS self.score_erase_rect = self.last_s_rect self.fuel_erase_rect = self.last_f_rect self.bombs_erase_rect = self.last_b_rect self.lives_erase_rect = self.last_l_rect def drawlegend(self): """draw the legend on the score display""" surface = self.surface dirty = gfx.dirty surface.blit(self.scoreimg, self.scoreimgrect) dirty(self.scoreimgrect) surface.blit(self.fuelimg, self.fuelimgrect) dirty(self.fuelimgrect) surface.blit(self.bombsimg, self.bombsimgrect) dirty(self.bombsimgrect) surface.blit(self.livesimg, self.livesimgrect) dirty(self.livesimgrect) def erase(self, rect): self.surface.fill(0,rect) def update(self,score,fuel,bombs,lives): """update the score display""" surface = self.surface dirty = gfx.dirty2 if score != self.lastscore: self.erase(self.score_erase_rect) s_img,s_rect = self.num2image(score) s_rect.topleft = SCORE_POS surface.blit(s_img,s_rect) dirty(s_rect,self.last_s_rect) self.last_s_rect = s_rect self.lastscore = score if fuel != self.lastfuel: self.erase(self.fuel_erase_rect) f_img,f_rect = self.num2image(fuel) f_rect.topleft = FUEL_POS surface.blit(f_img,f_rect) dirty(f_rect,self.last_f_rect) self.last_f_rect = f_rect self.lastfuel = fuel if bombs != self.lastbombs: self.erase(self.bombs_erase_rect) b_img,b_rect = self.num2image(bombs) b_rect.topleft = BOMBS_POS surface.blit(b_img,b_rect) dirty(b_rect,self.last_b_rect) self.last_b_rect = b_rect self.lastbombs = bombs if lives != self.lastlives: self.erase(self.lives_erase_rect) l_img,l_rect = self.num2image(lives) l_rect.topleft = LIVES_POS surface.blit(l_img,l_rect) dirty(l_rect,self.last_l_rect) self.last_l_rect = l_rect self.lastlives = lives