-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_master.py
102 lines (88 loc) · 3.72 KB
/
game_master.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
import random
import itertools
import pygame
from pygame import Vector2
from ai import Ai
from user import User
from board import Board
class GameMaster(Ai, User, Board):
def __init__(self, x, y, pixels, border, main_window) -> None:
super().__init__(x, y, pixels, border, main_window)
def user_shoot(self):
x_border, y_border = (self.border*self.pixels) / \
2, (self.border*self.pixels)/4
mouse_position_x, mouse_position_y = pygame.mouse.get_pos()
x_block = (mouse_position_x - x_border) // self.pixels
y_block = (mouse_position_y - y_border) // self.pixels
vector = Vector2(round(x_block, 0), round(y_block, 0))
print(x_block, y_block, vector)
ai_ships = list(itertools.chain.from_iterable(self.ai_ships))
if vector in self.user_shots:
return False
else:
self.user_shots.append(vector)
if vector in ai_ships:
self.user_hits.append(vector)
return True
def game_over_check(self):
ai_ships = list(itertools.chain.from_iterable(self.ai_ships))
user_ships = list(itertools.chain.from_iterable(self.user_ships))
if len(ai_ships) == len(self.user_hits):
return True
if len(user_ships) == len(self.ai_hits):
return True
else:
return False
def ai_take_shot(self):
while True:
vector = Vector2(random.randint(0, self.x-1),
random.randint(0, self.y-1))
if vector not in self.ai_shots:
self.ai_shots.append(vector)
if vector in itertools.chain.from_iterable(self.user_ships):
self.ai_hits.append(vector)
print('Hit - One of your ships has been struck.')
else:
print('your opponent takes a shot but misses')
break
def play(self, game_started):
if game_started:
if pygame.mouse.get_pressed()[0]:
mouse_pos = pygame.mouse.get_pos()
if mouse_pos[0] > 486 or mouse_pos[-1] > 482:
pygame.event.wait()
else:
if self.user_shoot():
self.gameplay_draw_top()
if self.game_over_check():
print('GAME OVER - YOU WIN!')
pygame.time.wait(5000)
pygame.quit()
pygame.time.wait(300)
self.ai_take_shot()
self.gameplay_draw_bottom()
if self.game_over_check():
print('GAME OVER AI WINS')
pygame.time.wait(5000)
pygame.quit()
def draw_text(self, message: str, x_coord, y_coord, font_size, colour=(255, 255, 255), on_game_screen=False):
font = pygame.font.SysFont('Comic Sans MS', font_size)
text = font.render(message, True, colour)
text_rect = text.get_rect()
text_rect.x, text_rect.y = x_coord*self.pixels, y_coord*self.pixels
text_rect.center
if on_game_screen:
self.board_window.blit(text, text_rect)
else:
self.main_window.blit(text, text_rect)
def gameplay_draw_top(self):
self.surface_init('Grey')
self.draw(self.user_shots, 'Dark Grey')
self.draw(self.user_hits, 'Green')
self.update()
def gameplay_draw_bottom(self):
self.surface_init('Grey')
self.draw(itertools.chain.from_iterable(self.user_ships), 'Blue')
self.draw(self.ai_shots, 'Dark Grey')
self.draw(self.ai_hits, 'Red')
self.update(False)