Pygame window flickering - python

This is my text adventure code, pieces. When i start my code, my buttons and windows always flickering, and i don't know how to fix that problem. I tried pygame.window.update(), but it did not bring anything. Maybe it's problem with FPS, i don't know. :)
...............................................................................
import pygame,sys
pygame.init()
#############
pygame.mixer.music.load('Invincible.mp3')
pygame.mixer.music.play()
#############
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
block_color = (53,115,255)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('One Day After')
clock = pygame.time.Clock()
gameIcon = pygame.image.load('gameicon.jpg')
pygame.display.set_icon(gameIcon)
pause = False
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def GameOver():
####################################
pygame.mixer.Sound.play("smb_gameover.wav")
pygame.mixer.music.stop()
####################################
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("Game Over", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Play Again",150,450,100,50,green,bright_green,game_loop)
button("Quit",550,450,100,50,red,bright_red,quitgame)
pygame.display.update()
clock.tick(15)
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
pygame.mixer.music.stop()
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def quitgame():
pygame.quit()
sys.exit()
quit()
def unpause():
global pause
pygame.mixer.music.unpause()
pause = False
def paused():
############
pygame.mixer.music.pause()
#############
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("Paused", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Continue",150,450,100,50,green,bright_green,unpause)
button("Quit",550,450,100,50,red,bright_red,quitgame)
pygame.display.update()
clock.tick(15)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pilt1 = pygame.image.load('apoc2.jpg').convert()
gameDisplay.blit(pilt1, [0,0])
pygame.display.flip()
button("Start",150,450,100,50,green,bright_green,game_loop)
button("Quit",550,450,100,50,red,bright_red,quitgame)
pygame.display.update()
def game_loop():
global pause
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
quit()
gameDisplay.fill(white)
pygame.display.flip()
Backgroundpic = pygame.image.load('back.jpg').convert()
gameDisplay.blit(Backgroundpic, [0,0])
pygame.display.flip()
tekst = "This game will go as far as you choose!"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (50,50,155))
gameDisplay.blit(teksti_pilt, (100, 250))
tekst2 = "You are the smith of your destiny"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst2, False, (50,50,155))
gameDisplay.blit(teksti_pilt, (100, 400))
pygame.display.flip()
game_intro()
game_loop()
pygame.quit()
quit()

You seem to be flipping twice. You must flip only once, after everything is drawn. Remove the first call to flip() and your flicker should reduce.

Related

How Can I Make This Start Button Load My Game?

I made a menu start game and quit game I am really confused on how to put my main loop in a function and call that function when I click the button please help! I tried to put my main loop in a def funtion but my screen will appear black and nothing shows up here is an image of my start menu Video
my full code script
this is my game intro
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def game_intro():
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
window.fill((255,255,255))
largeText = pygame.font.Font('BLOODY.ttf',115)
TextSurf, TextRect = text_objects("Stolen Hearts!", largeText)
TextRect.center = ((800/2), (800/2))
window.blit(TextSurf, TextRect)
# make the square brighter if collideded with the buttons
mouse = pygame.mouse.get_pos()
if 150+120 > mouse[0] > 150 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, bright_green,(150,450,120,50))
else:
pygame.draw.rect(window, green,(150,450,120,50))
if 550+110 > mouse[0] > 550 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, bright_red,(550,450,110,50))
else:
pygame.draw.rect(window, red,(550,450,110,50))
# ---------------------------------------------------------------------
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects("Start Game", smallText)
textRect.center = ( (150+(120/2)), (450+(50/2)) )
window.blit(textSurf, textRect)
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects("Quit Game", smallText)
textRect.center = ( (150+(910/2)), (450+(50/2)) )
window.blit(textSurf, textRect)
pygame.display.update()
clock.tick(15)
Create a boolean called main_hover, and set it to false. Set it to true when the mouse is over the play button. If it isn't, set it to false.
In your event for loop, you will want to add this:
if event.type == pygame.MOUSEBUTTONUP:
if main_hover == True:
main_loop()
Do the same, but for the quit button.
Your code should look like this:
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def game_intro():
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
intro = True
main_hover = False
quit_hover = False
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONUP:
if main_hover == True:
main_loop()
if quit_hover == True:
quit()
window.fill((255,255,255))
largeText = pygame.font.Font('BLOODY.ttf',115)
TextSurf, TextRect = text_objects("Stolen Hearts!", largeText)
TextRect.center = ((800/2), (800/2))
window.blit(TextSurf, TextRect)
# make the square brighter if collideded with the buttons
mouse = pygame.mouse.get_pos()
if 150+120 > mouse[0] > 150 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, bright_green,(150,450,120,50))
main_hover = True
else:
pygame.draw.rect(window, green,(150,450,120,50))
main_hover = False
if 550+110 > mouse[0] > 550 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, bright_red,(550,450,110,50))
quit_hover = True
else:
pygame.draw.rect(window, red,(550,450,110,50))
quit_hover = False
# ---------------------------------------------------------------------
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects("Start Game", smallText)
textRect.center = ( (150+(120/2)), (450+(50/2)) )
window.blit(textSurf, textRect)
Hope this helps

Pygame Bool error when pressing a button

#campaign module for blunt wars
import pygame
import time
import sys
pygame.init()
pygame.mixer.music.load("Library\Sound\Light.wav")
size = [500, 500]
width = size[0]
height = size[1]
#colors
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
blue = (0,0,255)
bright_red = (255,0,0)
bright_green = (0,255,0)
#end colors
screen = pygame.display.set_mode((size), pygame.RESIZABLE)
pygame.display.set_caption('Blunt Wars - Campaign')
clock = pygame.time.Clock()
Icon = pygame.image.load('Library\Image\Icon.png')
pygame.display.set_icon(Icon)
bg = pygame.image.load('bg.png')
text = pygame.font.SysFont('Arial', 30)
def text_objects(text, font):
textSurface = font.render(text, True, black)
# Create the text rect.
textRect = textSurface.get_rect()
# Return a tuple consisting of the surface and the rect.
return textSurface, textRect
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(screen, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
screen.blit(textSurf, textRect)
def intro():
intro = True
#screen.fill(white)
#screen.blit(bg, (0,0))
pygame.mixer.music.play(-1)
hel = False
while intro:
screen.blit(bg, (0,0))
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
#quit()
import Blunt_Wars
if event.type == pygame.VIDEORESIZE:
surface = pygame.display.set_mode((event.w, event.h),pygame.RESIZABLE)
#screen.fill(blue)
largeText = pygame.font.SysFont('Castellar',50)
textSurf, textRect = text_objects("Blunt Wars - Campaign", largeText)
textRect.center = ((700),(100))
screen.blit(textSurf, textRect)
button("Start New",650,200,100,50,green,bright_green,game_loop)
button("Load Save",650,300,100,50,green,bright_green,load_save)
button("Help",650,400,100,50,green,bright_green,hel)
button("Settings",650,500,100,50,green,bright_green,settings)
button("Main Menu",650,600,100,50,red,bright_red,leave)
pygame.display.update()
clock.tick(15)
def leave():
pygame.mixer.music.stop()
pygame.quit()
import Blunt_Wars
def settings():
pygame.mixer.music.stop()
print("settings loaded")
time.sleep(4)
pygame.quit()
quit()
largeText = pygame.font.SysFont('Castellar',50)
def hel():
intro = False
help = True
while help:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#import Blunt_Wars
quit()
#pygame.mixer.music.stop()
screen.fill(white)
screen.blit(bg, (0,0))
textSurf, textRect = text_objects("Help Page", largeText)
textRect.center = ((700),(100))
screen.blit(textSurf, textRect)
pygame.display.update()
def load_save():
pygame.mixer.music.stop()
print("save loaded")
time.sleep(2)
pygame.quit()
quit()
def game_loop():
pygame.mixer.music.stop()
print("game loop ran")
time.sleep(10)
pygame.quit()
quit()
This is how I make my button in my game. It works for all buttons other than the one which causes this error:
line 50, in button
action()
TypeError: 'bool' object is not callable
If anyone can help me, that would be great and any improvement on my code would be great as well.
You have a local hel variable (a boolean) in your intro function which gets passed to the button function instead of the global hel function. Just remove this line hel = False, so that Python can find the global hel function and pass it to button.
Take a look at this question: Short Description of the Scoping Rules?
Also, you're blitting the buttons outside the screen. Correct their coordinates.

python pygame screen.fill

I have a problem. I have a screen that is already full of text and buttons and I want to press the button and fill my background with color. ( I know there are hundreds of questions on this topic, but I can't understand them ). This is a section of the code, where I have the buttons. I thought it would be easy to make a function that changes the color when I press the button, but when I press the button it changes color, as long as I am holding the mouse button. When I am not holding, it just returns to a screen with text and buttons.
def game_loop():
global display_text
global display_button_1
global display_button_2
gameExit = False
display_text = True
display_button_1 = True
display_button_2 = False
meie_font = pygame.font.SysFont("Arial", 36)
tekst = "This game will go as far as you choose!"
teksti_pilt1 = meie_font.render(tekst, False, (50,50,155))
tekst2 = "You are the smith of your destiny"
teksti_pilt = meie_font.render(tekst2, False, (50,50,155))
########################################################################
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
gameDisplay.fill(white)
########################################################################
if display_text:
gameDisplay.blit(teksti_pilt1, (100, 250))
gameDisplay.blit(teksti_pilt, (100, 400))
########################################################################
if display_button_1:
button("Start playing", 300,500,150,50,green,bright_green, hide_text)
########################################################################
if display_button_2:
#gameDisplay.fill(white)
bg = pygame.image.load("natsalo.jpg")
gameDisplay.blit(bg, (0, 0))
tekst = "You, just have a friendly dinner with your family!"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (100, 30))
tekst = "It wasn't so usualy as always, it was always something...!"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (50, 100))
tekst = "You are Lee Everett, you are a professor who taught history"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (25, 170))
tekst = "For over six years at the University of Georgia"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (100, 240))
tekst = "You, just have a friendly dinner with your family!"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (100, 310))
button("I am to lazy to wash hands, just sit", 100,500,600,50,green,bright_green, hide_text)
button("Wash your hands", 100,400,600,50,green,bright_green, next_screen)
def next_screen():
gameDisplay.fill( (0,0,0) )
pygame.display.update()
This is full code, if it is needed.
import pygame
import sys
pygame.init()
#############
pygame.mixer.music.load('Invincible.mp3')
pygame.mixer.music.play()
#############
clock = pygame.time.Clock()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
block_color = (53,115,255)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('One Day After')
clock = pygame.time.Clock()
gameIcon = pygame.image.load('gameicon.jpg')
pygame.display.set_icon(gameIcon)
pause = False
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def GameOver():
####################################
pygame.mixer.Sound.play("smb_gameover.wav")
pygame.mixer.music.stop()
####################################
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("Game Over", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Play Again",150,450,100,50,green,bright_green,game_loop)
button("Quit",550,450,100,50,red,bright_red,quitgame)
pygame.display.update()
clock.tick(15)
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
pygame.mixer.music.stop()
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def quitgame():
pygame.quit()
sys.exit()
quit()
def hide_text():
global display_text
global display_button_1
global display_button_2
display_text = False
display_button_1 = False
display_button_2 = True
def unpause():
global pause
pygame.mixer.music.unpause()
pause = False
def paused():
############
pygame.mixer.music.pause()
#############
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("Paused", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Continue",150,450,100,50,green,bright_green,unpause)
button("Quit",550,450,100,50,red,bright_red,quitgame)
pygame.display.update()
clock.tick(15)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pilt1 = pygame.image.load('apoc2.jpg').convert()
gameDisplay.blit(pilt1, [0,0])
button("Start",150,450,100,50,green,bright_green,game_loop)
button("Quit",550,450,100,50,red,bright_red,quitgame)
pygame.display.update()
def game_loop():
global display_text
global display_button_1
global display_button_2
gameExit = False
display_text = True
display_button_1 = True
display_button_2 = False
meie_font = pygame.font.SysFont("Arial", 36)
tekst = "This game will go as far as you choose!"
teksti_pilt1 = meie_font.render(tekst, False, (50,50,155))
tekst2 = "You are the smith of your destiny"
teksti_pilt = meie_font.render(tekst2, False, (50,50,155))
########################################################################
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
gameDisplay.fill(white)
########################################################################
if display_text:
gameDisplay.blit(teksti_pilt1, (100, 250))
gameDisplay.blit(teksti_pilt, (100, 400))
########################################################################
if display_button_1:
button("Start playing", 300,500,150,50,green,bright_green, hide_text)
########################################################################
if display_button_2:
#gameDisplay.fill(white)
bg = pygame.image.load("natsalo.jpg")
gameDisplay.blit(bg, (0, 0))
tekst = "You, just have a friendly dinner with your family!"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (100, 30))
tekst = "It wasn't so usualy as always, it was always something...!"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (50, 100))
tekst = "You are Lee Everett, you are a professor who taught history"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (25, 170))
tekst = "For over six years at the University of Georgia"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (100, 240))
tekst = "You, just have a friendly dinner with your family!"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (100, 310))
button("I am to lazy to wash hands, just sit", 100,500,600,50,green,bright_green, hide_text)
button("Wash your hands", 100,400,600,50,green,bright_green, next_screen)
def next_screen():
gameDisplay.fill( (0,0,0) )
pygame.display.update()
game_intro()
game_loop()
pygame.quit()
quit()
You have to reorganize code.
I create "subscenes" with fill(), text, buttons and update().
And I use variable display_subscene with number to decide which subscene display.
You could use classes to better organize it.
Now main problem is button which use mouse.get_pressed() so it keeps pressing buttons in the same place in different subscenes. I had to move buttons to different places.
You would have to use event.type == MOUSEBUTTONDOWN
EDIT in every loop I use event.type == MOUSEBUTTONDOWN to set variable mouse_button which I use in button() (as new argument) and this way it doesn't click all the time when I hold pressed button. Variable mouse_button has to be set 0 before every for event.
I add half-transparent background for screen Pause - you can press p game_loop to see it.
import pygame
import sys
# --- constants ---
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
block_color = (53,115,255)
# --- functions ---
def text_objects(text, font, color=black):
textSurface = font.render(text, True, color)
return textSurface, textSurface.get_rect()
def button(msg, x, y, w, h, ic, ac, click, action=None):
mouse = pygame.mouse.get_pos()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click == 1 and action != None: # <-- click instead of click[0]
pygame.mixer.music.stop()
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def quitgame():
pygame.quit()
sys.exit()
quit()
# --- scenes ---
# - scene Paused -
def unpause():
global pause
#pygame.mixer.music.unpause()
pause = False
def paused():
global pause
pause = True
background_image = pygame.Surface((display_width, display_height)).convert()
background_rect = background_image.get_rect(center=(display_width//2, display_height//2))
background_image.set_alpha(220)
gameDisplay.blit(background_image, background_rect)
largeText = pygame.font.SysFont("comicsansms", 115)
TextSurf, TextRect = text_objects("Paused", largeText, white)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
while pause:
mouse_button = 0 # reset in every loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_button = event.button
button("Continue",150,450,100,50,green,bright_green, mouse_button, unpause)
button("Quit",550,450,100,50,red,bright_red, mouse_button, quitgame)
pygame.display.update()
clock.tick(15)
# - scene GameOver -
def GameOver():
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("Game Over", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
while True:
mouse_button = 0 # reset in every loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_button = event.button
button("Play Again",150,450,100,50,green,bright_green, mouse_button, game_loop)
button("Quit",550,450,100,50,red,bright_red, mouse_button, quitgame)
pygame.display.update()
clock.tick(15)
# - scene GameIntro -
def game_intro():
# - objects -
#pilt1 = pygame.image.load('apoc2.jpg').convert()
# - loop -
intro = True
while intro:
# - events -
mouse_button = 0 # reset in every loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_button = event.button
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused()
# - updates -
button("Start",150,450,100,50,green,bright_green, mouse_button, game_loop)
button("Quit",550,450,100,50,red,bright_red, mouse_button, quitgame)
# - draws -
#gameDisplay.blit(pilt1, [0,0])
pygame.display.update()
# - scene GameLoop -
def goto_subscene(number):
global display_subscene
display_subscene = number
def game_loop():
global display_subscene
# at start display subscene 1
display_subscene = 1
#bg = pygame.image.load("natsalo.jpg")
meie_font = pygame.font.SysFont("Arial", 36)
# - subscene 1 -
tekst = "This game will go as far as you choose!"
subscene_1_text_1 = meie_font.render(tekst, False, (50,50,155))
tekst = "You are the smith of your destiny"
subscene_1_text_2 = meie_font.render(tekst, False, (50,50,155))
# - subscene 2 -
tekst = "You, just have a friendly dinner with your family!"
subscene_2_text_1 = meie_font.render(tekst, False, (25,25,155))
tekst = "It wasn't so usualy as always, it was always something...!"
subscene_2_text_2 = meie_font.render(tekst, False, (25,25,155))
tekst = "You are Lee Everett, you are a professor who taught history"
subscene_2_text_3 = meie_font.render(tekst, False, (25,25,155))
tekst = "For over six years at the University of Georgia"
subscene_2_text_4 = meie_font.render(tekst, False, (25,25,155))
tekst = "You, just have a friendly dinner with your family!"
subscene_2_text_5 = meie_font.render(tekst, False, (25,25,155))
# - subscene 3 -
tekst = "You killed by viruses !"
subscene_3_text_1 = meie_font.render(tekst, False, white)
# - subscene 4 -
tekst = "You lost searching bathroom !"
subscene_4_text_1 = meie_font.render(tekst, False, white)
# - subscene 5 -
tekst = "Good Bye !"
subscene_5_text_1 = meie_font.render(tekst, False, white)
# - loop -
gameExit = False
while not gameExit:
mouse_button = 0 # reset in every loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_button = event.button
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused()
if display_subscene == 1:
gameDisplay.fill(white)
gameDisplay.blit(subscene_1_text_1, (100, 250))
gameDisplay.blit(subscene_1_text_2, (100, 400))
button("Start playing", 300,500,150,50,green,bright_green, mouse_button, lambda:goto_subscene(2))
pygame.display.update()
elif display_subscene == 2:
gameDisplay.fill(white)
gameDisplay.blit(subscene_2_text_1, (100, 30))
gameDisplay.blit(subscene_2_text_2, (50, 100))
gameDisplay.blit(subscene_2_text_3, (25, 170))
gameDisplay.blit(subscene_2_text_4, (100, 240))
gameDisplay.blit(subscene_2_text_5, (100, 310))
button("Wash your hands", 100,400,600,50,green,bright_green, mouse_button, lambda:goto_subscene(4))
button("I am to lazy to wash hands, just sit", 100,500,600,50,green,bright_green, mouse_button, lambda:goto_subscene(3))
pygame.display.update()
elif display_subscene == 3:
gameDisplay.fill(black)
gameDisplay.blit(subscene_3_text_1, (100, 30))
button("Go forward", 100,500,600,50,green,bright_green, mouse_button, lambda:goto_subscene(5))
pygame.display.update()
elif display_subscene == 4:
gameDisplay.fill(black)
gameDisplay.blit(subscene_4_text_1, (100, 30))
button("Go forward", 100,500,600,50,green,bright_green, mouse_button, lambda:goto_subscene(5))
pygame.display.update()
elif display_subscene == 5:
gameDisplay.fill(black)
gameDisplay.blit(subscene_5_text_1, (100, 30))
button("Exit", 100,500,600,50,green,bright_green, mouse_button, quitgame)
pygame.display.update()
# --- main ---
# - init -
pygame.init()
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('One Day After')
# - m
clock = pygame.time.Clock()
pause = False
game_intro()
game_loop()
pygame.quit()

Python: What is wrong with my "Try Again" button function?

import pygame
import time
import random
pygame.init()
display_height = 800#display width and height
display_width = 1000
black = (0,0,0)
white = (255,255,255)#defining colours
red = (255,0,0)
blue = (127, 179, 213)
peru = (139,119,101)
beige = (250,250,225)
lightb = (245,222,179)
bg = (255,248,220)
grey_slate = (50,50,50)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('The Algebra Game')
clock = pygame.time.Clock()
def text_objects(text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',50)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def message_display2(text):
largeText = pygame.font.Font('freesansbold.ttf',30)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/3))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def Try_Again1(msg,x,y,w,h,inac,act,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, act,(x,y,w,h))
if click[0] == 1 and action != None:
if action == "Try Again":
Addition_loop1()
else:
pygame.draw.rect(gameDisplay, inac,(x,y,w,h))
def home_button(msg,x,y,w,h,inac,act,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.ellipse(gameDisplay, act,(x,y,w,h))
if click[0] == 1 and action != None:
if action == "Home":
game_intro()
else:
pygame.draw.ellipse(gameDisplay, inac,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def button(msg,x,y,w,h,inac,act,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, act,(x,y,w,h))
if click[0] == 1 and action != None:
if action == "Addition":
Addition_loop1()
elif action == "Subtraction":
Subtraction_loop1()
elif action == "Multiplication":
Multiplication_loop1()
elif action == "Division":
Division_loop1()
elif action == "Option 1":
incorrect_loop1()
else:
pygame.draw.rect(gameDisplay, inac,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def incorrect_loop1():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
message_display2("You are Incorrect")
Try_Again1("Try Again",25,25,150,50,peru,grey_slate,"Try Again")
pygame.display.update()
def Addition_loop1():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
home_button("Home",25,25,150,50,peru,grey_slate,"Home")
addition = pygame.image.load("addition.jpg")
gameDisplay.blit(addition,(330,50))
button("Option 1",50,500,200,50,peru,black,"Option 1")
pygame.display.update()
clock.tick(60)
def Subtraction_loop1():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
home_button("Home",25,25,150,50,peru,grey_slate,"Home")
pygame.display.update()
clock.tick(60)
def Multiplication_loop1():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
home_button("Home",25,25,150,50,peru,grey_slate,"Home")
pygame.display.update()
clock.tick(60)
def Division_loop1():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
home_button("Home",25,25,150,50,peru,grey_slate,"Home")
pygame.display.update()
clock.tick(60)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(grey_slate)
largeText = pygame.font.Font('freesansbold.ttf',50)
TextSurf, TextRect = text_objects("The Algebra Game", largeText)
TextRect.center = ((display_width/2),(display_height/14))
gameDisplay.blit(TextSurf, TextRect)
button("Addition",250,125,500,85,peru,black,"Addition")
button("Subtraction",250,250,500,85,peru,black,"Subtraction")
button("Multiplication",250,375,500,85,peru,black,"Multiplication")
button("Division",250,500,500,85,peru,black,"Division")
mouse = pygame.mouse.get_pos()
pygame.display.update()
clock.tick(15)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
clock.tick(60)
game_intro()
pygame.quit()
quit
Hi, I am experiencing some difficulties getting a "Try_Again1" button function to work, it is currently not showing up text or taking me to the desired page (Addition_loop1) once clicked, the button is also flickering in and out of view from the screen when running. I am using Python 3.5 (32-bit). Any help with my issue would be much appreciated. Thankyou

How do I stop my game from restarting when my pause plays my game_loop function in pygame?

So, I am making a simple video game in pygame, and it is very close to finishing. I was working on my pause screen, but when I hit p(the button I chose) it paused but when I hit continue it restarted the game. I went over the code and found nothing wrong. Then, I went to the question forum here and looked up the issue, I found something but they arent doing the code like I am, and on top of that the answer is hard to understand. The pause function is paused() and the unpause is, suprise, unpause(). Can you try to find where it went wrong? Here is my scource code:
import pygame
import time
import random
pygame.init()
display_width = 800
display_height = 600
dark_blue = (0,178,210)
blue = (109,178,201)
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
brown = (102, 51, 0)
ship_width = 96
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('_Asteroid Belt_')
clock = pygame.time.Clock()
thingImg = pygame.image.load("/home/pi/Desktop/asteroid.png")
shipImg = pygame.image.load('/home/pi/Desktop/spaceship.png')
bg = pygame.image.load("/home/pi/Desktop/py.png")
bgi = pygame.image.load("/home/pi/Desktop/introbg.jpg")
pause = False
def quitgame():
pygame.quit()
quit()
def unpause():
global pause
pause = False
def paused():
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects("Paused", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
button("Start Flight",100,450,200,100,dark_blue,blue,game_loop)
button("Land on Desktop",500,450,200,100,dark_blue,blue,quitgame)
#pygame.draw.rect(gameDisplay,dark_blue,(500,450,200,100))
pygame.display.update()
clock.tick(15)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.blit(bgi,(0,0))
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects("Asteroid Belt", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
button("Start Flight",100,450,200,100,dark_blue,blue,game_loop)
button("Land on Desktop",500,450,200,100,dark_blue,blue,quitgame)
#pygame.draw.rect(gameDisplay,dark_blue,(500,450,200,100))
pygame.display.update()
clock.tick(15)
def message_display2(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
def things_dodged(count):
font = pygame.font.SysFont(None,25)
text = font.render("Dodged: "+str(count), True, white)
gameDisplay.blit(text, (0,0))
def things(x,y, thing_width, thing_height,thing_startx,thing_starty):
gameDisplay.blit(thingImg, (x,y))
def ship(x,y):
gameDisplay.blit(shipImg,(x,y))
def text_objects(text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display("You Crashed!")
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w>mouse[0]>x and y+h > mouse[1] >y:
pygame.draw.rect(gameDisplay,ac,(x,y,w,h))
if click[0] == 1 and action != None:
if action == game_loop:
game_loop()
elif action == quitgame:
pygame.quit()
quit()
else:
pygame.draw.rect(gameDisplay,ic,(x,y,w,h))
smallText = pygame.font.Font('freesansbold.ttf',20)
textSurf, textRect = text_objects(msg,smallText)
textRect.center = ((x+(w/2)),(y+(h/2)))
gameDisplay.blit(textSurf, textRect)
def game_loop():
global pause
x = (300)
y = (410)
x_change=0
thing_startx = random.randrange(0, display_width)
thing_starty = -600
thing_speed = 5
thing_width = 100
thing_height = 100
dodged = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -20
if event.key == pygame.K_RIGHT:
x_change = 20
if event.key == pygame.K_p:
pause = True
paused()
if event.type == pygame.KEYUP:
if event .key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x+= x_change
gameDisplay.blit(bg, (0,0))
things(thing_startx,thing_starty,thing_width,thing_height,x,y)
thing_starty += thing_speed
thing_height=100
ship(x,y)
things_dodged(dodged)
if x > display_width - ship_width or x<0:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(0, display_width)
dodged += 1
thing_speed+=2
if y < thing_starty+thing_height:
cow = 5
if x > thing_startx and x < thing_startx + thing_width or x+ship_width > thing_startx and x+ship_width< thing_startx +thing_width:
crash()
pygame.display.update()
clock.tick(60)
game_intro()
game_loop()
pygame.quit()
quit()
It seems that you do not unpause in your pause() function.
You should be adding something like this:
def paused():
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.K_p:
unpause()
In my code, I set the unpause button function to game_loop() instead of unpause(). So every time I went to unpause, I restarted the game loop! This is an example that you have to pay attention to every section of your code, not just the function thats going wrong.

Categories