Drem Edys
Drem Edys

Reputation: 133

Trying to write two games in one code by PYGAME

I need to make the main menu where I can press 1 to play one game and 2 to another game. I already have two while loops for games, but have no idea how to write it in one code.

Upvotes: 0

Views: 78

Answers (1)

Thomas
Thomas

Reputation: 1212

You can separate each of the game's main loops with functions. First you can create a GUI window. One of the most common and easy GUI windows to use is tkinter. Import it at the top of you code with from tkinter import *. You could create a simple GUI like this:

global window
window = Tk()
window.title("Choose Game")
GAbutton = Button(window, text="Game One", width=10, command=GameOne)
GAbutton.grid(row=0, column=0, sticky=W)
GBbutton = Button(window, text="Game Two", width=10, command=GameTwo)
GBbutton.grid(row=1, column=0, sticky=W)

The command is the name of the function. Full code could look like:

from tkinter import *
import pygame
def GameOne():
    window.destroy()
    #Your first game goes here

def GameTwo():
    window.destroy()
    #Your second game goes here

global window
window = Tk()
window.title("Choose Game")
GAbutton = Button(window, text="Game One", width=10, command=GameOne)
GAbutton.grid(row=0, column=0, sticky=W)
GBbutton = Button(window, text="Game Two", width=10, command=GameTwo)
GBbutton.grid(row=1, column=0, sticky=W)

All you need to do is to make sure that each game is in the same indentation level as the comment.

Upvotes: 2

Related Questions