Pygame screen looping back - python

I am making a game in python, which has a main menu, followed by a difficulty select screen. When I select the play game button, the screen changes to the difficulty select screen as intended, but when I click a difficulty, it returns to the main menu screen, which is fully functional, leading me to believe that the function is continuing to run. If I then return to the difficulty select screen and select a difficulty a second time the game progresses as expected. No other button leads to this error which leads me to believe the error is in the select difficulty function itself.
def gameIntro(): # game intro loop
playButton=classes.Button(100,200,275,350,selectDifficulty) #classes is external file which contains a button class. params (x,y,w,h,action=None)
leaderButton=classes.Button(700,200,275,350,leaderboard)
quitButton=classes.Button(1000,200,250,350,quit)
instructButton=classes.Button(425,200,275,350,instructions)
spriteGroup = pygame.sprite.Group(playButton, leaderButton, quitButton, instructButton)
while not(playButton.loop and leaderButton.loop and quitButton.loop and instructButton.loop): #all of button objects have loop set to false, until they are clicked
eventLoop(spriteGroup) #runs an event handler for all of objects in spriteGroup
mainMenuScreen = pygame.image.load('Menu.jpg') # loads the menu Screens
mainMenuScreen = pygame.transform.scale(mainMenuScreen, (displayWidth, displayHeight)) #adjusts image to be same size as display
gameDisplay.blit(mainMenuScreen, (0, 0)) #displays the image
pygame.display.update() #updates the display
clock.tick(fps) #increments the clock by the fps amount to keep the screen changing
def selectDifficulty(): #selects the difficulty so that the appropriate AI is loaded
import Main
easyButton=classes.Button(70,150,300,400,Main.easyAIMaker)
mediumButton=classes.Button(450,150,300,400)
hardButton=classes.Button(800,150,300,400)
spriteGroup = pygame.sprite.Group(easyButton,mediumButton,hardButton)
while not (easyButton.loop and mediumButton.loop and hardButton.loop): #loops the screen while a choice hasnt been made
eventLoop(spriteGroup)
difficultyScreen = pygame.image.load('difficulty screen.jpg')
difficultyScreen = pygame.transform.scale(difficultyScreen, (displayWidth, displayHeight))
gameDisplay.blit(difficultyScreen, (0, 0))
pygame.display.update()
clock.tick(fps)

Problem was I didn't realise how python treated imported code. At the very end of intro.py the gameintro() function is called to start the game. When I imported intro.py this code was rerun causing the loop. By shielding game intro inside:
Def main():
gameIntro()
If __name__=="__main__":
Main()
The gameintro function only runs once

Related

Pyglet window does not reopen after being run

I made a Snake game program and I've made a function called Starting_screen() that opens a window with labels and sprites that shows the starting menu of the game where the player can choose to start a new game, check the leader board, or quit the game.
My main problem is that after the player finishes a game of Snake, I want the program to return to the main menu but it just hangs after the game exits. I've tried putting print statements within the Starting_Screen() function and it actually prints twice, indicating that it re-enters the function but does not open the window again. The flow of the program looks somewhat like this (in simpler sense):
The game is being run by main.py that has:
import interface
import engine
interface.main()
print('outside the interface.main()')
interface.main()
As you can see I've just initially tried to put two interface.main() functions so that I can check if the program runs the second one. It actually prints the statement which indicates it exits the function.
The interface.main() runs only the Starting_screen(), and the Starting_screen() looks like this (simplified):
def Starting_screen():
print('inside starting screen')
window = pyglet.window.Window()
some_text = pyglet.text.Label()
some_image = pyglet.image.load()
#window.event
def on_draw():
window.clear()
some_text.draw()
some_image.draw()
#window.event
def on_key_press(symbol,modifier):
if symbol == key.P:
window.close()
engine.main()
pyglet.app.run()
and the engine.main() just runs the game (using pygame) until the player loses and it will pygame.display.quit() that will terminate the engine.main() function and returns to the Starting_screen() which in turn terminates the function.
I was hoping it would open the main menu window again but it does not. But interestingly enough, it runs print('inside starting screen') twice, indicating that it runs the function again, but it does not open the window like what would normally happen when it was run for the first time.
Any suggestions would be appreciated.

Pygame Display Update Glitch?

