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 screen(tetro, field, next, points, level): """ Prints the ingame screen to the terminal, consisting of the matrix, size-, points- and level sections, and the active tetromino. """ ground(field) nextsection(field, next) valsection(field, len(field), 1, "SIZE:") valsection(field, points, len(field) - 1, "PTS:") valsection(field, level, len(field) - 4, "LVL:") tetro.print() pyconio.flush()
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 logo(file="logo.txt"): """ Prints the content of the given file under logos/ (or logo.txt as default), used for printing the title Tetris (in Russian), with the original colors. To use different colors for printing, a pipe character must be used as separator between different sections. """ pyconio.gotoxy(0, 5) colors = [ pyconio.RED, pyconio.BROWN, pyconio.YELLOW, pyconio.GREEN, pyconio.CYAN, pyconio.MAGENTA ] with open("logos/{}".format(file), "rt", encoding="utf-8") as logo: for line in logo: letters = line.rstrip("\n").split("|") for letter in range(len(letters)): pyconio.textcolor(colors[letter]) pyconio.write(letters[letter]) pyconio.write("\n") pyconio.flush() pyconio.textbackground(pyconio.RESET) pyconio.textcolor(pyconio.RESET)
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()
def run(self): if self.gameState == 0: self.j.mozgat() for e in self.enemies: tav_jatekostol = tavolsag((self.j.x, self.j.y), (e.x, e.y)) if e.state == 1: if tav_jatekostol >= 10: e.state = 0 else: if tav_jatekostol <= 2.2: self.fut = csata(self.j, e) self.k.kirajzol() if not self.fut: break elif tav_jatekostol <= 8: e.target = Coord(self.j.x, self.j.y) if e.state == -1: e.meghal() self.enemies.remove(e) self.k.kirajzol() elif e.state == 0: e.mozgat() self.k.render() if keyboard.is_pressed('f'): if self.j.amin_all == '☼': valasz = kerdes( 'You have found {}! Do you want to change your {}?'. format( self.dolgok.at((round(self.j.x), round(self.j.y))), self.j.fegyver)) if valasz: self.j.fegyver = self.dolgok.at( (round(self.j.x), round(self.j.y))) self.dolgok.kivesz((round(self.j.x), round(self.j.y))) self.j.amin_all = ' ' self.k.kirajzol() self.j.statok() elif self.j.amin_all == 'X': r = random.randint(0, len(uzenetek_halott_ellenfel) - 1) uzenet(uzenetek_halott_ellenfel[r]) self.k.kirajzol() if keyboard.is_pressed('m'): self.gameState = 1 self.k.terkep_kirajzol(11, 1, self.j, True) elif self.gameState == 1: self.timers[0].tick() if self.timers[0].value == self.timers[0].max - 1: if self.timers[0].bool: self.k.terkep_kirajzol(11, 1, self.j, False) else: self.k.terkep_kirajzol(11, 1, self.j, True) if keyboard.is_pressed('backspace'): self.k.kirajzol() self.gameState = 0 if keyboard.is_pressed('escape'): valasz = kerdes( 'You sure you want to quit the game? You can\'t save progress yet!' ) if valasz: self.fut = False else: self.k.kirajzol() flush()