python - 无法获取按钮来切换 pygame 中的更改

标签 python button pygame transition

我想知道为什么我的“GO”按钮不会切换

def game_start()

永久地,它会在按住按钮的同时进行切换,但是当您松开按钮时,它会返回主菜单?

我也很好奇是否有一种方法可以使文本和按钮消失 游戏开始时你按下开始按钮吗?

我对 python 很陌生,所以对于我犯了错误和/或需要添加/更改的任何代码,解释都会很好。

import sys
import pygame
from pygame.locals import *
pygame.init()

size = width, height = 720, 480
speed = [2, 2]

#Colours
black = (0,0,0)
blue = (0,0,255)
green = (0,200,0)
red = (200,0,0)
green_bright = (0,255,0)
red_bright = (255,0,0)

screen = pygame.display.set_mode(size)

#Pictures
road = pygame.image.load(r"C:\Users\John\Desktop\Michael\V'Room External\1.png")
BackgroundPNG = pygame.image.load(r"C:\Users\John\Desktop\Michael\V'Room External\BackgroundPNG.png")
carImg = pygame.image.load(r"C:\Users\John\Desktop\Michael\V'Room External\Sp1.png").convert_alpha()


pygame.display.set_caption("Broom! || BETA::00.0.3")

clock = pygame.time.Clock()

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

