def GameSecret4(): """the mathematics behind the game, part 1""" global game_dict while True: for event in pg.event.get(): if event.type == pg.QUIT: GameQuit() gameDisplay.fill(adt.color_use('background')) pgImage((10, 10), (80, 80), BUTTON_BACK, GameHomePage) pgText(game_dict[lang]['secret'], (GAME_WIDTH / 2, 50), 50) pgParagraf(game_dict[lang]['secret_text4'], (GAME_WIDTH / 2 - 250, 100, 500, GAME_HEIGHT)) pgButton("1", (100, GAME_HEIGHT / 2 - 130), (50, 35), adt.color_use('button_ok'), adt.color_use('button_ok_hover'), GameSecret1) pgButton("2", (100, GAME_HEIGHT / 2 - 60), (50, 35), adt.color_use('button_ok'), adt.color_use('button_ok_hover'), GameSecret2) pgButton("3", (100, GAME_HEIGHT / 2 + 10), (50, 35), adt.color_use('button_ok'), adt.color_use('button_ok_hover'), GameSecret3) pgButton("4", (100, GAME_HEIGHT / 2 + 80), (50, 35), adt.color_use('button_ok'), adt.color_use('button_ok_hover'), GameSecret4) # refresh tampilan game pg.display.update() gameClock.tick(15)
def pgParagraf(text, rect_info, font_name=GAME_FONT): """Menulis paragraf rata kiri warna hitam dengan warna background windows pada layar""" font = pygame.font.Font(font_name, 20) rect = pg.Rect(rect_info) rendered = textrect.render_textrect(text, font, rect, adt.color_use("black"), adt.color_use("background"), 0) if rendered: gameDisplay.blit(rendered, rect.topleft)
def GameHomePage(): """Menu utama dari game""" while True: for event in pg.event.get(): if event.type == pg.QUIT: GameQuit() gameDisplay.fill(adt.color_use('background')) pgText(game_dict[lang]['game_title'], (GAME_WIDTH / 2, 100), 90, font_name=GAME_FONT2) # buat pilihan menu utama pgImage((GAME_WIDTH / 2 - 100, GAME_HEIGHT / 2 - 100), (200, 200), PLAY_HANOI, GameHistory) pgImage((GAME_WIDTH / 6 - 40, 450), (80, 80), BUTTON_ABOUT, GameAbout) pgImage((GAME_WIDTH / 6 * 2 - 40, 450), (80, 80), BUTTON_SECRET, GameSecret1) pgImage((GAME_WIDTH / 6 * 3 - 40, 450), (80, 80), BUTTON_HOF, GameHOF) pgImage((GAME_WIDTH / 6 * 4 - 40, 450), (80, 80), BUTTON_SETTING, GameSetting) pgImage((GAME_WIDTH / 6 * 5 - 40, 450), (80, 80), BUTTON_EXIT, GameQuit) # refresh tampilan game pg.display.update() gameClock.tick(15)
def pgText(text, pos, font_size, font_name=GAME_FONT): """Menulis teks rata tengah warna hitam pada layar.""" font = pg.font.Font(font_name, font_size) textSurf = font.render(text, True, adt.color_use("black")) textRect = textSurf.get_rect() textRect.center = pos gameDisplay.blit(textSurf, textRect)
def __init__(self, GAME_WIDTH, GAME_HEIGHT): # Game sprites groups self.sprites_list = pg.sprite.Group() self.pos_sprites_list = pg.sprite.Group() # Game constants self.BOARD_WIDTH = GAME_WIDTH / 2 self.BOARD_HEIGHT = 50 self.BOARD_X = GAME_WIDTH * 0.25 self.BOARD_Y = GAME_HEIGHT - 55 self.POS_WIDTH = 20 self.POS_HEIGHT = 200 self.DISC_WIDTH = 200 self.DISC_HEIGHT = self.POS_WIDTH # pos and discs lists self.pos = [] self.discs = [] # Draw the game board and it's pos self.game_board = Block(adt.color_use("board_color"), self.BOARD_WIDTH, self.BOARD_HEIGHT) self.game_board.rect.x = self.BOARD_X self.game_board.rect.y = self.BOARD_Y first_pos = Position(0, adt.color_use("board_color"), self.POS_WIDTH, self.POS_HEIGHT) first_pos.rect.x = self.BOARD_X first_pos.rect.y = self.BOARD_Y - self.POS_HEIGHT second_pos = Position(1, adt.color_use("board_color"), self.POS_WIDTH, self.POS_HEIGHT) second_pos.rect.x = (self.BOARD_X + (self.BOARD_WIDTH / 2)) - (self.POS_WIDTH / 2) second_pos.rect.y = self.BOARD_Y - self.POS_HEIGHT third_pos = Position(2, adt.color_use("board_color"), self.POS_WIDTH, self.POS_HEIGHT) third_pos.rect.x = (self.BOARD_X + self.BOARD_WIDTH) - self.POS_WIDTH third_pos.rect.y = self.BOARD_Y - self.POS_HEIGHT self.pos = [first_pos, second_pos, third_pos] self.sprites_list.add([self.game_board, self.pos]) self.pos_sprites_list.add(self.pos)
def __init__(self, text, color, width, height, text_color=adt.color_use("black"), text_size=30, text_font=GAME_FONT): Block.__init__(self, color, width, height) self.text = text self.text_color = text_color self.font = pg.font.SysFont(text_font, text_size, False, False) self.text_render = self.font.render(text, 1, text_color) self.value = None
def GameInstruction(): """bagian cara bermain""" global game_dict while True: for event in pg.event.get(): if event.type == pg.QUIT: GameQuit() gameDisplay.fill(adt.color_use('background')) pgImage((10, 10), (80, 80), BUTTON_BACK, GameHistory) pgText(game_dict[lang]['instruction'], (GAME_WIDTH / 2, 50), 50) pgParagraf(game_dict[lang]['instruction_text'], (GAME_WIDTH / 2 - 250, 100, 500, GAME_HEIGHT)) pgButton((game_dict[lang]['play_now']), (GAME_WIDTH / 2 - 100, GAME_HEIGHT - 100), (200, 50), adt.color_use('button_ok'), adt.color_use('button_ok_hover'), GamePlay) # refresh tampilan game pg.display.update() gameClock.tick(15)
def GameHOF(): """Menu utama dari game""" teks_hof = adt.algo_makeHOF(game_data['hof']) while True: for event in pg.event.get(): if event.type == pg.QUIT: GameQuit() gameDisplay.fill(adt.color_use('background')) pgImage((10, 10), (80, 80), BUTTON_BACK, GameHomePage) pgText(game_dict[lang]['highscore'], (GAME_WIDTH / 2, 50), 50) pgParagraf(teks_hof, (GAME_WIDTH / 2 - 250, 100, 500, GAME_HEIGHT)) # refresh tampilan game pg.display.update() gameClock.tick(15)
def GameAbout(): """Menu utama dari game""" while True: for event in pg.event.get(): if event.type == pg.QUIT: GameQuit() gameDisplay.fill(adt.color_use('background')) pgImage((10, 10), (80, 80), BUTTON_BACK, GameHomePage) pgText(game_dict[lang]['about'], (GAME_WIDTH / 2, 50), 50) pgParagraf(game_dict[lang]['about_text'], (GAME_WIDTH / 2 - 250, 100, 500, GAME_HEIGHT)) pgParagraf(game_dict[lang]['citation_text'], (GAME_WIDTH / 2 - 250, 350, 500, GAME_HEIGHT)) # refresh tampilan game pg.display.update() gameClock.tick(15)
def GameSetting(): """Menu utama dari game""" global game_data global lang while True: for event in pg.event.get(): if event.type == pg.QUIT: GameQuit() gameDisplay.fill(adt.color_use('background')) pgImage((10, 10), (80, 80), BUTTON_BACK, GameHomePage) pgText(game_dict[lang]['settings'], (GAME_WIDTH / 2, 50), 50) pgText(game_dict[lang]['sound'], (GAME_WIDTH / 2 - 200, GAME_HEIGHT / 2 - 50), 40) pgText(game_dict[lang]['language'], (GAME_WIDTH / 2 - 200, GAME_HEIGHT / 2 + 50), 40) # Slider untuk music game_data['sound'] = pgSlider( (GAME_WIDTH / 2 + 200, GAME_HEIGHT / 2 - 75), game_data['sound']) pgMusic() # Slider untuk bahasa game_data['lang'] = pgSlider( (GAME_WIDTH / 2 + 200, GAME_HEIGHT / 2 + 25), game_data['lang']) if game_data['lang']: # True berarti memilih Bahasa Inggris lang = 'en' else: lang = 'id' # Simpan semua setting ke database adt.data_write(game_data) # refresh tampilan game pg.display.update() gameClock.tick(15)
def __init__(self, GAME_WIDTH, GAME_HEIGHT): game_data = adt.data_read() # baca database if game_data['lang']: lang = 'en' else: lang = 'id' self.GAME_WIDTH = GAME_WIDTH self.GAME_HEIGHT = GAME_HEIGHT self.sprites_list = pg.sprite.Group() self.btn_discs = [] self.label = Button(game_dict[lang]['number_discs'], adt.color_use("background"), GAME_WIDTH / 2, GAME_HEIGHT / 3 - 150) self.label.rect.x = self.GAME_WIDTH / 4 self.label.rect.y = self.GAME_HEIGHT / 2 - 60 # pilihan menu for i in range(3, 8): btn = Button(str(i), adt.color_use("button_ok"), 20, 30) btn.rect.x = self.GAME_WIDTH / 3 + 50 * (i - 2) - 12 btn.rect.y = self.GAME_HEIGHT / 2 btn.render_text() btn.set_value(i) self.btn_discs.append(btn) self.sprites_list.add(self.btn_discs) self.label.render_text() self.sprites_list.add(self.label) # Game over buttons self.btn_play_again = Button(game_dict[lang]['play_again'], adt.color_use("button_ok"), len(game_dict[lang]['play_again']) * 14, 30) self.btn_return = Button(game_dict[lang]['change_disc'], adt.color_use("button_normal"), len(game_dict[lang]['change_disc']) * 12, 30) self.btn_quit = Button(game_dict[lang]['main_menu'], adt.color_use("button_normal"), len(game_dict[lang]['main_menu']) * 14, 30) self.btn_play_again.rect.x = self.GAME_WIDTH / 2 - ( self.btn_return.image.get_width()) - 25 self.btn_play_again.rect.y = self.GAME_HEIGHT / 2 - 40 self.btn_play_again.render_text() self.btn_return.rect.x = self.GAME_WIDTH / 2 - self.btn_return.image.get_width( ) / 2 self.btn_return.rect.y = self.GAME_HEIGHT / 2 - 40 self.btn_return.render_text() self.btn_quit.rect.x = self.GAME_WIDTH / 2 + ( self.btn_return.image.get_width()) / 2 + 14 self.btn_quit.rect.y = self.GAME_HEIGHT / 2 - 40 self.btn_quit.render_text()
def GamePlay(): """literally game hanoi-nya.""" global username # Create main menu object menu = models.MainMenu(GAME_WIDTH, GAME_HEIGHT) # Create game object game = models.Game(GAME_WIDTH, GAME_HEIGHT) # Discs' move variables done = False drag = False drop = False move = False game_over = False init_game = False disc_index = None last_pos = [0, 0] # Moves counter moves_counter = 0 play_time = 0 # -------- Main Game Loop ----------- while not done: # --- Main event loop for event in pg.event.get(): if event.type == pg.QUIT: done = True elif event.type == pg.MOUSEBUTTONDOWN: drag = True drop = False if init_game: if not game_over: for i in range(0, game.n_discs): if game.discs[i].is_clicked(): current_pos = game.discs[i].current_pos pos_length = len(game.pos[current_pos].discs) if game.discs[i] == game.pos[ current_pos].discs[pos_length - 1]: disc_index = i last_pos = [ game.discs[i].rect.x, game.discs[i].rect.y ] move = True else: # ranking hasil user game_data['hof'] = adt.algo_addSorted( game_data['hof'], game.n_discs, [username, play_time, moves_counter]) # masukkan ke database adt.data_write(game_data) if menu.btn_quit.is_clicked(): done = True GameHomePage() if menu.btn_play_again.is_clicked(): game.sprites_list.remove(game.discs) game.pos[2].discs = [] moves_counter = 0 game.discs = [] game.draw_discs() game_over = False if menu.btn_return.is_clicked(): menu.sprites_list.remove([ menu.btn_play_again, menu.btn_return, menu.btn_quit ]) menu.sprites_list.add([menu.btn_discs, menu.label]) game.sprites_list.remove(game.discs) init_game = False else: start_ticks = pg.time.get_ticks( ) # Waktu semenjak pg.init() dimulai for i in range(0, len(menu.btn_discs)): if menu.btn_discs[i].is_clicked(): game.set_n_discs(menu.btn_discs[i].value) game.sprites_list.remove(game.discs) game.discs = [] game.pos[2].discs = [] moves_counter = 0 game.draw_discs() init_game = True game_over = False break elif event.type == pg.MOUSEBUTTONUP: drag = False drop = True gameDisplay.fill(adt.color_use("background")) pgImage((10, 10), (80, 80), BUTTON_BACK, GameHomePage) if init_game: pgImage((10, 10), (80, 80), BUTTON_BACK, GameHomePage) pgText( game_dict[lang]['play_info'].format(str(moves_counter), str(round(play_time, 1))), (GAME_WIDTH / 2, 120), 20) if game_over: start_ticks = pg.time.get_ticks( ) # Waktu semenjak pg.init() dimulai part 2, bisa lebih efisien enggak ya? menu.sprites_list.draw(gameDisplay) if len(game.pos[2].discs) == game.n_discs: if moves_counter == game.min_moves: pgText(game_dict[lang]['congrats_min'], (GAME_WIDTH / 2, GAME_HEIGHT / 3), 20) else: pgText(game_dict[lang]['congrats_aja'], (GAME_WIDTH / 2, GAME_HEIGHT / 3), 20) else: play_time = (pg.time.get_ticks() - start_ticks) / 1000 # Actual timer if drag: if move: pos = pg.mouse.get_pos() game.discs[disc_index].rect.x = pos[0] - ( game.discs[disc_index].width / 2) game.discs[disc_index].rect.y = pos[1] - ( game.discs[disc_index].height / 2) elif drop: if move: current_pos = game.discs[disc_index].current_pos new_pos = None change = False turn_back = True position = pg.sprite.spritecollideany( game.discs[disc_index], game.pos_sprites_list) if position is not None: new_pos = position.pos_index if new_pos != current_pos: disc_length = len(position.discs) if disc_length == 0: turn_back = False change = True elif game.discs[disc_index].id > position.discs[ disc_length - 1].id: turn_back = False change = True if change: moves_counter = moves_counter + 1 game.pos[current_pos].discs.remove( game.discs[disc_index]) game.discs[disc_index].current_pos = new_pos game.pos[new_pos].discs.append( game.discs[disc_index]) new_pos_length = len(game.pos[new_pos].discs) game.discs[disc_index].rect.x = game.pos[new_pos].rect.x - \ ((game.DISC_WIDTH/(game.discs[disc_index].id+1)/2)-(game.DISC_HEIGHT/2)) game.discs[disc_index].rect.y = ( game.BOARD_Y - game.DISC_HEIGHT) - (game.DISC_HEIGHT * (new_pos_length - 1)) # Check if the game is over if len(game.pos[2].discs) == game.n_discs: game_over = True menu.sprites_list.add([ menu.btn_play_again, menu.btn_quit, menu.btn_return ]) menu.sprites_list.remove( [menu.label, menu.btn_discs]) # ngatur kalau dilepas diudara if turn_back: game.discs[disc_index].rect.x = last_pos[0] game.discs[disc_index].rect.y = last_pos[1] move = False game.sprites_list.draw(gameDisplay) else: menu.sprites_list.draw(gameDisplay) # update gameDisplay. pg.display.flip() gameClock.tick(60) pg.quit()