1import pygame 2import os 3import sys 4import codecs 5 6import util 7from locals import * 8from water import Water 9 10import cloud 11 12class Highscores: 13 sky = None 14 def __init__(self, screen, new_score = -1, endless = False, online = False): 15 self.screen = screen 16 17 if not endless: 18 self.title = util.bigfont.render("Story Mode", True, (0,0,0)) 19 elif not online: 20 self.title = util.bigfont.render("Endless Mode", True, (0,0,0)) 21 else: 22 self.title = util.bigfont.render("Endless Online", True, (0,0,0)) 23 24 if not Highscores.sky: 25 Highscores.sky = util.load_image("taivas") 26 Highscores.sky = pygame.transform.scale(Highscores.sky, (SCREEN_WIDTH, SCREEN_HEIGHT)) 27 28 self.inputting = False 29 self.input_score = -1 30 31 self.scores = [] 32 self.done = False 33 34 self.endless = endless 35 36 if online: 37 if not self.online_init(): 38 self.done = True 39 print("Could not get online highscores!") 40 return 41 42 self.pathname = util.get_config_path() 43 if not endless: 44 self.filename = self.pathname + "/scores" 45 else: 46 self.filename = self.pathname + "/endless_scores" 47 48 try: 49 if not os.path.exists(self.pathname): 50 os.mkdir(self.pathname) 51 except: 52 print("Can't make directory " + self.pathname) 53 self.done = True 54 return 55 56 if not os.path.exists(self.filename): 57 #print "Creating dummy high scores" 58 self.dummy_scores() 59 else: 60 try: 61 f = codecs.open(self.filename, "r", "utf_8") 62 i = 0 63 name, score = "", 0 64 for line in f: 65 if i % 2 == 0: 66 name = line.strip() 67 else: 68 try: 69 score = int(line) 70 except: 71 print("Corrupt high score file.") 72 self.dummy_scores() 73 break 74 self.scores.append((name, score)) 75 i += 1 76 except: 77 self.dummy_scores() 78 print("Can't open file " + self.filename + " or file corrupt") 79 80 if len(self.scores) < 10: 81 print("Corrupt high score file.") 82 self.dummy_scores() 83 84 # The online highscore is always submitted 85 if endless and new_score != -1: 86 self.submit_score(Variables.name, new_score) 87 88 if new_score > self.scores[9][1]: 89 #print "It's a new high score!" 90 #self.inputting = True 91 for i in range(10): 92 if self.scores[i][1] < new_score: 93 self.input_score = i 94 for j in range(9 - i): 95 self.scores[9 - j] = self.scores[8 - j] 96 self.scores[i] = [Variables.name, new_score] 97 break 98 99 self.write_scores() 100 101 def online_init(self): 102 HIGHSCORE_URL = "http://funnyboat.sourceforge.net/cgi-bin/top10.py" 103 try: 104 import socket 105 socket.setdefaulttimeout(20) 106 import urllib2 107 f = urllib2.urlopen(HIGHSCORE_URL) 108 for line in f.readlines(): 109 name,score = line.split("\t") 110 score = int(score) 111 name = unicode(name, "utf-8") 112 self.scores.append((name,score)) 113 f.close() 114 except: 115 # Getting highscores failed 116 return False 117 # Success 118 return True 119 120 def submit_score(self, name, score): 121 SUBMIT_URL = "http://funnyboat.sourceforge.net/cgi-bin/submit.py" 122 try: 123 import socket 124 socket.setdefaulttimeout(20) 125 import urllib 126 import urllib2 127 #name,score = self.scores[self.input_score] 128 data = urllib.urlencode({'name' : name.encode('utf-8'), 'score' : score}) 129 req = urllib2.Request(SUBMIT_URL, data) 130 f = urllib2.urlopen(req) 131 f.close() 132 except: 133 print("Failed to submit highscore") 134 135 def run(self): 136 water = Water.global_water 137 water.set_amplitude(SCREEN_HEIGHT / 8.0) 138 water_sprite = pygame.sprite.Group() 139 water_sprite.add(water) 140 while not self.done: 141 self.screen.blit(Highscores.sky, self.screen.get_rect()) 142 water.update() 143 cloud.update() 144 cloud.draw(self.screen) 145 water_sprite.draw(self.screen) 146 147 rect = self.title.get_rect() 148 rect.centerx = self.screen.get_rect().centerx 149 rect.top = 10 150 151 self.screen.blit(self.title, rect) 152 153 for i in range(10): 154 color = (0,0,0) 155 #if self.inputting and self.input_score == i: 156 if self.input_score == i: 157 color = (220, 120, 20) 158 score = self.scores[i] 159 image = 0 160 try: 161 image = util.smallfont.render(str(i + 1) + ". " + score[0], True, color) 162 except: 163 image = util.smallfont.render(str(i + 1) + ". Error", True, color) 164 rect = image.get_rect() 165 rect.top = 50 + i * 1.5 * rect.height 166 rect.left = 10 167 self.screen.blit(image, rect) 168 169 image = util.smallfont.render(str(score[1]), True, color) 170 rect = image.get_rect() 171 rect.top = 50 + i * 1.5 * rect.height 172 rect.right = self.screen.get_rect().right - 10 173 self.screen.blit(image, rect) 174 175 pygame.display.flip() 176 177 nextframe = False 178 while not nextframe: 179 pygame.event.post(pygame.event.wait()) 180 for event in pygame.event.get(): 181 if event.type == NEXTFRAME: 182 nextframe = True 183 continue 184 if self.inputting: 185 if event.type == QUIT: 186 self.inputting = False 187 self.write_scores() 188 if event.type == KEYDOWN: 189 if event.key == K_RETURN or event.key == K_ESCAPE: 190 self.inputting = False 191 if self.endless: 192 self.submit_score() 193 self.write_scores() 194 elif event.key == K_BACKSPACE: 195 if len(self.scores[self.input_score][0]) != 0: 196 self.scores[self.input_score][0] = self.scores[self.input_score][0][:-1] 197 elif event.key == K_SPACE or event.unicode != " ": 198 if len(self.scores[self.input_score][0]) < 32: 199 self.scores[self.input_score][0] += event.unicode 200 else: 201 if event.type == KEYDOWN or event.type == QUIT or event.type == JOYBUTTONDOWN: 202 self.done = True 203 nextframe = True 204 205 def dummy_scores(self): 206 self.scores = [] 207 self.scores.append(("Funny Boat", 2000)) # 1 208 self.scores.append(("Hectigo", 1500)) # 2 209 self.scores.append(("JDruid", 1000)) # 3 210 self.scores.append(("Pekuja", 750)) # 4 211 self.scores.append(("Pirate", 500)) # 5 212 self.scores.append(("Shark", 400)) # 6 213 self.scores.append(("Seagull", 300)) # 7 214 self.scores.append(("Naval mine", 200)) # 8 215 self.scores.append(("Cannonball", 100)) # 9 216 self.scores.append(("Puffy the Cloud", 50)) #10 217 218 self.write_scores() 219 220 def write_scores(self): 221 try: 222 f = codecs.open(self.filename, "w", "utf_8") 223 for i in range(10): 224 print(self.scores[i][0], file=f) 225 print(self.scores[i][1], file=f) 226 except: 227 print("Failed to write high scores to file " + self.filename) 228 self.done = True 229 return 230