Python游戏屏幕.fi

2024-09-28 03:12:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个问题。我有一个屏幕,已经充满了文本和按钮,我想按下按钮,填充我的背景颜色。(我知道关于这个话题有上百个问题,但我无法理解)。这是代码的一部分,我有按钮。我以为当我按下按钮时,很容易做出一个改变颜色的功能,但是当我按下按钮时,它会改变颜色,只要我按住鼠标键。当我不按住时,它只会返回一个带有文本和按钮的屏幕。在

def game_loop():
    global display_text
    global display_button_1
    global display_button_2

    gameExit = False
    display_text = True
    display_button_1 = True
    display_button_2 = False

    meie_font = pygame.font.SysFont("Arial", 36)

    tekst = "This game will go as far as you choose!"
    teksti_pilt1 = meie_font.render(tekst, False, (50,50,155))

    tekst2 = "You are the smith of your destiny"
    teksti_pilt = meie_font.render(tekst2, False, (50,50,155))
########################################################################
    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

        gameDisplay.fill(white)
########################################################################
        if display_text:
            gameDisplay.blit(teksti_pilt1, (100, 250))
            gameDisplay.blit(teksti_pilt, (100, 400))
########################################################################
        if display_button_1:
            button("Start playing", 300,500,150,50,green,bright_green, hide_text)
########################################################################
        if display_button_2:
            #gameDisplay.fill(white)
            bg = pygame.image.load("natsalo.jpg")
            gameDisplay.blit(bg, (0, 0))

            tekst = "You, just have a friendly dinner with your family!"
            meie_font = pygame.font.SysFont("Arial", 36)
            teksti_pilt = meie_font.render(tekst, False, (25,25,155))
            gameDisplay.blit(teksti_pilt, (100, 30))

            tekst = "It wasn't so usualy as always, it was always something...!"
            meie_font = pygame.font.SysFont("Arial", 36)
            teksti_pilt = meie_font.render(tekst, False, (25,25,155))
            gameDisplay.blit(teksti_pilt, (50, 100))

            tekst = "You are Lee Everett, you are a professor who taught history"
            meie_font = pygame.font.SysFont("Arial", 36)
            teksti_pilt = meie_font.render(tekst, False, (25,25,155))
            gameDisplay.blit(teksti_pilt, (25, 170))

            tekst = "For over six years at the University of Georgia"
            meie_font = pygame.font.SysFont("Arial", 36)
            teksti_pilt = meie_font.render(tekst, False, (25,25,155))
            gameDisplay.blit(teksti_pilt, (100, 240))

            tekst = "You, just have a friendly dinner with your family!"
            meie_font = pygame.font.SysFont("Arial", 36)
            teksti_pilt = meie_font.render(tekst, False, (25,25,155))
            gameDisplay.blit(teksti_pilt, (100, 310))

            button("I am to lazy to wash hands, just sit", 100,500,600,50,green,bright_green, hide_text)
            button("Wash your hands", 100,400,600,50,green,bright_green, next_screen)

        def next_screen():
            gameDisplay.fill( (0,0,0) )


        pygame.display.update()

这是完整的代码,如果需要的话。在

^{pr2}$

Tags: falsedisplaybuttonrender按钮pygamefontblit
1条回答
网友
1楼 · 发布于 2024-09-28 03:12:13

你必须重新组织代码。在

我用fill()、文本、按钮和update()创建{}。在

我使用带数字的变量display_subscene来决定显示哪个子序列。在

你可以用类来更好地组织它。在

现在主要的问题是使用mouse.get_pressed()的按钮,所以它总是在不同的子场景中的同一个位置按下按钮。我不得不把按钮移到不同的地方。在

您必须使用event.type == MOUSEBUTTONDOWN


编辑在每个循环中,我都使用event.type == MOUSEBUTTONDOWN来设置变量mouse_button,我在button()中使用这个变量(作为新参数),这样当我按住按钮时,它就不会一直单击。变量mouse_button必须在每个^{之前设置0。在

我为屏幕添加了半透明背景Pause-你可以按pgame_loop来查看它。在

import pygame
import sys

#  - constants  -

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)

red = (200,0,0)
green = (0,200,0)

bright_red = (255,0,0)
bright_green = (0,255,0)

block_color = (53,115,255)

#  - functions  -

def text_objects(text, font, color=black):
    textSurface = font.render(text, True, color)
    return textSurface, textSurface.get_rect()

def button(msg, x, y, w, h, ic, ac, click, action=None):

    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))
        if click == 1 and action != None: # <  click instead of click[0]
            pygame.mixer.music.stop()
            action()
    else:
        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )

    gameDisplay.blit(textSurf, textRect)

def quitgame():
    pygame.quit()
    sys.exit()
    quit()

#  - scenes  -

# - scene Paused -

def unpause():
    global pause
    #pygame.mixer.music.unpause()

    pause = False

