python pygame screen.fill

时间:2017-12-12 13:08:14

标签: python python-3.x pygame

我有问题。我有一个已经充满文字和按钮的屏幕,我想按下按钮并用颜色填充我的背景。 (我知道关于这个主题有数百个问题,但我无法理解它们)。这是代码的一部分,我有按钮。当我按下按钮时,我认为制作一个改变颜色的功能会很容易,但是当我按下按钮时它会改变颜色,只要我按住鼠标按钮。当我没有按住时,它只返回带有文本和按钮的屏幕。

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()

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

import pygame
import sys


pygame.init()
#############
pygame.mixer.music.load('Invincible.mp3')
pygame.mixer.music.play()

#############
clock = pygame.time.Clock() 

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)


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

gameIcon = pygame.image.load('gameicon.jpg')
pygame.display.set_icon(gameIcon)

pause = False

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


def GameOver():
    ####################################
    pygame.mixer.Sound.play("smb_gameover.wav")
    pygame.mixer.music.stop()
    ####################################
    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:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()



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

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

def button(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(gameDisplay, ac,(x,y,w,h))
        if click[0] == 1 and action != None:
            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()

def hide_text():
    global display_text
    global display_button_1
    global display_button_2

    display_text = False
    display_button_1 = False
    display_button_2 = True

def unpause():
    global pause
    pygame.mixer.music.unpause()
    pause = False


def paused():
    ############
    pygame.mixer.music.pause()
    #############
    largeText = pygame.font.SysFont("comicsansms",115)
    TextSurf, TextRect = text_objects("Paused", largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)


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


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

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


def game_intro():

    intro = True

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

        pilt1 = pygame.image.load('apoc2.jpg').convert()
        gameDisplay.blit(pilt1, [0,0])

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

        pygame.display.update()

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()



game_intro()
game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:0)

您必须重新组织代码。

我使用"subscenes",文字,按钮和fill()创建update()

我使用带有数字的变量display_subscene来决定显示哪个子场景。

您可以使用类来更好地组织它。

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

您必须使用event.type == MOUSEBUTTONDOWN

每个循环中的

编辑我使用event.type == MOUSEBUTTONDOWN来设置我在mouse_button中使用的变量button()(作为新参数),这样它就不会#39} ;按住按下按钮的所有时间点击。变量mouse_button必须在每0之前设置for event

我为屏幕Pause添加了半透明背景 - 您可以按p game_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()