<commit_before>import pyglet

from pyglet.window.key import ENTER, SPACE, BACKSPACE

from game_piece import Problem
from levels import Levels, Level

window = pyglet.window.Window()#fullscreen=True)

class TheGame(Level):
  def __init__(self):
    self.user_answer = pyglet.text.Label('', x=10, y=window.height-30, font_size=20)
    self.score_text = pyglet.text.Label('0', x=window.width-100, y=window.height-30, font_size=20)
    self.score = 0
    self.game_piece = Problem(window)
    super(TheGame, self).__init__()


  def key(self, symbol, mod):
    num = set(range(ord("0"), ord("9") + 1))
    # If answer box, check to see if they were right
    if symbol == ENTER:
      score = self.game_piece.check(self.user_answer.text) # Get back time elapsed or None
      if score:
        # score is elapsed time on the answer. Give bonus on <2 seconds answers
        score = 2 - score
        if score < 0:
          score = 0
        self.score += 1 + score
        self.score_text.text = "%.02f" % self.score
      self.user_answer.text = ''
    elif symbol in num:
      self.user_answer.text += chr(symbol)

  def draw(self):
    window.clear()
    self.game_piece.draw()
    self.user_answer.draw()
    self.score_text.draw()

  def clock(self, dt):
    self.game_piece.update(dt)
    if self.game_piece.fail():
      self.container.next()
      # Pass score to next "level"
      self.container.current.msg(score=self.score)

  def reset(self):
    self.game_piece.reset()
    self.score_text.text = ""
    self.score = 0

class IntroScreen(Level):
  def __init__(self):
    self.welcome = pyglet.text.Label('WELCOME\n\tTO\nCLAIRE', x=100, y=window.height-80,
                                    width=window.width//2,
                                    font_size=40, multiline=True)
    
    scores = self.get_scores()
    print scores
    self.scores = pyglet.text.Label("".join(scores), x=100, y=window.height-280,
                                    width=int(window.width * .8),
                                    font_size=30, multiline=True)
    super(IntroScreen, self).__init__()

  def get_scores(self):
    with open("scores.txt") as fp:
      lines = fp.readlines()
    lines = sorted(lines, reverse=True, key=lambda l: float(l.split(': ')[-1]))
    return ["{}. {}".format(i, line) for i, line in enumerate(lines[:5], 1)]

  def key(self, symbol, mod):
    if symbol == SPACE:
      self.container.next()

  def draw(self):
    window.clear()
    self.welcome.draw()
    self.scores.draw()

class GameOver(Level):
  def __init__(self):
    self.text = 'Enter Initials: '
    self.welcome = pyglet.text.Label('Game\nOver', x=200, y=window.height-200,
                                    width=window.width//2,
                                    font_size=40, multiline=True)
    self.initials = pyglet.text.Label(self.text, x=200, y=window.height-400,
                                    font_size=20)
    super(GameOver, self).__init__()


  def hi_score(self, user, score):
    with open("scores.txt", "a") as fp:
      fp.write("%s: %s\n" % (user, round(score, 2)))

  def draw(self):
    window.clear()
    self.welcome.draw()
    self.initials.draw()

  def key(self, symbol, mod):
    """Let user enter 3 characters for high score list."""
    if symbol == ENTER:
      user = self.initials.text[len(self.text):]
      self.hi_score(user, str(self.messages.get("score", 0)))
      self.container.level = 0
    elif symbol == BACKSPACE:
      self.initials.text = self.initials.text[:-1]
    elif len(self.initials.text) < len(self.text) + 3:
      try:
        self.initials.text += chr(symbol)
      except:
        pass


levels = Levels([IntroScreen(), TheGame(), GameOver()])
pyglet.clock.schedule(levels.clock)
<commit_msg>Speed up with score<commit_after>import pyglet

from pyglet.window.key import ENTER, SPACE, BACKSPACE

from game_piece import Problem
from levels import Levels, Level

window = pyglet.window.Window()#fullscreen=True)

