Search code examples
pythonmenupython-3.42d-gamesstartmenu

How can i make a startmenu in python?


i have been trying to add a start menu to a game i have been making and have looked everywhere to see how to make a start menu. I did try to use a tutorial by sentdex but it didn't work for my code! And i have also looked in many other places. Does anyone know how to make a start menu - thanks! Here is my code! (Sorry for the bad indents and spaces and stuff, i found it hard to get it to work on here!

from tkinter import * 
import pongtablev2, redball, bat


black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

x_velocity = 10
y_velocity = 0
score_left = 0
score_right = 0
score_top = 0
score_bottom = 0
first_serve = True


window = Tk()
window.title("MyPong")


my_pongtablev2 = pongtablev2.Table(window, net_colour="blue",vertical_net=True, horizontal_net=True)


my_redball = redball.Ball(table=my_pongtablev2, x_speed=x_velocity,y_speed=y_velocity,
                          width=24, height=24, colour="green", x_start=288, y_start=290)

bat_L = bat.Bat(table=my_pongtablev2, width=15, height=100, x_posn=20, y_posn=250, colour="red")
bat_R = bat.Bat(table=my_pongtablev2, width=15, height=100, x_posn=575, y_posn=250, colour="red")
bat_T = bat.Bat(table=my_pongtablev2, width=100, height=15, x_posn=250, y_posn=15, colour="red")
bat_B = bat.Bat(table=my_pongtablev2, width=100, height=15, x_posn=250, y_posn=570, colour="red")


def game_flow():
    global first_serve
    global score_left
    global score_right
    global score_top
    global score_bottom


if(first_serve == True):
    my_redball.stop_ball()
    first_serve = False

bat_L.detect_collision(my_redball)
bat_R.detect_collision(my_redball)
bat_T.detect_collision(my_redball)
bat_B.detect_collision(my_redball)


if(my_redball.x_posn <= 3):
    my_redball.stop_ball()
    my_redball.start_position()
    bat_L.start_position()
    bat_R.start_position()
    bat_T.start_position()
    bat_B.start_position
    my_pongtablev2.move_item(bat_L.rectangle, 20, 250, 35, 350)
    my_pongtablev2.move_item(bat_R.rectangle, 575, 250, 590, 350)
    my_pongtablev2.move_item(bat_T.rectangle, 250, 15, 350, 30)
    my_pongtablev2.move_item(bat_B.rectangle, 250, 570, 350, 585) 
    score_left = score_left + 1
    if(score_left >= 10):
        score_left = "WINNER!!!"
        score_right = "Looser"
        score_top = "Looser"
        score_bottom = "Looser"
    first_serve = True
    my_pongtablev2.draw_score(score_left, score_right)
    my_pongtablev2.draw_score2(score_top)
    my_pongtablev2.draw_score3(score_bottom)

if(my_redball.x_posn + my_redball.width >= my_pongtablev2.width - 3):
    my_redball.stop_ball()
    my_redball.start_position()
    bat_L.start_position()
    bat_R.start_position()
    bat_T.start_position()
    bat_B.start_position()
    my_pongtablev2.move_item(bat_L.rectangle, 20, 250, 35, 350)
    my_pongtablev2.move_item(bat_R.rectangle, 575, 250, 590, 350)
    my_pongtablev2.move_item(bat_T.rectangle, 250, 15, 350, 30)
    my_pongtablev2.move_item(bat_B.rectangle, 250, 570, 350, 585) 
    score_right = score_right + 1
    if(score_right >= 10):
        score_right = "WINNER!!!"
        score_left = "Looser"
        score_top = "Looser"
        score_bottom = "Looser"
    first_serve = True
    my_pongtablev2.draw_score(score_left, score_right)
    my_pongtablev2.draw_score2(score_top)
    my_pongtablev2.draw_score3(score_bottom)


if(my_redball.y_posn <= 3):
    my_redball.stop_ball()
    my_redball.start_position()
    bat_L.start_position()
    bat_R.start_position()
    bat_T.start_position()
    bat_B.start_position()
    my_pongtablev2.move_item(bat_L.rectangle, 20, 250, 35, 350)
    my_pongtablev2.move_item(bat_R.rectangle, 575, 250, 590, 350)
    my_pongtablev2.move_item(bat_T.rectangle, 250, 15, 350, 30)
    my_pongtablev2.move_item(bat_B.rectangle, 250, 570, 350, 585)
    score_top = score_top + 1
    if(score_top >= 10):
        score_top = "WINNER!!!"
        score_left = "Looser"
        score_right = "Looser"
        score_bottom = "Looser"
    first_serve = True
    my_pongtablev2.draw_score3(score_bottom)
    my_pongtablev2.draw_score(score_left, score_right)
    my_pongtablev2.draw_score2(score_top)


