-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ship.py
94 lines (68 loc) · 4.44 KB
/
Ship.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import pygame, random
class Ship:
def __init__(self,length,screen,cell_width,window_border_thickness,ship_file_path): # Constructor for the "Ship" class
# Ship parameters
self.length = length
self.ship_file_path = ship_file_path
# Other parameters
self.screen = screen
self.cell_width = cell_width
self.width = length*self.cell_width
self.height = self.width/self.length
self.window_border_thickness = window_border_thickness
def draw_player_ships(self,restricted_cells): # Draw all of the friendly (left board) ships with RNG (random number generation)
repeat = True # assume that the current generation will be invalid
while (repeat):
self.origin = [random.randint(0,9),random.randint(0,9)] # generate a random origin
self.orientation = random.getrandbits(1) # 0 = right, 1 = up
if self.origin[0] + self.length > 9: # If the ship can't lay sideways
self.origin[0] = self.origin[0] - self.length + 1
if self.origin[1] + self.length > 9: # If the ship can't lay upright
self.origin[1] = self.origin[1] - self.length + 1
spaces_occupied = []
if self.orientation == 0: # horizontal
for num in range(self.length):
spaces_occupied.append([self.origin[0]+num,self.origin[1]])
elif self.orientation == 1: # vertical
for num in range(self.length):
spaces_occupied.append([self.origin[0],self.origin[1]+num])
for cell in spaces_occupied:
if cell in restricted_cells: # if the cell is in the restricted list, restart while loop
repeat = True
break
else: # otherwise, move on, but iterate through all cells in this for loop to ensure no repeats
repeat = False
ship_img = pygame.image.load(self.ship_file_path) # load the ship image from a local file
ship_img = pygame.transform.scale(ship_img,(self.width,self.height)) # scale the image to fit the game board
if self.orientation == 0: # horizontal
imagerect = self.origin[0]*self.cell_width+self.window_border_thickness,self.origin[1]*self.cell_width+self.window_border_thickness,self.width,self.height
self.screen.blit(ship_img, imagerect)
elif self.orientation == 1: # vertical
imagerect = self.origin[0]*self.cell_width+self.window_border_thickness,self.origin[1]*self.cell_width+self.window_border_thickness,self.height,self.width
ship_img = pygame.transform.rotate(ship_img,90) # rotate the image to be vertical
self.screen.blit(ship_img, imagerect)
self.screen.blit(ship_img, imagerect)
return spaces_occupied # return the spaces [x,y] that the ship that was drawn takes up
def generate_opponent_ship_locations(self,restricted_cells): # Generate random ship locations for the enemy (right board)
repeat = True # assume that the current generation will be invalid
while (repeat):
self.origin = [random.randint(0,9),random.randint(0,9)] # generate a random origin
self.orientation = random.getrandbits(1) # 0 = right, 1 = up
if self.origin[0] + self.length > 9: # If the ship can't lay sideways
self.origin[0] = self.origin[0] - self.length + 1
if self.origin[1] + self.length > 9: # If the ship can't lay upright
self.origin[1] = self.origin[1] - self.length + 1
spaces_occupied = []
if self.orientation == 0: # horizontal
for num in range(self.length):
spaces_occupied.append([self.origin[0]+num,self.origin[1]])
elif self.orientation == 1: # vertical
for num in range(self.length):
spaces_occupied.append([self.origin[0],self.origin[1]+num])
for cell in spaces_occupied:
if cell in restricted_cells: # if the cell is in the restricted list, restart while loop
repeat = True
break
else: # other wise, move on, but iterate through all cells in this for loop to ensure no respeats
repeat = False
return spaces_occupied # return the spaces [x,y] that the current ship takes up