Snake fully merged with main project

This commit is contained in:
adithyagenie 2022-11-24 13:16:04 +05:30
parent d5ec6036c5
commit 9f2b0fde42
6 changed files with 240 additions and 110 deletions

91
maze/menu.py Normal file
View file

@ -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

View file

@ -10,7 +10,7 @@ from time import sleep
import mysql.connector import mysql.connector
import maze.modules.maze import maze.menu
from .password_forget import sender from .password_forget import sender
@ -139,7 +139,7 @@ def screenhandler(screen): # MAIN MENU
elif loggedin: elif loggedin:
logout(screen) logout(screen)
elif key == 27: elif key == 27:
maze.modules.maze.menu(screen) maze.menu.menu(screen)
break break
screen.refresh() screen.refresh()
@ -161,6 +161,7 @@ def input(
): # Function to get type-able inputs, with delete, esc and other keys ): # Function to get type-able inputs, with delete, esc and other keys
inputted = "" inputted = ""
orig_y, orig_x = y, x orig_y, orig_x = y, x
screen.nodelay(False)
while True: while True:
key = screen.getch() key = screen.getch()
if key == 10: if key == 10:
@ -174,7 +175,7 @@ def input(
global quitting global quitting
quitting = True quitting = True
break break
else: elif key != -1:
inputted += chr(key) inputted += chr(key)
if ispassword: if ispassword:
screen.addstr(y, x, "*") 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 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 global quitting, U, gamerid, loggedin
screen.clear() screen.clear()
screen.refresh() screen.refresh()
@ -220,7 +221,7 @@ def login(screen, calledby=False): # Function to log in
while True: while True:
key = screen.getch() key = screen.getch()
if key == ord("y"): if key == ord("y"):
if calledby: if calledby is not None:
new_add(screen, calledby=calledby) new_add(screen, calledby=calledby)
else: else:
new_add(screen) new_add(screen)
@ -252,7 +253,7 @@ def login(screen, calledby=False): # Function to log in
gamerid = res[0][1] gamerid = res[0][1]
U = inputU U = inputU
screen.addstr(y // 2 + 2, x // 2 - 4, "Login Successful!") 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.addstr(y // 2 + 3, x // 2 - 4, "Updating score...")
screen.refresh() screen.refresh()
sleep(3) sleep(3)
@ -389,7 +390,7 @@ def email(screen, sy, sx, optionaltxt="Enter Email: "): # Function to accept em
return email return email
def new_add(screen, calledby=False): def new_add(screen, calledby=None):
screen.clear() screen.clear()
screen.refresh() screen.refresh()
screen.border() screen.border()
@ -428,7 +429,7 @@ def new_add(screen, calledby=False):
'{add_password}')" '{add_password}')"
) )
screen.refresh() screen.refresh()
if calledby: if calledby is not None:
screen.addstr( screen.addstr(
y // 2 + 2, 5, "Account has been created. Returning to login..." y // 2 + 2, 5, "Account has been created. Returning to login..."
) )
@ -802,5 +803,5 @@ def leaderboard(screen):
if key == 27: if key == 27:
break break
screen.refresh() screen.refresh()
maze.modules.maze.menu(screen) maze.menu.menu(screen)
return return

View file

@ -1,4 +1,4 @@
import maze.modules.maze import maze.menu
def about(screen): def about(screen):
@ -45,11 +45,11 @@ def about(screen):
) )
screen.addstr(20, 5, "Signing off,") screen.addstr(20, 5, "Signing off,")
screen.addstr(21, 5, "The Labyrinth") 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() screen.refresh()
while True: while True:
key = screen.getch() key = screen.getch()
if key == 10: if key == 27:
break break
maze.modules.maze.menu(screen) maze.menu.menu(screen)
return return

View file

