1

我已经完成了谷歌搜索并搜索了我的两本 python 初学者书籍以找到如何做到这一点。我认为这必须是一项简单的任务。基本上,我正在使用带有 python 的 pygame。

我希望如果我单击 button1_image,它会更改为 button1select_image,对吗?如果单击 button2_image,它会将 button1select_image 设置回 button1_image,并且 button2_image 会更改为 button2select_image。

所以我想知道它是一个简单的 if else 语句还是更复杂。显然,这些按钮稍后会做其他事情,但我找不到关于如何根据用户鼠标点击执行此类操作的教程。

# Button Mouse Click Image Change
# Demonstrates changing from one button image to another based on click of mouse.

from livewires import games, color

games.init(screen_width = 281, screen_height = 500, fps = 50)

button1_image = games.load_image("button1.png", transparent = False)
button1 = games.Sprite(image = button1_image, x = 28,y = 18)
games.screen.add(button1)

button1select_image = games.load_image("button1select.png", transparent = False)
button1select = games.Sprite(image = button1select_image, x = 28,y = 18)
games.screen.add(button1select)

button2_image = games.load_image("button2.png", transparent = False)
button2 = games.Sprite(image = button2_image, x = 56,y = 18)
games.screen.add(button2)

button2select_image = games.load_image("button2select.png", transparent = False)
button2select = games.Sprite(image = button2select_image, x = 56,y = 18)
games.screen.add(button2select)

games.screen.mainloop()
4

3 回答 3

5

在这里,我把它翻了出来,以展示鼠标是如何工作的。该行if event.button == 1:检查是否已按下鼠标左键,如果需要鼠标右键,请将 1 更改为 2。

import pygame, sys
from pygame.locals import *

TIMER = 30
SCREEN_X = 200
SCREEN_Y = 200

screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y))
clock = pygame.time.Clock() #tick-tock

ending = button1 = button2 = False

corner1 = (28,18)  #Top Left corner of button 1
corner2 = (56,18)  #Top Left corner of button 2

image_length = 100 #length of the buttons
image_height = 100 #height of the buttons

counter = 0

#Main Loop:
while ending==False:
    counter+=1
    clock.tick(TIMER)
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                ending=True # Time to leave
                print("Game Stopped Early by user")
        elif event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                mouse_x, mouse_y = event.pos
                if (mouse_x >= corner1[0]) and (mouse_x <= corner1[0]+image_length) and (mouse_y >= corner1[1]) and (mouse_y <= corner1[1]+image_height):
                    print ("Button one is selected")
                    button1=True
                    button2=False
                elif (mouse_x >= corner2[0]) and (mouse_x <= corner2[0]+image_length) and (mouse_y >= corner2[1]) and (mouse_y <= corner2[1]+image_height):
                    print ("Button two is selected")
                    button1=False
                    button2=True
                else:
                    print ("That's not a button")
                    button1=False
                    button2=False
    if counter == TIMER:  #prints the statements once a second
        counter=0
        if button1==True:
            print ("Button one is currently selected")
        elif button2==True:
            print ("Button two is currently selected")
        else:
            print ("No buttons currently selected")

在底部的打印语句中。如果 button1 或 button2 变量分别为 ,则只需将所选图像用于按钮 1 或 2 True。否则,如果未选择任何图像,则您将两个图像都作为未选择的按钮。如果您不知道如何使用图像等,请在此处查看:http: //www.pygame.org/docs/这对我很有帮助。自己尝试一下,如果您仍然遇到问题,Stack Exchange 仍然会在这里解答您的问题 :)

希望能帮助到你

于 2012-06-04T20:47:56.957 回答
0

这是一些伪代码:

selected_button = None
buttons = [button1, button2]

...

for event in pygame.event.get():
  ...
  if event.type == MOUSEBUTTONDOWN:
      for b in buttons:
          if b.rect.collidepoint(event.pos):
              if selected_button == b:
                  # unselect this button
              else:
                  # unselect the old button (if there's one) and select this one
  ...
于 2012-06-04T22:15:22.700 回答
0

看看这个示例菜单:https ://stackoverflow.com/a/10747990/341744 特别是他的代码:https ://gist.github.com/2802185

它创建了一个类Option,当你鼠标悬停时它会改变颜色。您可以扩展它,因此单击时,按钮会调用一个函数。(即:new_game()show_options()

于 2012-06-05T06:22:40.753 回答