1/29/2024 0 Comments Play sound in python![]() """Replaces the original run_game(), so we can interject our own """Automatic player for Alien Invasion.""" # Need a reference to the game object. Here’s the core structure of the AIPlayer class:įrom alien_invasion import AlienInvasion class AIPlayer : def _init_ ( self, ai_game ): # 1 Then we’ll write a new run_game() method, where we can interject code that controls the elements of the game. Instead, we’ll write our own class called AIPlayer, and we’ll give this class the game object. We’d normally call run_game() to start a game, but that would start the main game loop and we’d never be able to take control except by using the keyboard. To automate the game play, we need to make an instance of AlienInvasion. We’ll do everything from here on out in ai_player.py the point of this is to automate the game play without touching any of the original game code. Start by making a new file called ai_player.py, in the same directory as alien_invasion.py. Let’s start out by writing a program that takes control of the ship, and fires as often as possible. It’s perfectly reasonable to read through this guide to get started, and then explore further automation ideas on your own. If you’re new to programming, though, getting this set up can be pretty challenging. If you want to try this on your own before reading ahead, you can see the challenges here. You might also add sounds for reaching certain scoring milestones like 10,000 points, 100,000 points, and so on. You might also want to add sounds for the start of a game, clearing a level, an alien hitting the ship, an alien hitting the ground, and the game ending. Now your game should sound much more interesting! You should hear a steady stream of sounds as you fire bullets and destroy aliens. We play the alien sound whenever there’s a collisions dictionary, indicating that an alien has just been destroyed (1). aliens : # Destroy existing bullets and create new fleet. ![]() aliens, True, True ) if collisions : for aliens in collisions. This file is pretty short:ĭef _check_bullet_alien_collisions ( self ): """Respond to bullet-alien collisions.""" # Remove any bullets and aliens that have collided.Ĭollisions = pygame. We’ll start by making a new file called sound_effects.py, where we can define all of the sound effects we’ll use in the game. Store the sound files you want to use in your sounds folder. This folder should be at the same directory level as your images folder. Make a new folder in your alien_invasion folder called sounds. I chose laser1.wav from user dklon for firing bullets, and Explosion_02.wav from Little Robot Sound Factory for an alien being hit. There are lots of resources available for finding sound effects. (If the official Pygame docs are “sleeping”, try this mirror site.) You can take a look at the documentation I also found this Nerd Paradise post helpful. The Pygame Mixer Module manages music and sound effects. In this section, we’re going to play a firing sound each time the ship fires a bullet, and an explosion sound each time an alien is shot down. If you want to take this as a challenge before reading this guide, feel free to look at the Pygame documentation, and see if you can add sounds on your own. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |