如何使在Python / pygame的按钮? [英] How to make buttons in python/pygame?
问题描述
我在做一个游戏pygame的和第一个屏幕,我希望这是你可以preSS到(我)开始游戏按钮上,(二)装入说明一个新的屏幕,以及(iii )退出程序。
我已经找到了按钮使这个code网上,但我并不真正了解它(我不是在面向对象的编程好)。如果我能得到一些解释,它在做什么,将是巨大的。此外,当我使用它,并尝试使用的文件路径打开我的电脑上的文件,我得到的错误SH:文件路径:权限被拒绝,我不知道如何解决。
。 #load_image在加载图像最pygame的程序中使用
DEF load_image(姓名,色键=无):
全名= os.path.join('数据',名称)
尝试:
图像= pygame.image.load(全称)
除了pygame.error,消息:
打印无法加载图片:',全名
提高SystemExit,消息
图像= image.convert()
如果colorkey的不是无:
如果colorkey的是-1:
色键= image.get_at((0,0))
image.set_colorkey(colorkey的,RLEACCEL)
返回图像,image.get_rect()
一流的按钮(pygame.sprite.Sprite):
类用于创建按钮,使用setCords设置
左上边角的位置。方法pressed()返回
一个布尔值,应输入循环中被称为
高清__init __(个体经营):
pygame.sprite.Sprite .__的init __(个体经营)
self.image,self.rect = load_image('button.png',-1) 高清setCords(个体经营,X,Y):
self.rect.topleft = X,Y DEF pressed(个体经营,鼠标):
如果小鼠[0]≥ self.rect.topleft [0]:
如果小鼠[1]≥ self.rect.topleft [1]:
如果小鼠[0]&下; self.rect.bottomright [0]:
如果小鼠[1]; self.rect.bottomright [1]:
返回True
其他:返回False
其他:返回False
其他:返回False
其他:返回False
高清的main():
按钮=创建按钮()#键类
button.setCords(200,200)#键显示在200,200
而1:
在pygame.event.get事件():
如果event.type == MOUSEBUTTONDOWN:
鼠标= pygame.mouse.get_pos()
如果按钮pressed(鼠):#巴顿的pressed方法被调用
打印(按钮点击)
如果__name__ =='__main__':main()的
感谢您的人谁可以帮我。
我没有一个code例如你的,但我怎么会做的是:
- 请一个Button类,用文字去的按钮作为构造函数参数
- 创建一个pygame的表面,无论是图像或填充矩形
- 渲染与Font.Render的东西就可以了文本的Pygame
- BLIT到游戏画面,保存正确。
- 检查,对鼠标点击,看mouse.get_pos(),它的按钮,主表面的blit返回的矩形坐标一个匹配。
这是类似你的例子是干什么的,但不同的静止。
I'm making a game in pygame and on the first screen I want there to be buttons that you can press to (i) start the game, (ii) load a new screen with instructions, and (iii) exit the program.
I've found this code online for button making, but I don't really understand it (I'm not that good at object oriented programming). If I could get some explanation as to what it's doing that would be great. Also, when I use it and try to open a file on my computer using the file path, I get the error sh: filepath :Permission denied, which I don't know how to solve.
#load_image is used in most pygame programs for loading images
def load_image(name, colorkey=None):
fullname = os.path.join('data', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image:', fullname
raise SystemExit, message
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
class Button(pygame.sprite.Sprite):
"""Class used to create a button, use setCords to set
position of topleft corner. Method pressed() returns
a boolean and should be called inside the input loop."""
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('button.png', -1)
def setCords(self,x,y):
self.rect.topleft = x,y
def pressed(self,mouse):
if mouse[0] > self.rect.topleft[0]:
if mouse[1] > self.rect.topleft[1]:
if mouse[0] < self.rect.bottomright[0]:
if mouse[1] < self.rect.bottomright[1]:
return True
else: return False
else: return False
else: return False
else: return False
def main():
button = Button() #Button class is created
button.setCords(200,200) #Button is displayed at 200,200
while 1:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
mouse = pygame.mouse.get_pos()
if button.pressed(mouse): #Button's pressed method is called
print ('button hit')
if __name__ == '__main__': main()
Thank you to anyone who can help me.
I don't have a code example for you, but how I would do it is to:
- Make a Button class, with the text to go on the button as a constructor argument
- Create a PyGame surface, either of an image or filled Rect
- Render text on it with the Font.Render stuff in Pygame
- Blit to game screen, save that rect.
- Check, on mouse click, to see the mouse.get_pos() matches a coord in the rect that it returned by the blit of the button to the main surface.
That is similar to what your example is doing, although different still.
这篇关于如何使在Python / pygame的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!