def paused():
    global pause

    pause = True

    background_image = pygame.Surface((display_width, display_height)).convert()
    background_rect = background_image.get_rect(center=(display_width//2, display_height//2))
    background_image.set_alpha(220)

    gameDisplay.blit(background_image, background_rect)

    largeText = pygame.font.SysFont("comicsansms", 115)
    TextSurf, TextRect = text_objects("Paused", largeText, white)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    while pause:

        mouse_button = 0 # reset in every loop

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_button = event.button

        button("Continue",150,450,100,50,green,bright_green, mouse_button, unpause)
        button("Quit",550,450,100,50,red,bright_red, mouse_button, quitgame)

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

# - scene GameOver -

def GameOver():

    largeText = pygame.font.SysFont("comicsansms",115)
    TextSurf, TextRect = text_objects("Game Over", largeText)
    TextRect.center = ((display_width/2),(display_height/2))

    gameDisplay.blit(TextSurf, TextRect)

    while True:

        mouse_button = 0 # reset in every loop

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_button = event.button

        button("Play Again",150,450,100,50,green,bright_green, mouse_button, game_loop)
        button("Quit",550,450,100,50,red,bright_red, mouse_button, quitgame)

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

# - scene GameIntro -

def game_intro():

    # - objects -

    #pilt1 = pygame.image.load('apoc2.jpg').convert()

    # - loop -

    intro = True

    while intro:

        # - events -

        mouse_button = 0 # reset in every loop

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_button = event.button
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    paused()

        # - updates -

        button("Start",150,450,100,50,green,bright_green, mouse_button, game_loop)
        button("Quit",550,450,100,50,red,bright_red, mouse_button, quitgame)

        # - draws -

        #gameDisplay.blit(pilt1, [0,0])
        pygame.display.update()

# - scene GameLoop -

def goto_subscene(number):
    global display_subscene

    display_subscene = number

def game_loop():
    global display_subscene

    # at start display subscene 1
    display_subscene = 1 

    #bg = pygame.image.load("natsalo.jpg")

    meie_font = pygame.font.SysFont("Arial", 36)

    # - subscene 1 -

    tekst = "This game will go as far as you choose!"
    subscene_1_text_1 = meie_font.render(tekst, False, (50,50,155))

    tekst = "You are the smith of your destiny"
    subscene_1_text_2 = meie_font.render(tekst, False, (50,50,155))

    # - subscene 2 -

    tekst = "You, just have a friendly dinner with your family!"
    subscene_2_text_1 = meie_font.render(tekst, False, (25,25,155))

    tekst = "It wasn't so usualy as always, it was always something...!"
    subscene_2_text_2 = meie_font.render(tekst, False, (25,25,155))

    tekst = "You are Lee Everett, you are a professor who taught history"
    subscene_2_text_3 = meie_font.render(tekst, False, (25,25,155))

    tekst = "For over six years at the University of Georgia"
    subscene_2_text_4 = meie_font.render(tekst, False, (25,25,155))

    tekst = "You, just have a friendly dinner with your family!"
    subscene_2_text_5 = meie_font.render(tekst, False, (25,25,155))

    # - subscene 3 -

    tekst = "You killed by viruses !"
    subscene_3_text_1 = meie_font.render(tekst, False, white)

    # - subscene 4 -

    tekst = "You lost searching bathroom !"
    subscene_4_text_1 = meie_font.render(tekst, False, white)

    # - subscene 5 -

    tekst = "Good Bye !"
    subscene_5_text_1 = meie_font.render(tekst, False, white)

    # - loop -

    gameExit = False

    while not gameExit:

        mouse_button = 0 # reset in every loop

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_button = event.button
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    paused()

        if display_subscene == 1:
            gameDisplay.fill(white)

            gameDisplay.blit(subscene_1_text_1, (100, 250))
            gameDisplay.blit(subscene_1_text_2, (100, 400))

            button("Start playing", 300,500,150,50,green,bright_green, mouse_button, lambda:goto_subscene(2))

            pygame.display.update()

        elif display_subscene == 2:
            gameDisplay.fill(white)

            gameDisplay.blit(subscene_2_text_1, (100, 30))
            gameDisplay.blit(subscene_2_text_2, (50, 100))
            gameDisplay.blit(subscene_2_text_3, (25, 170))
            gameDisplay.blit(subscene_2_text_4, (100, 240))
            gameDisplay.blit(subscene_2_text_5, (100, 310))

            button("Wash your hands", 100,400,600,50,green,bright_green, mouse_button, lambda:goto_subscene(4))
            button("I am to lazy to wash hands, just sit", 100,500,600,50,green,bright_green, mouse_button, lambda:goto_subscene(3))

            pygame.display.update()

        elif display_subscene == 3:
            gameDisplay.fill(black)

            gameDisplay.blit(subscene_3_text_1, (100, 30))
            button("Go forward", 100,500,600,50,green,bright_green, mouse_button, lambda:goto_subscene(5))

            pygame.display.update()

        elif display_subscene == 4:
            gameDisplay.fill(black)

            gameDisplay.blit(subscene_4_text_1, (100, 30))
            button("Go forward", 100,500,600,50,green,bright_green, mouse_button, lambda:goto_subscene(5))

            pygame.display.update()

        elif display_subscene == 5:
            gameDisplay.fill(black)

            gameDisplay.blit(subscene_5_text_1, (100, 30))
            button("Exit", 100,500,600,50,green,bright_green, mouse_button, quitgame)

            pygame.display.update()

#  - main  -

# - init -

pygame.init()
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('One Day After')

# - m

clock = pygame.time.Clock() 
pause = False

game_intro()
game_loop()
pygame.quit()

相关问题 更多 >

    热门问题