-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
139 lines (106 loc) · 3.73 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import single_player
import pygame
import colors
from single_player import size
size = 20
margin = 3
end = False
#clock = pygame.time.Clock
screen = single_player.screen
player1 = single_player.player.Player(10, 5)
sea1 = player1.sea
computer = single_player.player.Player(10, 5)
sea2 = computer.sea
grid1 = [[0 for i in range(10)] for i in range(10)]
grid2 = [[0 for i in range(10)] for i in range(10)]
def draw_board():
screen.fill(colors.white)
for row in range(10):
for column in range(10):
pygame.draw.rect(screen,
colors.colors[grid1[row][column]],
[(margin+size)*column+margin,
(margin+size)*row+margin,
size,
size])
for row in range(10):
for column in range(10):
pygame.draw.rect(screen,
colors.colors[grid2[row][column]],
[(margin+size)*(column+13)+margin,
(margin+size)*row+margin,
size,
size])
def draw_put_ships_board(width, height):
color = colors.blue
draw_board()
pos = pygame.mouse.get_pos()
pygame.draw.rect(screen,
colors.gray,
[pos[0] - size/2,
pos[1] - size/2,
width*(size+margin),
height*(size+margin)])
pygame.display.flip()
def put_your_ships(player1):
ships = [2, 3, 3, 4, 5]
direction = 0
while ships != []:
ship_size = [ships[-1], 1]
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
direction = 1 - direction
draw_put_ships_board(ship_size[direction], ship_size[1-direction])
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if single_player.put_one_ship(player1, pos, ships[-1], direction, grid1):
ships.pop()
def win_window(winner):
dimension = [640, 465]
screen = pygame.display.set_mode(dimension)
pygame.display.set_caption("Winner")
if winner == player1:
print("You won!")
image = pygame.image.load(".\images\you-win.jpg")
screen.blit(image, (0,0))
pygame.display.flip()
else:
print("You lost!")
image = pygame.image.load(".\images\you_lost.jpg")
screen.blit(image, (30,100))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
turn = True
put_your_ships(player1)
single_player.computer_put_his_ships(computer)
while True:
draw_board()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
while turn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
turn = single_player.player_make_move(pos, sea2, grid2, grid1)
draw_board()
if computer.check_ships():
win_window(player1)
while not turn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
turn = single_player.computer_make_move(player1, sea1, grid1, grid2)
draw_board()
if player1.check_ships():
win_window(computer)
#clock.tick(20)
pygame.quit()