logo

كيفية تطوير لعبة في بايثون

اللغة الأكثر قابلية للتكيف هي لغة بايثون، والتي يتم استخدامها في كل الصناعات تقريبًا، بما في ذلك تطوير الألعاب، وتطوير الويب، والتعلم الآلي، والذكاء الاصطناعي، وتطبيقات واجهة المستخدم الرسومية.

تم تطوير اللعبة باستخدام حزمة pygame، وهي إحدى ميزات Python المضمنة. من خلال الفهم الأولي لبرمجة Python، يمكننا إنشاء ألعاب جذابة بصريًا مع الرسوم المتحركة والمؤثرات الصوتية والموسيقى المناسبة باستخدام وحدة pygame.

يتم استخدام مكتبة مشتركة بين الأنظمة الأساسية تسمى Pygame لإنشاء ألعاب الفيديو. يحتوي على مكتبات صوتية ومرئيات كمبيوتر لتزويد اللاعب بتجربة ألعاب نموذجية.

يقوم Pete Shinners بتطويره ليحل محل PySDL.

المتطلبات الأساسية لPygame

نحن بحاجة إلى فهم لغة البرمجة بايثون لكي نتعلم Pygame.

استيراد الماسح الضوئي جافا

تثبيت بيجامي

لتثبيت Pygame، افتح محطة سطر الأوامر وأدخل الأمر التالي.

ترتيب بالإدراج
 pip install pygame 

يمكننا أيضًا تثبيته من خلال IDE. لمزيد من دليل التثبيت، تفضل بزيارة البرنامج التعليمي الكامل لـ pygame (https://www.javatpoint.com/pygame). ستجد هنا جميع شروحات pygame الأساسية.

مثال بسيط على لعبة Pygame

فيما يلي المثال التالي لإنشاء نافذة pygame بسيطة.

 import pygame pygame.init() screen = pygame.display.set_mode((400,500)) done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.display.flip() 

انتاج:

كيفية تطوير لعبة في بايثون

توضيح:

يفتح الكود المقدم نافذة Pygame مقاس 400 × 500 بكسل ويبدأ حلقة تراقب الأحداث طوال الوقت. تقوم الحلقة بتغيير المتغير 'done' إلى True، مما يؤدي إلى إنهاء الحلقة وإنهاء البرنامج في حالة اكتشاف حدث QUIT (عادةً عند خروج المستخدم من النافذة). يقوم بتحديث العرض والتأكد من ظهور أي تغييرات على الشاشة بفضل طريقة 'pygame.display.flip()'. باختصار، يُنشئ الكود نافذة Pygame صغيرة تظل نشطة حتى يرفضها المستخدم، وتعرض بنية برنامج أساسية تعتمد على الأحداث.

ساعي البريد

سيتم رسم جميع الرسومات في نافذة pygame.

دعونا نفهم بناء الجملة الأساسي للبرنامج أعلاه.

    استيراد بيجامي:إنها الوحدة التي تتيح لنا الوصول إلى جميع وظائف Pygame.حرارة():يتم استخدامه لتهيئة كل وحدة من وحدات pygame الضرورية.Display.set_mode((العرض، الارتفاع)):يتم استخدامه لضبط حجم النافذة. وسوف يعيد هذا البند على السطح. يتم تنفيذ الإجراءات الرسومية من خلال الكائن السطحي.حدث.get():والنتيجة هي قائمة انتظار الأحداث فارغة. إذا لم نستدعيها، فسوف ينظر نظام التشغيل إلى اللعبة على أنها لا تستجيب، وستبدأ رسائل النافذة في التراكم.يترك:عندما ننقر على زر التقاطع الموجود في زاوية النافذة، يتم استخدامه لتجاهل الحدث.عرض الوجه ():تنعكس فيه أي تحديثات للعبة. يجب علينا الاتصال بالشاشة عندما نقوم بإجراء أي تغييرات.flip() هي وظيفة.

يمكن رسم أي شكل على سطح Pygame، بما في ذلك الخطوط والصور الجميلة. تتيح لك العديد من الطرق المدمجة في Pygame رسم أشكال هندسية على الشاشة. تمثل هذه النماذج الخطوات الأولى في إنشاء لعبة.

دعونا نفحص الرسم التوضيحي التالي لنموذج يتم رسمه على الشاشة.

مثال -

 import pygame from math import pi pygame.init() # size variable is using for set screen size size = [400, 300] screen = pygame.display.set_mode(size) pygame.display.set_caption('Example program to draw geometry') # done variable is using as flag done = False clock = pygame.time.Clock() while not done: # clock.tick() limits the while loop to a max of 10 times per second. clock.tick(10) for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked on close symbol done = True # done variable that we are complete, so we exit this loop # All drawing code occurs after the for loop and but # inside the main while done==False loop. # Clear the default screen background and set the white screen background screen.fill((0, 0, 0)) # Draw on the screen a green line which is 5 pixels wide. pygame.draw.line(screen, (0, 255, 0), [0, 0], [50, 30], 5) # Draw on the screen a green line which is 5 pixels wide. pygame.draw.lines(screen, (0, 0, 0), False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5) # Draw a rectangle outline pygame.draw.rect(screen, (0, 0, 0), [75, 10, 50, 20], 2) # Draw a solid rectangle pygame.draw.rect(screen, (0, 0, 0), [150, 10, 50, 20]) # This draw an ellipse outline, using a rectangle as the outside boundaries pygame.draw.ellipse(screen, (255, 0, 0), [225, 10, 50, 20], 2) # This draw a solid ellipse, using a rectangle as the outside boundaries pygame.draw.ellipse(screen, (255, 0, 0), [300, 10, 50, 20]) # Draw a triangle using the polygon function pygame.draw.polygon(screen, (0, 0, 0), [[100, 100], [0, 200], [200, 200]], 5) # This draw a circle pygame.draw.circle(screen, (0, 0, 255), [60, 250], 40) # This draw an arc pygame.draw.arc(screen, (0, 0, 0), [210, 75, 150, 125], 0, pi / 2, 2) # This function must write after all the other drawing commands. pygame.display.flip() # Quite the execution when clicking on close pygame.quit() 

انتاج:

كيفية تطوير لعبة في بايثون

توضيح:

يقوم برنامج Python المحدد بإنشاء نافذة رسومية بأشكال هندسية مختلفة مرسومة فيها باستخدام حزمة Pygame. يقوم البرنامج بإنشاء نافذة Pygame بحجم 400 × 300 بكسل، ثم يبدأ تشغيل حلقة تراقب الأحداث طوال الوقت، مع التأكد من بقاء النافذة مفتوحة حتى يقوم المستخدم بإنهائها. يقوم بمسح الشاشة في بداية هذه الحلقة ثم يستخدم إجراءات الرسم الخاصة بـ Pygame لإنشاء مجموعة متنوعة من النماذج بألوان مختلفة وعرض الخطوط. تتضمن هذه الأشكال الخطوط والمضلعات والقطع الناقص والدوائر والأقواس. كل نموذج له إحداثياته ​​وخصائصه المحددة. وأخيرًا، يتم استخدام 'pygame.display.flip()' لتحديث العرض. ينهي البرنامج pygame عند إغلاق النافذة.

ثوابت جافا

يعد التطبيق بمثابة مثال على القدرة على التكيف والتنوع لقدرات Pygame في الرسم ويوفر إطارًا لتطوير التطبيقات الرسومية التفاعلية. يمكن للمطورين إنشاء مجموعة واسعة من المكونات المرئية داخل بيئة Pygame الرسومية عن طريق ضبط المعلمات مثل الإحداثيات والألوان وعرض الخطوط. وهذا يسمح ببناء تطبيقات الوسائط المتعددة التفاعلية والألعاب وعمليات المحاكاة.

مثال - تطوير لعبة الثعبان باستخدام Pygame

برنامج -

إيقاف تشغيل وضع المطور
 # Snake Tutorial Using Pygame import math import random import pygame import tkinter as tk from tkinter import messagebox class cube(object): rows = 20 w = 500 def __init__(self, start, dirnx=1, dirny=0, color=(255, 0, 0)): self.pos = start self.dirnx = 1 self.dirny = 0 self.color = color def move(self, dirnx, dirny): self.dirnx = dirnx self.dirny = dirny self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny) def draw(self, surface, eyes=False): dis = self.w // self.rows i = self.pos[0] j = self.pos[1] pygame.draw.rect(surface, self.color, (i * dis + 1, j * dis + 1, dis - 2, dis - 2)) if eyes: centre = dis // 2 radius = 3 circleMiddle = (i * dis + centre - radius, j * dis + 8) circleMiddle2 = (i * dis + dis - radius * 2, j * dis + 8) pygame.draw.circle(surface, (0, 0, 0), circleMiddle, radius) pygame.draw.circle(surface, (0, 0, 0), circleMiddle2, radius) # This class is defined for snake design and its movement class snake(object): body = [] turns = {} def __init__(self, color, pos): self.color = color self.head = cube(pos) self.body.append(self.head) self.dirnx = 0 self.dirny = 1 def move(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() keys = pygame.key.get_pressed() # It will manage the keys movement for the snake for key in keys: if keys[pygame.K_LEFT]: self.dirnx = -1 self.dirny = 0 self.turns[self.head.pos[:]] = [self.dirnx, self.dirny] elif keys[pygame.K_RIGHT]: self.dirnx = 1 self.dirny = 0 self.turns[self.head.pos[:]] = [self.dirnx, self.dirny] elif keys[pygame.K_UP]: self.dirnx = 0 self.dirny = -1 self.turns[self.head.pos[:]] = [self.dirnx, self.dirny] elif keys[pygame.K_DOWN]: self.dirnx = 0 self.dirny = 1 self.turns[self.head.pos[:]] = [self.dirnx, self.dirny] # Snake when hit the boundary wall for i, c in enumerate(self.body): p = c.pos[:] if p in self.turns: turn = self.turns[p] c.move(turn[0], turn[1]) if i == len(self.body) - 1: self.turns.pop(p) else: if c.dirnx == -1 and c.pos[0] = c.rows - 1: c.pos = (0, c.pos[1]) elif c.dirny == 1 and c.pos[1] &gt;= c.rows - 1: c.pos = (c.pos[0], 0) elif c.dirny == -1 and c.pos[1] <= 0 1 0: c.pos="(c.pos[0]," c.rows - 1) else: c.move(c.dirnx, c.dirny) def reset(self, pos): self.head="cube(pos)" self.body="[]" self.body.append(self.head) self.turns="{}" self.dirnx="0" self.dirny="1" # it will add the new cube in snake tail after every successful score addcube(self): dx, dy="tail.dirnx," tail.dirny if dx="=" and self.body.append(cube((tail.pos[0] 1, tail.pos[1]))) elif -1 + 1: self.body.append(cube((tail.pos[0], tail.pos[1] 1))) -1: self.body[-1].dirnx="dx" self.body[-1].dirny="dy" draw(self, surface): for i, c enumerate(self.body): i="=" c.draw(surface, true) c.draw(surface) drawgrid(w, rows, sizebtwn="w" rows x="0" y="0" l range(rows): draw grid line pygame.draw.line(surface, (255, 255, 255), (x, 0), w)) (0, y), (w, y)) this class define game surface redrawwindow(surface): global width, s, snack is used to surface.fill((0, 0, 0)) s.draw(surface) snack.draw(surface) drawgrid(width, surface) pygame.display.update() randomsnack(rows, item): positions="item.body" while true: len(list(filter(lambda z: z.pos="=" positions)))> 0: continue else: break return (x, y) # Using Tkinter function to display message def message_box(subject, content): root = tk.Tk() root.attributes(&apos;-topmost&apos;, True) root.withdraw() messagebox.showinfo(subject, content) try: root.destroy() except: pass # main() function def main(): global width, rows, s, snack width = 500 rows = 20 win = pygame.display.set_mode((width, width)) s = snake((255, 0, 0), (10, 10)) snack = cube(randomSnack(rows, s), color=(0, 255, 0)) flag = True clock = pygame.time.Clock() while flag: pygame.time.delay(50) clock.tick(10) s.move() if s.body[0].pos == snack.pos: s.addCube() snack = cube(randomSnack(rows, s), color=(0, 255, 0)) for x in range(len(s.body)): if s.body[x].pos in list(map(lambda z: z.pos, s.body[x + 1:])): print(&apos;Score: 
&apos;, len(s.body)) message_box(&apos;You Lost!
&apos;, &apos;Play again...
&apos;) s.reset((10, 10)) break redrawWindow(win) pass main() </=>

