改变形象的基础上单击pygame的 [英] change image based on click in pygame
问题描述
我已经做了谷歌搜索,并通过我的两个Python初学者书籍搜索以找到如何做到这一点。我认为它是一个简单的任务。基本上,我与蟒蛇pygame的工作。
我想,如果我点击button1_image,它变为button1select_image,对不对?如果你点击button2_image,它设置button1select_image回button1_image和button2_image变化button2select_image。
那么,我想知道是,如果它是一个简单的if else语句或者是它要复杂得多。显然,按钮会做其他的事情以后,但我无法找到如何基于用户点击鼠标做这样的事情的教程。
#键鼠标点击图片变化
#演示了一个按钮图像转换到另一个基于鼠标的点击。从livewires导入游戏,颜色games.init(SCREEN_WIDTH = 281,SCREEN_HEIGHT = 500,FPS = 50)button1_image = games.load_image(button1.png,透明= FALSE)
按钮1 = games.Sprite(图像= button1_image,X = 28,Y = 18)
games.screen.add(按钮1)button1select_image = games.load_image(button1select.png,透明= FALSE)
button1select = games.Sprite(图像= button1select_image,X = 28,Y = 18)
games.screen.add(button1select)button2_image = games.load_image(button2.png,透明= FALSE)
按钮2 = games.Sprite(图像= button2_image,X = 56,Y = 18)
games.screen.add(按钮2)button2select_image = games.load_image(button2select.png,透明= FALSE)
button2select = games.Sprite(图像= button2select_image,X = 56,Y = 18)
games.screen.add(button2select)games.screen.mainloop()
在这里,我掀起这件事说明如何使用鼠标工作。
行如果event.button == 1:
检查是否鼠标左键一直pressed,1,如果你想鼠标右键更改为2。
进口pygame的,SYS
从pygame.locals导入*TIMER = 30
SCREEN_X = 200
SCREEN_Y = 200屏幕= pygame.display.set_mode((SCREEN_X,SCREEN_Y))
时钟= pygame.time.Clock()#滴答滴答结束= =按钮1 =的Button2假corner1 =(28,18)#top返回左按钮1角
corner2 =(56,18)#top返回左按钮2角image_length = 100 #length的按钮中的
IMAGE_HEIGHT = 100 #height的按钮中的计数器= 0#Main循环:
而截至==错误:
计数器+ = 1
clock.tick(TIMER)
在pygame.event.get事件():
如果event.type == KEYDOWN:
如果event.key == K_ESCAPE:
结束= TRUE#时间离开
打印(游戏停止的早期用户)
ELIF event.type == MOUSEBUTTONDOWN:
如果event.button == 1:
mouse_x,mouse_y = event.pos
如果(mouse_x&GT = corner1 [0])和(mouse_x&下; = corner1 [0] + image_length)和(mouse_y&GT = corner1 [1])和(mouse_y&下; = corner1 [1] + IMAGE_HEIGHT):
打印(按钮,一个是选择了)
按钮1 = TRUE
按钮2 =假
elif的(mouse_x&GT = corner2 [0])和(mouse_x&下; = corner2 [0] + image_length)和(mouse_y&GT = corner2 [1])和(mouse_y&下; = corner2 [1] + IMAGE_HEIGHT):
打印(两个按钮选择)
BUTTON1 =假
按钮2 = TRUE
其他:
打印(这不是一个按钮)
BUTTON1 =假
按钮2 =假
如果计数器==定时器:#prints每秒一次的声明
计数器= 0
如果BUTTON1 ==真:
打印(按钮,一个是当前选择)
ELIF按钮2 ==真:
打印(两个按钮当前选择)
其他:
打印(当前选择否按钮)
在底部的打印报表。只需使用按钮1或2所选择的形象,如果Button1的或按钮2变量,分别是真
。所以你有两个图像的未选定按钮如果都没有选对别人会。如果你不知道如何使用图像等,看看在这里: HTTP:// WWW。 pygame.org/docs/ 的这真的帮助了我。尝试一下自己,如果你还停留堆栈交易所将仍然在这里为您的问题:)
希望它能帮助
I've done a google search and searched through my two python beginner books to find how to do this. I assume it has to be a simple task. Basically, I'm working with pygame with python.
I want that if I click on the button1_image, it changes to button1select_image, right? And if you click on button2_image, it sets button1select_image back to button1_image, and button2_image changes to button2select_image.
So what I am wondering is if it is a simple if else statement or is it much more complicated. Obviously, the buttons will do something else later but I cannot find a tutorial on how to do something like this based on the click of the users mouse.
# 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()
Here, I whipped this up to show how the mouse works.
The line if event.button == 1:
checks if the left mouse button has been pressed, change the 1 to a 2 if you want the right mouse button.
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")
At the print statements at the bottom. Simply use the selected image for buttons 1 or 2 if the button1 or button2 variable, respectively, is True
. The else would be if none are selected so you have both of the images as the unselected button. If you don't know how to use images and the like, have a look around here: http://www.pygame.org/docs/ It really helped me. Try it out yourself, and if you're still stuck Stack Exchange will still be here for your questions :)
Hope it helps
这篇关于改变形象的基础上单击pygame的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!