第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何為開始游戲按鈕創(chuàng)建一個功能來禁用開始屏幕并加載我的主游戲?

我如何為開始游戲按鈕創(chuàng)建一個功能來禁用開始屏幕并加載我的主游戲?

慕容708150 2023-01-04 15:50:56
所以我在這里有一個游戲介紹,我想知道當我的 *** 鼠標單擊開始游戲按鈕以禁用 game_intro 時,我該如何制作它?它目前沒有做任何事情和視頻就像我如何在單擊它時為開始按鈕創(chuàng)建一個功能,它應該刪除 game_intro 或禁用它并加載我的主游戲這是我現(xiàn)在的游戲介紹#---------------------------------------------------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))我的完整代碼腳本
查看完整描述

1 回答

?
胡子哥哥

TA貢獻1825條經(jīng)驗 獲得超6個贊

你必須刪除錯誤button()并且你必須button()使用game_intro()

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

按鈕可以工作,但仍然不理想。如果新場景在同一個地方有按鈕就會有問題——它會自動點擊它——但它需要完全不同的代碼。更多關于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 代碼風格指南


查看完整回答
反對 回復 2023-01-04
  • 1 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號