--- a/game.py +++ b/game.py @@ -110,7 +110,7 @@ self.spacepressed = None - def run(self): + async def run(self): while not self.done: if not self.pause: if not self.gameover: @@ -208,7 +208,7 @@ self.draw() - self.handle_events() + await self.handle_events() return self.score.get_score() @@ -250,16 +250,16 @@ i += 1 filename = "sshot" + str(i) + ".tga" - pygame.image.save(self.screen, filename) - print("Screenshot saved as " + filename) + # (screenshots are not saved in the browser version) + return - def handle_events(self): + async def handle_events(self): nextframe = False framecount = 0 while not nextframe: # wait until there's at least one event in the queue #nextframe = True - pygame.event.post(pygame.event.wait()) + await util.next_frame() for event in pygame.event.get(): #event = pygame.event.wait() if event.type == QUIT or \ --- a/highscores.py +++ b/highscores.py @@ -11,15 +11,13 @@ class Highscores: sky = None - def __init__(self, screen, new_score = -1, endless = False, online = False): + def __init__(self, screen, new_score = -1, endless = False): self.screen = screen if not endless: self.title = util.bigfont.render("Story Mode", True, (0,0,0)) - elif not online: - self.title = util.bigfont.render("Endless Mode", True, (0,0,0)) else: - self.title = util.bigfont.render("Endless Online", True, (0,0,0)) + self.title = util.bigfont.render("Endless Mode", True, (0,0,0)) if not Highscores.sky: Highscores.sky = util.load_image("taivas") @@ -33,12 +31,6 @@ self.endless = endless - if online: - if not self.online_init(): - self.done = True - print("Could not get online highscores!") - return - self.pathname = util.get_config_path() if not endless: self.filename = self.pathname + "/scores" @@ -81,10 +73,6 @@ print("Corrupt high score file.") self.dummy_scores() - # The online highscore is always submitted - if endless and new_score != -1: - self.submit_score(Variables.name, new_score) - if new_score > self.scores[9][1]: #print "It's a new high score!" #self.inputting = True @@ -98,41 +86,7 @@ self.write_scores() - def online_init(self): - HIGHSCORE_URL = "http://funnyboat.sourceforge.net/cgi-bin/top10.py" - try: - import socket - socket.setdefaulttimeout(20) - import urllib2 - f = urllib2.urlopen(HIGHSCORE_URL) - for line in f.readlines(): - name,score = line.split("\t") - score = int(score) - name = unicode(name, "utf-8") - self.scores.append((name,score)) - f.close() - except: - # Getting highscores failed - return False - # Success - return True - - def submit_score(self, name, score): - SUBMIT_URL = "http://funnyboat.sourceforge.net/cgi-bin/submit.py" - try: - import socket - socket.setdefaulttimeout(20) - import urllib - import urllib2 - #name,score = self.scores[self.input_score] - data = urllib.urlencode({'name' : name.encode('utf-8'), 'score' : score}) - req = urllib2.Request(SUBMIT_URL, data) - f = urllib2.urlopen(req) - f.close() - except: - print("Failed to submit highscore") - - def run(self): + async def run(self): water = Water.global_water water.set_amplitude(SCREEN_HEIGHT / 8.0) water_sprite = pygame.sprite.Group() @@ -176,7 +130,7 @@ nextframe = False while not nextframe: - pygame.event.post(pygame.event.wait()) + await util.next_frame() for event in pygame.event.get(): if event.type == NEXTFRAME: nextframe = True @@ -188,8 +142,6 @@ if event.type == KEYDOWN: if event.key == K_RETURN or event.key == K_ESCAPE: self.inputting = False - if self.endless: - self.submit_score() self.write_scores() elif event.key == K_BACKSPACE: if len(self.scores[self.input_score][0]) != 0: --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ - #!/usr/bin/python +# import pygame +import asyncio import math import random import sys @@ -36,7 +37,7 @@ mine.init() seagull.init() -def main(): +async def main(): global SCREEN_FULLSCREEN pygame.init() @@ -77,48 +78,42 @@ # It's not a critical problem if there's no music pass - pygame.time.set_timer(NEXTFRAME, 1000 // FPS) # 30 fps - Water.global_water = Water() main_selection = 0 while True: - main_selection = Menu(screen, ("New Game", "High Scores", "Options", "Quit"), main_selection).run() + main_selection = await Menu(screen, ("New Game", "High Scores", "Options", "Quit"), main_selection).run() if main_selection == 0: # New Game - selection = Menu(screen, ("Story Mode", "Endless Mode")).run() + selection = await Menu(screen, ("Story Mode", "Endless Mode")).run() if selection == 0: # Story - score = Game(screen).run() - Highscores(screen, score).run() + score = await Game(screen).run() + await Highscores(screen, score).run() elif selection == 1: # Endless - score = Game(screen, True).run() - Highscores(screen, score, True).run() + score = await Game(screen, True).run() + await Highscores(screen, score, True).run() elif main_selection == 1: # High Scores selection = 0 while True: - selection = Menu(screen, ("Story Mode", "Endless Mode", "Endless Online"), selection).run() + selection = await Menu(screen, ("Story Mode", "Endless Mode"), selection).run() if selection == 0: # Story - Highscores(screen).run() + await Highscores(screen).run() elif selection == 1: # Endless - Highscores(screen, endless = True).run() - elif selection == 2: - # Online - Highscores(screen, endless = True, online = True).run() + await Highscores(screen, endless = True).run() else: break elif main_selection == 2: # Options - selection = Options(screen).run() + selection = await Options(screen).run() else: #if main_selection == 3: - # Quit - return + # Quit: there is nowhere to quit to in the browser, so go back to the menu + main_selection = 0 -if __name__ == '__main__': - main() +asyncio.run(main()) --- a/menu.py +++ b/menu.py @@ -26,7 +26,7 @@ self.selection = selection self.t = 0 - def run(self): + async def run(self): done = False while not done: @@ -46,18 +46,13 @@ rect.top = 0 self.screen.blit(Menu.logo, rect) - image = util.smallfont.render("http://funnyboat.sourceforge.net/", True, (0,0,0)) - rect = image.get_rect() - rect.midbottom = self.screen.get_rect().midbottom - self.screen.blit(image, rect) - pygame.display.flip() self.t += 1 nextframe = False while not nextframe: - pygame.event.post(pygame.event.wait()) + await util.next_frame() for event in pygame.event.get(): if event.type == QUIT or \ event.type == KEYDOWN and event.key == K_ESCAPE: --- a/options.py +++ b/options.py @@ -41,7 +41,7 @@ "Music: " + (Variables.music and "on" or "off"), "Player Name: " + Variables.name) - def run(self): + async def run(self): done = False while not done: @@ -61,18 +61,13 @@ rect.top = 0 self.screen.blit(Options.logo, rect) - image = util.smallfont.render("http://funnyboat.sourceforge.net/", True, (0,0,0)) - rect = image.get_rect() - rect.midbottom = self.screen.get_rect().midbottom - self.screen.blit(image, rect) - pygame.display.flip() self.t += 1 nextframe = False while not nextframe: - pygame.event.post(pygame.event.wait()) + await util.next_frame() for event in pygame.event.get(): if event.type == QUIT or \ event.type == KEYDOWN and event.key == K_ESCAPE: --- a/util.py +++ b/util.py @@ -1,3 +1,4 @@ +import asyncio import pygame import codecs import os @@ -14,6 +15,9 @@ def get_config_path(): pathname = "" + if os.path.isdir("/saves"): + # on the web the page keeps the /saves folder in the browser + return "/saves" try: pathname = os.environ["HOME"] + "/.funnyboat" except: @@ -112,6 +116,12 @@ # The all-caps ogg is because the original file just happened to be that way pygame.mixer.music.load("data/" + name + ".ogg") +async def next_frame(): + # Replaces pygame.event.wait() + the NEXTFRAME timer: yields to the browser + # for one frame and then posts the NEXTFRAME event. + await asyncio.sleep(1.0 / FPS) + pygame.event.post(pygame.event.Event(NEXTFRAME)) + def rotate(surf, angle): if Variables.aa: return pygame.transform.rotozoom(surf, angle, 1.0)