def game_intro():
    intro = True

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

        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        print(mouse)
        print(click)

        screen.fill(blue)
        screen.blit(BackgroundPNG,(0,0))

        largeText = pygame.font.Font('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("V'Room!", largeText)
        TextRect.center = ((width/2),(height/2))
        screen.blit(TextSurf, TextRect)

        #Button
        if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
            pygame.draw.rect(screen, green_bright,(75,400,100,50))

            if click != None and click[0] == 1:
                print("GO == 1 ! == None")
                x = 350
                y = 370
                game_start()
        else:
            pygame.draw.rect(screen, green,(75,400,100,50))

        smallText = pygame.font.Font("freesansbold.ttf",20)
        TextSurf, TextRect = text_objects("GO", smallText)
        TextRect.center = ((75+(100/2)),(400+(50/2)))
        screen.blit(TextSurf, TextRect)

        if 550+100 > mouse[0] > 550 and 400+50 > mouse[1] > 400:
            pygame.draw.rect(screen, red_bright,(550,400,100,50))

            if click != None and click[0] == 1:
                pygame.quit()
                quit()
        else:
            pygame.draw.rect(screen, red,(550,400,100,50))

        TextSurf, TextRect = text_objects("Exit", smallText)
        TextRect.center = ((550+(100/2)),(400+(50/2)))
        screen.blit(TextSurf, TextRect)

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

def game_start():
   print("Car Loaded Sucessfully")
   screen.blit(road, (0,0))
   screen.blit(carImg, (350,370))

game_intro()

最佳答案

解决方案之一是使用 game_started 变量而不是函数 game_start() 并用它来决定绘制什么 - 标题或汽车和道路、GO 或 STOP 按钮等

我使用矩形代替位图来制作完整的工作示例。

import pygame

# --- constants ----

size = width, height = 720, 480
speed = [2, 2]

#Colours

black = (0,0,0)
blue = (0,0,255)
green = (0,200,0)
red = (200,0,0)
green_bright = (0,255,0)
red_bright = (255,0,0)

# --- functions ---

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


def game_intro():

    largeText = pygame.font.Font('freesansbold.ttf',115)
    smallText = pygame.font.Font("freesansbold.ttf",20)

    text_vroom, text_vroom_rect = text_objects("V'Room!", largeText)
    text_vroom_rect.center = ((width/2),(height/2))

    text_go, text_go_rect = text_objects("GO", smallText)
    text_go_rect.center = ((75+(100/2)),(400+(50/2)))

    text_stop, text_stop_rect = text_objects("STOP", smallText)
    text_stop_rect.center = ((75+(100/2)),(400+(50/2)))

    text_exit, text_exit_rect = text_objects("Exit", smallText)
    text_exit_rect.center = ((550+(100/2)),(400+(50/2)))

    game_started = False 

    intro = True

    while intro:

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

        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        screen.fill(blue)
        #screen.blit(BackgroundPNG,(0,0))


        # road and car - or title

        if game_started:
           screen.blit(road, (0,0))
           screen.blit(carImg, (350,370))
        else:
            screen.blit(text_vroom, text_vroom_rect)

        # Button GO/STOP

        if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
            pygame.draw.rect(screen, green_bright,(75,400,100,50))

            if click != None and click[0] == 1:
                # toggle True/False
                game_started = not game_started
        else:
            pygame.draw.rect(screen, green,(75,400,100,50))

        # draw GO or STOP
        if not game_started:
            screen.blit(text_go, text_go_rect)
        else:
            screen.blit(text_stop, text_stop_rect)

        # Button EXIT

        if 550+100 > mouse[0] > 550 and 400+50 > mouse[1] > 400:
            pygame.draw.rect(screen, red_bright,(550,400,100,50))

            if click != None and click[0] == 1:
                pygame.quit()
                quit()
        else:
            pygame.draw.rect(screen, red,(550,400,100,50))

        screen.blit(text_exit, text_exit_rect)

        pygame.display.flip()

        clock.tick(15)

# --- main ---

pygame.init()

screen = pygame.display.set_mode(size)
pygame.display.set_caption("Broom! || BETA::00.0.3")

#Pictures
road = pygame.surface.Surface( size )
road.fill(black)

carImg =  pygame.surface.Surface( (10,10) )
road.fill(green)

clock = pygame.time.Clock()

game_intro()

编辑:第二个解决方案是使用自己的while循环、自己的按钮等创建函数(game_running)。

我必须使用pygame.time.wait(),因为pygame.mouse.get_pressed()对于单击按钮来说不是一个好的功能。计算机(和 while 循环)太快(对于人类点击而言)并且 pygame.mouse.get_pressed() 多次切换按钮。最好使用 pygame.event.get() 来进行单击。

import pygame

# --- constants ----

size = width, height = 720, 480
speed = [2, 2]

#Colours

black = (0,0,0)
blue = (0,0,255)
green = (0,200,0)
red = (200,0,0)
green_bright = (0,255,0)
red_bright = (255,0,0)

# --- functions ---

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


def game_intro():

    text_vroom, text_vroom_rect = text_objects("V'Room!", largeText)
    text_vroom_rect.center = ((width/2),(height/2))

    text_go, text_go_rect = text_objects("GO", smallText)
    text_go_rect.center = ((75+(100/2)),(400+(50/2)))

    text_exit, text_exit_rect = text_objects("Exit", smallText)
    text_exit_rect.center = ((550+(100/2)),(400+(50/2)))

    running = True

    while running:

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

        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        screen.fill(blue)
        screen.blit(text_vroom, text_vroom_rect)

        # Button GO

        if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
            pygame.draw.rect(screen, green_bright,(75,400,100,50))

            if click != None and click[0] == 1:
                # wait because `pygame.mouse.get_pressed()` is too fast for human clik
                pygame.time.wait(100)
                # run game
                game_running()
        else:
            pygame.draw.rect(screen, green,(75,400,100,50))

        screen.blit(text_go, text_go_rect)

        # Button EXIT

        if 550+100 > mouse[0] > 550 and 400+50 > mouse[1] > 400:
            pygame.draw.rect(screen, red_bright,(550,400,100,50))

            if click != None and click[0] == 1:
                pygame.quit()
                quit()
        else:
            pygame.draw.rect(screen, red,(550,400,100,50))

        screen.blit(text_exit, text_exit_rect)

        pygame.display.flip()

        clock.tick(15)

def game_running():

    text_stop, text_stop_rect = text_objects("STOP", smallText)
    text_stop_rect.center = ((75+(100/2)),(400+(50/2)))

    text_exit, text_exit_rect = text_objects("Exit", smallText)
    text_exit_rect.center = ((550+(100/2)),(400+(50/2)))

    running = True

    while running:

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

        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        screen.fill(blue)
        #screen.blit(BackgroundPNG,(0,0))


        # road and car - or title

        screen.blit(road, (0,0))
        screen.blit(carImg, (350,370))

        # Button STOP

        if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
            pygame.draw.rect(screen, green_bright,(75,400,100,50))

            if click != None and click[0] == 1:
                # return to menu
                return
        else:
            pygame.draw.rect(screen, green,(75,400,100,50))

        # draw STOP
        screen.blit(text_stop, text_stop_rect)

        # Button EXIT

        if 550+100 > mouse[0] > 550 and 400+50 > mouse[1] > 400:
            pygame.draw.rect(screen, red_bright,(550,400,100,50))

            if click != None and click[0] == 1:
                pygame.quit()
                quit()
        else:
            pygame.draw.rect(screen, red,(550,400,100,50))

        screen.blit(text_exit, text_exit_rect)

        pygame.display.flip()

        clock.tick(15)


# --- main ---

pygame.init()

screen = pygame.display.set_mode(size)
pygame.display.set_caption("Broom! || BETA::00.0.3")

# pictures
road = pygame.surface.Surface( size )
road.fill(black)

carImg =  pygame.surface.Surface( (10,10) )
road.fill(green)

# fonts
largeText = pygame.font.Font('freesansbold.ttf',115)
smallText = pygame.font.Font("freesansbold.ttf",20)

# others

clock = pygame.time.Clock()

game_intro()

关于python - 无法获取按钮来切换 pygame 中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34458800/

相关文章:

python - 为什么在 IF 条件后出现 'Invalid syntax' 错误?

python - 调用变量父类(super class)方法

python - 如何确定数据帧与 Nan 的相关性?

css - 联系表格 WP 插件不显示提交按钮

python - PyGame MOUSEBUTTONDOWN 事件未注册?

python - ActivePython 和 Python 有什么区别?

jquery - 如何确保我的 jQueryUI 按钮文本不超过 25 个字符?

java - 错误: cannot assign a value to final variable

python - Pygame 和 Sprite 的旋转

Python 速成类(class) - 外星人入侵 - 错误背景