Skip to content

Instantly share code, notes, and snippets.

@albionbrown
Last active April 27, 2022 16:39
Show Gist options
  • Select an option

  • Save albionbrown/045002da6ac9ce78e9dbdb8ed7b8fcda to your computer and use it in GitHub Desktop.

Select an option

Save albionbrown/045002da6ac9ce78e9dbdb8ed7b8fcda to your computer and use it in GitHub Desktop.
#! /bin/python
import turtle
print("Let's play Battleships!")
columns = 8
rows = 8
line_length = 300
cell_length = 42
your_ships = []
all_cells = []
originX = -100
originY = 100
ship_offset = -21
def print_grid():
#screen_turtle = turtle.Turtle()
#screen_turtle.bgcolor('blue')
grid_turtle = turtle.Turtle()
grid_turtle.penup()
grid_turtle.speed("fastest")
# Draw rows
grid_turtle.setx(originX)
grid_turtle.sety(originY)
grid_turtle.pendown()
for i in range(0, rows):
grid_turtle.forward(line_length)
grid_turtle.penup()
grid_turtle.back(line_length)
grid_turtle.right(90)
grid_turtle.forward(cell_length)
grid_turtle.left(90)
grid_turtle.pendown()
# Draw columns
grid_turtle.setx(-100)
grid_turtle.sety(100)
grid_turtle.pendown()
grid_turtle.right(90)
for i in range(0, columns):
grid_turtle.forward(line_length)
grid_turtle.penup()
grid_turtle.back(line_length)
grid_turtle.left(90)
grid_turtle.forward(cell_length)
grid_turtle.right(90)
grid_turtle.pendown()
# End of print_grid
def print_my_ships():
for i in your_ships:
ship = turtle.Turtle()
ship.penup()
ship.speed("fastest")
ship.shape("circle")
ship.color("blue")
ship.setx(originX + (i["column"] * cell_length) + ship_offset)
ship.sety(originY - ((i["row"] - 1) * cell_length) + ship_offset)
# End of print_my_ships
# Set up the game
print_grid()
print("Your turn to place your battleships")
for i in range(0, 2):
column = int(input("Enter the column: "))
row = int(input("Enter the row: "))
orientation = input("Enter the orientation. H for horizontal and V for vertical")
your_ships.append({
"column": column,
"row": row,
"orientation": orientation
})
print_my_ships()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment