Python pygame परिचय
In this page:
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?
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
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
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
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
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()
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- Python PEP 8 Style Guide
- Python Debugging Techniques
- Python Testing with unittest
- Python Common Mistakes
- Python Interview Questions
- Python map() & filter()
- Python reduce()
- Python zip() & enumerate()
- Python sorted() & key Functions
- Python Comprehensions Advanced
- Python Turtle Graphics
- Python tkinter Introduction
- Python tkinter Widgets
- Python pygame Introduction
- Python Mini Projects