From 9f2b0fde42f6923eda508550284ece85615e7cf4 Mon Sep 17 00:00:00 2001 From: adithyagenie Date: Thu, 24 Nov 2022 13:16:04 +0530 Subject: [PATCH] Snake fully merged with main project --- maze/menu.py | 91 ++++++++++++++ maze/modules/PlayerBase_func.py | 19 +-- maze/modules/about.py | 8 +- maze/modules/maze.py | 203 +++++++++++++++++-------------- maze/modules/maze_saveandload.py | 2 +- snake.py | 27 ++-- 6 files changed, 240 insertions(+), 110 deletions(-) create mode 100644 maze/menu.py diff --git a/maze/menu.py b/maze/menu.py new file mode 100644 index 0000000..5d890d7 --- /dev/null +++ b/maze/menu.py @@ -0,0 +1,91 @@ +import curses +import maze.modules.maze as m1 +import maze.modules.maze_saveandload as sl +import maze.modules.PlayerBase_func as database +from maze.modules.about import about +import time +import sys +import snake + + +def menu(screen): + exit = False + y, x = screen.getmaxyx() + screen.clear() + screen.nodelay(True) + screen.refresh() + text = """ + \t\t\t██ █████ ██████ ██ ██ ██████ ██ ███ ██ ████████ ██ ██ + \t\t\t██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ + \t\t\t██ ███████ ██████ ████ ██████ ██ ██ ██ ██ ██ ███████ + \t\t\t██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ + \t\t\t███████ ██ ██ ██████ ██ ██ ██ ██ ██ ████ ██ ██ ██""" + + screen.addstr(1, 5, str(text)) + screen.addstr(10, x // 2 - 2, "MENU") + screen.addstr(13, 1, "space - Play") + screen.addstr(14, 1, "f - Load game from file") + screen.addstr(15, 1, "a - Account Settings") + screen.addstr(16, 1, "l - Leaderboard") + screen.addstr(17, 1, "x - About") + screen.addstr(18, 1, "esc - Quit") + screen.border() + while True: + if exit: + break + key = screen.getch() + if key == ord(" "): + screen.clear() + screen.refresh() + screen.border() + screen.addstr(1, x // 2 - 2, "PLAY") + screen.addstr(y // 2 - 4, x // 2 - 2, "1. MAZE") + screen.addstr(y // 2 - 2, x // 2 - 2, "2. PONG") + screen.addstr(y // 2, x // 2 - 2, "3. SNAKE") + screen.addstr(y // 2 + 2, x // 2 - 2, "4. WORDLE") + while True: + key2 = screen.getch() + if key2 == ord("1"): + m1.play(screen) + elif key2 == ord("2"): + pass + elif key2 == ord("3"): + snake.main(screen) + elif key2 == ord("4"): + pass + elif key2 == 27: + menu(screen) + break + elif key == 27: + screen.clear() + screen.refresh() + screen.border() + screen.addstr(y // 2 - 5, x // 2 - 5, "THANK YOU!") + while True: + breakkey = screen.getch() + if breakkey: + time.sleep(1) + sys.exit() + elif key == ord("a"): + database.screenhandler(screen) + elif key == ord("l"): + database.leaderboard(screen) + elif key == ord("x"): + about(screen) + elif key == ord("f"): + present = sl.check() + if present: + maze = sl.load(screen) + if maze: + m1.play(screen, maze[0], maze[1], maze[2]) + return + else: + screen.addstr( + 20, 5, "No saved mazes present. Press enter to continue..." + ) + while True: + key2 = screen.getch() + if key2 == 10: + screen.addstr(20, 5, " " * (x - 10)) + break + diff --git a/maze/modules/PlayerBase_func.py b/maze/modules/PlayerBase_func.py index 73816a8..4343ddd 100644 --- a/maze/modules/PlayerBase_func.py +++ b/maze/modules/PlayerBase_func.py @@ -10,7 +10,7 @@ from time import sleep import mysql.connector -import maze.modules.maze +import maze.menu from .password_forget import sender @@ -139,7 +139,7 @@ def screenhandler(screen): # MAIN MENU elif loggedin: logout(screen) elif key == 27: - maze.modules.maze.menu(screen) + maze.menu.menu(screen) break screen.refresh() @@ -161,6 +161,7 @@ def input( ): # Function to get type-able inputs, with delete, esc and other keys inputted = "" orig_y, orig_x = y, x + screen.nodelay(False) while True: key = screen.getch() if key == 10: @@ -174,7 +175,7 @@ def input( global quitting quitting = True break - else: + elif key != -1: inputted += chr(key) if ispassword: screen.addstr(y, x, "*") @@ -194,7 +195,7 @@ def list_getter(field): # Feed in the field name you want, get all records of i return return_list -def login(screen, calledby=False): # Function to log in +def login(screen, calledby=None): # Function to log in global quitting, U, gamerid, loggedin screen.clear() screen.refresh() @@ -220,7 +221,7 @@ def login(screen, calledby=False): # Function to log in while True: key = screen.getch() if key == ord("y"): - if calledby: + if calledby is not None: new_add(screen, calledby=calledby) else: new_add(screen) @@ -252,7 +253,7 @@ def login(screen, calledby=False): # Function to log in gamerid = res[0][1] U = inputU screen.addstr(y // 2 + 2, x // 2 - 4, "Login Successful!") - if calledby: + if calledby is not None: screen.addstr(y // 2 + 3, x // 2 - 4, "Updating score...") screen.refresh() sleep(3) @@ -389,7 +390,7 @@ def email(screen, sy, sx, optionaltxt="Enter Email: "): # Function to accept em return email -def new_add(screen, calledby=False): +def new_add(screen, calledby=None): screen.clear() screen.refresh() screen.border() @@ -428,7 +429,7 @@ def new_add(screen, calledby=False): '{add_password}')" ) screen.refresh() - if calledby: + if calledby is not None: screen.addstr( y // 2 + 2, 5, "Account has been created. Returning to login..." ) @@ -802,5 +803,5 @@ def leaderboard(screen): if key == 27: break screen.refresh() - maze.modules.maze.menu(screen) + maze.menu.menu(screen) return diff --git a/maze/modules/about.py b/maze/modules/about.py index 08b9e2d..cee5c21 100644 --- a/maze/modules/about.py +++ b/maze/modules/about.py @@ -1,4 +1,4 @@ -import maze.modules.maze +import maze.menu def about(screen): @@ -45,11 +45,11 @@ def about(screen): ) screen.addstr(20, 5, "Signing off,") screen.addstr(21, 5, "The Labyrinth") - screen.addstr(y - 2, x - 33, "Press Enter to exit this screen.") + screen.addstr(y - 2, x - 31, "Press Esc to exit this screen.") screen.refresh() while True: key = screen.getch() - if key == 10: + if key == 27: break - maze.modules.maze.menu(screen) + maze.menu.menu(screen) return diff --git a/maze/modules/maze.py b/maze/modules/maze.py index 9a5c058..e81939b 100644 --- a/maze/modules/maze.py +++ b/maze/modules/maze.py @@ -8,6 +8,7 @@ from math import cos, exp, fabs, pi import maze.modules.maze_saveandload as sl import maze.modules.PlayerBase_func as database +from maze.menu import menu from .about import about @@ -252,8 +253,8 @@ def pathfinding_demo( reset(finish, cell, curses.color_pair(2)) reset(start, (0, 0), curses.color_pair(2)) else: - # current_coords = [maxy - 5, maxx - 27] - current_coords = [1, 1] + current_coords = [maxy - 5, maxx - 27] + #current_coords = [1, 1] screen.addstr(current_coords[0], current_coords[1], "█", curses.color_pair(2)) WALL = ["═", "║", "╗", "╚", "╝", "╔", "╠", "╣", "╦", "╩", "╬", "═", "═", "║", "║"] pause_elapsed = 0 @@ -409,67 +410,97 @@ def pathfinding_demo( # print(screen.instr(current_coords[0],current_coords[1]+1,1).decode("utf-8"), "RSIDE PRESS") -def menu(screen): - y, x = screen.getmaxyx() - screen.clear() - screen.refresh() - text = """ -\t\t\t██ █████ ██████ ██ ██ ██████ ██ ███ ██ ████████ ██ ██ -\t\t\t██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ -\t\t\t██ ███████ ██████ ████ ██████ ██ ██ ██ ██ ██ ███████ -\t\t\t██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -\t\t\t███████ ██ ██ ██████ ██ ██ ██ ██ ██ ████ ██ ██ ██""" +# def menu(screen): +# y, x = screen.getmaxyx() +# screen.clear() +# screen.refresh() +# text = """ +# \t\t\t██ █████ ██████ ██ ██ ██████ ██ ███ ██ ████████ ██ ██ +# \t\t\t██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ +# \t\t\t██ ███████ ██████ ████ ██████ ██ ██ ██ ██ ██ ███████ +# \t\t\t██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +# \t\t\t███████ ██ ██ ██████ ██ ██ ██ ██ ██ ████ ██ ██ ██""" +# +# screen.addstr(1, 5, str(text)) +# screen.addstr(10, x // 2 - 2, "MENU") +# screen.addstr(13, 1, "space - Play") +# screen.addstr(14, 1, "f - Load game from file") +# screen.addstr(15, 1, "a - Account Settings") +# screen.addstr(16, 1, "l - Leaderboard") +# screen.addstr(17, 1, "x - About") +# screen.addstr(18, 1, "esc - Quit") +# screen.border() +# while True: +# key = screen.getch() +# if key == ord(" "): +# play(screen) +# elif key == 27: +# screen.clear() +# screen.refresh() +# screen.border() +# screen.addstr(y // 2 - 5, x // 2 - 5, "THANK YOU!") +# while True: +# breakkey = screen.getch() +# if breakkey: +# time.sleep(1) +# sys.exit() +# elif key == ord("a"): +# database.screenhandler(screen) +# elif key == ord("l"): +# database.leaderboard(screen) +# elif key == ord("x"): +# about(screen) +# elif key == ord("f"): +# present = sl.check() +# if present: +# maze = sl.load(screen) +# if maze: +# play(screen, maze[0], maze[1], maze[2]) +# return +# else: +# screen.addstr( +# 20, 5, "No saved mazes present. Press enter to continue..." +# ) +# while True: +# key2 = screen.getch() +# if key2 == 10: +# screen.addstr(20, 5, " " * (x - 10)) +# break - screen.addstr(1, 5, str(text)) - screen.addstr(10, x // 2 - 2, "MENU") - screen.addstr(13, 1, "space - Play") - screen.addstr(14, 1, "f - Load game from file") - screen.addstr(15, 1, "a - Account Settings") - screen.addstr(16, 1, "l - Leaderboard") - screen.addstr(17, 1, "x - About") - screen.addstr(18, 1, "esc - Quit") - screen.border() - while True: - key = screen.getch() - if key == ord(" "): - play(screen) - elif key == 27: - screen.clear() - screen.refresh() - screen.border() - screen.addstr(y // 2 - 5, x // 2 - 5, "THANK YOU!") - while True: - breakkey = screen.getch() - if breakkey: - time.sleep(1) - sys.exit() - elif key == ord("a"): - database.screenhandler(screen) - elif key == ord("l"): - database.leaderboard(screen) - elif key == ord("x"): - about(screen) - elif key == ord("f"): - present = sl.check() - if present: - maze = sl.load(screen) - if maze: - play(screen, maze[0], maze[1], maze[2]) - return - else: - screen.addstr( - 20, 5, "No saved mazes present. Press enter to continue..." - ) - while True: - key2 = screen.getch() - if key2 == 10: - screen.addstr(20, 5, " " * (x - 10)) - break - - -def play(screen, loadedmaze=None, loadedcoords=None, loadedtime=0): +def play(screen, loadedmaze=None, loadedcoords=None, loadedtime=0, executeguest = False, outerscore = 0): y, x = screen.getmaxyx() height, width = int((y - 2) / 2), int((x - 2) / 2) + def guestswitch(score): + screen.clear() + screen.refresh() + screen.addstr( + y // 2 - 5, x // 2 - 8, str("Your score is: " + str(int(score))) + ) + res = database.Update_score(int(score)) + if res == "guest": + screen.addstr( + height - 1, + 5, + "You are not signed in. You will lose your score if you proceed.", + ) + screen.addstr( + height, 5, "Do you want to login and save your progress? (y/n)" + ) + while True: + key = screen.getch() + if key == ord("y"): + database.login(screen, calledby=int(score)) + break + elif key == ord("n"): + break + screen.clear() + screen.refresh() + menu(screen) + return + if executeguest: + guestswitch(outerscore) + return + screen.clear() screen.refresh() if not loadedmaze: @@ -500,9 +531,6 @@ def play(screen, loadedmaze=None, loadedcoords=None, loadedtime=0): came_out = 1 while True: if came_out != 0: - screen.clear() - screen.refresh() - screen.erase() global WON if WON != 0: tt = (start_ts - end_ts) / 300 @@ -511,31 +539,30 @@ def play(screen, loadedmaze=None, loadedcoords=None, loadedtime=0): WON = 0 else: score = 0 - screen.addstr( - y // 2 - 5, x // 2 - 8, str("Your score is: " + str(int(score))) - ) - res = database.Update_score(int(score)) - if res == "guest": - screen.addstr( - height - 1, - 5, - "You are not signed in. You will lose your score if you proceed.", - ) - screen.addstr( - height, 5, "Do you want to login and save your progress? (y/n)" - ) - while True: - key = screen.getch() - if key == ord("y"): - database.login(screen, calledby=int(score)) - break - elif key == ord("n"): - break - screen.clear() - screen.refresh() - came_out = 0 - menu(screen) - return + + guestswitch(score) + # res = database.Update_score(int(score)) + # if res == "guest": + # screen.addstr( + # height - 1, + # 5, + # "You are not signed in. You will lose your score if you proceed.", + # ) + # screen.addstr( + # height, 5, "Do you want to login and save your progress? (y/n)" + # ) + # while True: + # key = screen.getch() + # if key == ord("y"): + # database.login(screen, calledby=int(score)) + # break + # elif key == ord("n"): + # break + # screen.clear() + # screen.refresh() + # came_out = 0 + # menu(screen) + # return def main(screen): diff --git a/maze/modules/maze_saveandload.py b/maze/modules/maze_saveandload.py index efadbe3..d3eabc3 100644 --- a/maze/modules/maze_saveandload.py +++ b/maze/modules/maze_saveandload.py @@ -2,7 +2,7 @@ import os import pickle from time import sleep -import maze.modules.maze as m +import maze.menu as m from .PlayerBase_func import input, screenwipe diff --git a/snake.py b/snake.py index 31c796e..0ed5a2a 100644 --- a/snake.py +++ b/snake.py @@ -2,6 +2,8 @@ import curses import random import time from curses import textpad +import maze.menu +import maze.modules.maze as m OPPOSITE_DIRECTION_DICT = { curses.KEY_UP: curses.KEY_DOWN, @@ -31,7 +33,8 @@ def main(stdscr): curses.curs_set(0) stdscr.nodelay(1) stdscr.timeout(100) - + stdscr.clear() + stdscr.refresh() # create a game box sh, sw = stdscr.getmaxyx() box = [[3, 3], [sh - 3, sw - 3]] # [[ul_y, ul_x], [dr_y, dr_x]] @@ -58,6 +61,12 @@ def main(stdscr): # non-blocking input key = stdscr.getch() + if key == 27: + stdscr.clear() + stdscr.refresh() + time.sleep(1) + maze.menu.menu(stdscr) + return # set direction if user pressed any arrow key and that key is not opposite of current direction if key in DIRECTIONS_LIST and key != OPPOSITE_DIRECTION_DICT[direction]: direction = key @@ -103,10 +112,12 @@ def main(stdscr): ): msg = "Game Over!" stdscr.addstr(sh // 2, sw // 2 - len(msg) // 2, msg) - stdscr.nodelay(0) - stdscr.getch() - time.sleep(5) - break - - -curses.wrapper(main) + while stdscr.getch() == -1: + pass + time.sleep(2) + m.play(stdscr, executeguest=True, outerscore=score) + #Call play with guestcheck to update scores + stdscr.clear() + stdscr.refresh() + maze.menu.menu(stdscr) + return score