Python Battleship Game


Project Overview

A battleship game written in Python (version 2.7) utilizing the pygame library. It is a single-player game where the goal is to sink all of the ships in the shortest number of turns possible. The game is played on an interactive 10x10 grid. As the game is played, feedback is given on the game board as it changes colors, sound is played, and text is printed out to let the player know what coordinates are being chosen, and if their choice was a hit or a miss. Every time the game is started, a random ship placement is created with the use of the imported random library.

How To Play

  1. To use this program, first run main.py
    • This will immediately bring up the instructions for playing the game, which are located in a .txt file.
  2. When “1” is entered, a small 10x10 grid will appear with the title “Battleship”.
    • This grid was created with the use of a 10-element-long list within another 10-element-long list that behaves like a 2D matrix. The first index of the list is the row and the second is the column. This creates the 100 elements that make up the entire grid. Nested for loops are used to access the elements of the 2D list.
  3. A randomly generated ship layout is created within the grid. To play the game, click on the white grid spaces. This will result in the selected space chagning color to either red (hit) or green (miss) and a sound will play according to the result.
    • Every time a space is clicked, a counter is incremented to represent the number of turns it takes to sink all of the ships. The final score will be printed in a .txt file to display a high scores list at the conclusion of the game.
  4. After each turn, the current ships sunk will be printed out along with the turn count to show game progress. The game continues until all ships have been sunk.
  5. When the game is completed, the game grid is deactivated and a prompt to enter the player name appears.
    • The name is placed in a python dictionary along with the number of turns it took to sink all of the ships and a timestamp for when the game was played. This is then added to the high scores list.
  6. After viewing the high scores, press “2” to exit the program.

Learning Outcomes

This was my first major project using Python. Through the creation of this game, I learned how to write separate python class files and package them together to form a single program. I also learned how to utilize imported libraries such as pygame, random, and time.

Project Repo: https://github.com/Novota15/PythonBattleship

Top