मैं पायगम में एक गेम बना रहा हूं और पहली स्क्रीन पर मैं चाहता हूं कि बटन बनें (i) खेल शुरू करें, (ii) निर्देशों के साथ एक नई स्क्रीन लोड करें, और (iii) प्रोग्राम से बाहर निकलें ।पायथन/पायगमेम में बटन कैसे बनाएं?
मुझे यह कोड बटन बनाने के लिए ऑनलाइन मिला है, लेकिन मुझे वास्तव में यह समझ में नहीं आता है (मैं ऑब्जेक्ट उन्मुख प्रोग्रामिंग पर अच्छा नहीं हूं)। अगर मुझे कुछ स्पष्टीकरण मिल सकता है कि यह क्या कर रहा है तो यह बहुत अच्छा होगा। साथ ही, जब मैं इसका उपयोग करता हूं और फ़ाइल पथ का उपयोग कर अपने कंप्यूटर पर एक फ़ाइल खोलने का प्रयास करता हूं, तो मुझे त्रुटि मिलती है: filepath: अनुमति अस्वीकार कर दी गई है, जिसे मैं हल नहीं करना चाहता हूं।
#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