我已经做了谷歌搜索,通过我的两个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()
在这里,我掀起这达到显示鼠标的工作原理。 该生产线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")
在底部的打印报表。 简单地使用按钮1或2所选择的图像,如果按钮1或BUTTON2变量分别是True
。 所以你有两个图像未选定按钮如果都没有选的人会是。 如果你不知道如何使用图像等,看看在这里: http://www.pygame.org/docs/这真的帮助了我。 尝试一下你自己,如果你还停留堆栈交易所将仍然在这里为您的问题:)
希望能帮助到你
下面是一些伪代码:
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
...
看看这个例子菜单: https://stackoverflow.com/a/10747990/341744专门针对他的代码: https://gist.github.com/2802185
它创建一个类Option
,当你将鼠标放置哪些改变颜色。 你可以扩展它,于是就点击,按钮调用一个函数。 (即: new_game()
show_options()
等)