
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.
Play sounds python free#
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. Speeding up the game for development work.You can find complete versions of this project in the beyond_pcc/ai_player/ folder in the downloadable resources for the book.

Automating game play is more complicated, but it’s a really interesting and satisfying exercise. Adding sound doesn’t take much code, but it makes the game much more interesting. The first part focuses on adding sound to the Alien Invasion project, and the second focuses on writing a separate program that plays the game automatically.

Pygame - Adding Sound and Automating Game Play This site uses Just the Docs, a documentation theme for Jekyll.

Play sounds python code#
Role of self and ai_game in code that creates the ship.
