Home‎ > ‎GameDev Blog‎ > ‎

Code Session: Coding A Functional iPod in Python

posted Nov 11, 2015, 12:06 PM by oneclipleft   [ updated Dec 2, 2015, 2:28 PM ]
The most recent challenge I completed in my journey to learn how to code was coding a simple mp3 player that functions in-game. Here's how I accomplished that in Python/Pygame!

1. Here’s an in-game view of the iPod. Each song is selectable and will play a different .ogg file that is loaded when a new game starts. I’m hoping to find a good way to speed up the load times for this, since it currently takes a little longer than I’d like. I could stream the tracks, but that makes the iPod’s in-game behavior kinda erratic.


2. Here is the Button class that I use for pretty much anything requiring menu-style buttons. This handles the track names in the iPod display screen, allowing the player to choose a song and then play it.


3. Here is the code that determines if the iPod should be drawn on the screen, loads the music tracks, determines which song should be selected, and creates the buttons from the above Button class (also adding them to a sprite group). This code could probably be simplified or improved, but it works as intended, so it’s unlikely that I’ll alter it.


4. This code handles player input (in this case, hitting the “F” key to use an item). It will display the iPod if it is not already displayed. If it is already displayed, then it will play the currently selected track. I also have similar code for switching between selected tracks (which again, could probably be improved, but it works so, y’know). Missing here is the code for controller support, but it’s practically identical and not worth examining here. I’ll show controller code next time!


5. This clunky-ass code takes the number stored in “abbysipodsongselected” and uses that to determine which song should be highlighted and ready to play. I’ve learned an easier way to code this since I wrote it, but I generally refuse to change perfectly functional code just so it takes up fewer lines (unless I’m testing it just to see if it still works a different way).


6. This is the code that actually draws the elements to the screen. Pretty straight-forward I think.


Well, that’s all for this time. I have a few more interesting things to code, then all the dependencies for winning the game (it’s not possible to lose). After that? Art! Music! Narrative! Peace!
-oneclipleft
Comments