This post is also available in: English العربية (Arabic)
पायथन में अपना खुद का कंप्यूटर गेम बनाना भाषा सीखने का एक शानदार तरीका है।
गेम बनाने के लिए, आपको कई मुख्य प्रोग्रामिंग कौशलों का उपयोग करने की आवश्यकता होगी। वास्तविक दुनिया की प्रोग्रामिंग में आप जिस प्रकार के कौशल देखेंगे। खेल के विकास में, आप चर, लूप, सशर्त बयान, फ़ंक्शन, ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग और प्रोग्रामिंग तकनीकों और एल्गोरिदम के एक पूरे समूह का उपयोग करेंगे।
एक प्लस के रूप में, आपको वह गेम खेलने की संतुष्टि होगी जो आपने अभी बनाया है!
खेल विकास के लिए पायथन फ्रेमवर्क
पायथन इकोसिस्टम में, आपको टूल, लाइब्रेरी और फ्रेमवर्क का एक समृद्ध सेट मिलेगा जो आपके गेम को जल्दी से बनाने में आपकी मदद करेगा।
खेल निर्माण के लिए पायथन में उपलब्ध कुछ सबसे महत्वपूर्ण रूपरेखाएँ हैं:
1. पायगेम
Pygame एक ओपन-सोर्स पायथन लाइब्रेरी है, जो मल्टीमीडिया एप्लिकेशन जैसे उत्कृष्ट एसडीएल लाइब्रेरी के शीर्ष पर बनाए गए गेम बनाने के लिए है। यह पुस्तकालय सी, पायथन, नेटिव और ओपनजीएल का संयोजन है। Pygame उपयोगकर्ताओं को पूरी तरह से चित्रित गेम के साथ-साथ Python प्रोग्रामिंग के साथ मल्टीमीडिया प्रोग्राम बनाने में सक्षम बनाता है। यह अत्यधिक पोर्टेबल है और लगभग हर प्लेटफॉर्म और ऑपरेटिंग सिस्टम पर चलता है।
कुछ विशेषताएं इस प्रकार हैं:-
- मल्टीकोर सीपीयू को आसानी से इस्तेमाल किया जा सकता है
- मुख्य कार्यों के लिए अनुकूलित सी और असेंबली कोड का उपयोग करता है
- सरल और पोर्टेबल
- एक छोटे कोड की जरूरत होती है
2. पायकिरा
PyKyra, Python के लिए सबसे तेज़ गेम डेवलपमेंट फ्रेमवर्क में से एक है जो SDL (सॉफ्टवेयर और डॉक्यूमेंटेशन लोकलाइज़ेशन) और Kyra इंजन दोनों पर आधारित है। इस ढांचे की मानक विशेषताओं के साथ, यह एमपीईजी वीडियो, एमपी 3, ओग वोरबिस, वाव, आदि जैसी ध्वनियों, प्रत्यक्ष छवि पढ़ने और बहुत कुछ का भी समर्थन करता है।
3. पिगलेट
पाइगलेट पायथन के लिए एक ओपन-सोर्स, क्रॉस-प्लेटफ़ॉर्म विंडोिंग और मल्टीमीडिया लाइब्रेरी है। यह एक शक्तिशाली पायथन लाइब्रेरी है जिसका उपयोग विंडोज़, मैक ओएस एक्स और लिनक्स पर गेम और अन्य दृष्टि से समृद्ध अनुप्रयोगों के विकास के लिए किया जा सकता है। पाइगलेट विंडोिंग, यूजर इंटरफेस इवेंट हैंडलिंग, जॉयस्टिक्स, ओपनजीएल ग्राफिक्स, लोडिंग इमेज और वीडियो और साउंड और म्यूजिक चलाने का समर्थन करता है। पाइगलेट पायथन 3.5+ के तहत चलता है और अन्य पायथन दुभाषियों जैसे कि PyPy पर भी काम करता है।
कुछ विशेषताएं हैं-
- कोई बाहरी निर्भरता या स्थापना आवश्यकता नहीं
- लचीली नेटिव विण्डोविंग
- छवियों और ऑडियो के लिए अंतर्निहित समर्थन
- शुद्ध पायथन में लिखा गया
4. पायओपेनजीएल
PyOpenGL ओपनजीएल और संबंधित एपीआई के लिए सबसे आम क्रॉस-प्लेटफ़ॉर्म पायथन बाइंडिंग में से एक है। PyOpenGL में, मानक ctypes लाइब्रेरी का उपयोग करके बाइंडिंग बनाई जाती है। PyOpenGL, Python के लिए बड़ी संख्या में बाहरी GUI लाइब्रेरी के साथ इंटरऑपरेबल है, जिसमें PyGame, PyQt, Raw XLib, आदि शामिल हैं।
5. किवी
मल्टी-टच ऐप जैसे नए यूजर इंटरफेस से लैस, किवी अनुप्रयोगों के तेजी से विकास के लिए एक ओपन-सोर्स और क्रॉस-प्लेटफॉर्म पायथन लाइब्रेरी है। किवी लिनक्स, विंडोज, ओएस एक्स, एंड्रॉइड, आईओएस और रास्पबेरी पाई पर चलता है और WM_Touch, WM_Pen, Mac OS X ट्रैकपैड, और मैजिक माउस, Mtdev, Linux कर्नेल HID, और TUIO सहित अधिकांश इनपुट, प्रोटोकॉल और उपकरणों का मूल रूप से उपयोग कर सकता है। . पुस्तकालय GPU त्वरित है, और यह 20 से अधिक विजेट्स के साथ आता है जो सभी अत्यधिक एक्स्टेंसिबल हैं।
6. पांडा 3 डी
पायथन और सी ++ में लिखा गया, पांडा 3 डी रीयल-टाइम 3 डी गेम, विज़ुअलाइज़ेशन, सिमुलेशन, प्रयोग और बहुत कुछ के लिए एक ओपन-सोर्स और पूरी तरह से फ्री-टू-यूज़ इंजन है। पांडा3डी में स्रोत संपत्तियों के प्रसंस्करण और अनुकूलन के लिए कमांड-लाइन टूल शामिल हैं जो उपयोगकर्ताओं को सामग्री उत्पादन पाइपलाइन को स्वचालित और स्क्रिप्ट करने की अनुमति देते हैं। यह कई लोकप्रिय तृतीय-पक्ष लाइब्रेरीज का समर्थन करता है, जैसे कि बुलेट भौतिकी इंजन, असिंप मॉडल लोडर, ओपनएएल और एफएमओडी ध्वनि लाइब्रेरीज, और बहुत कुछ।
7. कोकोस 2 डी
पायथन, सी ++, और ऑब्जेक्टिव सी में लिखा गया, Cocos2d एक ओपन-सोर्स सॉफ़्टवेयर फ्रेमवर्क है जिसका उपयोग गेम, ऐप्स और अन्य क्रॉस-प्लेटफ़ॉर्म जीयूआई-आधारित इंटरैक्टिव प्रोग्राम बनाने के लिए किया जा सकता है। Cocos2d परिवार में Cocos2d-x, Cocos2d-JS, Cocos2d-XNA और Cocos2d (पायथन) शामिल हैं। Cocos2d-x एक ओपन-सोर्स गेम फ्रेमवर्क है जो C++ में लिखा गया है, जिसमें एक पतली प्लेटफॉर्म-निर्भर परत है, और इसका उपयोग गेम, ऐप्स और अन्य क्रॉस-प्लेटफ़ॉर्म GUI- आधारित इंटरैक्टिव प्रोग्राम बनाने के लिए किया जा सकता है। इसके अलावा, Cocos2d-JS Cocos2d-x इंजन का जावास्क्रिप्ट संस्करण है जो सरलीकृत जावास्क्रिप्ट-अनुकूल API के सेट के साथ पूर्ण Cocos2d-x कार्यक्षमता का समर्थन करता है।
8. पाइथन-ओग्रे
Python-Ogre या PyOgre, OGRE 3D इंजन के लिए C++ लाइब्रेरी का पायथन बाइंडिंग है। प्योग्रे एक क्रॉस-प्लेटफ़ॉर्म है और गति और लचीलेपन को विरासत में मिला है। Ogre एक 3D रेंडरिंग इंजन है जो पूरी तरह से C++ में लिखा गया है जो एक प्रभावशाली फीचर सेट का समर्थन करता है और प्रभावशाली गेम बनाने के लिए इसका उपयोग किया गया है। प्योग्रे में वास्तव में दो लाइब्रेरी होती हैं। पहला Ogre3d है, जो एक 3D रेंडरिंग इंजन है और दूसरा CEGUi है, जो एक एम्बेडेड GUI सिस्टम है।
9. रेन’पाई
पायथन और साइथन में लिखा गया, रेन’पाई डिजिटल कहानी कहने के लिए एक स्वतंत्र और क्रॉस-प्लेटफ़ॉर्म इंजन है जो दृश्य उपन्यास और जीवन सिमुलेशन गेम बनाने के लिए शब्दों, छवियों और ध्वनियों को संयोजित करना आसान बनाता है। पायथन स्क्रिप्टिंग भाषा के लिए रेन’पाई का समर्थन डेवलपर्स को जटिल गेम लॉजिक के साथ काम करने की अनुमति देता है। Ren’Py का सबसे बड़ा लाभ यह है कि यह लगभग हर कंप्यूटर सिस्टम पर चलता है।
पायथन में खेल जो बच्चे बना सकते हैं
हम आपके लिए 5 दिलचस्प गतिविधियाँ और खेल लाए हैं जो बच्चे पायथन में बना सकते हैं।
1. पेंट
हम में से अधिकांश लोग विंडोज़ में पेंट एप्लिकेशन से परिचित हैं जिसका उपयोग हम चित्र बनाने और उन्हें रंगने के लिए करते हैं। लेकिन क्या होगा अगर आप Pygame के साथ अपना खुद का पेंट प्रोग्राम बनाते हैं। क्या यह रोमांचक नहीं है?
संपूर्ण एप्लिकेशन को निम्नलिखित उप-मॉड्यूल के रूप में देखा जा सकता है: ड्रॉ करने के लिए एक विंडो बनाना: पहले चरण के रूप में, हम एक विंडो बनाएंगे जिस पर हम ड्रॉ करेंगे। इस विंडो का उपयोग खींचे जा रहे चित्रों को रखने के लिए किया जाएगा। पेंसिल के रंगों को परिभाषित करना: बहुरंगी चित्र बनाने के लिए, आपको विभिन्न रंगों की पेंसिल चुनने के विकल्प की आवश्यकता होती है। और अंत में, एक छवि खींचने के लिए एक कोड जब माउस को खिड़की के पार ले जाया जाता है।
import pygameimport pygame.gfxdraw#Initializing pygame modulepygame.init() #Screen variablesscreen_width = 700screen_height =500 # Defining Colorswhite = (255, 255, 255)blue = (67,238,250)red = (255, 0, 0)black = (0, 0, 0)green = (38,245,45)pink = (255,192,203)orange = (255,165,0)yellow = (255,255,0)violet = (177, 3, 252) #Setting default color to blackpencolour = black drawingwindow = pygame.display.set_mode((screen_width,screen_height))pygame.display.set_caption(“Paint – By Bhavik Agarwal”)drawingwindow.fill((255,255,255)) #Loading backgroud image and drawing it on screenbackimg = pygame.image.load(“paint.png”).convert_alpha()drawingwindow.blit(backimg, (0,0)) #rect for the drawing areaclearrect = (119, 17, 562, 465) #Defining rect value for colors in colorboxcol1= (22, 81, 30, 34)col2= (56, 81, 34, 34)col3= (22, 120, 30, 33)col4= (56, 120, 34, 32)col5= (22, 156, 30, 33)col6= (56, 156, 34, 32)col7= (22, 192, 30, 33)col8= (56, 192, 34, 32) #Rect that highlight which button is selectedbuttonselect = (22, 81, 30, 34) #Function to draw color boxdef drawrectangle(): pygame.gfxdraw.box(drawingwindow, col1, black) pygame.gfxdraw.box(drawingwindow, col2, blue) pygame.gfxdraw.box(drawingwindow, col3, red) pygame.gfxdraw.box(drawingwindow, col4, green) pygame.gfxdraw.box(drawingwindow, col5, pink) pygame.gfxdraw.box(drawingwindow, col6, orange) pygame.gfxdraw.box(drawingwindow, col7, yellow) pygame.gfxdraw.box(drawingwindow, col8, violet)drawrectangle() #Set mouse cursor for pencil (default)pygame.mouse.set_cursor(*pygame.cursors.broken_x)exit_game = False #Gameloopwhile not exit_game: for event in pygame.event.get(): if event.type == pygame.QUIT: exit_game = True #Check for button clicks t = pygame.mouse.get_pressed() if t[0] == 1: mousepos = pygame.mouse.get_pos() if 122 < mousepos[0] < 678 and 21 < mousepos[1] < 480: pygame.gfxdraw.filled_ellipse(drawingwindow,mousepos[0], mousepos[1],4,4,pencolour) elif 22 < mousepos[0] < 52 and 81 < mousepos[1] < 115: pencolour = black drawrectangle() buttonselect = (22, 81, 30, 34) elif 56 < mousepos[0] < 90 and 81 < mousepos[1] < 115: pencolour = blue drawrectangle() buttonselect = (56, 81, 34, 34) elif 22 < mousepos[0] < 52 and 120 < mousepos[1] < 153: pencolour = red drawrectangle() buttonselect = (22, 120, 30, 33) elif 56 < mousepos[0] < 90 and 120 < mousepos[1] < 152: pencolour = green drawrectangle() buttonselect = (56, 120, 34, 32) elif 22 < mousepos[0] < 52 and 156 < mousepos[1] < 189: pencolour = pink drawrectangle() buttonselect = (22, 156, 30, 33) elif 56 < mousepos[0] < 90 and 156 < mousepos[1] < 188: pencolour = orange drawrectangle() buttonselect = (56, 156, 34, 32) elif 22 < mousepos[0] < 52 and 192 < mousepos[1] < 225: pencolour = yellow drawrectangle() buttonselect = (22, 192, 30, 33) elif 56 < mousepos[0] < 90 and 192 < mousepos[1] < 224: pencolour = violet drawrectangle() buttonselect = (56, 192, 34, 32) #Eraser elif 13 < mousepos[0] < 54 and 247 < mousepos[1] < 285: pencolour = white drawrectangle() pygame.mouse.set_cursor(*pygame.cursors.diamond) #Pencil elif 59 < mousepos[0] < 97 and 247 < mousepos[1] < 288: pencolour = black drawrectangle() pygame.mouse.set_cursor(*pygame.cursors.broken_x) buttonselect = (22, 81, 30, 34) elif 15 < mousepos[0] < 96 and 363 < mousepos[1] < 400: pygame.gfxdraw.box(drawingwindow, clearrect, white) pygame.gfxdraw.rectangle(drawingwindow, buttonselect, white) pygame.display.update() pygame.quit()
2. सांप का खेल
आप सभी ने स्नेक गेम खेला है और निश्चित रूप से, आप कभी हारना नहीं चाहते थे। बच्चों के रूप में, हम सभी “गेम ओवर” संदेश को कभी नहीं देखने के लिए धोखा देना पसंद करते थे। लेकिन तकनीकी विशेषज्ञ के तौर पर आप इस ‘सांप’ को अपनी बीट्स पर डांस कराना चाहेंगे।
आप मॉड्यूल बनाकर स्नेक गेम बना सकते हैं – स्क्रीन बनाएं, स्नेक बनाएं, स्नेक को मूव करें, गेम ओवर जब स्नेक सीमाओं से टकराए, खाना जोड़ना, सांप की लंबाई बढ़ाना, स्कोर प्रदर्शित करना।
import pygameimport timeimport random pygame.init() white = (255, 255, 255)yellow = (255, 255, 102)black = (0, 0, 0)red = (213, 50, 80)green = (0, 255, 0)blue = (50, 153, 213) dis_width = 600dis_height = 400 dis = pygame.display.set_mode((dis_width, dis_height))pygame.display.set_caption(‘Snake Game’) clock = pygame.time.Clock() snake_block = 10snake_speed = 15 font_style = pygame.font.SysFont(“bahnschrift”, 25)score_font = pygame.font.SysFont(“comicsansms”, 35) def Your_score(score): value = score_font.render(“Your Score: ” + str(score), True, yellow) dis.blit(value, [0, 0]) def our_snake(snake_block, snake_list): for x in snake_list: pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block]) def message(msg, color): mesg = font_style.render(msg, True, color) dis.blit(mesg, [dis_width / 6, dis_height / 3]) def gameLoop(): game_over = False game_close = False x1 = dis_width / 2 y1 = dis_height / 2 x1_change = 0 y1_change = 0 snake_List = [] Length_of_snake = 1 foodx = round(random.randrange(0, dis_width – snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height – snake_block) / 10.0) * 10.0 while not game_over: while game_close == True: dis.fill(blue) message(“You Lost! Press C-Play Again or Q-Quit”, red) Your_score(Length_of_snake – 1) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: gameLoop() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_block y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_block x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_block x1_change = 0 if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change dis.fill(blue) pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block]) snake_Head = [] snake_Head.append(x1) snake_Head.append(y1) snake_List.append(snake_Head) if len(snake_List) > Length_of_snake: del snake_List[0] for x in snake_List[:-1]: if x == snake_Head: game_close = True our_snake(snake_block, snake_List) Your_score(Length_of_snake – 1) pygame.display.update() if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, dis_width – snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height – snake_block) / 10.0) * 10.0 Length_of_snake += 1 clock.tick(snake_speed) pygame.quit() quit() gameLoop()
3. डॉट कनेक्ट
डॉट कनेक्ट गेम वह है जहां एक खिलाड़ी बॉक्स बनाने के लिए डॉट्स को जोड़ता है। नियम सर्वविदित हैं: प्रत्येक खिलाड़ी को दो बिंदुओं के बीच एक नई लाइन बनाने के लिए मिलता है (जिस लाइन को आप बनाना चाहते हैं उसके पास सिंगल-क्लिक करें)। यदि कोई खिलाड़ी एक बॉक्स बनाता है, तो उसे एक अंक मिलता है और उसे फिर से जाना होता है। (दो बक्सों को दो अंक मिलते हैं, लेकिन केवल एक अतिरिक्त मोड़।) 81 बक्सों में से उच्चतम स्कोर जीतता है!
from Tkinter import *import tkFont TOL = 8CELLSIZE = 40OFFSET = 10CIRCLERAD = 2DOTOFFSET = OFFSET + CIRCLERADGAME_H = 400GAME_W = 400 class Player(object): def __init__(self, name, color=”black”): self.score = 0 self.str = StringVar() self.name = name self.color = color def update(self): self.str.set(self.name + “: %d” % self.score) class MyFrame(Frame): def __init__(self, master): Frame.__init__(self, master) self.GO_font = tkFont.Font(self, name=”GOFont”, family = “Times”, weight=”bold”, size=36) self.canvas = Canvas(self, height = GAME_H, width = GAME_W) self.canvas.bind(“<Button-1>”, lambda e:self.click(e)) self.canvas.grid(row=0,column=0) self.dots = [[self.canvas.create_oval(CELLSIZE*i+OFFSET, CELLSIZE*j+OFFSET, CELLSIZE*i+OFFSET+2*CIRCLERAD, CELLSIZE*j+OFFSET+2*CIRCLERAD, fill=”black”) for j in range(10)] for i in range(10)] self.lines = [] self.infoframe = Frame(self) self.players = [Player(“Player 1″,”blue”), Player(“Player 2″,”red”)] self.infoframe.players = [Label(self.infoframe, textvariable = i.str) for i in self.players] for i in self.infoframe.players: i.grid() self.turn = self.players[0] self.update_players() self.infoframe.grid(row = 0, column = 1, sticky = N) self.grid() def update_players(self): for i in self.players: i.update() def click(self, event): x,y = event.x, event.y orient = self.isclose(x,y) if orient: if self.line_exists(x,y, orient): return l = self.create_line(x,y, orient) score = self.new_box_made(l) if score: self.turn.score += score self.turn.update() self.check_game_over() else: index = self.players.index(self.turn) self.turn = self.players[1-index] self.lines.append(l) def create_line(self, x, y, orient): startx = CELLSIZE * ((x-OFFSET)//CELLSIZE) + DOTOFFSET starty = CELLSIZE * ((y-OFFSET)//CELLSIZE) + DOTOFFSET tmpx = (x-OFFSET)//CELLSIZE tmpy = (y-OFFSET)//CELLSIZE if orient == HORIZONTAL: endx = startx + CELLSIZE endy = starty else: endx = startx endy = starty + CELLSIZE #print “line drawn: %d,%d to %d,%d” % (startx,starty,endx,endy) return self.canvas.create_line(startx,starty,endx,endy) def new_box_made(self, line): score = 0 x0,y0,x1,y1 = self.canvas.coords(line) if x0 == x1: # vertical line midx = x0 midy = (y0+y1)/2 pre = (x0 – CELLSIZE/2, midy) post = (x0 + CELLSIZE/2, midy) elif y0 == y1: # horizontal line midx = (x0 + x1)/2 midy = y0 pre = (midx, y0 – CELLSIZE/2) post = (midx, y0 + CELLSIZE/2) if len(self.find_lines(pre)) == 3: # not 4, because newly created line is self.fill_in(pre) # is not returned (?!) score += 1 if len(self.find_lines(post)) == 3: self.fill_in(post) score += 1 return score def find_lines(self, coords): x, y = coords if x < 0 or x > GAME_W: return [] if y < 0 or y > GAME_W: return [] #print “Cell center: %d,%d” % (x,y) lines = [x for x in self.canvas.find_enclosed(x-CELLSIZE, y-CELLSIZE, x+CELLSIZE, y+CELLSIZE) if x in self.lines] #print lines return lines def fill_in(self, coords): x,y = coords self.canvas.create_text(x,y,text=self.turn.name, fill=self.turn.color) def isclose(self, x, y): x -= OFFSET y -= OFFSET dx = x – (x//CELLSIZE)*CELLSIZE dy = y – (y//CELLSIZE)*CELLSIZE if abs(dx) < TOL: if abs(dy) < TOL: return None # mouse in corner of box; ignore else: return VERTICAL elif abs(dy) < TOL: return HORIZONTAL else: return None def line_exists(self, x,y, orient): id_ = self.canvas.find_closest(x,y,halo=TOL)[0] if id_ in self.lines: return True else: return False def check_game_over(self): total = sum([x.score for x in self.players]) if total == 81: self.canvas.create_text(GAME_W/2, GAME_H/2, text=”GAME OVER”, font=”GOFont”, fill=”#888″) mainw = Tk()mainw.f = MyFrame(mainw)mainw.mainloop()
4. पिंग पोंग
पोंग टेबल टेनिस का अनुकरण करने वाले सबसे प्रसिद्ध आर्केड खेलों में से एक है। प्रत्येक खिलाड़ी खेल में पैडल को स्क्रीन के बाईं या दाईं ओर लंबवत खींचकर नियंत्रित करता है। गेंद पर आगे और पीछे प्रहार करने के लिए खिलाड़ी अपने पैडल का उपयोग करते हैं।
टर्टल पायथन में एक इनबिल्ट ग्राफिक मॉड्यूल है। यह चित्रों को चित्रित करने के लिए एक पैनल और कलम का उपयोग करता है।
नीचे उपयोग किए गए चरण हैं:
- चरण 1) स्क्रीन के बाएँ और दाएँ तरफ दो पैडल A और B बनाएँ।
- चरण 2) एक गेंद बनाएं।
- चरण 3) एक निश्चित कुंजी दबाकर पैडल को लंबवत रूप से स्थानांतरित करने के लिए एक ईवेंट बनाएं।
- चरण 4) प्रत्येक खिलाड़ी के टकराव से चूकने के बाद स्कोर को अपडेट करने के लिए फ़ंक्शन बनाएं।
# Import required libraryimport turtle # Create screensc = turtle.Screen()sc.title(“Pong game”)sc.bgcolor(“white”)sc.setup(width=1000, height=600) # Left paddleleft_pad = turtle.Turtle()left_pad.speed(0)left_pad.shape(“square”)left_pad.color(“black”)left_pad.shapesize(stretch_wid=6, stretch_len=2)left_pad.penup()left_pad.goto(-400, 0) # Right paddleright_pad = turtle.Turtle()right_pad.speed(0)right_pad.shape(“square”)right_pad.color(“black”)right_pad.shapesize(stretch_wid=6, stretch_len=2)right_pad.penup()right_pad.goto(400, 0) # Ball of circle shapehit_ball = turtle.Turtle()hit_ball.speed(40)hit_ball.shape(“circle”)hit_ball.color(“blue”)hit_ball.penup()hit_ball.goto(0, 0)hit_ball.dx = 5hit_ball.dy = -5 # Initialize the scoreleft_player = 0right_player = 0 # Displays the scoresketch = turtle.Turtle()sketch.speed(0)sketch.color(“blue”)sketch.penup()sketch.hideturtle()sketch.goto(0, 260)sketch.write(“Left_player : 0 Right_player: 0″,align=”center”, font=(“Courier”, 24, “normal”)) # Functions to move paddle verticallydef paddleaup():y = left_pad.ycor()y += 20left_pad.sety(y) def paddleadown():y = left_pad.ycor()y -= 20left_pad.sety(y) def paddlebup():y = right_pad.ycor()y += 20right_pad.sety(y) def paddlebdown():y = right_pad.ycor()y -= 20right_pad.sety(y) # Keyboard bindingssc.listen()sc.onkeypress(paddleaup, “e”)sc.onkeypress(paddleadown, “x”)sc.onkeypress(paddlebup, “Up”)sc.onkeypress(paddlebdown, “Down”) while True:sc.update() hit_ball.setx(hit_ball.xcor()+hit_ball.dx)hit_ball.sety(hit_ball.ycor()+hit_ball.dy) # Checking bordersif hit_ball.ycor() > 280:hit_ball.sety(280)hit_ball.dy *= -1 if hit_ball.ycor() < -280:hit_ball.sety(-280)hit_ball.dy *= -1 if hit_ball.xcor() > 500:hit_ball.goto(0, 0)hit_ball.dy *= -1left_player += 1sketch.clear()sketch.write(“Left_player : {} Right_player: {}”.format(left_player, right_player), align=”center”,font=(“Courier”, 24, “normal”)) if hit_ball.xcor() < -500:hit_ball.goto(0, 0)hit_ball.dy *= -1right_player += 1sketch.clear()sketch.write(“Left_player : {} Right_player: {}”.format(left_player, right_player), align=”center”,font=(“Courier”, 24, “normal”)) # Paddle ball collisionif (hit_ball.xcor() > 360 andhit_ball.xcor() < 370) and(hit_ball.ycor() < right_pad.ycor()+40 andhit_ball.ycor() > right_pad.ycor()-40):hit_ball.setx(360)hit_ball.dx*=-1if (hit_ball.xcor()<-360 andhit_ball.xcor()>-370) and(hit_ball.ycor()<left_pad.ycor()+40 andhit_ball.ycor()>left_pad.ycor()-40):hit_ball.setx(-360)hit_ball.dx*=-1
5. टिक टैक् टो
टिक टैक् टो सबसे अधिक खेले जाने वाले खेलों में से एक है और यह सबसे अच्छा समय हत्यारा खेल है जिसे आप सिर्फ एक कलम और कागज के साथ कहीं भी खेल सकते हैं। यदि आप नहीं जानते कि इस खेल को कैसे खेलना है, तो चिंता न करें, आइए पहले इसे समझते हैं।
खेल दो व्यक्तियों द्वारा खेला जाता है। सबसे पहले, हम 3×3 वर्ग ग्रिड के साथ एक बोर्ड बनाते हैं। पहला खिलाड़ी ‘X’ चुनता है और इसे किसी भी वर्ग ग्रिड पर खींचता है, फिर दूसरे खिलाड़ी के पास उपलब्ध स्थानों पर ‘O’ खींचने का मौका होता है। इस तरह, खिलाड़ी खाली जगहों पर वैकल्पिक रूप से ‘X’ और ‘O’ खींचते हैं, जब तक कि कोई खिलाड़ी क्षैतिज, ऊर्ध्वाधर या विकर्ण तरीके से लगातार 3 अंक प्राप्त करने में सफल नहीं हो जाता। फिर खिलाड़ी गेम जीत जाता है अन्यथा सभी स्पॉट भर जाने पर गेम ड्रॉ हो जाता है।
# Tic-Tac-Toe Program using# random number in Python# importing all necessary librariesimport numpy as npimport randomfrom time import sleep# Creates an empty boarddef create_board():return(np.array([[0, 0, 0],[0, 0, 0],[0, 0, 0]]))# Check for empty places on boarddef possibilities(board):l = []for i in range(len(board)):for j in range(len(board)):if board[i][j] == 0:l.append((i, j))return(l)# Select a random place for the playerdef random_place(board, player):selection = possibilities(board)current_loc = random.choice(selection)board[current_loc] = playerreturn(board)# Checks whether the player has three# of their marks in a horizontal rowdef row_win(board, player):for x in range(len(board)):win = Truefor y in range(len(board)):if board[x, y] != player:win = Falsecontinueif win == True:return(win)return(win)# Checks whether the player has three# of their marks in a vertical rowdef col_win(board, player):for x in range(len(board)):win = Truefor y in range(len(board)):if board[y][x] != player:win = Falsecontinueif win == True:return(win)return(win)# Checks whether the player has three# of their marks in a diagonal rowdef diag_win(board, player):win = Truey = 0for x in range(len(board)):if board[x, x] != player:win = Falseif win:return winwin = Trueif win:for x in range(len(board)):y = len(board) – 1 – xif board[x, y] != player:win = Falsereturn win# Evaluates whether there is# a winner or a tiedef evaluate(board):winner = 0for player in [1, 2]:if (row_win(board, player) orcol_win(board,player) ordiag_win(board,player)):winner = playerif np.all(board != 0) and winner == 0:winner = -1return winner# Main function to start the gamedef play_game():board, winner, counter = create_board(), 0, 1print(board)sleep(2)while winner == 0:for player in [1, 2]:board = random_place(board, player)print(“Board after ” + str(counter) + ” move”)print(board)sleep(2)counter += 1winner = evaluate(board)if winner != 0:breakreturn(winner)# Driver Codeprint(“Winner is: ” + str(play_game()))