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()
Related
#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.
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.
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
in the game when you first start it there's the game start menu/intro, it has 2 buttons (Start Game) (Quit)
Now when you start the game and you're actually playing, when you press P (Paused) there're 2 buttons (Continue) (Main Menu) if you click continue it completes the game normally. However when you click main menu it closes the game instead of returning to the intro, same problem is present when you crash with the car you have (Try Again) and (Main Menu) and also if you click Main Menu it closes the game
Any ideas on what might be the problem ?(ignore the comments)
import pygame
import time
import random
pygame.init()
crash_sound = pygame.mixer.Sound("C:\Users\itzrb_000\Documents\Untitled.wav")
pygame.mixer.music.load("C:\Users\itzrb_000\Downloads\Cool_Ride.mp3")
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
blue = (0,0,255)
bright_green = (0,255,0)
bright_red = (255,0,0)
car_width = 50
gameDisplay = pygame.display.set_mode((display_width,display_height))#game screen size
pygame.display.set_caption('What A Ride')
clock = pygame.time.Clock()
carImg = pygame.image.load('C:\Users\itzrb_000\Downloads\Car_Green_Front.png')
gameBackground = pygame.image.load('C:\WhatARide_Background.png')
icon = pygame.image.load('C:\Users\itzrb_000\Downloads\Car_Green_Front - Copy.png')
pygame.display.set_icon(icon)
pause = False
car1 = pygame.image.load('C:\Users\itzrb_000\Downloads\download (3).png')
car2 = pygame.image.load('C:\Users\itzrb_000\Downloads\download (2).png')
car3 = pygame.image.load('C:\Users\itzrb_000\Downloads\images.png')
rock = pygame.image.load('C:\Users\itzrb_000\Downloads\Rock.png')
def background():
gameDisplay.blit(gameBackground,(0,0))
'''def cars(ystart):
objects = [car1, car2, car3, rock]
num_of_objects = random.randint(1,4)
for x in range(num_of_objects):
y = random.choice(objects)
objectblit = random.randrange(130, 625) - (y.get_width())
gameDisplay.blit(y,(random.randrange(130, 625)))'''
def score(count):
font = pygame.font.SysFont(None,25)
text = font.render("Score: "+str(count),True, blue)
gameDisplay.blit(text,(0,0))
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
def car(x,y):
gameDisplay.blit(carImg,(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*0.5),(display_height*0.5))
gameDisplay.blit(TextSurf, TextRect)#blit display object
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
pygame.mixer.music.stop()
pygame.mixer.Sound.play(crash_sound)
largeText = pygame.font.Font("freesansbold.ttf", 115)
TextSurf, TextRect = text_objects("You Crashed", largeText)
TextRect.center = ((display_width * 0.5), (display_height * 0.5))
gameDisplay.blit(TextSurf, TextRect) # blit display object
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Try Again!", 300, 400, 200, 50, green, bright_green, game_loop)
button("Main Menu!", 300, 470, 200, 50, red, bright_red, game_intro)
pygame.display.update()
clock.tick(20)
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:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.SysFont("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def quitgame():
pygame.quit()
quit()
def unpause():
global pause
pause = False
pygame.mixer.music.unpause()
def paused():
global pause
pause = True
pygame.mixer.music.pause()
largeText = pygame.font.Font("freesansbold.ttf", 115)
TextSurf, TextRect = text_objects("Paused", largeText)
TextRect.center = ((display_width * 0.5), (display_height * 0.5))
gameDisplay.blit(TextSurf, TextRect) # blit display object
while pause == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Continue!", 300, 400, 200, 50, green, bright_green,unpause)
button("Main Menu", 300, 470, 200, 50, red, bright_red,game_intro)
pygame.display.update()
clock.tick(20)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(blue)
largeText = pygame.font.SysFont("comicsansms", 115)
TextSurf, TextRect = text_objects("What A Ride", largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
button("Start Game", 300, 400, 200, 50, green, bright_green,game_loop)
button("Quit", 300, 470, 200, 50, red, bright_red,quitgame)
pygame.display.update()
clock.tick(20)
def game_loop():
global pause
pygame.mixer.music.play(-1)
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
thing_startx = random.randrange(130,625)
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 = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
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
print event
x += x_change
background()
things(thing_startx, thing_starty, thing_width, thing_height, black)
thing_starty += thing_speed
car(x,y)
score(dodged)
if x > 625 - car_width or x < 130:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(130,625)
dodged += 1
thing_speed += 1
thing_width += (dodged*1.2)
if y < thing_starty+thing_height:
if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx+thing_width:
crash()
pygame.display.update()
clock.tick(51) #fps
game_intro()
game_loop()
pygame.quit()
quit()
Because you're not waiting for the mouse button to be released before you trigger your buttons.
When pause() starts, it brings up two buttons.
User moves mouse to Main Menu.
User clicks mouse.
As soon as the mouse button is depressed, game_intro() is called, which puts a Quit button in the same place.
The mouse button is still depressed, so the game quits.
import pygame
import time
import random
pygame.mixer.pre_init(44100, 16, 2, 4096)
pygame.init()
boop_sound = pygame.mixer.Sound("boop.wav")
display_width = 800
display_height = 600
white = (255,255,255)
black = (0,0,0)
blue = (0, 0, 150)
red = (200,0,0)
light_red = (255,0,0)
yellow = (200,200,0)
light_yellow = (255,255,0)
green = (34,177,76)
light_green = (0, 255, 0)
clock = pygame.time.Clock()
smallfont = pygame.font.SysFont("Rockwell", 25)
medfont = pygame.font.SysFont("Rockwell", 35)
largefont = pygame.font.SysFont("Rockwell", 50)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("How fast can you tap?")
##icon = pygame.image.load("apple.png")#should be 32x32
##pygame.display.set_icon(icon)
pygame.display.update()
def score(score):
text = smallfont.render("Clicks: "+str(score), True, blue)
gameDisplay.blit(text, [2,0])
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type== pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
intro = False
if event.key == pygame.K_q:
pygame.quit()
quit()
gameDisplay.fill(white)
message_to_screen("How many times can you",
blue,
-80,
"large")
message_to_screen("click the button before the time runs out?",
blue,
-10,
"medium")
#message_to_screen("Press C to play, P to pause or Q to quit.",black,180)
button("Play", 150, 500, 100, 50, green, light_green, action="Play")
button("How to play", 325,500,150,50, yellow, light_yellow, action="How to play")
button("Quit", 550,500,100,50, red, light_red, action="Quit")
pygame.display.update()
clock.tick(15)
def game_over():
game_over = True
while game_over:
for event in pygame.event.get():
if event.type== pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
message_to_screen("Out Of Time!",
red,
-100,
"large")
message_to_screen("You clicked: " + str(click) + " times",
blue,
-30)
button("Play Again", 325, 440, 150, 50, green, light_green, action="Play")
button("Quit", 350,500,100,50, red, light_red, action="Quit")
pygame.display.update()
clock.tick(15)
def text_objects(text, color, size):
if size == "small":
textSurface = smallfont.render(text, True, color)
elif size == "medium":
textSurface = medfont.render(text, True, color)
elif size == "large":
textSurface = largefont.render(text, True, color)
return textSurface, textSurface.get_rect()
def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size = "small"):
textSurf, textRect = text_objects(msg, color, size)
textRect.center = ((buttonx +((buttonwidth/2)), buttony+(buttonheight/2)))
gameDisplay.blit(textSurf, textRect)
def message_to_screen(msg,color, y_displace=0, size = "small"):
textSurf, textRect = text_objects(msg, color, size)
#screen_text = font.render(msg, True, color)
#gameDisplay.blit(screen_text, [display_width/2, display_height/2])
textRect.center = (display_width/ 2), (display_height / 2) + y_displace
gameDisplay.blit(textSurf, textRect)
def game_controls():
gcont = True
while gcont:
for event in pygame.event.get():
if event.type== pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
message_to_screen("How to play", blue, -100,"large")
message_to_screen("You have to click the button as many times", black, -40)
message_to_screen("as you possible can before the time runs out", black, -20)
button("Play", 150, 500, 100, 50, green, light_green, action="Play")
button("Main", 350,500,100,50, yellow, light_yellow, action="Main")
button("Quit", 550,500,100,50, red, light_red, action="Quit")
pygame.display.update()
clock.tick(15)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
cur = pygame.mouse.get_pos()
clicked = pygame.mouse.get_pressed()
global click
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
if clicked[0] == 1 and action != None:
if action == "Quit":
pygame.quit()
quit()
if action == "How to play":
game_controls()
if action == "Play":
gameLoop()
if action == "Main":
game_intro()
if action == "Click":
click += 1
else:
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
text_to_button(text, black, x, y, width, height)
ENDTIMER = pygame.USEREVENT+1
def gameLoop():
gameExit = False
gameOver = False
FPS = 15
click = 0
global click
timed = pygame.time.set_timer(ENDTIMER, 25000)
while not gameExit:
gameDisplay.fill(white)
button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action=None)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.MOUSEBUTTONDOWN:
pygame.mixer.Sound.play(boop_sound)
button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action="Click")
button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action=None)
elif event.type == ENDTIMER:
game_over()
score(click)
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
game_intro()
gameLoop()
I am trying to make it so that pygame.time.set_time(), will only run after click > 0. I have tried placing the pygame.time.set_time in an if statement, but that did not work. Any responses are greatly appreciated! When placing the timer start in the MOUSEBUTTONDOWN event it will only start when the mouse is clicked, however everytime i click it resets.
def gameLoop():
ENDTIMER = pygame.USEREVENT+1
gameExit = False
gameOver = False
FPS = 15
click = 0
global click
timer_on =False
while not gameExit:
gameDisplay.fill(white)
button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action=None)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.MOUSEBUTTONDOWN:
if not timer_on:
timed = pygame.time.set_timer(ENDTIMER, 25000)
pygame.mixer.Sound.play(boop_sound)
button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action="Click")
button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action=None)
elif event.type == ENDTIMER:
gameExit = True
score(click)
pygame.display.update()
clock.tick(FPS)
game_over()
pygame.quit()
quit()