30 Days of Python: Day 26 Cryptoquip

I’m making a small project every day in python for the next 30 days (minus some vacation days). I’m hoping to learn many new packages and  make a wide variety of projects, including games, computer tools, machine learning, and maybe some science. It should be a good variety and I think it will be a lot of fun.

Day 26: Cryptoquip

For today’s project, I made a cryptoquip game. This is my favorite newspaper puzzle where the goal is to decode a witty saying that’s been encrypted with a substitution cipher. Spaces and punctuation are left in tack so it’s all about guessing which letters are which by looking for letters that might be by themselves (a, I) or double letters (often e, l, etc.). It’s a fun puzzle to play. Here’s a screenshot of one that’s nearly solved:

Einstein Quote

The encryption method is very easy to implement. The key is a dictionary with a random shuffle of a ciphertext alphabet mapped to the plaintext alphabet. The ciphertext (the encoded message) uses all caps, while the plaintext uses lower case. A simple list comprehension can do the substitution:

def encrypt_substitute(message, key):
    '''Encrypts the message with a substitution cipher'''
    return ''.join([key[m] if m in key else m for m in message])

For the guessing portion, the player moves the cursor around with the arrows and can work in either the message or on the side in the key. The working key is initialized with all underscores for each ciphertext letter. The same encryption method does the encryption with the reversed key. I wanted to add the ability click on the letter you want to change but I ran out of time. Perhaps I’ll come back and add that after the 30 days are up, which isgetting quite close.

For those who are interested here’s my github repo for the games project: https://github.com/robb07/python_games

 

 

Advertisements

30 Days of Python: Day 22 Game Menu

I’m making a small project every day in python for the next 30 days (minus some vacation days). I’m hoping to learn many new packages and  make a wide variety of projects, including games, computer tools, machine learning, and maybe some science. It should be a good variety and I think it will be a lot of fun.

Day 22: Game Menu

Today’s project was making a central game menu so I can play different games without having to run a new program. This required some reworking of how frames start and stop in simplegui. The menu module imports each game’s module and runs it there. Instead of quitting pygame when the window is closed frame.quit() gets called after the frame stops running but only if the game is the main module. This means if the menu opens the game, then closing the game returns to the menu. To get the display to return to the menu size, the menu frame is restarted.

menu

I also added some features to breakout: levels, a faster ball, and animated points replace the broken bricks to display the points earned. These new features make that game more challenging and more fun.

breakout
For those who are interested here’s my github repo for the games project: https://github.com/robb07/python_games

30 Days of Python: Day 21 Tetris Fixes

I’m making a small project every day in python for the next 30 days (minus some vacation days). I’m hoping to learn many new packages and  make a wide variety of projects, including games, computer tools, machine learning, and maybe some science. It should be a good variety and I think it will be a lot of fun.

Day 21: Tetris Fixes

I’m back from vacation but I decided to start off with a small project today. Today’s project was fixing some bugs and adding some features to Tetris. The main bug was that the game slowed down more and more as blocks were added to the game. This was because I was checking for overlap with all of the other blocks. The fix was pretty simple: group the dropped blocks by rows instead of all in one list. That way I can just select the appropriate row to check for overlaps greatly reducing the number of blocks to check.

The features I added included, flashing the blocks after a line was complete, tracking the high score, and speeding up as the game progresses. I could easy flash the blocks because of the new organization of the blocks into rows that I mentioned before. That allowed me to track the row position that needed to flash. The new features really improve the game play and make it slightly addicting but I guess that’s the sign of a good game.

For those who are interested here’s my github repo for the games project: https://github.com/robb07/python_games

30 Days of Python: Day 18 Breakout

I’m making a small project every day in python for the next 30 days (minus some vacation days). I’m hoping to learn many new packages and  make a wide variety of projects, including games, computer tools, machine learning, and maybe some science. It should be a good variety and I think it will be a lot of fun.

Day 18: Breakout

I made the game breakout for today’s project. Breakout is another classic game where you use a paddle to bounce a ball up at a wall of bricks, breaking the bricks one at a time. If the paddle is moving when the ball bounces off of it, some of the paddle’s velocity gets imparted onto the ball. That means you can speed up the ball or slow it down if you are careful. More often than not though, the ball gets going too fast and you lose it. Another fun feature I added was an extra multiplier for the score when you get more than block before the ball hits the paddle again. If you break into the space above the bricks you can get very long runs going. Here’s what the game looks like:

Breakout

Since this is the fifth game in my games repository, I tried to focus on putting any new features into the sprite class instead of writing one off code for breakout. In fact, I didn’t make any new classes for this game. All the pieces are vanilla sprites with some game functions to manage the objects. To the sprite class I added the ability to set the draw and update methods from some standard methods included in the sprite module. The default draw method is a rectangle but now there’s a function for making a sprite be a circle, which the ball uses. The update method is used to move the objects around on the screen with some velocity. In addition to the default of objects moving on to infinity forever, there are three update methods that each handle the walls in different ways:

  1. update_bounce: sprites bounce off of the world’s walls
  2. update_toroid: sprites pass through and wrap around to the opposite side to come back onto the screen
  3. update_stay_in_world: sprites stop moving at a wall or slide along the wall if they were moving at a diagonal

The ball from breakout, the snake from snake, and the paddle from breakout are examples of each of these update methods respectively. Tetris could use the update_stay_in_world method but I haven’t changed that over yet because the nested blocks make it a bit more complicated. Overall, I’d say I’m starting to see how to make python code that reuses old code and doesn’t necessitate excessive new code for every project.

