小编Ste*_*ven的帖子

根据pygame中的点击更改图片

我已经完成了一次Google搜索,并搜索了我的两本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 = …
Run Code Online (Sandbox Code Playgroud)

python pygame onclick button

1
推荐指数
1
解决办法
8150
查看次数

为什么双位数选择不能在python中返回正确的响应?

我正在学习如何使用python.在了解我不理解的定义时发现了一个问题.我给出一个简单的菜单,选择0-4.如果用户选择4以上,则应该收到一条消息"这不是一个有效的选择..."

但是,如果您输入的值大于或等于10,则除菜单外不会返回任何内容...没有消息.

在此先感谢任何想法.

这是我的代码:

# Multitasker
# Allows User to Pick an Item that is Defined.

def exit():
    print("See You Later!")
def task1():
    print("This is Task 1!")
def task2():
    print("This is Task 2!")
def task3():
    print("This is Task 3!")
def task4():
    print("This is Task 4!")

choice = None
while choice != "0":
    print(
        """
        Multitask Selector

        0 - Quit
        1 - Task 1
        2 - Task 2
        3 - Task 3
        4 - Task 4
        """
        )

    choice = input("Pick …
Run Code Online (Sandbox Code Playgroud)

python task

0
推荐指数
1
解决办法
92
查看次数

标签 统计

python ×2

button ×1

onclick ×1

pygame ×1

task ×1