【问题标题】:Erasing screen with button用按钮擦除屏幕
【发布时间】:2023-04-01 19:32:01
【问题描述】:
#Imported Pygame
import pygame

#The Colors
BLACK = ( 0, 0, 0)
GREEN = ( 0, 255, 0)
WHITE = ( 255, 255, 255)
RED = ( 255, 0, 0)
ORANGE = ( 255, 115, 0)
YELLOW = ( 242, 255, 0)
BROWN = ( 115, 87, 39)
PURPLE = ( 298, 0, 247)
GRAY = ( 168, 168, 168)
PINK = ( 255, 0, 234)
pygame.init()
#The Screen
screen = pygame.display.set_mode([1000,500])
#Name of the window
pygame.display.set_caption("My first game")

clock = pygame.time.Clock()

#The sounds

# Positions of graphics
background_position = [0,0]
singleplayer_position = [350, 200]
tutorial_position = [350,300]
sorry_position = [0,0]
developer_position = [0,450]
rules_position = [0,0]
#The graphics
background_image = pygame.image.load("Castle.png").convert()
singleplayer_image = pygame.image.load("SinglePlayer.png").convert()
singleplayer_image.set_colorkey(WHITE)
tutorial_button = pygame.image.load("Tutorial_button.png").convert()
sorry_message = pygame.image.load("Sorry.png").convert()
sorry_message.set_colorkey(WHITE)
developer_message = pygame.image.load("Developer.png").convert()
developer_message.set_colorkey(WHITE)
Rules_image = pygame.image.load("Rules.png").convert()
#Main Loop __________________________

done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    # Copy of background or main menu
    screen.blit(background_image, background_position)

    #Copy of other images
    mouse_pos = pygame.mouse.get_pos()
    my_rect = pygame.Rect(350,200,393,75)
    tutorial_rect = pygame.Rect(350,300,393,75)
    screen.blit(singleplayer_image, singleplayer_position)
    screen.blit(tutorial_button, tutorial_position)
    screen.blit(developer_message, developer_position)
    if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
       screen.blit(sorry_message, sorry_position)
       correct = False
    if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
          #Here I make the screen fill white


    if python.mouse.get_pressed()[0]tutorial_rect.collidepoint(mouse.pos):
        correct = True
    if correct == True:
        screen.blit(Rules_image, rules_position)





    pygame.display.flip()
    clock.tick(60)
#To quit game
pygame.quit()

这基本上是我的代码...当我点击单人游戏按钮时,我将区域变为白色,但它不会留在那里。就像当我点击它并按住单人游戏按钮时它保持白色但当我取消点击屏幕时回到原来的样子。有没有我可以在点击单人游戏按钮时删除我之前所做的一切并开始一个新屏幕?'

好的,回到你给我的答案.. 我按照你说的那样构建了我的代码。

 if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
          color_white = True
          if color_white = True
             screen.fill(WHITE)

这不起作用,因为它仍然不能使屏幕保持白色。 我试过了。

if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
     color_white = True
if color_white = True
     screen.fill(WHITE)

这似乎也不起作用,因为它一直说 color_white 未定义。

【问题讨论】:

  • 您的代码在 tutorial_rect.collidepoint(mouse_pos): 处看起来已损坏
  • 哦,tutorial_rect 只是在这里打印代码时出错。在实际屏幕上它是缩进的。
  • 那你为什么不编辑你的问题呢?冒号是干什么用的?
  • 什么冒号? if python.mouse.get_pressed()[0]tutorial_rect.collidepoint(mouse.pos): 这个?我用它来设置正确为真。
  • 是的,编辑后更有意义

标签: python button pygame


【解决方案1】:

您的困惑源于 while 循环及其行为方式,因此我将解释这一点以回答您的问题。

快速说明:如果您没有使用代码末尾带有刻度的 pygame 时钟对象,请发表评论,我会在最后解释这一点,这样做很重要。(http://www.pygame.org/docs/ref/time.html)

好的,问题是:您的图片在点击后没有保持白色。如果你按住鼠标,它会保持白色,但一旦你抬起它就会消失。我假设您希望它在您解除鼠标点击后仍保持白色。

目前,您的代码在 if 语句中将图片涂成白色。

if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):

查看有关 .get_pressed() 功能的文档。如果按下鼠标按钮,则返回 True。因此,当您单击它时,它会显示 True,如果您按住它,它会显示 True。如果您没有单击或按住,则为 False。因此,它仅在单击或按住鼠标时将其着色为白色,因为那是它被告知这样做的时候。使它恢复正常的原因是您在循环中较早的 blits。每个循环,pygame 使图像正常(通过 blit)如果您的语句评估为 True,则将图片着色为白色。这意味着只要您的 if 语句为 False,图片就会保持正常。

要使其保持为白色,请使用布尔值。

    if pygame.mouse.get_pressed()[0] and  my_rect.collidepoint(mouse_pos):
        color_white = True

然后,不要将用于将白色着色的代码放在现在将布尔值设置为 true 的 if 语句中,而是在循环结束之前创建一个新的 if 语句。

if color_white:
    # Code to make the screen white.

这样,即使不按住它也可以保持白色。如果您想再次单击使其恢复正常。您可以扩展您的第一个 if 语句。

if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
      if color_white is True:
          color_white = False
      else:
          color_white = True

可以用更短的方式编码...

color_white = False if color_white == True else True

编辑:我写了前面的代码考虑事件。如果您使用 MOUSEBUTTONDOWN 事件更改颜色,此代码将起作用。但是,如果要使用 get_pressed(),则必须使用不同的鼠标按钮。如果你只使用左键,程序怎么知道是关闭还是打开这么多循环?

我会考虑 get_pressed 重写代码。

    if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
        color_white = True

    if pygame.mouse.get_pressed()[1] and my_rect.collidepoint(mouse_pos): # Not sure if it should be 1 or 2 in get_pressed, but I'm assuming they represent the right click and middle mouse button. So you can use these to turn the screen back to normal.
        color_white = False

Edit2:您的 color_white 未定义,因为直到代码中的 if 语句之后才定义它。因此,在您有机会单击(并定义它)之前,会运行一个循环并到达

if color_white:

但是 color_white 还不存在于计算机中。要解决此问题,请在您的 while 循环之前定义 color_white。

color_white = False # Default to not color the screen white.

【讨论】:

  • 为什么要检查color_white 是否等于True?您可以删除== True
  • 为了清楚起见,我把它写出来了,但你是对的,== True 并不真正需要。
  • 不起作用...我确实让 color_white = True 然后我做了一个 if 语句,如果 color_white 为真,则屏幕应该是白色的。但是,当我单击图像/按钮时,它会将屏幕变为白色,但是当我释放它时,它仍然会恢复到以前的图像。
  • 我编辑了问题以表明我尝试按照你说的做
  • 已编辑,如果这不起作用并且我在第三次尝试时无法弄清楚,我将删除答案。使用所有鼠标按钮(左、中、右)来打开和关闭颜色。通过几个测试,您可以确定要在 get_pressed 中使用哪个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多