Permalink
Browse files

added scoreboard system when the game is over. The scores are stored …

…into a file
  • Loading branch information...
michael michael
michael authored and michael committed Aug 6, 2012
1 parent 500ac75 commit dfd3121b1fe13159bab3fa38fc9abad72336f3db
Showing with 99 additions and 9 deletions.
  1. +21 −7 sources/game.py
  2. +10 −2 sources/lib/eztext.py
  3. +68 −0 sources/scoreboard.py
View
@@ -17,6 +17,7 @@ def game():
import lasers
import input
import lib.eztext
+ import scoreboard
pygame = common_pygame.pygame
screen= common_pygame.screen
@@ -90,8 +91,6 @@ def game():
#bonus processing
scoreBonus=bonus.Bonus(sounds, menu)
- ship.life=0
-
thegame=True
level =-1
spawnedBoss=False
@@ -272,7 +271,7 @@ def game():
#presskey = font.render("press any key to quit", True, (255,255, 255))
#yourscore = font.render("Your score : "+ str(ship.score), True, (255,255, 255))
youlost = pygame.font.Font("BITSUMIS.TTF",105).render("Game over", True, (255,255, 255))
- presskey = pygame.font.Font("BITSUMIS.TTF",23).render("press any key to quit", True, (255,255, 255))
+ presskey = pygame.font.Font("BITSUMIS.TTF",23).render("press escape to quit", True, (255,255, 255))
yourscore = pygame.font.Font("BITSUMIS.TTF",30).render("Your score : "+ str(ship.score), True, (255,255, 255))
yourname = pygame.font.Font("BITSUMIS.TTF",55).render("Your name : ", True, (255,255, 255))
@@ -298,6 +297,7 @@ def game():
txtbx.set_pos( 230,180)
txtbx.set_font(pygame.font.Font("BITSUMIS.TTF",30))
nametyped = False
+ scoreObj = scoreboard.ScoreBoard()
while exitloop:
exitcountdown =exitcountdown+ 1
@@ -309,12 +309,12 @@ def game():
background.blitPlanets()
#show the fog
background.blitFog()
- screen.blit(youlost, (110,50 ))
+ screen.blit(youlost, (110,35 ))
screen.blit(yourscore, (130,150 ))
#screen.blit(yourname, (180,330 ))
#screen.blit(pygame.font.Font("BITSUMIS.TTF",55)\
#.render(name, True, (255,0, 0)), (300, 330))
- screen.blit(presskey, (150,450 ))
+ screen.blit(presskey, (270,520 ))
#car = str(input.keyInput())
#if isinstance(car, str):
@@ -327,7 +327,14 @@ def game():
txtbx.update(pygame.event.get())
# blit txtbx on the sceen
- txtbx.draw(screen)
+ #if exitcountdown%20>10:
+ #txtbx.draw(screen)
+
+ if txtbx.hasTyped() ==False:
+ if exitcountdown%20>10:
+ txtbx.draw(screen)
+ elif nametyped == False:
+ txtbx.draw(screen)
if exitcountdown==30:
@@ -342,7 +349,14 @@ def game():
exit()
exitloop=False
if pygame.key.get_pressed()[K_RETURN]:
- nametyped = True
+ if not nametyped:
+ scoreObj.addScore(ship.score, txtbx.getText())
+ nametyped = True
+
+
+ scoreObj.printScore()
+
+
#if pygame.KEYDOWN:
#print("exiting")
View
12 sources/lib/eztext.py 100755 → 100644
@@ -45,8 +45,16 @@ def draw(self, surface):
surface.blit(text, (self.x, self.y))
def getText(self):
- return self.prompt+self.value
-
+ return self.value
+
+ def hasTyped(self):
+ if self.value =="":
+ return False
+ else:
+ return True
+
+
+
def update(self, events):
""" Update the input based on passed events """
for event in events:
View
@@ -0,0 +1,68 @@
+import common_pygame
+import load_resources
+import os
+import pickle
+import operator
+
+
+pygame = common_pygame.pygame
+screen= common_pygame.screen
+
+
+class ScoreBoard():
+
+ #init the score object
+ def __init__(self):
+ #empty score list
+ self.scores = list()
+ self.scorefont = pygame.font.Font("BITSUMIS.TTF",30)
+ #fill the score list with the existing file if it exists
+ if os.path.exists(os.path.join('data','scores.txt')):
+ with open(os.path.join('data','scores.txt'), 'rb') as fichier:
+ depickler = pickle.Unpickler(fichier)
+ self.scores= depickler.load()
+
+ def addScore(self, score, name):
+ #append the new score into the list
+ self.scores.append((score,name))
+ #sort the list by score :
+ self.scores = sorted(self.scores, key=operator.itemgetter(0), reverse=True)
+ #write the config into the file
+ with open(os.path.join('data','scores.txt'), 'wb') as fichier:
+ mon_pickler = pickle.Pickler(fichier)
+ mon_pickler.dump(self.scores)
+
+ def printScore(self):
+ #print(self.scores)
+ #print the background rectangle
+ s = pygame.Surface((500,310)) # the size of your rect
+ s.set_alpha(64) # alpha level
+ s.fill((99, 0, 201)) # this fills the entire surface
+ screen.blit(s, (150,210)) # (0,0) are the top-left coordinates
+
+
+ max = len(self.scores)
+
+ for i in range(min(max, 10)):
+ (score, name)=self.scores[i]
+ screen.blit(self.scorefont.render(str(name) ,True, \
+ (255,255, 255)),(160,220+i*30))
+ screen.blit(self.scorefont.render(str(score) ,True, \
+ (255,255, 255)),(450,220+i*30))
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

0 comments on commit dfd3121

Please sign in to comment.