下面是我想在高中学习的python初学者课程中制作的游戏代码。我一直遇到的问题是我做了一个按钮功能,但是每次我点击按钮它都会显示水平选择功能(我想要的那个),但只有我点击鼠标的时间,但是一旦我放手它会回到主菜单。如果有人可以帮助我,我会非常感激:)
Imports
import pygame
import time
import random
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
pygame.init()
#Music and Sound Imports
pygame.mixer.music.load("MenuMusic.wav")
# Set the width and height of the screen [width, height]
size = (1920, 980)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Panda On The Run v1")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
#Main Menu
Main_menu = pygame.image.load("Main Menu Backgroundv3.png")
# -------- Main Program Loop -----------
def level_select():
screen.fill(WHITE)
def button(msg, x,y,ic,ac,action = None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#Level Select Colour Change
if x + 625 > mouse[0] > x and y + 225 > mouse[1] > y:
font = pygame.font.SysFont('Calibri', 100, True, False)
text = font.render(msg,True,ac)
screen.blit(text, [90, 420])
if click[0] == 1 and action != None:
if action == "level":
level_select()
else:
font = pygame.font.SysFont('Calibri', 100, True, False)
text = font.render(msg,True,ic)
screen.blit(text, [90, 420])
#Settings Colour Change
if 1300 + 1900 > mouse[0] > 1300 and 350 + 250 > mouse[1] > 350:
font = pygame.font.SysFont('Calibri', 100, True, False)
text = font.render("Settings",True,RED)
screen.blit(text, [1450, 420])
#Controls Colour Change
if 35 + 800 > mouse[0] > 35 and 600 + 500 > mouse[1] > 600:
font = pygame.font.SysFont('Calibri', 100, True, False)
text = font.render("Controls",True,RED)
screen.blit(text, [150, 770])
#About Panda Run
if 1285 + 625 > mouse[0] > 1285 and 677 + 250 > mouse[1] > 677:
font = pygame.font.SysFont('Calibri', 80, True, False)
text = font.render("About Panda Run",True,RED)
screen.blit(text, [1325, 775])
#Main Intro
def game_intro():
#pygame.mixer.music.play(5)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
intro = True
while intro:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.blit(Main_menu, [0,-100])
#Level Select
font = pygame.font.SysFont('Calibri', 100, True, False)
text = font.render("Level Select",True,BLACK)
screen.blit(text, [90, 420])
#Settings
font = pygame.font.SysFont('Calibri', 100, True, False)
text = font.render("Settings",True,BLACK)
screen.blit(text, [1450, 420])
#Controls
font = pygame.font.SysFont('Calibri', 100, True, False)
text = font.render("Controls",True,BLACK)
screen.blit(text, [150, 770])
#About Panda Run
font = pygame.font.SysFont('Calibri', 80, True, False)
text = font.render("About Panda Run",True,BLACK)
screen.blit(text, [1325, 775])
button("Level Select", 35,335,BLACK,RED,"level")
pygame.display.update()
clock.tick(15)
# First, clear the screen to white. Don't put other drawing
# above this, or they will be erased with this command.
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
game_intro()
pygame.quit()
答案 0 :(得分:0)
它返回主菜单,因为level_select()
什么都不做。它用白色填充屏幕,然后返回bottom()
,您再次进入主菜单。不要指望(几乎)空函数会做一些魔术。
如果您要留在level_select
,那么您必须做点什么。
如果level_select
必须显示新菜单或其他内容,则在此功能中创建新的“mainloop”,它将显示level_select
中的元素,检查事件等。
每个"stage"
都需要拥有"mainloop"
(图片上为"while running"
),如下所示:
但是如果你使用课程会更容易。
GitHub上的“舞台示例”:furas/python-examples/pygame/stage-example