def __init__(self, name=''): ''' Inicializa os atributos da classe mãe, cria um Ranking objeto para lidar com o arquivo de texto de alta pontuação, e cria duas listas paralelas para armazenar as altas pontuações atuais. Args: name (string): nome do jogador, opcional ''' super().__init__(name) self.ranking = Ranking('high_scores.txt') self.names = [] self.scores = [] score = self.ranking.get_players() for name in self.ranking.get_leaderboard()[:10]: self.names.append( render_font(name, font=MEDIUM_FONT, color=[0, 0, 255])) self.scores.append( render_font(str(score[name]), font=MEDIUM_FONT, color=[0, 0, 255]))
def __init__(self, is_easy): ''' Initializes all attributes which will be shown on screen: math question , player score, shuriken count. ''' self.is_easy = is_easy self.math_question = Question(is_easy) self.math_question_text = render_font( self.math_question.get_string(), font=INTERMEDIATE_FONT ) self.keyboard_input = '' self.keyboard_input_text = render_font( self.keyboard_input, font=INTERMEDIATE_FONT ) self.text_box = pygame.Surface([100, 40]) self.text_box.fill([200, 200, 200]) self.score = 75 self.score_text = render_font( f'Ninja IQ: {self.score}', font=INTERMEDIATE_FONT ) self.shuriken_count = 0 self.shuriken_count_text = render_font( str(self.shuriken_count), font=INTERMEDIATE_FONT )
def process_keyboard(self, key): '''Processed keyboard input (player typing answer)''' key_pressed = pygame.key.name(key) if key_pressed in '0123456789': self.keyboard_input += key_pressed else: if key == pygame.K_MINUS: self.keyboard_input += '-' if len( self.keyboard_input) == 0 else '' if key == pygame.K_RETURN: if len(self.keyboard_input) <= 0 or self.keyboard_input == '-': return if self.math_question.try_answer(self.keyboard_input): self.shuriken_count += 1 self.math_question.new_question() self.keyboard_input = '' if key == pygame.K_BACKSPACE: self.keyboard_input = self.keyboard_input[:-1] self.math_question_text = render_font( self.math_question.get_string(), font=INTERMEDIATE_FONT ) self.keyboard_input_text = render_font( self.keyboard_input, font=INTERMEDIATE_FONT ) self.shuriken_count_text = render_font( str(self.shuriken_count), font=INTERMEDIATE_FONT )
def __init__(self, is_easy): ''' Inicializa todos os atributos que serão mostrados na tela: pergunta matemática, pontuação do jogador, contagem shuriken. ''' self.is_easy = is_easy self.math_question = Question(is_easy) self.math_question_text = render_font( self.math_question.get_string(), font=INTERMEDIATE_FONT ) self.keyboard_input = '' self.keyboard_input_text = render_font( self.keyboard_input, font=INTERMEDIATE_FONT ) self.text_box = pygame.Surface([100, 40]) self.text_box.fill([200, 200, 200]) self.score = 75 self.score_text = render_font( f'Ninja IQ: {self.score}', font=INTERMEDIATE_FONT ) self.shuriken_count = 0 self.shuriken_count_text = render_font( str(self.shuriken_count), font=INTERMEDIATE_FONT )
def __init__(self, name=''): ''' Initializes parent class attributes, creates a Ranking object to deal with the high scores textfile, and creates two parallel lists to store the current high scores. Args: name (string): name of the player, optional ''' super().__init__(name) self.ranking = Ranking('high_scores.txt') self.names = [] self.scores = [] score = self.ranking.get_players() for name in self.ranking.get_leaderboard()[:10]: self.names.append( render_font(name, font=MEDIUM_FONT, color=[0, 0, 255]) ) self.scores.append( render_font(str(score[name]), font=MEDIUM_FONT, color=[0, 0, 255]) )
def spend_shuriken(self): '''Updates shuriken count when player throws a shuriken''' self.shuriken_count -= 1 self.shuriken_count_text = render_font( str(self.shuriken_count), font=INTERMEDIATE_FONT )
def spend_shuriken(self): '''Atualiza a contagem shuriken quando o jogador joga um shuriken''' self.shuriken_count -= 1 self.shuriken_count_text = render_font( str(self.shuriken_count), font=INTERMEDIATE_FONT )
def add_score(self): '''Updates player score depending on game difficulty''' if self.is_easy: self.score += 5 else: self.score += 10 self.score_text = render_font( f'Ninja IQ: {self.score}', font=INTERMEDIATE_FONT )
def add_score(self): '''Atualiza a pontuação do jogador, dependendo da dificuldade do jogo''' if self.is_easy: self.score += 5 else: self.score += 10 self.score_text = render_font( f'Ninja IQ: {self.score}', font=INTERMEDIATE_FONT )
def __init__(self, text, position): ''' Initializes the button text, position, box (which will be the button's background), and which state the button is in (not active at first) Args: text (string): text which appear on the button position (list): position of the button on screen ''' self.text = render_font(text, font=MEDIUM_FONT) self.position = position # pygame rectangle which appears as the button's background self.box = pygame.Surface([200, 40]) self.box.fill([255, 255, 255]) self.active = False
from utils import render_font pygame.font.init() pygame.mixer.init() SCREEN_NAMES = ['menu', 'hard_game', 'easy_game', 'how_to_play', 'high_scores'] TINY_FONT = pygame.font.Font('freesansbold.ttf', 17) SMALL_FONT = pygame.font.Font('freesansbold.ttf', 18) INTERMEDIATE_FONT = pygame.font.Font('freesansbold.ttf', 24) MEDIUM_FONT = pygame.font.Font('freesansbold.ttf', 26) BIG_FONT = pygame.font.Font('freesansbold.ttf', 34) MENU_CONSTANTS = { 'TITLE': render_font('The Meditating Dog', font=BIG_FONT), 'NAME': render_font('SEU NOME: ', font=MEDIUM_FONT), 'NAME_BOX': pygame.Surface([250, 40]), 'WARNING': render_font('Digite seu nome antes de jogar', font=SMALL_FONT), 'NINJA_IMAGE': pygame.transform.scale(pygame.image.load('images/m_ninja.png'), [183, 198]), } MENU_CONSTANTS['NAME_BOX'].fill([255, 255, 255]) GAME_CONSTANTS = { 'GATE_IMAGE':
begin = True while 1: menu = Menu(screen, game.score) launch = menu.main() if begin == False: #reinit game after menu to have score and not welcome message... game = Game(screen) # init game class begin = False while launch: #game loop for event in pygame.event.get( ): #checkin for click on the red cross to quit if event.type == pygame.QUIT: launch = False quit() game.player.player_update() #function playerUpdate of the class player screen.blit(back, [0, 0]) #print back image utils.render_font(screen, game.player.lives, game.score) if len(game.enemies) == 0: #no more enemies game.generate_enemies() for enemy in game.enemies: #iterate in list of enemies enemy.movement( game.player.rect.x, game.player.rect.y) #movement function of the enemy class if enemy.collision(game.player): if game.player.lives <= 0: screen.blit(over, (202, 230)) launch = False break else: #game.player.lives > 0: game.player.lives -= 1
from utils import render_font pygame.font.init() pygame.mixer.init() SCREEN_NAMES = ['menu', 'hard_game', 'easy_game', 'how_to_play', 'high_scores'] TINY_FONT = pygame.font.Font('freesansbold.ttf', 17) SMALL_FONT = pygame.font.Font('freesansbold.ttf', 18) INTERMEDIATE_FONT = pygame.font.Font('freesansbold.ttf', 24) MEDIUM_FONT = pygame.font.Font('freesansbold.ttf', 26) BIG_FONT = pygame.font.Font('freesansbold.ttf', 34) MENU_CONSTANTS = { 'TITLE': render_font('The Meditating Ninja', font=BIG_FONT), 'NAME': render_font('ENTER NAME: ', font=MEDIUM_FONT), 'NAME_BOX': pygame.Surface([250, 40]), 'WARNING': render_font('Type your name before playing.', font=SMALL_FONT), 'NINJA_IMAGE': pygame.transform.scale(pygame.image.load('images/m_ninja.png'), [183, 198]), } MENU_CONSTANTS['NAME_BOX'].fill([255, 255, 255]) GAME_CONSTANTS = { 'GATE_IMAGE':