Опубликован: 06.08.2013 | Доступ: свободный | Студентов: 831 / 13 | Длительность: 27:51:00
Лекция 10:

Four Extra Games

< Лекция 9 || Лекция 10 || Лекция 11 >
Аннотация: Programming games.

Four Extra Games

Included in this chapter is the source code for four extra games. Unfortunately, only the source code (including comments) is in this chapter without any detailed explanation of the code. By now, you can play these games and figure out how the code works by looking at the source code and comments. The games are:

  • Flippy – An "Othello" clone where the player tries to flip the computer AI player’s tiles.
  • Ink Spill – A "Flood It" clone that makes use of the flood fill algorithm.
  • Four in a Row – A "Connect Four" clone against the computer AI player.
  • Gemgem – A "Bejeweled" clone where the player swaps gems to try to get three identical gems in a row.

If you have any questions about the source code in this book, feel free to email the author at al@inventwithpython.com Buggy versions of these programs are also available if you want to practice fixing bugs:

Flippy, an "Othello" Clone


Рис. 10.1.

Othello, also known by the generic name Reversi, has an 8 x 8 board with tiles that are black on one side and white on the other. The starting board looks like Figure 10-1. Each player takes turn placing down a new tile of their color. Any of the opponent's tiles that are between the new tile and the other tiles of that color is flipped. The goal of the game is to have as many of the tiles with your color as possible. For example, Figure 10-2 is what it looks like if the white player places a new white tile on space 5, 6.

On the left image - The starting Reversi board has two white tiles  and two black tiles. On the right image - White places a new tile.

Рис. 10.2. On the left image - The starting Reversi board has two white tiles and two black tiles. On the right image - White places a new tile.

The black tile at 5, 5 is in between the new white tile and the existing white tile at 5, 4. That black tile is flipped over and becomes a new white tile, making the board look like Figure 10-3. Black makes a similar move next, placing a black tile on 4, 6 which flips the white tile at 4, 5. This results in a board that looks like Figure 10-4.

On the left image - White's move will flip over one of black's tiles.  On the right image - Black places a new tile, which flips over one of  white's tiles.

Рис. 10.3. On the left image - White's move will flip over one of black's tiles. On the right image - Black places a new tile, which flips over one of white's tiles.

Tiles in all directions are flipped as long as they are in between the player's new tile and existing tile. In Figure 10-5, the white player places a tile at 3, 6 and flips black tiles in both directions (marked by the lines). The result is in Figure 10-6.

On the left image - White's second move at 3, 6 will flip two of  black's tiles.   On the right image - The board after white's second move.

Рис. 10.4. On the left image - White's second move at 3, 6 will flip two of black's tiles. On the right image - The board after white's second move.

As you can see, each player can quickly grab a majority of the tiles on the board in just one or two moves. Players must always make a move that captures at least one tile. The game ends when a player either cannot make a move, or the board is completely full. The player with the most tiles of their color wins.

You can learn more about Reversi from Wikipedia: http://en.wikipedia.org/wiki/Reversi. A text version of this game that uses print() and input() instead of Pygame is featured in Chapter 15 of "Invent Your Own Computer Games with Python". You can read that chapter for details about how the computer AI’s algorithm was put together. http://inventwithpython.com/chapter15.html

The computer AI for this game is pretty good, because it is easy for a computer to simulate every possible move and take the one that flips over the most tiles. It usually beats me whenever I play it.

Source Code for Flippy

This source code can be downloaded from http://invpy.com/flippy.py. The image files that Flippy uses can be downloaded from http://invpy.com/flippyimages.zip.

Ink Spill, a "Flood It" Clone


Рис. 10.5.

The game "Flood It" begins with a board filled with colored tiles. On each turn the player chooses a new color to paint the top left tile and any tiles adjacent to it of that same color. This game makes use of the flood fill algorithm (described in the Star Pusher chapter). The goal of the game is to turn the entire board into a single color before running out of turns.

This game also has a Settings screen where the player can change the size of the board and the difficulty of the game. If the player gets board of the colors, there are a few other color schemes they can switch to as well.

Source Code for Ink Spill