if(my_redball.y_posn + my_redball.height >= my_pongtablev2.height - 3):
    my_redball.stop_ball()
    my_redball.start_position()
    bat_L.start_position()
    bat_R.start_position()
    bat_T.start_position()
    bat_B.start_position()
    my_pongtablev2.move_item(bat_L.rectangle, 20, 250, 35, 350)
    my_pongtablev2.move_item(bat_R.rectangle, 575, 250, 590, 350)
    my_pongtablev2.move_item(bat_T.rectangle, 250, 15, 350, 30)
    my_pongtablev2.move_item(bat_B.rectangle, 250, 570, 350, 585) 
    score_bottom = score_bottom + 1
    if(score_bottom >= 10):
        score_bottom = "WINNER!!!"
        score_right = "Looser"
        score_left = "Looser"
        score_top = "Looser"
    first_serve = True
    my_pongtablev2.draw_score2(score_top)
    my_pongtablev2.draw_score3(score_bottom)
    my_pongtablev2.draw_score(score_left, score_left)

my_redball.move_next()
window.after(17, game_flow)


def restart_game(master):
global score_left
global score_right
global score_top
global score_bottom
my_redball.start_ball(x_speed=x_velocity, y_speed=11)
if(score_left == "WINNER!!!" or score_right == "Looser" or  score_top == "Looser" or score_bottom == "Looser"):
    score_left = 0
    score_right = 0
    score_top = 0
    score_bottom = 0
my_pongtablev2.draw_score(score_left, score_right,)
my_pongtablev2.draw_score2(score_top)
my_pongtablev2.draw_score3(score_bottom)


 window.bind("q", bat_L.move_up)
 window.bind("a", bat_L.move_down)
 window.bind("<Up>", bat_R.move_up)
 window.bind("<Down>", bat_R.move_down)
 window.bind("u", bat_T.move_left)
 window.bind("i", bat_T.move_right)
 window.bind("n", bat_B.move_left)
 window.bind("m", bat_B.move_right)


window.bind("<space>", restart_game)


game_flow()


window.mainloop()

Solution

  • I'm assuming by "start menu" you mean what is commonly called a "toplevel menu", "application menu", or "application menu-bar". On MS-Windows, for example, this would be a menu that appears across the top of the application window.

    Here is a link to some good help about menus: The Tkinter Menu Widget. That site has plenty of other good info about Tkinter accessible from links on that page. Also, Tkinter 8.5 reference: a GUI for Python is an excellent resource.

    But to address your specific question, and using your code, just after these two lines:

    window = Tk()
    window.title("MyPong")
    

    First, add these two lines:

    menu_bar = Menu(window)
    window.config(menu=menu_bar)
    

    This creates the application menu (aka menu-bar). Note that the config() function (with the menu=parameter) is called on the Tk object. This is what makes it an application window. (You can associate menus with other windows and widgets too, and create context menus, etc. all using the same Tk.Menu class.)

    Then add whatever menus you want to have on this menu-bar by using Menu.add_cascade(). For example, a "File" menu is quite common:

    file_menu = Menu(menu_bar)
    menu_bar.add_cascade(label="File", menu=file_menu)
    

    And finally, you add the menu commands using Menu.add_command() and passing a callable object via the command= parameter:

    file_menu.add_command(label="New", command=create_a_new_game)
    file_menu.add_command(label="Open", command=open_a_saved_game)
    file_menu.add_command(label="Save", command=save_the_game)
    file_menu.add_separator()
    file_menu.add_command(label="Exit", command=callback)
    

    Just use Menu.add_cascade() to add other menus. For example, another popular menu is "Help":

    help_menu = Menu(menu_bar)
    help_menu.add_cascade(label="Help", menu=help_menu)
    help_menu.add_command(label="About", command=show_help)
    

    If you want sub-menus, you can use just use Menu.add_cascade() on the menu you added to the menu-bar:

    file_submenu = Menu(file_menu)
    file_menu.add_cascade(label="Submenu", menu=file_submenu)
    file_submenu.add_command(label="Something", command=something)
    file_submenu.add_command(label="Something Else", command=another)