Readme update 2
This commit is contained in:
13
README.md
13
README.md
@@ -1,13 +1,18 @@
|
|||||||
|
# Battleships
|
||||||
|
|
||||||
This is the classic battleships game, using the terminal and pygame in python.
|
This is the classic battleships game, using the terminal and pygame in python.
|
||||||
You use the terminal for inputting the positions, for either firing or creating the layout for your fleet.
|
You use the terminal for inputting the positions, for either firing or creating the layout for your fleet.
|
||||||
|
|
||||||
The project was made using the latest available python currently (Python 3.13.5). Have not tested on other versions.
|
The project was made using the latest available python currently (Python 3.13.5). Have not tested on other versions.
|
||||||
It uses the uv package manager.
|
It uses the uv package manager.
|
||||||
|
|
||||||
To run the game:
|

|
||||||
1. Clone the repository, or download it as a zip file. Then unzip it.
|
|
||||||
2. Then run the command uv run main.py in the terminal. You should see a window pop up, and the terminal asking for you to input the position for your fleet, with instructions.
|
|
||||||
|
|
||||||
This game was made for the boot.dev 2025 summer hackathon. This is my first major project that I have written without any kind of tutorial. I am happy with what I have achieved,
|
## To run the game:
|
||||||
|
1. Clone the repository with `git clone https://github.com/htamas1210/battleships.git`, or download it as a zip file, by clicking on the green Download button, then clicking Download Zip. Then unzip it.
|
||||||
|
2. In the folder that you dowloaded, open a terminal window. Then run the command `uv run main.py` in the terminal, this will run the game. You should see a window pop up, and the terminal asking for you to input the position for your fleet, with instructions.
|
||||||
|
3. If you are done with the game. In the terminal window press Ctrl+C to exit early.
|
||||||
|
|
||||||
|
This game was made for the boot.dev 2025 summer hackathon. This is my first major project that I have written without any kind of tutorial (or ai). I am happy with what I have achieved,
|
||||||
but not satisfied! I know the code is a mess and there are a lot of things I could have done better. I will fix them later, or just rewrite the whole thing to be better.
|
but not satisfied! I know the code is a mess and there are a lot of things I could have done better. I will fix them later, or just rewrite the whole thing to be better.
|
||||||
Anyways, this was really fun to make, and to those who try out this really basic and simple game, I hope you enjoy it for what it is, and thank you for trying it out even if you do not vote for it.
|
Anyways, this was really fun to make, and to those who try out this really basic and simple game, I hope you enjoy it for what it is, and thank you for trying it out even if you do not vote for it.
|
||||||
|
|||||||
18
main.py
18
main.py
@@ -38,7 +38,7 @@ def main():
|
|||||||
player = Player(player_board, ship_pos.copy(), "Player")
|
player = Player(player_board, ship_pos.copy(), "Player")
|
||||||
enemy = Player(enemy_board, ship_pos.copy(), "Enemy")
|
enemy = Player(enemy_board, ship_pos.copy(), "Enemy")
|
||||||
|
|
||||||
print_board(player_board)
|
#print_board(player_board)
|
||||||
|
|
||||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||||
|
|
||||||
@@ -93,7 +93,7 @@ def check_win(player, enemy):
|
|||||||
def check_valid_input(inp):
|
def check_valid_input(inp):
|
||||||
ship_pos = []
|
ship_pos = []
|
||||||
s = inp.split(' ')
|
s = inp.split(' ')
|
||||||
print("s", s)
|
#print("s", s)
|
||||||
board = []
|
board = []
|
||||||
for i in range(BOARD_SIZE):
|
for i in range(BOARD_SIZE):
|
||||||
board.append([])
|
board.append([])
|
||||||
@@ -107,10 +107,10 @@ def check_valid_input(inp):
|
|||||||
count = 0
|
count = 0
|
||||||
for position in s:
|
for position in s:
|
||||||
count += 1
|
count += 1
|
||||||
print("c", count)
|
#print("c", count)
|
||||||
position = position.lower()
|
position = position.lower()
|
||||||
t = position.split("-")
|
t = position.split("-")
|
||||||
print("t", t)
|
#print("t", t)
|
||||||
ship_pos.append((t[0], t[1]))
|
ship_pos.append((t[0], t[1]))
|
||||||
|
|
||||||
if t[0][0] == t[1][0] and ord(t[0][0]) >= 97 and ord(t[0][0]) <= 106 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 106: #check if the fist chars match
|
if t[0][0] == t[1][0] and ord(t[0][0]) >= 97 and ord(t[0][0]) <= 106 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 106: #check if the fist chars match
|
||||||
@@ -126,9 +126,9 @@ def check_valid_input(inp):
|
|||||||
return (False, board, ship_pos)
|
return (False, board, ship_pos)
|
||||||
|
|
||||||
board[ord(t[0][0])-97][column] = 0b01 #first means if it is hit, second means placement
|
board[ord(t[0][0])-97][column] = 0b01 #first means if it is hit, second means placement
|
||||||
print_board(board)
|
#print_board(board)
|
||||||
else:
|
else:
|
||||||
print("e1")
|
#print("e1")
|
||||||
return (False, board, ship_pos)
|
return (False, board, ship_pos)
|
||||||
elif t[0][1] == t[1][1] and int(t[0][1]) <= 9 and int(t[1][1]) >= 0 and int(t[1][1]) <= 9 and abs(int(t[0][1]) - int(t[1][1]))+1 < 6:
|
elif t[0][1] == t[1][1] and int(t[0][1]) <= 9 and int(t[1][1]) >= 0 and int(t[1][1]) <= 9 and abs(int(t[0][1]) - int(t[1][1]))+1 < 6:
|
||||||
if ord(t[0][0]) >= 97 and ord(t[0][0]) <= 106 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 106 and t[0][0] != t[1][0]:
|
if ord(t[0][0]) >= 97 and ord(t[0][0]) <= 106 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 106 and t[0][0] != t[1][0]:
|
||||||
@@ -143,12 +143,12 @@ def check_valid_input(inp):
|
|||||||
return (False, board, ship_pos)
|
return (False, board, ship_pos)
|
||||||
|
|
||||||
board[row][int(t[0][1])] = 0b01 #first means if it is hit, second means placement
|
board[row][int(t[0][1])] = 0b01 #first means if it is hit, second means placement
|
||||||
print_board(board)
|
#print_board(board)
|
||||||
else:
|
else:
|
||||||
print("e2")
|
#print("e2")
|
||||||
return (False, board, ship_pos)
|
return (False, board, ship_pos)
|
||||||
|
|
||||||
print("e3")
|
#print("e3")
|
||||||
return (True, board, ship_pos)
|
return (True, board, ship_pos)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class Player():
|
|||||||
self.board[ord(position[0])-97][int(position[1])] = 0b11
|
self.board[ord(position[0])-97][int(position[1])] = 0b11
|
||||||
|
|
||||||
def shoot(self, position, enemy):
|
def shoot(self, position, enemy):
|
||||||
print("p", position)
|
#print("p", position)
|
||||||
|
|
||||||
if enemy.board[ord(position[0])-97][int(position[1])] == 0b00:
|
if enemy.board[ord(position[0])-97][int(position[1])] == 0b00:
|
||||||
print("No ship at this position")
|
print("No ship at this position")
|
||||||
@@ -107,6 +107,6 @@ class Player():
|
|||||||
check = self.__check_valid_input(position)
|
check = self.__check_valid_input(position)
|
||||||
|
|
||||||
|
|
||||||
print("rand pos: ", position)
|
print("enemy shoots at: ", position)
|
||||||
|
|
||||||
self.shoot(position, enemy)
|
self.shoot(position, enemy)
|
||||||
|
|||||||
BIN
screenshot/preview.png
Normal file
BIN
screenshot/preview.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 76 KiB |
Reference in New Issue
Block a user