class TheGame(Level):
  def __init__(self):
    self.user_answer = pyglet.text.Label('', x=10, y=window.height-30, font_size=20)
    self.score_text = pyglet.text.Label('0', x=window.width-100, y=window.height-30, font_size=20)
    self.score = 0
    self.game_piece = Problem(window)
    super(TheGame, self).__init__()


  def key(self, symbol, mod):
    self.game_piece.speed = self.score / 10 + 1
    num = set(range(ord("0"), ord("9") + 1))
    # If answer box, check to see if they were right
    if symbol == ENTER:
      score = self.game_piece.check(self.user_answer.text) # Get back time elapsed or None
      if score:
        # score is elapsed time on the answer. Give bonus on <2 seconds answers
        score = 2 - score
        if score < 0:
          score = 0
        self.score += 1 + score
        self.score_text.text = "%.02f" % self.score
      self.user_answer.text = ''
    elif symbol in num:
      self.user_answer.text += chr(symbol)

  def draw(self):
    window.clear()
    self.game_piece.draw()
    self.user_answer.draw()
    self.score_text.draw()

  def clock(self, dt):
    self.game_piece.update(dt)
    if self.game_piece.fail():
      self.container.next()
      # Pass score to next "level"
      self.container.current.msg(score=self.score)

  def reset(self):
    self.game_piece.reset()
    self.score_text.text = ""
    self.score = 0

class IntroScreen(Level):
  def __init__(self):
    self.welcome = pyglet.text.Label('WELCOME\n\tTO\nCLAIRE', x=100, y=window.height-80,
                                    width=window.width//2,
                                    font_size=40, multiline=True)

    scores = self.get_scores()
    self.scores = pyglet.text.Label("".join(scores), x=100, y=window.height-280,
                                    width=int(window.width * .8),
                                    font_size=30, multiline=True)
    super(IntroScreen, self).__init__()

  def reset(self):
    scores = self.get_scores()
    self.scores.text = "".join(scores)

  def get_scores(self):
    with open("scores.txt") as fp:
      lines = fp.readlines()
    lines = sorted(lines, reverse=True, key=lambda l: float(l.split(': ')[-1]))
    return ["{}. {}".format(i, line) for i, line in enumerate(lines[:5], 1)]

  def key(self, symbol, mod):
    if symbol == SPACE:
      self.container.next()

  def draw(self):
    window.clear()
    self.welcome.draw()
    self.scores.draw()

class GameOver(Level):
  def __init__(self):
    self.text = 'Enter Initials: '
    self.welcome = pyglet.text.Label('Game\nOver', x=200, y=window.height-200,
                                    width=window.width//2,
                                    font_size=40, multiline=True)
    self.initials = pyglet.text.Label(self.text, x=200, y=window.height-400,
                                    font_size=20)
    super(GameOver, self).__init__()


  def hi_score(self, user, score):
    with open("scores.txt", "a") as fp:
      fp.write("%s: %s\n" % (user, round(float(score), 2)))

  def draw(self):
    window.clear()
    self.welcome.draw()
    self.initials.draw()

  def key(self, symbol, mod):
    """Let user enter 3 characters for high score list."""
    if symbol == ENTER:
      user = self.initials.text[len(self.text):]
      self.hi_score(user, str(self.messages.get("score", 0)))
      self.container.level = 0
    elif symbol == BACKSPACE:
      self.initials.text = self.initials.text[:-1]
    elif len(self.initials.text) < len(self.text) + 3:
      try:
        self.initials.text += chr(symbol)
      except:
        pass


levels = Levels([IntroScreen(), TheGame(), GameOver()])
pyglet.clock.schedule(levels.clock)

{'fraction_non_alphanumeric': 0.141642688987706, 'fraction_numerical': 0.018441014909756734, 'mean_word_length': 2.4508122743682312, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1482917', 'n_tokens_mistral': 2680, 'n_tokens_neox': 2515, 'n_words': 557}