저는 파이 게임 (pygame)에서 게임을 만들고 있습니다. 첫 번째 화면에는 (i) 게임을 시작하고, (ii) 지시 사항이있는 새 화면을로드하고, iii) 프로그램을 종료 할 수있는 버튼이 있어야합니다. .파이썬/파이 게임에서 버튼을 만드는 방법?
이 코드는 버튼 만들기 용으로 온라인에서 찾았지만 실제로 이해하지는 못합니다 (객체 지향 프로그래밍이 좋지 않습니다). 만약 내가하고있는 일에 대해 몇 가지 설명을 할 수 있다면 좋을 것입니다. 또한 파일 경로를 사용하여 컴퓨터에서 파일을 열려고하면 오류가 발생합니다. filepath : Permission denied, 해결 방법을 모르겠습니다.
#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