How Can I Make This Start Button Load My Game? - python

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

Related

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.

Pygame window flickering

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.

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

Pygame (A bit racey) game bug

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.

Main menu to Game transition

This code simply will open a pygame window and show a picture with functioning buttons (main menu) but I was wondering if there was a way to make the code skip down to the bottom (def car(x,y):) where the game will start after I press the Go button? And if there is a way, how can I achieve this?
import sys
import pygame
from pygame.locals import *
pygame.init()
size = width, height = 720, 480
speed = [2, 2]
#Colours
black = (0,0,0)
blue = (0,0,255)
green = (0,200,0)
red = (200,0,0)
green_bright = (0,255,0)
red_bright = (255,0,0)
screen = pygame.display.set_mode(size)
#Pictures
BackgroundPNG = pygame.image.load(r"C:\Users\John\Desktop\Michael\BroomBatchPython__\BackgroundPNG.png")
carImg = pygame.image.load(r"C:\Users\John\Desktop\Michael\BroomBatchPython__\BV_Sp1.png").convert_alpha()
pygame.display.set_caption("Broom! || BETA::00.0.3")
clock = pygame.time.Clock()
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
print(mouse)
print(click)
screen.fill(blue)
screen.blit(BackgroundPNG,(0,0))
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects("V'Room!", largeText)
TextRect.center = ((width/2),(height/2))
screen.blit(TextSurf, TextRect)
#Button
#GO BUTTON | V
if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
pygame.draw.rect(screen, green_bright,(75,400,100,50))
if click[0] == 1 and click != None:
print("GO == 1 ! == None")
car
else:
pygame.draw.rect(screen, green,(75,400,100,50))
smallText = pygame.font.Font("freesansbold.ttf",20)
TextSurf, TextRect = text_objects("GO", smallText)
TextRect.center = ((75+(100/2)),(400+(50/2)))
screen.blit(TextSurf, TextRect)
if 550+100 > mouse[0] > 550 and 400+50 > mouse[1] > 400:
pygame.draw.rect(screen, red_bright,(550,400,100,50))
if click[0] == 1 and click != None:
pygame.quit()
quit()
else:
pygame.draw.rect(screen, red,(550,400,100,50))
TextSurf, TextRect = text_objects("Exit", smallText)
TextRect.center = ((550+(100/2)),(400+(50/2)))
screen.blit(TextSurf, TextRect)
pygame.display.flip()
pygame.display.update()
clock.tick(15)
game_intro()
#################################
def car(x,y):
x = (width * 0.45)
y = (height * 0.8)
screen.blit(carImg, (x,y))
You have stupid mistake - to call function car you need parenthesis and arguments (for example 10,10)
if click != None and click[0] == 1:
print("GO == 1 ! == None")
car(10, 10) # parenthesis and arguments
By the way: better first check click != None, next click[0] == 1.
If click is None then checking first click[0] == 1 give you error. if check is None and you first check click != None then and doesn't check click[0] == 1.

Categories