drawing player boards, ship placement and shot positions

This commit is contained in:
2025-07-26 20:41:16 +02:00
parent 07374b4aa1
commit c8751e39ba
3 changed files with 81 additions and 27 deletions

View File

@@ -1,2 +1,3 @@
SCREEN_WIDTH = 1000 SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800 SCREEN_HEIGHT = 1000
BOARD_SIZE = 10

41
main.py
View File

@@ -13,9 +13,9 @@ def main():
sys.exit(1) sys.exit(1)
print("Please input your list of positions, from smallest ship to the largest, for your ships.") print("Please input your list of positions, from smallest ship to the largest, for your ships.")
print("Format: a[A]1-a[A]5 b[B]2-b[B]3. [A] means it can either be upper or lower case.") print("Format: a[A]1-a[A]5 b[B]2-b[B]3. [A] means it can either be upper or lower case. Please input the positions from lowest to highest.")
print("You need to place five ships with sizes: 5, 4, 3, 3, 2. Otherwise the game won't start") print("You must have 5 ships. The ships can be of any size between 1 and 5.")
print("Have fun") print("Have fun.")
good_inp = False good_inp = False
player_board = [] player_board = []
@@ -28,9 +28,8 @@ def main():
player = Player(player_board) player = Player(player_board)
enemy = Player(enemy_board) enemy = Player(enemy_board)
for i in range(BOARD_SIZE):
for i in range(10): for j in range(BOARD_SIZE):
for j in range(10):
print(f"{player.board[i][j]} ", end="") print(f"{player.board[i][j]} ", end="")
print() print()
@@ -38,6 +37,7 @@ def main():
time = pygame.time.Clock() time = pygame.time.Clock()
dt = 0 dt = 0
first_iteration = True
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
@@ -47,13 +47,15 @@ def main():
pygame.Surface.fill(screen, (255,255,255)) pygame.Surface.fill(screen, (255,255,255))
if first_iteration == False:
player.update(enemy_board)
else:
first_iteration = False
player.draw(screen) player.draw(screen)
pygame.display.flip() #refresh screen pygame.display.flip() #refresh screen
r = input("add input")
print(r)
dt = time.tick(60) / 1000 #converted to ms dt = time.tick(60) / 1000 #converted to ms
@@ -61,16 +63,19 @@ def check_valid_input(inp):
s = inp.split(' ') s = inp.split(' ')
print("s", s) print("s", s)
board = [] board = []
for i in range(10): for i in range(BOARD_SIZE):
board.append([]) board.append([])
for _ in range(10): for _ in range(BOARD_SIZE):
board[i].append(0) board[i].append(0)
if len(s) != 5: if len(s) != 5:
print("Not valid input. Please input again") print("Not valid input. Please input again")
return (False, board) return (False, board)
else: else:
count = 0
for position in s: for position in s:
count += 1
print("c", count)
position = position.lower() position = position.lower()
t = position.split("-") t = position.split("-")
print("t", t) print("t", t)
@@ -82,21 +87,21 @@ def check_valid_input(inp):
else: else:
step = 1 step = 1
print(int(t[0][1]), int(t[1][1]))
for column in range(int(t[0][1]), int(t[1][1])+1, step): for column in range(int(t[0][1]), int(t[1][1])+1, step):
if board[ord(t[0][0])-97][column] == 0b01: if board[ord(t[0][0])-97][column] == 0b01:
print("this position is already taken!") print("this position is already taken!")
return (False, board) return (False, board)
print("in")
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) for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
print(f"{board[i][j]} ", end="")
print()
else: else:
print("e1") print("e1")
return (False, board) return (False, board)
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:
print("in2")
if ord(t[0][0]) >= 97 and ord(t[0][0]) <= 107 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 107 and t[0][0] != t[1][0]: if ord(t[0][0]) >= 97 and ord(t[0][0]) <= 107 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 107 and t[0][0] != t[1][0]:
if ord(t[0][0]) > ord(t[1][0]): if ord(t[0][0]) > ord(t[1][0]):
step = -1 step = -1
@@ -108,10 +113,12 @@ def check_valid_input(inp):
print("this position is already taken!") print("this position is already taken!")
return (False, board) return (False, board)
print("in3")
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) for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
print(f"{board[i][j]} ", end="")
print()
else: else:
print("e2") print("e2")
return (False, board) return (False, board)

View File

@@ -4,25 +4,71 @@ class Player():
def __init__(self, board, ships = 5): def __init__(self, board, ships = 5):
self.board = board self.board = board
self.ships = ships #alive ships self.ships = ships #alive ships
self.shot_positions = [] #just add the shot positions here individually self.shot_positions = []
for i in range(0, len(board)):
self.shot_positions.append([])
for j in range(0, len(board[0])):
self.shot_positions[i].append(0)
#self.shot_positions[i][j] = 0b01
def get_hit(self, position): def get_hit(self, position):
pass pass
def shoot(self, position): def shoot(self, position):
pass pass
def update(self): def __check_valid_input(self, position):
pass if len(position) != 2 and position != None:
return False
if ord(position[0]) >= 97 and ord(position[0]) <= 107 and int(position[1]) <= 9 and int(position[1]) >= 0:
return True
return False
def update(self, enemy_board):
inp = input("Please input the position you want to fire.")
while self.__check_valid_input(inp) == False:
inp = input("Please input the position you want to fire.")
self.shoot(inp)
def draw(self, screen): def draw(self, screen):
start_pos_x = 125 start_pos_x = 125
start_pos_y = 30 start_pos_y = 30
width = 80
height = 50
for i in range(len(self.board)): for i in range(0, len(self.board)):
for j in range(len(self.board[0])): for j in range(0, len(self.board[0])):
pygame.draw.rect(screen, (0,0,0), Rect(start_pos_x, start_pos_y, 80, 80), 5) pygame.draw.rect(screen, (0,0,0), Rect(start_pos_x, start_pos_y, width, height), 5)
start_pos_x += 75
start_pos_y += 75 if self.shot_positions[i][j] == 0b01:
pygame.draw.circle(screen, (0,255,0), (start_pos_x+width//2, start_pos_y + height // 2), 20)
elif self.shot_positions[i][j] == 0b11:
pygame.draw.circle(screen, (255,0,0), (start_pos_x+width//2, start_pos_y + height // 2), 20)
start_pos_x += width - 5
start_pos_y += height - 5
start_pos_x = 125
start_pos_y += 50
#player ship board
for i in range(0, len(self.board)):
for j in range(0, len(self.board[0])):
pygame.draw.rect(screen, (0,0,0), Rect(start_pos_x, start_pos_y, width, height), 5)
if self.board[i][j] == 0b01:
pygame.draw.circle(screen, (255,255,0), (start_pos_x+width//2, start_pos_y + height // 2), 20)
elif self.board[i][j] == 0b11:
pygame.draw.circle(screen, (255,0,0), (start_pos_x+width//2, start_pos_y + height // 2), 20)
start_pos_x += width - 5
start_pos_y += height - 5
start_pos_x = 125 start_pos_x = 125