Python 根据pygame中的点击更改图像

Python 根据pygame中的点击更改图像,python,button,onclick,pygame,Python,Button,Onclick,Pygame,我在谷歌上搜索了两本python初学者书籍,找到了如何做到这一点。我想这一定是一项简单的任务。基本上,我正在使用pygame和python 我想如果我点击按钮1\u图像,它会变成按钮1选择图像,对吗?如果你点击button2_image,它会将button1选择_image设置回button1_image,而button2_image会更改为button2选择_image 所以我想知道的是,这是一个简单的if-else语句还是更复杂的语句。显然,这些按钮稍后会做一些其他的事情,但是我找不到一个基

我在谷歌上搜索了两本python初学者书籍,找到了如何做到这一点。我想这一定是一项简单的任务。基本上,我正在使用pygame和python

我想如果我点击按钮1\u图像,它会变成按钮1选择图像,对吗?如果你点击button2_image,它会将button1选择_image设置回button1_image,而button2_image会更改为button2选择_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()

在这里,我快速地将其显示出来,以显示鼠标是如何工作的。 行
如果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")
import pygame,sys
从pygame.locals导入*
计时器=30
屏幕X=200
屏幕Y=200
screen=pygame.display.set_模式((screen_X,screen_Y))
时钟=pygame.time.clock()#滴答作响
结束=按钮1=按钮2=错误
拐角1=(28,18)#按钮1的左上角
拐角2=(56,18)#按钮2的左上角
图像长度=100#按钮长度
图像高度=100按钮高度
计数器=0
#主回路:
结束时==False:
计数器+=1
时钟滴答(计时器)
对于pygame.event.get()中的事件:
如果event.type==KEYDOWN:
如果event.key==K_转义:
结束=正确#离开时间
打印(“用户提前停止游戏”)
elif event.type==MOUSEBUTTONDOWN:
如果event.button==1:
鼠标x,鼠标y=event.pos

如果(mouse_x>=corner1[0])和(mouse_x=corner1[1])和(mouse_y=corner2[0])和(mouse_x=corner2[1])和(mouse_y在这里,我将其加起来以显示鼠标是如何工作的。 行
如果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")
import pygame,sys
从pygame.locals导入*
计时器=30
屏幕X=200
屏幕Y=200
screen=pygame.display.set_模式((screen_X,screen_Y))
时钟=pygame.time.clock()#滴答作响
结束=按钮1=按钮2=错误
拐角1=(28,18)#按钮1的左上角
拐角2=(56,18)#按钮2的左上角
图像长度=100#按钮长度
图像高度=100按钮高度
计数器=0
#主回路:
结束时==False:
计数器+=1
时钟滴答(计时器)
对于pygame.event.get()中的事件:
如果event.type==KEYDOWN:
如果event.key==K_转义:
结束=正确#离开时间
打印(“用户提前停止游戏”)
elif event.type==MOUSEBUTTONDOWN:
如果event.button==1:
鼠标x,鼠标y=event.pos

如果(mouse_x>=corner1[0])和(mouse_x=corner1[1])和(mouse_y=corner2[0])和(mouse_x=corner2[1])和(mouse_y以下是一些伪代码:

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
  ...

下面是一些伪代码:

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
  ...

查看此示例菜单: 特别是他的代码:


它创建了一个类
选项
,当鼠标悬停时,该选项会改变颜色。您可以对其进行扩展,因此单击按钮即可调用一个函数。(即:
new\u game()
show\u options()
等)

查看此示例菜单: 特别是他的代码:


它创建了一个类
选项
,当鼠标悬停时,该选项会改变颜色。您可以对其进行扩展,因此,单击按钮可调用一个函数。(即:
new\u game()
show\u options()
等)

我对pyGame不是特别熟悉,但似乎您只想在鼠标上设置
按钮1
图像为
按钮1选择图像
。在事件发生之前,我添加了一条评论,但我忘了您需要处理事件才能获得鼠标点击。请看。请记住,作者是出于介绍目的而使用的ses,但你会在游戏中使用。我会研究一下。谢谢。此代码看起来不像pygame风格。我用pygame编写了很多代码,但我不能完全理解。也许你应该添加一个关于
livewire
的标签。我对pygame不是特别熟悉,但似乎你只想设置
button1
im年龄到
按钮1在鼠标上选择图像在事件之前我添加了一条评论,但我忘记了你需要处理事件才能获得鼠标点击。请看。请记住,作者使用的是介绍性的目的,但你会在游戏中使用。我会研究它。谢谢。此代码看起来不像pygame风格。我已经用pygame编写了很多代码,但我不能完全理解。也许你应该添加一个关于
livewire
的标签。非常感谢。我的一个问题是如何在代码中显示图像?我显然必须研究这一点,并了解正在做什么。在底部的打印语句中。只需将所选图像用于按钮如果button1或button2变量分别为
True
,则为ns 1或2。如果未选择任何变量,则为其他变量,因此您将两个图像都作为未选择的按钮。如果您不知道如何使用图像等,请在此处查看:这确实对我有帮助。您自己尝试一下,如果您仍然被卡住,则堆栈交换仍将继续我在这里回答你的问题:)谢谢!我一有时间,一定会查出来的!:)非常感谢。我的一个问题是如何在代码中显示图像?很明显,我必须研究这一点,并了解正在做什么。在底部的打印声明中。如果button1或button2变量分别为
True
,只需为按钮1或2使用所选图像即可。否则,如果没有选择任何图像,则将两个图像都作为未选择的按钮。如果你不知道如何使用图像之类的东西,看看这里:It r