Pygame_menu loop overrides the transition to another screen - python

Currently working on a backgammon game with eventual multiplayer support.
I have a mainmenu function that is working, and its exceptions and draws are handled by the mainloop() attribute. However, I have a button that, when pressed, should switch to the game with the backgammon board as the background.
The image appears briefly, then the main menu continues to run. I've tried to find ways to disable the menuloop using conditional statements to no avail.
Here is the full source code:
import sys, pygame, os, pygame_menu
width, height = 1280, 960
class mainBackgammonGame():
global width, height
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Bootleg Backgammon")
self.clock = pygame.time.Clock()
def updateScreen(self):
self.clock.tick(60)
pygame.display.flip()
def startGame():
surface = pygame.display.set_mode((width, height))
currPath = os.path.dirname(os.path.abspath(__file__))
boardPath = os.path.join(currPath, "images/mainboard.png")
boardImage = pygame.image.load(boardPath)
surface.blit(boardImage, (0,0))
pygame.display.flip()
def mainMenu():
surface = pygame.display.set_mode((width, height))
menu = pygame_menu.Menu(960, 1280, 'Welcome to Backgammon', theme = pygame_menu.themes.THEME_DARK)
menu.add_text_input("Your Name: ", default = "Joseph Smith")
menu.add_button('Play', mainBackgammonGame.startGame)
menu.add_button('Quit', pygame_menu.events.EXIT)
menu.mainloop(surface)
bg = mainBackgammonGame()
mainMenu()
while 1:
for event in pygame.event.get():
if event == pygame.QUIT:
exit()
bg.updateScreen()

Figured it out.
def mainMenu():
def disable():
menu.disable()
mainBackgammonGame.startGame()
surface = pygame.display.set_mode((width, height))
menu = pygame_menu.Menu(960, 1280, 'Welcome to Backgammon', theme = pygame_menu.themes.THEME_DARK)
menu.add_text_input("Your Name: ", default = "Joseph Smith")
menu.add_button('Play', disable)
menu.add_button('Quit', pygame_menu.events.EXIT)
menu.mainloop(surface)
Added a new function within the main menu function that initially disables the menu, then will call the startGame function that loads the screen.

I recommend reading the following example:
https://github.com/ppizarror/pygame-menu/blob/master/pygame_menu/examples/game_selector.py
You can see that the main part of that is having a main menu variable
main_menu: Optional['pygame_menu.Menu'] = None
Which gets initialized elsewhere in the code.
Additionally when it's time for that menu to leave, like if you want to move to the game after being in the menu then you want a function like this:
def start_game():
global menu
# Do stuff here
menu.disable()
Then in your pygame main loop you can guard the mainloop call like so:
if menu.is_enabled():
menu.mainloop(screen)

Related

Creating a Main Menu for connect 4 on python

I am trying to create a main menu (starting page) for a 3 player connect 4 but I am unable to create one I want 5 buttons (3 player game, 2 player game, single player, options, Quit).
Can anyone help? If possible can anyone give me example code which I could adapt to my game.
I haven't created a main menu before.
I don't mid use of tkinter or pygame.
A small example of a yellow button, that when clicked prints some text (more explanation in code comments):
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 400))
clock = pygame.time.Clock()
# create the surface that will be the button
btn = pygame.Surface((300, 200))
btn.fill((255, 255, 0))
rect = btn.get_rect(center=(250, 200))
clicked = False
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill((0, 0, 0))
# get mouse pos
mouse_pos = pygame.mouse.get_pos()
# get the state of left mouse button
left, *_ = pygame.mouse.get_pressed()
# check if mouse is in the button and if the mouse button has been clicked
# the flag `clicked` is for registering the click once when button is pressed
# otherwise it will loop again and the conditions will be true again
# you can also use the event loop above to detect whether the
# mouse button has been pressed, then the flag is not needed
if rect.collidepoint(mouse_pos) and left and not clicked:
clicked = True
print('clicked')
# reset flag
if not left:
clicked = False
# show the button
screen.blit(btn, rect)
pygame.display.update()
Useful:
pygame.Rect.collidepoint
Here’s a small example using Tkinter:
from tkinter import *
def click():
# Make another window.
top = Toplevel()
# You can add more elements here.
# Creating the Main Window
root = Tk()
root.title(“Connect 4”) # You can replace the title with your own
btn = Button(root, text=“Place Your Text”, command=click).pack()
# You can add as many of these buttons
root.mainloop()

Pygame screen output not displaying