انتاج:

كيفية تطوير لعبة في بايثون

إذا لمس الثعبان نفسه فسوف ينهي اللعبة ويعرض الرسالة التالية.

كيفية تطوير لعبة في بايثون

للاستئناف، ما عليك سوى النقر فوق الزر 'موافق'. تعرض محطة Pycharm درجاتنا (استخدمنا Pycharm IDE، ولكن يمكنك استخدام أي Python IDE).

كيفية تطوير لعبة في بايثون

توضيح:

يستخدم نص Python المقدم حزمة Pygame لإنشاء لعبة Snake مباشرة. يقوم بإنشاء فئات لكل من الثعبان والمكعبات المنفصلة التي يتكون منها جسمه. يتم استخدام Pygame لإعداد نافذة اللعبة، والتي تتميز بهيكل شبكي وثعبان متحرك. بعد تناول مكعب 'الوجبة الخفيفة'، يطول الثعبان، وإذا اصطدم بنفسه أو بالجدران الحدودية، تنتهي اللعبة.

من أجل إدارة اتجاه الثعبان، وإنشاء وجبات خفيفة في نقاط عشوائية على الشبكة، واكتشاف الاصطدامات بين رأس الثعبان وجسمه، وتحديث حالة اللعبة، وإعادة رسم نافذة اللعبة، يجب أن يتعامل منطق اللعبة مع مدخلات المستخدم. للتحكم في تفاعلات اللعبة والتحديثات المرئية، يستخدم البرنامج النصي أساليب التعامل مع الأحداث والرسم الخاصة بـ Pygame. كما أن لديها ميزة مربع الرسائل الأساسية التي تستخدم Tkinter لإظهار رسائل اللاعب، مثل عندما يخسر اللعبة. بشكل عام، يُظهر النص الأفكار الأساسية في برمجة ألعاب Pygame ويقدم عرضًا أوليًا للعبة Snake القديمة.