गेम भौतिकी में गेम को अधिक यथार्थवादी और आकर्षक बनाने के लिए वास्तविक दुनिया की भौतिकी का अनुकरण करना शामिल है। वेग, त्वरण और गुरुत्वाकर्षण जैसे बुनियादी भौतिकी सिद्धांत खेल में गतिविधियों और अंतःक्रियाओं को स्वाभाविक बना सकते हैं।
उदाहरण: वेग के साथ बुनियादी आंदोलन
import pygame # Initialize Pygame pygame.init() # Screen setup screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Basic Movement with Velocity") # Colors white = (255, 255, 255) black = (0, 0, 0) # Player setup player = pygame.Rect(375, 275, 50, 50) velocity = pygame.Vector2(0, 0) # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Keyboard input for movement keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: velocity.x = -5 elif keys[pygame.K_RIGHT]: velocity.x = 5 else: velocity.x = 0 if keys[pygame.K_UP]: velocity.y = -5 elif keys[pygame.K_DOWN]: velocity.y = 5 else: velocity.y = 0 # Update player position player.move_ip(velocity) # Clear screen screen.fill(white) # Draw player pygame.draw.rect(screen, black, player) # Update display pygame.display.flip() pygame.quit()
गुरुत्वाकर्षण वस्तुओं को नीचे की ओर खींचकर, पृथ्वी पर गुरुत्वाकर्षण के प्रभाव का अनुकरण करके खेलों में यथार्थवाद जोड़ता है।
उदाहरण: गिरती हुई वस्तु में गुरुत्वाकर्षण जोड़ना
import pygame # Initialize Pygame pygame.init() # Screen setup screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Gravity Simulation") # Colors white = (255, 255, 255) black = (0, 0, 0) # Object setup object = pygame.Rect(375, 50, 50, 50) gravity = 0.5 velocity_y = 0 # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Apply gravity velocity_y = gravity object.y = velocity_y # Reset object position if it falls off-screen if object.y > 600: object.y = 50 velocity_y = 0 # Clear screen screen.fill(white) # Draw object pygame.draw.rect(screen, black, object) # Update display pygame.display.flip() pygame.quit()
गतिशील गेम बनाने के लिए, अक्सर उछलती हुई वस्तुओं का अनुकरण करना आवश्यक होता है, जैसे कि दीवारों से उछलती हुई गेंद।
उदाहरण: बाउंसिंग बॉल सिमुलेशन
import pygame # Initialize Pygame pygame.init() # Screen setup screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Bouncing Ball") # Colors white = (255, 255, 255) black = (0, 0, 0) # Ball setup ball = pygame.Rect(375, 275, 50, 50) velocity = pygame.Vector2(4, 4) # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Move ball ball.move_ip(velocity) # Bounce off walls if ball.left = 800: velocity.x = -velocity.x if ball.top = 600: velocity.y = -velocity.y # Clear screen screen.fill(white) # Draw ball pygame.draw.ellipse(screen, black, ball) # Update display pygame.display.flip() pygame.quit()
लक्ष्य: एक गेम बनाएं जहां एक गेंद स्क्रीन के चारों ओर उछलती है, दीवारों से टकराने पर दिशा बदलती है।
import pygame # Initialize Pygame pygame.init() # Screen setup screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Bouncing Ball Game") # Colors white = (255, 255, 255) black = (0, 0, 0) # Ball setup ball = pygame.Rect(375, 275, 50, 50) velocity = pygame.Vector2(3, 3) # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Move ball ball.move_ip(velocity) # Bounce off walls if ball.left = 800: velocity.x = -velocity.x if ball.top = 600: velocity.y = -velocity.y # Clear screen screen.fill(white) # Draw ball pygame.draw.ellipse(screen, black, ball) # Update display pygame.display.flip() pygame.quit()
एक गहन गेम अनुभव बनाने के लिए ध्वनि प्रभाव और संगीत महत्वपूर्ण हैं। Pygame आपको अपने गेम में आसानी से ध्वनि जोड़ने की अनुमति देता है।
उदाहरण: ध्वनि प्रभाव जोड़ना
import pygame # Initialize Pygame and Mixer pygame.init() pygame.mixer.init() # Load sound effect bounce_sound = pygame.mixer.Sound("bounce.wav") # Screen setup screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Sound Effects") # Colors white = (255, 255, 255) black = (0, 0, 0) # Ball setup ball = pygame.Rect(375, 275, 50, 50) velocity = pygame.Vector2(3, 3) # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Move ball ball.move_ip(velocity) # Bounce off walls and play sound if ball.left = 800: velocity.x = -velocity.x bounce_sound.play() # Play sound on bounce if ball.top = 600: velocity.y = -velocity.y bounce_sound.play() # Clear screen screen.fill(white) # Draw ball pygame.draw.ellipse(screen, black, ball) # Update display pygame.display.flip() pygame.quit()
उदाहरण: पृष्ठभूमि संगीत जोड़ना
import pygame # Initialize Pygame and Mixer pygame.init() pygame.mixer.init() # Load and play background music pygame.mixer.music.load("background_music.mp3") pygame.mixer.music.play(-1) # Loop indefinitely # Screen setup screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Background Music") # Colors white = (255, 255, 255) black = (0, 0, 0) # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Clear screen screen.fill(white) # Update display pygame.display.flip() pygame.quit()
विशिष्ट गेम घटनाओं, जैसे टकराव या खिलाड़ी की गतिविधियों के आधार पर ध्वनि प्रभाव ट्रिगर किया जा सकता है।
उदाहरण: ध्वनि मेमोरी गेम
python import pygame import random # Initialize Pygame and Mixer pygame.init() pygame.mixer.init() # Load sounds sounds = [pygame.mixer.Sound(f"sound{i}.wav") for i in range(4)] # Screen setup screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Sound Memory Game") # Colors white = (255, 255, 255) black = (0, 0, 0) # Generate random sequence of sounds sequence = [random.choice(sounds) for _ in range(5)] current_step = 0 # Main game loop running = True while running:
अस्वीकरण: उपलब्ध कराए गए सभी संसाधन आंशिक रूप से इंटरनेट से हैं। यदि आपके कॉपीराइट या अन्य अधिकारों और हितों का कोई उल्लंघन होता है, तो कृपया विस्तृत कारण बताएं और कॉपीराइट या अधिकारों और हितों का प्रमाण प्रदान करें और फिर इसे ईमेल पर भेजें: [email protected] हम इसे आपके लिए यथाशीघ्र संभालेंगे।
Copyright© 2022 湘ICP备2022001581号-3