shooting
This commit is contained in:
50
main.py
50
main.py
@@ -3,6 +3,14 @@ import sys
|
|||||||
from constants import *
|
from constants import *
|
||||||
from player import *
|
from player import *
|
||||||
|
|
||||||
|
def print_board(board):
|
||||||
|
for i in range(BOARD_SIZE):
|
||||||
|
for j in range(BOARD_SIZE):
|
||||||
|
print(f"{board[i][j]} ", end="")
|
||||||
|
print()
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("Starting BattleShip!")
|
print("Starting BattleShip!")
|
||||||
|
|
||||||
@@ -20,18 +28,16 @@ def main():
|
|||||||
good_inp = False
|
good_inp = False
|
||||||
player_board = []
|
player_board = []
|
||||||
enemy_board = []
|
enemy_board = []
|
||||||
|
|
||||||
while good_inp == False:
|
while good_inp == False:
|
||||||
inp = input()
|
inp = input()
|
||||||
good_inp, player_board = check_valid_input(inp)
|
good_inp, player_board, ship_pos = check_valid_input(inp)
|
||||||
enemy_board = player_board.copy()
|
enemy_board = player_board.copy()
|
||||||
|
|
||||||
player = Player(player_board)
|
player = Player(player_board, ship_pos)
|
||||||
enemy = Player(enemy_board)
|
enemy = Player(enemy_board, ship_pos)
|
||||||
|
|
||||||
for i in range(BOARD_SIZE):
|
print_board(player_board)
|
||||||
for j in range(BOARD_SIZE):
|
|
||||||
print(f"{player.board[i][j]} ", end="")
|
|
||||||
print()
|
|
||||||
|
|
||||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||||
|
|
||||||
@@ -48,7 +54,7 @@ def main():
|
|||||||
pygame.Surface.fill(screen, (255,255,255))
|
pygame.Surface.fill(screen, (255,255,255))
|
||||||
|
|
||||||
if first_iteration == False:
|
if first_iteration == False:
|
||||||
player.update(enemy_board)
|
player.update(enemy)
|
||||||
else:
|
else:
|
||||||
first_iteration = False
|
first_iteration = False
|
||||||
|
|
||||||
@@ -60,6 +66,7 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
def check_valid_input(inp):
|
def check_valid_input(inp):
|
||||||
|
ship_pos = []
|
||||||
s = inp.split(' ')
|
s = inp.split(' ')
|
||||||
print("s", s)
|
print("s", s)
|
||||||
board = []
|
board = []
|
||||||
@@ -70,7 +77,7 @@ def check_valid_input(inp):
|
|||||||
|
|
||||||
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, ship_pos)
|
||||||
else:
|
else:
|
||||||
count = 0
|
count = 0
|
||||||
for position in s:
|
for position in s:
|
||||||
@@ -79,6 +86,7 @@ def check_valid_input(inp):
|
|||||||
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]))
|
||||||
|
|
||||||
if t[0][0] == t[1][0] and ord(t[0][0]) >= 97 and ord(t[0][0]) <= 107 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 107: #check if the fist chars match
|
if t[0][0] == t[1][0] and ord(t[0][0]) >= 97 and ord(t[0][0]) <= 107 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 107: #check if the fist chars match
|
||||||
if int(t[0][1]) >= 0 and int(t[0][1]) <= 9 and int(t[1][1]) >= 0 and int(t[1][1]) <= 9 and int(t[0][1]) != int(t[1][1]) and abs(int(t[0][1]) - int(t[1][1]))+1 < 6: #check if number is in correct range
|
if int(t[0][1]) >= 0 and int(t[0][1]) <= 9 and int(t[1][1]) >= 0 and int(t[1][1]) <= 9 and int(t[0][1]) != int(t[1][1]) and abs(int(t[0][1]) - int(t[1][1]))+1 < 6: #check if number is in correct range
|
||||||
@@ -90,17 +98,13 @@ def check_valid_input(inp):
|
|||||||
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, 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)
|
||||||
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, 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]) <= 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]):
|
||||||
@@ -111,22 +115,16 @@ def check_valid_input(inp):
|
|||||||
for row in range(ord(t[0][0])-97, ord(t[1][0])-97+1, step):
|
for row in range(ord(t[0][0])-97, ord(t[1][0])-97+1, step):
|
||||||
if board[row][int(t[0][1])] == 0b01:
|
if board[row][int(t[0][1])] == 0b01:
|
||||||
print("this position is already taken!")
|
print("this position is already taken!")
|
||||||
return (False, board)
|
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)
|
||||||
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, ship_pos)
|
||||||
|
|
||||||
print("e3")
|
print("e3")
|
||||||
return (True, board)
|
return (True, board, ship_pos)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
36
player.py
36
player.py
@@ -1,21 +1,31 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from pygame import Rect
|
from pygame import Rect
|
||||||
|
from ship import *
|
||||||
|
|
||||||
class Player():
|
class Player():
|
||||||
def __init__(self, board, ships = 5):
|
def __init__(self, board, ships):
|
||||||
self.board = board
|
self.board = board
|
||||||
self.ships = ships #alive ships
|
|
||||||
self.shot_positions = []
|
self.shot_positions = []
|
||||||
for i in range(0, len(board)):
|
for i in range(0, len(board)):
|
||||||
self.shot_positions.append([])
|
self.shot_positions.append([])
|
||||||
for j in range(0, len(board[0])):
|
for _ in range(0, len(board[0])):
|
||||||
self.shot_positions[i].append(0)
|
self.shot_positions[i].append(0)
|
||||||
#self.shot_positions[i][j] = 0b01
|
|
||||||
|
self.ships = []
|
||||||
|
for ship in ships: #[(start, end), (start, end)
|
||||||
|
self.ships.append(Ship(ship[0], ship[1]))
|
||||||
|
|
||||||
def get_hit(self, position):
|
def get_hit(self, position):
|
||||||
pass
|
self.board[ord(position[0])-97][int(position[1])] = 0b11
|
||||||
|
|
||||||
def shoot(self, position):
|
def shoot(self, position, enemy):
|
||||||
pass
|
print("p", position)
|
||||||
|
|
||||||
|
if enemy.board[ord(position[0])-97][int(position[1])] == 0b00:
|
||||||
|
print("No ship at this position")
|
||||||
|
else:
|
||||||
|
enemy.get_hit(position)
|
||||||
|
self.shot_positions[ord(position[0])-97][int(position[1])] = 0b11
|
||||||
|
|
||||||
def __check_valid_input(self, position):
|
def __check_valid_input(self, position):
|
||||||
if len(position) != 2 and position != None:
|
if len(position) != 2 and position != None:
|
||||||
@@ -24,16 +34,18 @@ class Player():
|
|||||||
if ord(position[0]) >= 97 and ord(position[0]) <= 107 and int(position[1]) <= 9 and int(position[1]) >= 0:
|
if ord(position[0]) >= 97 and ord(position[0]) <= 107 and int(position[1]) <= 9 and int(position[1]) >= 0:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
if self.shot_positions[ord(position[0])-97][int(position[1])] == 0b11:
|
||||||
|
print("You already shot at this position!")
|
||||||
|
return False
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def update(self, enemy):
|
||||||
|
|
||||||
def update(self, enemy_board):
|
|
||||||
inp = input("Please input the position you want to fire.")
|
inp = input("Please input the position you want to fire.")
|
||||||
while self.__check_valid_input(inp) == False:
|
while self.__check_valid_input(inp) == False:
|
||||||
inp = input("Please input the position you want to fire.")
|
inp = input("Please input the position you want to fire.")
|
||||||
|
|
||||||
self.shoot(inp)
|
self.shoot(inp, enemy)
|
||||||
|
|
||||||
def draw(self, screen):
|
def draw(self, screen):
|
||||||
start_pos_x = 125
|
start_pos_x = 125
|
||||||
|
|||||||
Reference in New Issue
Block a user