For those who are interested here’s my github repo for the games project: https://github.com/robb07/python_games

30 Days of Python: Day 7: Screen Captures

I’ve been learning a lot about Python over the last year, but I realized a few weeks back that I needed some more intensive time with it to get really fluent. That’s when I came up with 30 days of python. I’m going to make a small project every day in python for the next 30 days (probably a few days longer since there are some vacation days in there). I’m hoping to learn a bunch of different python packages and make a wide variety of projects  from games, to computer tools, to machine learning, and maybe some science. It should be a good variety and I think it will be a lot of fun.

Day 7: Screen Captures

Today’s python project was a short one due to time constraints but it’s a good one because it’s going to make my life so much better. For previous projects, in order to show off the game I’ve paused the game, used alt+print screen to get a screen shot, pasted it into paint, saved it, opened each up in GIMP, pasted it as a layer and then exported it to GIF. That is incredibly tedious and programming is about making tedious things automated. So in that vain I decided to automate the process and added a key handler for the print screen key. To save a screen shot of the display, I used:

pygame.image.save(self.screen, new_file)

I kept a running counter of the files saved and embeded that in new_file so each new file would be easy to sequence. I wrapped that function and then went a step further than just the print screen button. I made it so the games have an auto screen capture mode. Here’s the results for Tetris and Snake:

Tetris auto screen captured

Tetris auto screen captured

 

Snake auto screen captured

Snake auto screen captured

 

Now to be clear, I didn’t automate the whole process. I’m still building the GIF in GIMP by pasting in each layer. The process is much better now, but expect a follow on project of a utility that builds a GIF out of a list of image files.

For those who are interested here’s my github repo for the games project: https://github.com/robb07/python_games

30 Days of Python: Day 6: Images

I’ve been learning a lot about Python over the last year, but I realized a few weeks back that I needed some more intensive time with it to get really fluent. That’s when I came up with 30 days of python. I’m going to make a small project every day in python for the next 30 days (probably a few days longer since there are some vacation days in there). I’m hoping to learn a bunch of different python packages and make a wide variety of projects  from games, to computer tools, to machine learning, and maybe some science. It should be a good variety and I think it will be a lot of fun.

Day 6: Images

Today’s python project was to implement images in the simplegui module that I’ve created. The pygame package already has a lot of support for images to take advantage of and I’m sure that they can be used to better effect directly but writing a wrapper and getting it working is a good way to learn the basics. This tutorial came in handy with this project. Instead of implementing images in many places, I created an Image_Info class to handle the file name and size and an Image class to actually load in the image once the frame has started up. This is consistent with the coursera course that I started porting code from although I’m more focused on new games right now.  Because I had made a sprite class for the snake game yesterday, I added the image drawing feature there, which enabled me to draw images for the snake quite rapidly. I definitely learned more about the blit function although I still don’t know what it stands for. I was able to go back and rework the Tetris project to have the block class inherit from the sprite class. With some tweaking this gave the Tetris the ability to load images too. Here are the two resulting games:

Snake with Images

Snake with Images

 

Tetris with Images

Tetris with Images

I think I spent as much time in GIMP figuring out how to draw gradient effects as I did in implementing the images code. Overall I’m pretty pleased with the result but of course I can still see things that I’d like to tweak. One last note: to get portions of the image to show up as transparent you need to use convert_alpha instead of convert when you load the image in and convert it to pixel format.

For those who are interested here’s my github repo for the games project: https://github.com/robb07/python_games

30 Days of Python: Day 5 Snake

I’ve been learning a lot about Python over the last year but I realized a few weeks back that I needed some more intensive time with it to get really fluent. That’s when I came up with 30 days of python. I’m going to make a small project every day in python for the next 30 days (probably a few days longer since there are some vacation days in there). I’m hoping to learn a bunch of different python packages and make a wide variety of projects  from games, to computer tools, to machine learning, and maybe some science. It should be a good variety and I think it will be a lot of fun.

Day 5: Snake

Today’s python project hearkens back to my graphing calculator days.  Snake is a game where you steer the snake to the piece of food that appears on the screen. For each piece of food your snake eats, your snake gets one segment longer. It’s pretty simple to play until you get a really long snake at which point running into yourself becomes a lot easier and that’s how you lose.

The interesting aspect of Snake from a python perspective is queuing. The snake has three parts: a head, a body, and a tail. Really the tail is optional. The body is a list of body segments and I mange them as a queue. With the append function I add a new segment each time the head moves. With the pop function I take off a segment from the body and by choosing the first element of the list I get a queue that’s “first in, first out.” Which is to say, the oldest segment gets removed. So the body segments never actually move a new one gets added and the old one gets dropped — or stays if the snake just ate. The only pieces that move are the head and tail. The previous position of the head becomes the position for the new segment and the old segment’s position becomes the new tail position. To make the drawing and updating really easy, I created a sprite class that has all of the necessary infrastructure to show up on the board. If I want, I can customize the sprite with its own draw method like draw_head (adds eyes) or draw_tail (triangle). I even used the same sprite class for the food. Here’s what it looks like when you’ve been playing for a while:

snake

As I’m learning more about pygame, I’m starting to see how it differs and why it differs from the simplegui package we used in the coursera class I took. I’m also learning about some features I might want to use. For instance there’s already a sprite class in pygame. I might need to make a project out of adapting that to my games.

For those who are interested here’s my github repo for the games project: https://github.com/robb07/python_games