I'm working on a game project with Pygame. I'm only just beginning to work on the project and I'm kind of stuck. I have made three files each containing code to perform different functions. The first file - "alien_apocalypse.py"contains the class 'AlienApocalypse', which serves to start and monitor user events in the game and contains a few imported modules such as the game settings
(which is the 2nd file - 'game_settings.py') and the a class file which contains the all the attributes of one of the game characters 'knight.py'. I'm trying to run "alien_apocalypse.py" which is meant to display my character-knight and the bottom middle of the display but nothing shows up. I'm running it on a Mac with macOS Mojave, IDE is PyCharm. Here are the files:
File 1 - "alien_apocalypse.py":
import sys
import os
import pygame
from game_settings import GameSettings
from knight import Knight
class AlienApocalypse:
"""Overall class to manage game assets and behaviour"""
def __init__(self):
"""Initialize the game and create game resources"""
pygame.init()
self.settings = GameSettings()
drivers = ['directfb', 'fbcon', 'svgalib']
found = False
for driver in drivers:
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
print('Driver: {0} failed.'.format(driver))
continue
found = True
break
if not found:
raise Exception('No suitable video driver found!')
self.screen_window = pygame.display.set_mode((2880, 1800))
pygame.display.set_caption("Alien Apocalypse")
"""Setting background color"""
self.background_color = (230, 230, 255)
self.knight = Knight(self)
def run_game(self):
"""Start the main loop for the game"""
while True:
# Watch for keyboard and mouse actions
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop.
self.screen_window.fill(self.settings.background_color)
self.knight.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == "__main__":
"""Make a game instance, and run the game"""
ac = AlienApocalypse()
ac.run_game()
File 2 - game_settings.py
class GameSettings:
"""This class stores all the game settings"""
def __init__(self):
"""Initialize the game's settings attributes"""
# Screen settings
self.screen_width = 2880
self.screen_height = 1800
self.background_color = (230, 230, 255)
File 3 knight.py
import pygame
class Knight:
"""A class that manages the character knight"""
def __init__(self, ac_game):
"""Initialize the knight and set its starting position."""
self.screen_window = ac_game.screen_window
self.screen_window_rect = ac_game.screen_window.get_rect()
# Load the character - Knight image and get its rect.
image_file = "/Users/johnphillip/Downloads/craftpix-891165-assassin" \
"-mage-viking-free-pixel-art-game-heroes/PNG/Knight" \
"/knight.bmp "
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
# Start each new character at the bottom center of the screen.
self.rect.midbottom = self.screen_window_rect.midbottom
def blitme(self):
"""Draw the character at its current location."""
self.screen_window.blit(self.image, self.rect)
Issues I detected so far (now the window is displayed):
You have to set proper video driver
Then you have to initialize pygame.display
You were calling the run_game() function inside the class (ac.run_game()), not the other.
The run_game() inside the class does nothing (pass)
You have to replace the current run_game() inside the class with the one which is outside, so you can access "self things" like variables and functions.
You can not equal self to None as default value if not present (self is the class itself and everything it contains, so if you equal it to None you are "killing" yourself (the class)!!!)
Your alien_apocalypse.py could look like this:
import pygame
from game_settings import GameSettings
from knight import Knight
class AlienApocalypse:
"""Overall class to manage game assets and behaviour"""
def __init__(self):
"""Initialize the game and create game resources"""
pygame.init()
self.settings = GameSettings()
drivers = ['windib', 'directx']
found = False
for driver in drivers:
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
print('Driver: {0} failed.'.format(driver))
continue
found = True
break
if not found:
raise Exception('No suitable video driver found!')
self.screen_window = pygame.display.set_mode((2880, 1800))
pygame.display.set_caption("Alien Apocalypse")
"""Setting background color"""
self.background_color = (230, 230, 255)
self.knight = Knight(self)
def run_game(self):
"""Start the main loop for the game"""
while True:
# Watch for keyboard and mouse actions
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# sys.exit()
# Redraw the screen during each pass through the loop.
self.screen_window.fill(self.settings.background_color)
self.knight.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == "__main__":
"""Make a game instance, and run the game"""
ac = AlienApocalypse()
ac.run_game()

Python Basic control flow errors