@ -8,6 +8,7 @@ from math import cos, exp, fabs, pi
import maze.modules.maze_saveandload as sl import maze.modules.maze_saveandload as sl
import maze.modules.PlayerBase_func as database import maze.modules.PlayerBase_func as database
from maze.menu import menu
from .about import about from .about import about
@ -252,8 +253,8 @@ def pathfinding_demo(
reset(finish, cell, curses.color_pair(2)) reset(finish, cell, curses.color_pair(2))
reset(start, (0, 0), curses.color_pair(2)) reset(start, (0, 0), curses.color_pair(2))
else: else:
# current_coords = [maxy - 5, maxx - 27] current_coords = [maxy - 5, maxx - 27]
current_coords = [1, 1] #current_coords = [1, 1]
screen.addstr(current_coords[0], current_coords[1], "", curses.color_pair(2)) screen.addstr(current_coords[0], current_coords[1], "", curses.color_pair(2))
WALL = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""] WALL = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
pause_elapsed = 0 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") # print(screen.instr(current_coords[0],current_coords[1]+1,1).decode("utf-8"), "RSIDE PRESS")
def menu(screen): # def menu(screen):
y, x = screen.getmaxyx() # y, x = screen.getmaxyx()
screen.clear() # screen.clear()
screen.refresh() # screen.refresh()
text = """ # text = """
\t\t\t # \t\t\t██ █████ ██████ ██ ██ ██████ ██ ███ ██ ████████ ██ ██
\t\t\t # \t\t\t██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██
\t\t\t # \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)) def play(screen, loadedmaze=None, loadedcoords=None, loadedtime=0, executeguest = False, outerscore = 0):
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):
y, x = screen.getmaxyx() y, x = screen.getmaxyx()
height, width = int((y - 2) / 2), int((x - 2) / 2) 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.clear()
screen.refresh() screen.refresh()
if not loadedmaze: if not loadedmaze:
@ -500,9 +531,6 @@ def play(screen, loadedmaze=None, loadedcoords=None, loadedtime=0):
came_out = 1 came_out = 1
while True: while True:
if came_out != 0: if came_out != 0:
screen.clear()
screen.refresh()
screen.erase()
global WON global WON
if WON != 0: if WON != 0:
tt = (start_ts - end_ts) / 300 tt = (start_ts - end_ts) / 300
@ -511,31 +539,30 @@ def play(screen, loadedmaze=None, loadedcoords=None, loadedtime=0):
WON = 0 WON = 0
else: else:
score = 0 score = 0
screen.addstr(
y // 2 - 5, x // 2 - 8, str("Your score is: " + str(int(score))) guestswitch(score)
) # res = database.Update_score(int(score))
res = database.Update_score(int(score)) # if res == "guest":
if res == "guest": # screen.addstr(
screen.addstr( # height - 1,
height - 1, # 5,
5, # "You are not signed in. You will lose your score if you proceed.",
"You are not signed in. You will lose your score if you proceed.", # )
) # screen.addstr(
screen.addstr( # height, 5, "Do you want to login and save your progress? (y/n)"
height, 5, "Do you want to login and save your progress? (y/n)" # )
) # while True:
while True: # key = screen.getch()
key = screen.getch() # if key == ord("y"):
if key == ord("y"): # database.login(screen, calledby=int(score))
database.login(screen, calledby=int(score)) # break
break # elif key == ord("n"):
elif key == ord("n"): # break
break # screen.clear()
screen.clear() # screen.refresh()
screen.refresh() # came_out = 0
came_out = 0 # menu(screen)
menu(screen) # return
return
def main(screen): def main(screen):

View file

@ -2,7 +2,7 @@ import os
import pickle import pickle
from time import sleep from time import sleep
import maze.modules.maze as m import maze.menu as m
from .PlayerBase_func import input, screenwipe from .PlayerBase_func import input, screenwipe

View file

@ -2,6 +2,8 @@ import curses
import random import random
import time import time
from curses import textpad from curses import textpad
import maze.menu
import maze.modules.maze as m
OPPOSITE_DIRECTION_DICT = { OPPOSITE_DIRECTION_DICT = {
curses.KEY_UP: curses.KEY_DOWN, curses.KEY_UP: curses.KEY_DOWN,
@ -31,7 +33,8 @@ def main(stdscr):
curses.curs_set(0) curses.curs_set(0)
stdscr.nodelay(1) stdscr.nodelay(1)
stdscr.timeout(100) stdscr.timeout(100)
stdscr.clear()
stdscr.refresh()
# create a game box # create a game box
sh, sw = stdscr.getmaxyx() sh, sw = stdscr.getmaxyx()
box = [[3, 3], [sh - 3, sw - 3]] # [[ul_y, ul_x], [dr_y, dr_x]] 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 # non-blocking input
key = stdscr.getch() 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 # 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]: if key in DIRECTIONS_LIST and key != OPPOSITE_DIRECTION_DICT[direction]:
direction = key direction = key
@ -103,10 +112,12 @@ def main(stdscr):
): ):
msg = "Game Over!" msg = "Game Over!"
stdscr.addstr(sh // 2, sw // 2 - len(msg) // 2, msg) stdscr.addstr(sh // 2, sw // 2 - len(msg) // 2, msg)
stdscr.nodelay(0) while stdscr.getch() == -1:
stdscr.getch() pass
time.sleep(5) time.sleep(2)
break m.play(stdscr, executeguest=True, outerscore=score)
#Call play with guestcheck to update scores
stdscr.clear()
curses.wrapper(main) stdscr.refresh()
maze.menu.menu(stdscr)
return score