I have some code that is kinda funky. Basically, the variable gameStart is in a variable that uses the function, cursorOver, which finds and detects where and if the mouse button is and if it is pressed. I have 3 buttons and I want each button to become larger when the cursor is over the button. The first button implementation works. However, if I try to add another button, the button becomes enlarged, however, the button starts flickering.
window.blit(background,(0,0))
window.blit(title,(175,200))
pygame.draw.rect(window,PURPLE,(50,400,200,100),0)
pygame.draw.rect(window,PURPLE,(300,400,200,100),0)
pygame.draw.rect(window,PURPLE,(550,400,200,100),0)
close()
mouseX,mouseY = pygame.mouse.get_pos()
mouseClick = pygame.mouse.get_pressed()[0]
gameStartC = cursorOver(50,400,200,100,mouseX,mouseY)
instructionStartC = cursorOver(300,400,200,100,mouseX,mouseY)
objectiveStartC = cursorOver(550,400,200,100,mouseX,mouseY)
nextStartC = cursorOver(580,250,175,100,mouseX,mouseY)
if gameStartC == True:
while True:
pygame.draw.rect(window,PURPLE,(25,375,250,150),0)
pygame.display.update()
break
else:
pygame.draw.rect(window,PURPLE,(50,400,200,100),0)
pygame.display.update()
#this is the part where the code becomes glitchy
if instructionStartC == True:
while True:
pygame.draw.rect(window,PURPLE,(275,375,250,150),0)
pygame.display.update()
break
else:
pygame.draw.rect(window,PURPLE,(300,400,200,100),0)
pygame.display.update()
It's simply because you call pygame.display.update() multiple times.
You should create a standard game loop that typically does these three things:
handle input
update state
draw to screen
and then repeats.
In the 'draw to screen'-step, you draw all your sprites/rects/whatever to the screen surface, and then eventually call pygame.display.update() once at the end.
Calling pygame.display.update() multiple times, not clearing the screen between iterations of the loop and creating multiple unnecessary event loops are common beginner mistakes that lead to those kind of glitches IMHO.
So in your case, the code should probably look more like this:
if gameStartC:
pygame.draw.rect(window,PURPLE,(25,375,250,150),0)
else:
pygame.draw.rect(window,PURPLE,(50,400,200,100),0)
if instructionStartC:
pygame.draw.rect(window,PURPLE,(275,375,250,150),0)
else:
pygame.draw.rect(window,PURPLE,(300,400,200,100),0)
pygame.display.update()
I don't know what you expected the while-loops to do, and maybe you should use pygames Rect and Sprite classes. It will make your life easier.

I don't know what I am doing wrong. Code for opening a pygame window

import pygame
import sys
from pygame.locals import *
DISPLAY_SURF = pygame.display.set_mode((640,480))
#Sets the resolution to 640 pixels by 720 pixels
class Game:
def __init__(self):
pygame.init()
self.FPS = 60
self.fps_clock = pygame.time.Clock()
self.surface = pygame.display.set_mode((640, 480))
pygame.display.set_caption("The Hunt")
img = pygame.image.load("Graphics/background.png")
self.surface.blit(img)
#This class sets the basic attributes for the window.
#The clock is set to 60 and the name of the window
#is set to The Hunt which is a working title for my project
def run(self):
while True:
pygame.display.update()
self.fps_clock.tick(self.FPS)
self.process_game()
#This updates the window display to refresh every clock tick
def process_game(self):
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
game = Game()
#This creates an instance of the class Game which gives all the attributes
# and behaviours to this instance
game.run()
#Calling this function generates a window with the attributes defined.
I need some help. I have already checked if it is in the same folder, the file is definitely a png and I spelt all the folder names and the destination correctly. I'm open to any suggestions
I'm going to answer this question despite that it is not really a good one for Stack Overflow. On this site, you'll have to be more specific and detailed, because no one intends to read through a huge lot of code for you. I did however pickup some things that I think can be fixed ( some of these are opinion based, something that your question should never force an answer to have), but... here it is anyway:
For starters, when you construct a class, you use parenthesis after the class name, even if it's not going to inherit anything form another class. So change the line where you construct the Game class to this:
class Game():
Second thing about this code is that if your going to create the pygame window surface inside the Game() class, I don't understand why you're creating another window at the beginning of your code. If there is a reason for this please explain it in a comment in your code.
The last thing is more opinion-based . I don't know how many people create Pygame GUI applications like this, but it would be simpler to not use classes so you could understand the code better. When I create a Pygame GUI, I define the window, then the sprites, then I run the main game loop in a While Loop. Here is how I would normally structure your program:
#import pygame
import pygame, sys
from pygame.locals import *
#Initialize pygame and define colours
pygame.init()
white = 255,255,255
#Sets the resolution to 640 pixels by 720 pixels and caption for pygame window
DISPLAY_SURF = pygame.display.set_mode((640,480))
pygame.display.set_caption("The Hunt!")
#Create a clock object
clock = pygame.time.Clock()
FPS = 60
#Define a variable to refer to image
image = pygame.image.load("download.jpg")
#Start main loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAY_SURF.fill(white)
DISPLAY_SURF.blit(image,(0,0))
pygame.display.update()
I do use classes when creating a sprite, so I can create several instances of it, as well as keep functions I want to perform on the sprite all in one place. Doing this for the WHOLE program does work, and I suppose it's more "pythonic" (since python is object-oriented) but is still unnecessary for something like this. Here is a reference that teaches pygame in a similar way to how I code it, which I personally find to be an excellent tutorial.
Many people also put this code in a main() function and then run it, which is also a wildly used and accepted practice.

Creating a state machine in python

