Pygame ऑनलाइन
Pygame से गेम बनाएँ और सीधे ब्राउज़र में खेलें। यह असली pygame-ce है: गेम विंडो इसी पेज पर खुलती है, और कीबोर्ड, माउस और साउंड काम करते हैं। आपका सामान्य while लूप clock.tick(60) के साथ वैसे ही चलता है, asyncio की ज़रूरत नहीं। कोई उदाहरण चुनें, चलाएँ और खेलने के लिए विंडो में क्लिक करें।
Pygame कोड ऑनलाइन चलाएँ
Ctrl+Enter से प्रोग्राम चलता है। समझाएँ और ठीक करें बिना अकाउंट के मुफ़्त में आज़माएँ, ज़्यादा इस्तेमाल के लिए साइन इन करें।
प्ले या यह कोड चलाएँ दबाएँ: गेम यहाँ खुलेगा। कीबोर्ड इस्तेमाल करने के लिए विंडो में क्लिक करें।
Pygame गेम लूप
Pygame प्रोग्राम pygame.display.set_mode से विंडो खोलता है और फिर हर सेकंड कई बार वही लूप दोहराता है: इवेंट पढ़ना, गेम अपडेट करना, फ़्रेम बनाना, उसे pygame.display.flip() से दिखाना और clock.tick(60) से रुकना, ताकि गेम 60 फ़्रेम प्रति सेकंड पर चले।
pygame.key.get_pressed() बताता है कि इस समय कौन सी कीज़ दबी हैं, और स्मूद मूवमेंट के लिए यही चाहिए। यहाँ True को 1 गिना जाता है, इसलिए keys[K_RIGHT] - keys[K_LEFT] का नतीजा 1, -1 या 0 होता है।
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 320))
pygame.display.set_caption("Move the square")
clock = pygame.time.Clock()
font = pygame.font.SysFont("arial", 20)
x, y = 220, 140
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 5
y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 5
x = max(0, min(440, x))
y = max(0, min(280, y))
screen.fill((25, 28, 45))
pygame.draw.rect(screen, (255, 200, 0), (x, y, 40, 40), border_radius=6)
screen.blit(font.render("Click here, then use the arrow keys", True, (220, 220, 220)), (10, 10))
pygame.display.flip()
clock.tick(60)
pygame.quit()
माउस और साउंड वाला छोटा गेम
टोकरी MOUSEMOTION इवेंट के साथ चलती है, तारे pygame.Rect हैं और colliderect पकड़ को पहचानता है। "डिंग" की आवाज़ NumPy से बनाई जाती है और pygame.sndarray.make_sound से साउंड में बदली जाती है, इसलिए इस उदाहरण को किसी फ़ाइल की ज़रूरत नहीं। ब्राउज़र क्लिक के बाद ही आवाज़ चलाते हैं, इसलिए पहले विंडो में क्लिक करें।
import random
import numpy as np
import pygame
pygame.mixer.pre_init(44100, -16, 2)
pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Catch the stars")
clock = pygame.time.Clock()
font = pygame.font.SysFont("arial", 22, bold=True)
# a short "ding" made with NumPy, no sound file needed
t = np.linspace(0, 0.12, int(44100 * 0.12), False)
tone = (np.sin(880 * 2 * np.pi * t) * np.exp(-t * 30) * 20000).astype(np.int16)
ding = pygame.sndarray.make_sound(np.column_stack([tone, tone]))
basket = pygame.Rect(200, 330, 80, 14)
stars = []
score = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEMOTION:
basket.centerx = event.pos[0]
if random.random() < 0.04:
stars.append(pygame.Rect(random.randint(10, 460), -10, 12, 12))
for star in stars[:]:
star.y += 4
if star.colliderect(basket):
stars.remove(star)
score += 1
ding.play()
elif star.y > 360:
stars.remove(star)
screen.fill((10, 10, 30))
for star in stars:
pygame.draw.circle(screen, (255, 230, 90), star.center, 6)
pygame.draw.rect(screen, (90, 200, 255), basket, border_radius=4)
screen.blit(font.render(f"Score: {score}", True, (255, 255, 255)), (10, 10))
pygame.display.flip()
clock.tick(60)
pygame.quit()
50 लाइनों में स्नेक गेम
साँप खानों की एक लिस्ट है। हर कदम पर मौजूदा दिशा में नया सिर जुड़ता है और पूँछ हटती है, जब तक उसने अभी कुछ खाया न हो। KEYDOWN इवेंट दिशा बदलते हैं (सीधे उल्टा नहीं मुड़ सकते) और clock.tick(10) रफ़्तार तय करता है।
import random
import pygame
pygame.init()
CELL, COLS, ROWS = 20, 24, 16
screen = pygame.display.set_mode((CELL * COLS, CELL * ROWS))
pygame.display.set_caption("Snake")
clock = pygame.time.Clock()
font = pygame.font.SysFont("arial", 20)
snake = [(5, 8), (4, 8), (3, 8)]
direction = (1, 0)
food = (15, 8)
alive = True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
raise SystemExit
if event.type == pygame.KEYDOWN:
turns = {pygame.K_UP: (0, -1), pygame.K_DOWN: (0, 1), pygame.K_LEFT: (-1, 0), pygame.K_RIGHT: (1, 0)}
new = turns.get(event.key)
if new and (new[0] != -direction[0] or new[1] != -direction[1]):
direction = new
if event.key == pygame.K_SPACE and not alive:
snake, direction, alive = [(5, 8), (4, 8), (3, 8)], (1, 0), True
if alive:
head = ((snake[0][0] + direction[0]) % COLS, (snake[0][1] + direction[1]) % ROWS)
if head in snake:
alive = False
else:
snake.insert(0, head)
if head == food:
food = (random.randrange(COLS), random.randrange(ROWS))
else:
snake.pop()
screen.fill((20, 24, 20))
pygame.draw.rect(screen, (230, 70, 70), (food[0] * CELL, food[1] * CELL, CELL, CELL))
for x, y in snake:
pygame.draw.rect(screen, (90, 220, 110), (x * CELL + 1, y * CELL + 1, CELL - 2, CELL - 2))
text = f"Length {len(snake)}" if alive else "Game over - press Space"
screen.blit(font.render(text, True, (255, 255, 255)), (8, 6))
pygame.display.flip()
clock.tick(10)
Pygame की आम गलतियाँ
1. इवेंट, flip() और tick() के बिना लूप
pygame.event.get() के बिना विंडो जवाब देना बंद कर देती है, pygame.display.flip() के बिना आपका बनाया कुछ नहीं दिखता, और clock.tick() के बिना लूप उतनी तेज़ चलता है जितना कंप्यूटर चला सके। यह प्रोग्राम done, x = 360 प्रिंट करता है लेकिन चौकोर कभी नहीं दिखाता:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 240))
x = 0
# Wrong: no pygame.event.get(), no flip(), no tick()
# The window never shows the square and the program runs as fast as it can.
while x < 360:
x += 1
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 0), (x, 100, 40, 40))
print("done, x =", x)
तीनों स्टेप के साथ चौकोर लगभग तीन सेकंड में विंडो पार करता है:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 240))
clock = pygame.time.Clock()
x = 0
while x < 360:
for event in pygame.event.get(): # 1. read events (and QUIT)
if event.type == pygame.QUIT:
x = 360
x += 2
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 0), (x, 100, 40, 40))
pygame.display.flip() # 2. show the frame
clock.tick(60) # 3. 60 frames per second
print("done, x =", x)
pygame.quit()
2. इमेज और साउंड फ़ाइलें नहीं मिलतीं
pygame.image.load("player.png") फ़ाइल को प्रोग्राम के पास ढूँढता है। Python एडिटर में पहले फ़ाइल को अपने वर्कस्पेस में, प्रोग्राम वाले फ़ोल्डर में अपलोड करें।
3. pygame.init() से पहले pygame.font से टेक्स्ट लिखना
pygame.font.SysFont को pygame.init() से पहले बुलाने पर "font not initialized" एरर आती है। प्रोग्राम की शुरुआत में सबसे पहले pygame.init() बुलाएँ।
Pygame ब्राउज़र में कैसे चलता है
Python WebAssembly की मदद से आपके डिवाइस पर चलता है, और pygame-ce एक canvas पर ड्रॉ करता है जिसे यह पेज गेम विंडो की तरह दिखाता है। कीबोर्ड और माउस के इवेंट वापस Pygame को जाते हैं, और pygame.mixer ब्राउज़र के ज़रिए आवाज़ चलाता है। Chrome, Edge, Firefox और Safari में चलता है। पहली बार Python और Pygame डाउनलोड होते हैं; उसके बाद कैश से खुलते हैं।
कई फ़ाइलों, इमेज और साउंड वाले बड़े प्रोजेक्ट के लिए Python एडिटर इस्तेमाल करें: आपका वर्कस्पेस सेव होता है और Pygame गेम वहाँ भी अपनी विंडो खोलते हैं। turtle से ड्रॉ करते हैं? Python turtle ऑनलाइन आज़माएँ।
अक्सर पूछे जाने वाले सवाल
Pygame ऑनलाइन कैसे चलाएँ?
अपना Pygame कोड इस पेज के एडिटर में लिखें या पेस्ट करें और चलाएँ। गेम विंडो पेज पर खुलती है; उसमें क्लिक करें और कीबोर्ड व माउस से खेलें। कुछ इंस्टॉल नहीं करना।
क्या यह असली Pygame है?
हाँ। यह pygame-ce (Pygame का मेंटेन किया जाने वाला वर्ज़न) है जो ब्राउज़र के लिए कंपाइल किया गया है, इसलिए ड्रॉइंग, फ़ॉन्ट, Surface, Rect, स्प्राइट, transform और इमेज फ़ंक्शन आपके कंप्यूटर जैसे ही चलते हैं।
क्या साउंड चलता है?
हाँ। pygame.mixer.Sound, Channel और pygame.mixer.music WAV, OGG और MP3 फ़ाइलें चलाते हैं, और pygame.sndarray से बनी आवाज़ें भी। ब्राउज़र क्लिक या की-प्रेस के बाद ही आवाज़ चालू करते हैं, इसलिए पहले रन बटन या गेम विंडो पर क्लिक करें।
क्या मुझे अपना गेम लूप बदलना होगा?
नहीं। pygame.event.get(), pygame.display.flip() और clock.tick(60) वाला सामान्य while लूप वैसे ही चलता है। asyncio की ज़रूरत नहीं।
कौन से ब्राउज़र काम करते हैं?
Chrome, Edge, Firefox और Safari। सभी में कीज़ और क्लिक गेम तक पहुँचते हैं; Chrome और Edge सबसे तेज़ हैं।
क्या मैं इमेज और फ़ॉन्ट इस्तेमाल कर सकता हूँ?
हाँ। Python एडिटर में इमेज और साउंड अपने वर्कस्पेस में अपलोड करें और उन्हें नाम से लोड करें। pygame.font.SysFont आपके सिस्टम के फ़ॉन्ट इस्तेमाल करता है, इसलिए हिंदी, Arial, Segoe UI और चीनी, जापानी, कोरियाई फ़ॉन्ट सही दिखते हैं।