2025-07-26 14:49:49 +02:00
|
|
|
import pygame
|
|
|
|
|
from pygame import Rect
|
2025-07-27 16:02:25 +02:00
|
|
|
from ship import *
|
2025-07-27 20:10:59 +02:00
|
|
|
import random
|
2025-07-27 16:02:25 +02:00
|
|
|
|
2025-07-26 14:13:55 +02:00
|
|
|
class Player():
|
2025-07-28 09:20:41 +02:00
|
|
|
def __init__(self, board, ships, player_name):
|
2025-07-26 14:13:55 +02:00
|
|
|
self.board = board
|
2025-07-28 09:20:41 +02:00
|
|
|
self.name = player_name
|
2025-07-26 20:41:16 +02:00
|
|
|
self.shot_positions = []
|
|
|
|
|
for i in range(0, len(board)):
|
|
|
|
|
self.shot_positions.append([])
|
2025-07-27 16:02:25 +02:00
|
|
|
for _ in range(0, len(board[0])):
|
2025-07-26 20:41:16 +02:00
|
|
|
self.shot_positions[i].append(0)
|
2025-07-27 16:02:25 +02:00
|
|
|
|
|
|
|
|
self.ships = []
|
|
|
|
|
for ship in ships: #[(start, end), (start, end)
|
|
|
|
|
self.ships.append(Ship(ship[0], ship[1]))
|
2025-07-26 14:13:55 +02:00
|
|
|
|
|
|
|
|
def get_hit(self, position):
|
2025-07-27 16:02:25 +02:00
|
|
|
self.board[ord(position[0])-97][int(position[1])] = 0b11
|
2025-07-26 14:13:55 +02:00
|
|
|
|
2025-07-27 16:02:25 +02:00
|
|
|
def shoot(self, position, enemy):
|
|
|
|
|
print("p", position)
|
|
|
|
|
|
|
|
|
|
if enemy.board[ord(position[0])-97][int(position[1])] == 0b00:
|
|
|
|
|
print("No ship at this position")
|
2025-07-27 20:10:59 +02:00
|
|
|
self.shot_positions[ord(position[0])-97][int(position[1])] = 0b01
|
2025-07-27 16:02:25 +02:00
|
|
|
else:
|
|
|
|
|
enemy.get_hit(position)
|
|
|
|
|
self.shot_positions[ord(position[0])-97][int(position[1])] = 0b11
|
2025-07-26 20:41:16 +02:00
|
|
|
|
|
|
|
|
def __check_valid_input(self, position):
|
|
|
|
|
if len(position) != 2 and position != None:
|
|
|
|
|
return False
|
|
|
|
|
|
2025-07-27 16:02:25 +02:00
|
|
|
if self.shot_positions[ord(position[0])-97][int(position[1])] == 0b11:
|
|
|
|
|
print("You already shot at this position!")
|
|
|
|
|
return False
|
2025-07-26 20:41:16 +02:00
|
|
|
|
2025-07-27 20:10:59 +02:00
|
|
|
if ord(position[0]) >= 97 and ord(position[0]) <= 107 and int(position[1]) <= 9 and int(position[1]) >= 0:
|
|
|
|
|
return True
|
|
|
|
|
|
2025-07-27 16:02:25 +02:00
|
|
|
return False
|
2025-07-26 20:41:16 +02:00
|
|
|
|
2025-07-27 16:02:25 +02:00
|
|
|
def update(self, enemy):
|
2025-07-26 20:41:16 +02:00
|
|
|
inp = input("Please input the position you want to fire.")
|
2025-07-27 20:10:59 +02:00
|
|
|
check = self.__check_valid_input(inp)
|
|
|
|
|
|
|
|
|
|
while check == False:
|
|
|
|
|
inp = input("Please input the position you want to fire. ")
|
|
|
|
|
check = self.__check_valid_input(inp)
|
2025-07-27 16:02:25 +02:00
|
|
|
|
|
|
|
|
self.shoot(inp, enemy)
|
2025-07-26 14:49:49 +02:00
|
|
|
|
2025-07-28 09:20:41 +02:00
|
|
|
def draw(self, screen, enemy):
|
2025-07-26 14:49:49 +02:00
|
|
|
start_pos_x = 125
|
|
|
|
|
start_pos_y = 30
|
2025-07-26 20:41:16 +02:00
|
|
|
width = 80
|
|
|
|
|
height = 50
|
|
|
|
|
|
|
|
|
|
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.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)
|
|
|
|
|
|
2025-07-28 09:20:41 +02:00
|
|
|
if enemy.shot_positions[i][j] == 0b01:
|
|
|
|
|
pygame.draw.circle(screen, (0,255,0), (start_pos_x+width//2, start_pos_y + height // 2), 20)
|
|
|
|
|
|
2025-07-26 20:41:16 +02:00
|
|
|
start_pos_x += width - 5
|
2025-07-26 14:49:49 +02:00
|
|
|
|
2025-07-26 20:41:16 +02:00
|
|
|
start_pos_y += height - 5
|
2025-07-26 14:49:49 +02:00
|
|
|
start_pos_x = 125
|
2025-07-26 14:13:55 +02:00
|
|
|
|
2025-07-27 20:10:59 +02:00
|
|
|
|
|
|
|
|
def random_shoot(self, enemy):
|
2025-07-28 09:20:41 +02:00
|
|
|
position = chr(random.randint(97, 106))
|
2025-07-27 20:10:59 +02:00
|
|
|
position += str(random.randint(0,9))
|
2025-07-28 09:20:41 +02:00
|
|
|
|
|
|
|
|
check = self.__check_valid_input(position)
|
|
|
|
|
|
|
|
|
|
while check == False:
|
|
|
|
|
print("rand check loop")
|
|
|
|
|
position = chr(random.randint(97, 106))
|
|
|
|
|
position += str(random.randint(0,9))
|
|
|
|
|
check = self.__check_valid_input(position)
|
|
|
|
|
|
|
|
|
|
|
2025-07-27 20:10:59 +02:00
|
|
|
print("rand pos: ", position)
|
|
|
|
|
|
|
|
|
|
self.shoot(position, enemy)
|