私はパイゲームでゲームを作っています。最初の画面では、(i)ゲームを開始し、(ii)命令で新しい画面を読み込み、そして(iii)プログラムを終了するために押すことができるボタンが必要です。 。python/pygameでボタンを作る方法は?
私はこのコードをボタン作成用にオンラインで見つけましたが、私はそれを実際に理解していません(私はオブジェクト指向プログラミングでそれほど優れていません)。私はそれが何をしているかについていくつかの説明を得ることができればそれはすばらしいだろう。また、私はそれを使用し、ファイルパスを使用して私のコンピュータ上のファイルを開こうとすると、私はエラーshを取得します:ファイルパス:許可が拒否され、解決する方法がわかりません。
#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()
私を助けることができる誰かにありがとう。私はあなたのためのコード例を持っていないが、私はそれを行うだろうか
申し訳ありませんが、お試しいただきありがとうございます。 – user1334014