繁体   English   中英

我如何为开始游戏按钮制作 Function 以禁用开始屏幕并加载我的主游戏?

[英]How Could I Make A Function For The Start Game Button To Disable The Start Screen And Load My Main Game?

所以我在这里有一个游戏介绍,我想知道当我的 *** 鼠标单击开始游戏按钮以禁用 game_intro 时,我该如何制作它? 它目前没有做任何事情和视频

就像我如何在单击它时为开始按钮制作一个 function,它应该删除 game_intro 或禁用它并加载我的主游戏

这是我现在的游戏介绍


#---------------------------------------------------


def button(msg,x,y,w,h,ic,ac):
    mouse = pygame.mouse.get_pos()

    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
    else:
        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

        if clikc[0] == 1 and action != None:
            action()
        else:
            pygame.draw.rect(window,ic,(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 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)



#----------------------------------------------------------

我的完整代码脚本

您必须删除button()中的错误,并且必须在game_intro()中使用button() )

要解释的太多了,所以我只展示了最少的工作代码。

按钮可以工作,但仍然不理想。 如果新场景在同一个地方有按钮,就会有问题——它会自动点击它——但它需要完全不同的代码。 更多关于GitHub

import pygame

# --- constants --- (UPPER_CASE_NAMES)

RED = (200, 0, 0)
GREEN = (0, 200, 0)

BRIGHT_RED = (255, 0, 0)
BRIGHT_GREEN = (0, 255, 0)

BLACK = (0, 0, 0)

# --- all classes --- (CamelCaseNames)

class Player:
    pass
    # ... code ...

class Platform:
    pass
    # ... code ...

# etc.

# --- all functions --- (lower_case_names)

def text_objects(text, font):
    image = font.render(text, True, BLACK)
    rect  = image.get_rect()
    return image, rect

def button(window, 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(window, ac,(x,y,w,h))
        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(window, ic,(x,y,w,h))

    image = small_font.render(msg, True, BLACK)
    rect = image.get_rect()
    rect.center = (x+(w/2), y+(h/2))

    window.blit(image, rect)

def game_intro():

    running = True

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        window.fill((255,255,255))

        image, rect = text_objects("Stolen Hearts!", large_font)
        rect.center = (400, 400)
        window.blit(image, rect)

        button(window, "Start Game", 150, 450, 120, 50, GREEN, BRIGHT_GREEN, main_game)
        button(window, "Quit Game",  350, 450, 120, 50, RED, BRIGHT_RED, quit_game)

        pygame.display.update()
        clock.tick(15)

def main_game():

    running = True

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        window.fill((255,255,255))

        image, rect = text_objects("Main Game", large_font)
        rect.center = (400, 400)
        window.blit(image, rect)

        button(window, "Intro",     550, 450, 120, 50, GREEN, BRIGHT_GREEN, game_intro)
        button(window, "Quit Game", 350, 450, 120, 50, RED, BRIGHT_RED, quit_game)

        pygame.display.update()
        clock.tick(15)

def quit_game():
    print("You quit game")
    pygame.quit()
    quit()

# --- main ---

pygame.init()

window = pygame.display.set_mode((800, 800))

# it has to be after `pygame.init()`    
#small_font = pygame.font.Font("freesansbold.ttf", 20)
#large_font = pygame.font.Font('BLOODY.ttf', 115)
small_font = pygame.font.Font(None, 20)
large_font = pygame.font.Font(None, 115)

clock = pygame.time.Clock()

game_intro()

顺便说一句:我也以不同的方式组织代码

  • imports后直接将所有常量值放在一个地方,它们使用UPPER_CASE_NAMES (PEP8) 和
  • 所有类都在一个地方 - 直接在常量之后和pygame.init()之前 - 他们使用CamelCaseNames (PEP8)
  • 所有功能都在一个地方 - 在课程之后和之前pygame.init() - 他们使用lower_case_names (PEP8)

请参阅: PEP 8 -- Python 代码的样式指南

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM