Reputation: 117
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))
Upvotes: 2
Views: 1358
Reputation: 142711
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
.
Upvotes: 2