This source code can be downloaded from http://invpy.com/inkspill.py. The image files that Flippy uses can be downloaded from http://invpy.com/inkspillimages.zip.

Four-In-A-Row, a "Connect Four" Clone


Рис. 10.6.

The game "Connect Four" has a 7 x 6 board where the players take turns dropping tokens from the top of the board. The tokens will fall from the top of each column and come to rest on the bottom of the board or on top of the topmost token in that column. A player wins when four of their tokens line up in a row either horizontally, vertically, or diagonally.

The AI for this game is pretty good. It simulates every possible move it can make, then simulates every possible move the human player can make in response to each of those moves, and then simulates every possible move it can make in response to that, and then simulates every possible move the human player could make in response to each of those moves! After all that thinking, the computer determines which move is most likely to lead to it winning.

So the computer is kind of tough to beat. I usually lose to it.

Since there are seven possible moves you can make on your turn (unless some columns are full), and seven possible moves the opponent could make, and seven moves in response to that, and seven moves in response to that, that means that on each turn the computer is considering 7 x 7 x 7 x 7 = 2,401 possible moves. You can make the computer consider the game even further by setting the DIFFICULTY constant to a higher number, but when I set to a value larger than 2, the computer takes a long time to calculate its turn.

You can also make the computer easier by setting DIFFICULTY to 1. Then the computer only considers each of its moves and the player’s possible responses to those moves. If you set the DIFFICULTY to 0, then the computer loses all intelligence and simply makes random moves.

Source Code for Four-In-A-Row

This source code can be downloaded from http://invpy.com/fourinarow.py . The image files that Flippy uses can be downloaded from http://invpy.com/fourinarowimages.zip.

Gemgem, a "Bejeweled" Clone


Рис. 10.7.

"Bejeweled" is a game where gems fall to fill up a board. The player can swap any two adjacent gems to try to match three gems in a row (vertically or horizontally, but not diagonally). The matched gems then disappear, making way for new gems to fall from the top. Matching more than three gems, or causing a chain reaction of gem matches will result in more points. The player’s score slowly drops over time, so the player must constantly be making new matches. The game ends when no possible match can be made on the board.

Source Code for Gemgem

This source code can be downloaded from http://invpy.com/gemgem.py . The image files that Flippy uses can be downloaded from http://invpy.com/gemgemimages.zip.

Summary

I hope these game programs have given you your own ideas about what games you’d like to make and how you can write the code for them. Even if you don’t have any ideas of your own, it’s great practice to try to program clones of other games you’ve played. Here are several websites that can teach you more about programming Python:

  • http://pygame.org – The official Pygame website has the source code to hundreds of games that people have written that make use of the Pygame library. You can learn a lot by downloading and reading other people’s source code.
  • http://python.org/doc/ - More Python tutorials and the documentation of all the Python modules and functions.
  • http://pygame.org/docs/ Complete documentation on the modules and functions for Pygame.
  • http://reddit.com/r/learnpython and http://reddit.com/r/learnprogramming have several users that could help you with finding resources to learn programming.
  • http://inventwithpython.com/pygame - This book's website, which includes all the source code for these programs and additional information. This site also has the image and sound files used in the Pygame programs.
  • http://inventwithpython.com - The website for the book "Invent Your Own Computer Games with Python", which covers basic Python programming.
  • http://invpy.com/wiki - A wiki that covers individual Python programming concepts that you can look up if you need to learn about something specific.
  • http://invpy.com/traces - A web application that helps you trace through the execution of the programs in this book, step by step.
  • http://invpy.com/videos - Videos that accompany the programs in this book.
  • http://gamedevlessons.com - A helpful website about how to design and program video games.
  • al@inventwithpython.com - My email address. Feel free to email me your questions about this book or about Python programming.

Or you can find out more about Python by searching the World Wide Web. Go to the search website http://google.com and search for "Python programming" or "Python tutorials" to find web sites that can teach you more about Python programming."

Now get going and invent your own games. And good luck!

< Лекция 9 || Лекция 10 || Лекция 11 >
Константин Шилов
Константин Шилов
Россия
Павел Лафицкий
Павел Лафицкий
Украина