← Back to Python Course | Chapter 14: Advanced Python & Tools | Lesson 14 of 15

Python pygame परिचय

Pygame, Python में games बनाने के लिए एक toolkit है, बिल्कुल game बनाने के सामान का एक box जैसा। यह drawing, keys, और game loop को handle करता है जो चीज़ों को चलता रखता है।
Syntax
python
import pygame

pygame.init()
screen = pygame.display.set_mode((width, height))
# game loop
pygame.quit()

Pygame क्या है?

Pygame, SDL के ऊपर 2D games और interactive graphics बनाने के लिए एक third-party library है, जो window management, drawing और input को game-oriented तरीके से handle करती है, जिस तरह से Tkinter जैसे general GUI toolkits design नहीं किए गए हैं।

इसे चलाने के लिए भी असली graphical display चाहिए, हालाँकि Pyodide जैसे browser environments इसे भी चला सकते हैं।

उदाहरण: What is Pygame?

python
import pygame
print(pygame.ver)

Pygame को Initialize करना

किसी भी और Pygame functionality इस्तेमाल करने से पहले pygame.init() call करना ज़रूरी है -- यह Pygame के सभी internal modules (display, audio, input) को एक साथ initialize करता है, और इसे skip करने से library के unrelated हिस्सों में confusing failures आती हैं।

उदाहरण: Initializing Pygame

python
import pygame
pygame.init()  # must run before any other pygame functionality is used
print("Pygame initialized")
pygame.quit()

Game Loop

हर Pygame program एक central game loop के इर्द-गिर्द बना होता है -- एक जानबूझकर बनाया गया infinite loop, जो हर iteration पर input events process करता है, game state update करता है, और screen को दोबारा draw करता है, जब तक player quit न करे तब तक लगातार चलता रहता है।

उदाहरण: The Game Loop

python
import pygame
pygame.init()
running = True
frame = 0
while running and frame < 3:  # stands in for the continuous game loop
    frame += 1
    running = frame < 3
print("Game loop ran", frame, "frames")
pygame.quit()

System Events को Handle करना

pygame.event.get(), पिछली check के बाद से हुई सभी input events (key presses, mouse clicks, window-close requests) लाता है।

Game loop के अंदर quit event की जाँच करना ज़रूरी है, क्योंकि इसके बिना window का close button असल में program को रोकेगा नहीं।

उदाहरण: Handling System Events

python
import pygame
pygame.init()
events = pygame.event.get()  # all input events since the last check
print("Events checked:", events)
pygame.quit()

Simple Shapes बनाना

pygame.draw.rect() और pygame.draw.circle() हर frame पर सीधे screen surface पर basic shapes render करते हैं, जो वह foundation बनाते हैं जिस पर आमतौर पर ज़्यादा complex game graphics -- sprites, animations, UI elements -- बनाई जाती हैं।

उदाहरण: Drawing Simple Shapes

python
import pygame
pygame.init()
screen = pygame.Surface((100, 100))
pygame.draw.rect(screen, (255, 0, 0), (10, 10, 50, 50))  # red rectangle
pygame.draw.circle(screen, (0, 255, 0), (50, 50), 20)  # green circle
print("Shapes drawn to surface")
pygame.quit()
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.