diff --- a/capbattleship/logic.py +++ b/capbattleship/logic.py @@ -1,3 +1,5 @@ +import asyncio +import time from random import randrange, choice from enum import Enum, auto @@ -372,12 +374,17 @@ else: self.state.switch(self.State.Stage.PLAYER_TURN, options={ "message": "CPU sunk you."}) - def run(self): + async def run(self): + # Navegador: um quadro a cada 1/30 s (era clock.tick(30)), cedendo a vez ao pygbag entre os quadros. while not self.quit: + t0 = time.time() self.update() self.ui.draw(self) self.textui.draw(self) self.ui.read_events(self) + await asyncio.sleep(0) + while time.time() - t0 < 1 / 30: + await asyncio.sleep(0) def sync_config(data=None): from configparser import ConfigParser @@ -385,9 +392,13 @@ config = ConfigParser() - xdgprefix = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) - dirname = os.path.join(xdgprefix, "capbattleship") - os.makedirs(dirname, exist_ok=True) + # Navegador: a pagina espelha /saves no IndexedDB. + if os.path.isdir('/saves'): + dirname = '/saves' + else: + xdgprefix = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) + dirname = os.path.join(xdgprefix, "capbattleship") + os.makedirs(dirname, exist_ok=True) filename = os.path.join(dirname, "config") diff --- a/capbattleship/pygameui.py +++ b/capbattleship/pygameui.py @@ -1,6 +1,5 @@ import math import gettext -import locale from enum import Enum, auto from glob import glob from random import choice @@ -44,7 +43,7 @@ title_font = pygame.font.Font(FONT, 35) subtitle_font = pygame.font.Font(FONT, 18) title = title_font.render(_("CAP"), 1, WHITE) - subtitle = subtitle_font.render(_("Pirate Battleship"), 1, WHITE) + subtitle = subtitle_font.render(_("Pirate Fleet"), 1, WHITE) self.bg.blit(title, (20, 15)) self.bg.blit(subtitle, (20, 70)) @@ -68,7 +67,6 @@ self.menu_entries += [ (_("Manage options"), 'o', Stage.OPTIONS, {}), (_("See credits"), 'c', Stage.CREDITS, {}), - (_("Quit"), 'q', Stage.QUIT, {}), ] @@ -212,12 +210,6 @@ (_("Enabled"), True), ], "animations"), - (_("Fullscreen"), [ - (_("Disabled"), 0), - (_("Windowed"), 1), - (_("Enabled"), 2), - ], - "fullscreen"), ] ## Reference point for options @@ -451,10 +443,6 @@ "author": _("All the testers"), "descr": _("Thanks! (testing, advices, bugs)"), }, - { - "author": _("Contribute!"), - "descr": _("Join us on https://capbattleship.tuxfamily.org/"), - }, ] font_descr = pygame.font.Font(FONT, 18) @@ -1792,20 +1780,18 @@ class PyGameUI: def __init__(self): - (language, encoding) = locale.getdefaultlocale() - + # Navegador: o idioma vem da pagina que hospeda o jogo (html lang), ou do navegador. + language = "en" try: - locale_dir = gettext.bindtextdomain("capbattleship") - except: - locale_dir = None - finally: - if locale_dir is None: - locale_dir = "locale" - - try: - tr = gettext.translation("capbattleship", localedir=locale_dir, languages=[language[0:2].lower()]) - except: - tr = gettext.translation("capbattleship", localedir=locale_dir, languages=["en"]) + import platform + lang = str(platform.window.parent.document.documentElement.lang or "") + if not lang: + lang = str(platform.window.navigator.language or "") + language = lang.replace("-", "_") + except Exception: + pass + languages = ["pt_BR"] if language.lower().startswith("pt") else ["en"] + tr = gettext.translation("capbattleship", localedir="locale", languages=languages, fallback=True) tr.install() # workaround: disable sound if sdl1 is used @@ -1816,10 +1802,9 @@ icon = pygame.image.load("gfx/icon_256px.png") pygame.display.set_icon(icon) - self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode((WIDTH,HEIGHT)) - pygame.display.set_caption(_("CAP - Pirate Battleship")) + pygame.display.set_caption(_("CAP - Pirate Fleet")) self.gui = None @@ -1982,7 +1967,6 @@ self.need_full_redraw = False else: pygame.display.update(update_rects) - self.clock.tick(30) def read_events(self, rnd): @@ -1993,10 +1977,8 @@ pos_y = int((event.pos[1]-self.fs_off[1])/self.fs_ratio) event.pos = (pos_x, pos_y) - if event.type == pygame.QUIT or (event.type==pygame.KEYDOWN and event.unicode=='q'): - rnd.state.switch(Stage.QUIT) - - elif event.type == pygame.ACTIVEEVENT or event.type == pygame.VIDEOEXPOSE: + # Navegador: nao ha para onde sair (sem Quit, sem a tecla Q). + if event.type == pygame.ACTIVEEVENT or event.type == pygame.VIDEOEXPOSE: rnd.state.options['needs_redraw'] = True self.need_full_redraw = True elif rnd.state.stage == Stage.NOTIFICATION: diff --- a/main.py +++ b/main.py @@ -0,0 +1,14 @@ +import pygame +import asyncio + +from capbattleship.pygameui import PyGameUI +from capbattleship.textui import DummyUI +from capbattleship.logic import Round + + +async def main(): + game = Round(10, PyGameUI(), DummyUI()) + await game.run() + + +asyncio.run(main())