I have created various simple 2 dimensional games in python before and because they were simple, I did not need to create such a thing. But I now need it due to needing to go back and fourth.
To go forward though, I need some sort of direction...
I am around 200 lines into a new game and I haven't started on the actual game, its currently all dealing with the window, events and state's
### state machine
def run:
#~setup window
# Current state
state = menu()
running = True
while running:
#~event handler
returns = state.update(events)
if returns == "playgame":
state = game()
state.draw(window)
#menu state
class menu:
def __init__(self):
#~define vars for the menu (buttons, etc...)
self.clickables = [] # filled with gui etc..
self.image = image() # image of the menu
def update(self, events):
for event in events: # go through events
for clickable in self.clickables: # go through clickables
if clickable.testClicked(event.pos) != False: # Returns if clicked
return clickable.testClicked(event.pos)
def draw(self, window):
self.image.draw(window)
#game state
class game(menu): # Exactly the same as menu (just used as example)
def __init__(self):
super(menu).__init__()
#gui button
class button: # Extremely shortened for example use
def __init__(self, do): # do is a string
self.whenClickedDo = do
def testClicked(self, mousePos):
if mousePos in self.myRect: # myRect is the area of the button (Example!)
return self.whenClickedDo
return False
This above example was completely rushed but the question I ponder is... What is a better way to achieve this, or is the above an achievable/smart way to do things?
TLDR; A function "run" has a value "state" which can return a value which would be used to change itself into a different state. Is that a reasonable way to make a state machine?
I wrote a simple game engine in C++ and used a screen based system. I'll try and write a simple version in python for you now. The idea is that each part of the game is a different screen. So the main menu, is a screen, there is a gameplay screen (where all the action takes place), there might be an options screen for the user to change the settings etc etc. These are all managed by a screen list, each screen has a position in the list, and you can switch between screens depending on game events. So if the main menu screen is active, when the user clicks the 'play' button, the gameplay screen is now loaded.
I cant think of the top of my head exactly how I'd port it to python but this should give you somewhere to start at least.
So a screen would be a class something like this:
class Screen:
# you can have objects in the screen as variables of the class here
# e.g self.player = None
def onEntry(self):
# here you would init the assets for your screen
# this could be the interface, sprites etc
# e.g self.player = Player()
def onExit(self):
# do any clean up
# maybe save player data if this is a gameplay screen
# e.g self.player.save()
def update(self):
# main loop for this screen
# e.g self.player.update()
Each specific Screen for your game would inherit from the Screen class and implement those functions with custom logic for that screen. Then the screen list would basically just be a list of these custom Screen classes. Your game would just flip between these Screens.

Getting position of user click in pygame

I have a pygame window embedded in a a frame in tkinter. In another frame I have a button which calls the following function when clicked:
def setStart():
global start
# set start position
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
start = event.pos
print("start",start)
break
I am intending for the program to print the position of the place where the user clicked on the pygame surface after the button is clicked. However on the first click of the button and the following click of the pygame surface there is no output. It is on the second click of the button before the corresponding second click on the pygame surface that python prints out an output like :
('start', (166, 115))
How can I get it to give me a result right after the click on the pygame surface? I had the same problem when I had two seperate tkinter and pygame windows so the embedding of pygame into tkinter is unlikely to be the cause of the problem.
EDIT: after further testing it appears that if the button is pressed and then the pygame surface is clicked on multiple times, upon a second click of the button the coordinates of all of these clicks are printed out as a batch.
In the most basic form here's how you do it in pygame:
import pygame
pygame.init()
screen = pygame.display.set_mode((100, 100))
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP: # or MOUSEBUTTONDOWN depending on what you want.
print(event.pos)
elif event.type == pygame.QUIT:
quit()
pygame.display.update()
More information on how to handle pygame events can be found in the docs.
And here's how you do it in tkinter:
try:
import tkinter as tk # Python 3
except ImportError:
import Tkinter as tk # Python 2
root = tk.Tk()
def print_pos(event):
print(event.x, event.y)
root.bind("<Button-1>", print_pos)
root.mainloop()
More information on tkinter events can be found in effbots documentation.
I would suggest not putting break in an event loop in pygame. Doing so makes all other events go un-handled, meaning that it's possible for the program to not respond to certain events.
Your "EDIT: [...]" is unfortunately wrong, given the code you've given us. I had no problem with the code; it printed the position of where I released the mouse button and always printed just one position. So there have to be a logical error somewhere else in your code.
First I have to say that I don't know anything about pygame. However, if you are already using Tkinter, I could help you maybe: I would define a new function (let's call it mouse_click). In the button-click-function I would bind the new function to the game's surface. In the new function I print out the current mouse position:
def button_click(self):
game_surface.bind("<Button-1>", self.mouse_click)
def mouse_click(self, event):
print "X:", event.x
print "Y:", event.y
I hope this is helpful. Please notice that you should modify this code to make it work in your program (using the correct widget names and so on).
By the way, "Button-1" is the event identifier of the left mouse button.

Categories