When I run this code, it enters a while loop and checks every turn whether or not on_title_screen==True. If it is true, the program will continue to check for input, but if it is false, the loop will refresh the screen and begin the game. However, when start is clicked, and on_title_screen=False, the game still captures mouse input, and does not display the bird that it should.
import random
import pygame
from pygame import *
import math
import sys
#Presets for window
size=width,height=500,500
Ag=-9.80665
clock = pygame.time.Clock()
white=(255,255,255)
blue=(0,0,255)
red=(255,0,0)
gray_bgColor=(190,193,212)
#Initialise pygame Surface as screen
pygame.init()
pygame.font.init()
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Flappy Kid")
#Game Presets
vY=0
xPos,yPos=200,100
score=0
on_title_screen=True
def falling_loop():
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_UP:
vY=-10
if yPos>height-50:
yPos=100
vY+=1
yPos+=vY
class graphics():
#Holds the methods for loading/displaying graphics
def load_images(self):
#Loads the background and sprite images
self.background_image=pygame.image.load("flappy_background.png").convert()
self.bird_image=pygame.image.load("flappy_sprite.jpg").convert()
screen.set_colorkey(white)
self.birdHitBox=self.bird_image.get_rect()
def show_background(self):
#blits the background
screen.blit(self.background_image,[0,0])
def refresh_display(self):
#updates the display
screen.blit(self.background_image,[xPos,yPos],self.birdHitBox)
falling_loop()
screen.blit(self.bird_image,[xPos,yPos])
class titleScreen():
#Holds the methods for the title screen/menu
def title(self):
#Sets up the title
titleText="Flappy Game"
titlePos=(0,0)
currentFont=pygame.font.SysFont("arialms",30,bold=True,italic=True)
renderTitle=currentFont.render(titleText,1,blue,gray_bgColor)
self.titlex,self.titley=currentFont.size(titleText)
screen.blit(renderTitle,titlePos)
def start(self):
#Sets up the start Button
startText="Start Game"
self.startPos=(0,self.titley)
currentFont=pygame.font.SysFont("arialms",25,bold=False,italic=False)
renderStart=currentFont.render(startText,1,blue,gray_bgColor)
self.startx,self.starty=currentFont.size(startText)
self.start_rect = pygame.Rect(self.startPos[0],self.titley,self.startx,self.starty)
screen.blit(renderStart,self.startPos)
def quit(self):
#Sets up the quit button
quitText="Quit"
self.quitPos=(0,self.starty+self.titley)
currentFont=pygame.font.SysFont("arialms",25,bold=False,italic=False)
renderQuit=currentFont.render(quitText,1,red,gray_bgColor)
self.quitx,self.quity=currentFont.size(quitText)
self.quit_rect = pygame.Rect(self.quitPos[0],self.titley+self.starty,self.quitx,self.quity)
screen.blit(renderQuit,self.quitPos)
def get_click(self):
#Gets mouse click and processes outcomes
for event in pygame.event.get():
if event.type==pygame.MOUSEBUTTONDOWN:
x,y=pygame.mouse.get_pos()
#Tests for start:
if self.start_rect.collidepoint(x,y):
print("start")
on_title_screen=False
elif self.quit_rect.collidepoint(x,y):
print("quit")
sys.exit()
titleC=titleScreen()
graphicsC=graphics()
def setupTitle():
#bundles all title_screen functions
titleC.title()
titleC.start()
titleC.quit()
def main():
graphicsC.load_images()
graphicsC.show_background()
setupTitle()
while True:
clock.tick(30)
if on_title_screen==False:
graphicsC.refresh_display()
elif on_title_screen==True:
titleC.get_click()
pygame.display.flip()
main()
I think #TessellatingHeckler is right, on_title_screen is a shadow variable, not the same.
In this code, there is nowhere that on_title_screen (global) could ever be set to False.
A more powerful answer though is to explain how to find the problem. I strongly recommend using pdb or ipdb. In this case, I would put one just inside of the while loop and make sure that the variables are what I think they should be.

How to detect resizeable window state in pygame and de-maximize it in Linux?

