def add_score(score): """ Prompts the user to input their name, and appends the created Score object to its proper position in the scorelist. Returns None if the user decided not to save their score, or otherwise the scorelist. """ pyconio.clrscr() if save_topscore() == "continue": draw.logo() pyconio.gotoxy(13, 22) pyconio.normalmode() pyconio.write("Add meg a neved (ne használj ':'-ot):", end="\n") draw.cursor(True) pyconio.flush() name = input(" ") while ":" in name: pyconio.write("Hibás név, kérlek add meg ':' nélkül.", end="\n") name = input(" ") draw.cursor(False) pyconio.rawmode() scorelist = get_scores() if scorelist is not None: scorelist.append(Score(name, score)) scorelist.sort(key=int, reverse=True) if len(scorelist) > 5: scorelist.pop(-1) elif scorelist == -1: pyconio.gotoxy(15, 26) pyconio.write("Hibás file, ellenőrizd a pontszámokat!") else: scorelist = [Score(name, score)] return scorelist else: return None
def setting_adjust(setting, label, min_val, max_val, delta=1): """ Used in the options menu, lets the user change the default options with the UP and DOWN keys. The actual setting, its label, value interval (and granularity) should be given as parameters. """ draw.logo() pyconio.textbackground(pyconio.RESET) pyconio.textcolor(pyconio.RESET) pyconio.gotoxy(20, 20) pyconio.write("{}: {:3}".format(label, setting), flush=True) pyconio.gotoxy(15, 29) pyconio.write("Irányítás: ↑ ↓ ENTER ESC") initial = setting pyconio.rawmode() key = pyconio.getch() while key != pyconio.ENTER: if key == pyconio.UP: if setting < max_val: setting += delta elif key == pyconio.DOWN: if setting > min_val: setting -= delta elif key == pyconio.ESCAPE: setting = initial break pyconio.gotoxy(20, 20) pyconio.write("{}: {:3}".format(label, setting), flush=True) key = pyconio.getch() return setting
def menu(buttons, data=None): """ Common menu mechanism, including the indication of the selected button and navigation. Returns None if the user selected quit (either by pressing ESC or selecting the option), or executes the selected function otherwise, with optional data. """ pyconio.textcolor(pyconio.WHITE) buttons[0].active = True draw.logo() while True: for btn in buttons: if btn.active: pyconio.textbackground(pyconio.WHITE) pyconio.textcolor(pyconio.BLACK) else: pyconio.textbackground(pyconio.RESET) pyconio.textcolor(pyconio.RESET) pyconio.gotoxy(20, 20 + buttons.index(btn)) pyconio.write(btn) pyconio.gotoxy(15, 29) pyconio.textcolor(pyconio.RESET) pyconio.textbackground(pyconio.RESET) pyconio.write("Irányítás: ↑ ↓ ENTER ESC") pyconio.flush() pyconio.rawmode() key = pyconio.getch() active = buttons.index([x for x in buttons if x.active][0]) if key == pyconio.ENTER: return select(buttons[active].function, data) elif key == pyconio.DOWN: buttons[active].active = False if active == len(buttons) - 1: buttons[0].active = True else: buttons[active + 1].active = True elif key == pyconio.UP: buttons[active].active = False if active == 0: buttons[-1].active = True else: buttons[active - 1].active = True elif key == pyconio.ESCAPE: return select("quit")
def list_scores(scorelist, pos, data=None): """ Lists the scores from the given scorelist to the given position. Adds numbering and colors (for the top 3) to the list elements, and prints out the proper errors when needed. Returns to the main menu, with optional data if passed. """ pyconio.clrscr() draw.logo() pyconio.gotoxy(pos[0], pos[1]) pyconio.textcolor(pyconio.RESET) if scorelist is None: pyconio.gotoxy(pos[0] - 15, pos[1]) pyconio.write("A file nem található, biztosan játszottál már?") elif scorelist == -1: pyconio.gotoxy(pos[0] - 10, pos[1]) pyconio.write("Hibás file, ellenőrizd a pontszámokat!") else: for i in range(len(scorelist)): if i + 1 == 1: pyconio.textcolor(pyconio.YELLOW) elif i + 1 == 2: pyconio.textcolor(pyconio.LIGHTGRAY) elif i + 1 == 3: pyconio.textcolor(pyconio.BROWN) else: pyconio.textcolor(pyconio.RESET) pyconio.gotoxy(pos[0], pos[1] + i) pyconio.write("{}. {}".format(i + 1, scorelist[i])) pyconio.gotoxy(pos[0] + 2, pos[1] + pos[1] // 2) pyconio.textcolor(pyconio.RESET) pyconio.write("Vissza: ESC") pyconio.flush() pyconio.rawmode() key = pyconio.getch() while key != pyconio.ESCAPE: key = pyconio.getch() pyconio.clrscr() return main_menu(data)
def mainloop(tetro, field, next, points=0, level=1): """ Prints the field and the currently active tetromino to its given position. While ingame, you can control it with UP-DOWN-LEFT-RIGHT as in Tetris. Pause the loop with the ESC key, if you choose continue, the current loop ends, and is called again with the current parameters. """ game_sec = time.time() draw.screen(tetro, field, next, points, level) ingame = (True, 0) with pyconio.rawkeys(): while ingame[0]: current_sec = time.time() # Control mechanism if pyconio.kbhit(): ingame = control.ingame(tetro, field) points += ingame[1] * level draw.screen(tetro, field, next, points, level) # Fall mechanism if round(current_sec, min(level, 2)) >= round( game_sec, min(level, 2)): if control.move_valid(control.post_move(tetro, "down"), field): if control.hit(control.post_move(tetro, "down"), field): if tetro.pos[1] >= 1: last = tetro next.pos = [5, 0] tetro = next next = control.store_regen(last, field, next) # Game over if tetro hits another one, while Y pos is <1 else: ingame = (False, points) pyconio.clrscr() draw.logo("game_over.txt") time.sleep(1) # Add to top scores if needed scores = menu.get_scores() # If highscores.txt exists, is valid and not empty if scores not in (None, -1, []): if points > scores[-1].points: scorechoice = menu.add_score(points) if scorechoice is not None: menu.write_score(scorechoice) # If invalid elif scores == -1: pyconio.gotoxy(12, 25) pyconio.write("Hibás dicsőséglista!", end="\n") pyconio.gotoxy(8, 26) pyconio.write("Ellenőrizd a highscores.txt-t!") pyconio.flush() time.sleep(2.5) # If doesn't exist yet or empty else: scorechoice = menu.add_score(points) if scorechoice is not None: menu.write_score(scorechoice) return main() else: tetro.pos[1] += 1 # When hit, save tetro and generate new one. else: last = tetro next.pos = [len(field) // 4, 0] tetro = next next = control.store_regen(last, field, next) # Game ticks game_sec += control.speed_sec(level) draw.screen(tetro, field, next, points, level) # Line clear if control.line_full(field): points += control.delete_full(field) * level draw.screen(tetro, field, next, points, level) # Level up if points >= level**2 * 1000: level += 1 draw.screen(tetro, field, next, points, level) # Pause pausechoice = menu.pause() if pausechoice == "save": control.save_game(tetro, field, next, points, level) pausechoice = menu.save_menu() if pausechoice == "continue": return mainloop(tetro, field, next, points, level) elif pausechoice is None: return main()