2015-12-24 104 views
1

这段代码只是打开一个pygame窗口并显示一个带有功能按钮的图片(main menu),但我想知道是否有办法让代码跳到底部(def car(x,y):)我按下Go按钮后会开始吗?如果有办法,我该如何做到这一点?主菜单到游戏转换

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)) 

回答

1

你有愚蠢的错误 - 调用函数car需要括号和参数(如10,10)

if click != None and click[0] == 1: 
    print("GO == 1 ! == None") 
    car(10, 10) # parenthesis and arguments 

顺便说一句:最好先检查click != None,未来click[0] == 1。 如果clickNone,那么先检查click[0] == 1会报错。如果checkNone并且您首先检查click != None,则and不检查click[0] == 1