I have an application built in python with use of pygame that initially displays a login screen at a set size which is not RESIZABLE and then when user logs into the game the saved settings are being used to transform the window size. The window is turned into RESIZABLE after login. If user logs out the window is changed back to the initial size without RESIZABLE flag. Everything is OK as long as the user logs out from normal window, but when user hits the maximize button and then logs out in some distros the window still stays maximized and the login screen is being painted in a top left corner of the window.
And here comes the question, is there a way of detecting whether the window has been maximized so I can de-maximize it before sizing down?
I couldn't find anything that would help me with this in the pygame docs or anywhere online. I have found a way of getting a "handle" to the window by using:
pygame.display.get_wm_info()['window']
but not sure where to take it from here.
The way I set the sizes:
self.screen = pygame.display.set_mode((800, 480)) #login screen
self.screen = pygame.display.set_mode(user_saved_size, pygame.RESIZABLE) #game screen
get_wm_info()['wmwindow'] gives you windowID in Windows Manager (X.org) but it is "outside" of PyGame. Maybe with python library Xlib you could do something.
EDIT:
I tried example in Setting the window dimensions of a running application to change terminal size and it works but it don't change PyGame window size. I tried xlib to get PyGame window caption and it works but I could not set PyGame window caption.It seems PyGame doesn't respect new caption.
I use this code to test PyGame window caption - it can get caption but it can't set caption.
import sys
import pygame
from pygame.locals import *
import Xlib
import Xlib.display
WIDTH, HEIGHT = 1500, 300
pygame.init()
screen = pygame.display.set_mode((800,600),0,32)
print "wm_info:", pygame.display.get_wm_info()
print " window:", pygame.display.get_wm_info()['window']
print "fswindow:", pygame.display.get_wm_info()['fswindow']
print "wmwindow:", pygame.display.get_wm_info()['fswindow']
display = Xlib.display.Display()
root = display.screen().root
#windowID = root.get_full_property(display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0]
#print "Xlib windowID:", windowID
#window = display.create_resource_object('window', windowID)
window = display.create_resource_object('window', pygame.display.get_wm_info()['window'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib window get_wm_name():", window.get_wm_name()
window = display.create_resource_object('window', pygame.display.get_wm_info()['fswindow'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib fswindow get_wm_name():", window.get_wm_name()
window = display.create_resource_object('window', pygame.display.get_wm_info()['wmwindow'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib wmwindow get_wm_name():", window.get_wm_name()
print
print "Xlib wmwindow set_wm_name(hello world of xlib)"
window.set_wm_name("hello world of xlib")
display.sync()
print "Xlib wmwindow get_wm_name():", window.get_wm_name()
# --------------
fpsClock = pygame.time.Clock()
RUNNING = True
while RUNNING:
for event in pygame.event.get():
if event.type==QUIT:
RUNNING = False
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
RUNNING = False
fpsClock.tick(25)
# --------------
pygame.quit()
sys.exit()
I use this code to change window size - it works in terminal and DreamPie (python shell):
# https://unix.stackexchange.com/questions/5999/setting-the-window-dimensions-of-a-running-application
WIDTH, HEIGHT = 1500, 300
import Xlib
import Xlib.display
display = Xlib.display.Display()
root = display.screen().root
windowID = root.get_full_property(display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0]
window = display.create_resource_object('window', windowID)
window.configure(width = WIDTH, height = HEIGHT)
display.sync()
#windowIDs = root.get_full_property(display.intern_atom('_NET_CLIENT_LIST'), Xlib.X.AnyPropertyType).value
#for windowID in windowIDs:
# window = display.create_resource_object('window', windowID)
# name = window.get_wm_name() # Title
# pid = window.get_full_property(display.intern_atom('_NET_WM_PID'), Xlib.X.AnyPropertyType) # PID

Pygame Menu: calling another .py file

I'm learning pygame by helping a friend make a Menu for his interactive TicTacToe game. It's simple enough, a title and 2 buttons for starting and exiting the game. He has the game done, I just need to link my menu to his game.
After fiddling with pygame, I finally finished making the images appear in the pygame window(I never thought that seeing text appear in a blank window for the first time could look so beautiful! T^T). I believe that the next step is to make the images act like buttons to call another python file.
Here is my simple code for the menu:
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
resX, resY = 640, 480
windowObj = pygame.display.set_mode((resX,resY))
titleImg = pygame.image.load("title.png")
startImg = pygame.image.load("start.png")
exitImg = pygame.image.load("exit.png")
def width_center(img):
"""For horizontally setting an image."""
return resX/2 - x(img)/2
def height_center(img):
"""For vertically setting an image."""
return resY/2 - y(img)/2
def x(img):
"""Width of object."""
return img.get_width()
def y(img):
"""Height of object."""
return img.get_height()
while True:
pygame.display.update()
windowObj.fill((255,255,255))
windowObj.blit(titleImg,(width_center(titleImg), 30))
#This I want to link:
windowObj.blit(startImg,(width_center(startImg),height_center(startImg)-10))
windowObj.blit(exitImg,(width_center(exitImg),height_center(exitImg)+y(startImg)))
for i in pygame.event.get():
if i.type == QUIT:
exit()
Simply put, I want to know how to make the startImg call TTTClick.py. Should there also be a certain format for his TTTClick.py?
Thanks :)
If it's one project, you can have 'title screen' state, and 'game' states.
Psuedo code:
class Game():
def __init__(self):
self.done = False
self.state = "title"
def game_draw(self):
# game draw
def game_update(self):
# game event handling, and physics
def title_draw(self):
# title draw
def title_update(self):
# on event click
self.state = "game"
def loop(self):
# main loop
while not self.done:
if state == 'title':
title_update()
title_draw()
elif state == 'game':
game_update()
game_draw()
if __name__ == "__main__":
game = Game()
game.loop()
Note: x, height_center ect. already exist in pygame.Rect
# if Surface
titleImage.get_rect().width
# if Sprite
titleImage.rect.width
There's more, rect.center, rect.centerx, see the full listing at http://www.pygame.org/docs/ref/rect.html

Categories