这段代码只会打开一个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))
答案 0 :(得分: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
。
如果click
为None
,则先检查click[0] == 1
会给您错误。如果check
为None
并且您首先检查click != None
,则and
不会检查click[0] == 1
。