-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (88 loc) · 4.02 KB
/
Copy pathmain.py
File metadata and controls
110 lines (88 loc) · 4.02 KB
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
import pygame
import sys
from human import HumanPlayer
from engine import run
from constans import WAIT_FOR_HUMAN_MOVE, COLS, ROWS, CELL_SIZE
import ai_1
import ai_2
BOT_DEPTH = 4
WIDTH, HEIGHT = COLS * CELL_SIZE, ROWS * CELL_SIZE
BG = (24, 26, 32)
PANEL = (38, 42, 52)
PANEL_HOVER = (58, 64, 80)
ACCENT = (90, 170, 255)
TEXT = (235, 235, 240)
MUTED = (150, 155, 165)
def make_human():
return HumanPlayer(pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT,
pygame.K_SPACE, skip=pygame.K_s, wait_for_turn=WAIT_FOR_HUMAN_MOVE)
def make_bot1(pid):
return ai_1.MinMaxPlayer(my_pid=pid, depth=BOT_DEPTH)
def make_bot2(pid):
return ai_2.MinMaxPlayer(my_pid=pid, depth=BOT_DEPTH)
MODES = [
("Czlowiek vs Predator", "Ty (zielony) kontra Predator (czerwony)",
lambda: (make_human(), make_bot1("B"))),
("Czlowiek vs Potwor", "Ty (zielony) kontra Potwor (niebieski)",
lambda: (make_human(), make_bot2("B"))),
("Predator vs Potwor", "Predator (czerwony) kontra Potwor (niebieski)",
lambda: (make_bot1("A"), make_bot2("B"))),
]
def choose_mode(screen):
title_font = pygame.font.SysFont(None, 72, bold=True)
option_font = pygame.font.SysFont(None, 40, bold=True)
desc_font = pygame.font.SysFont(None, 24)
hint_font = pygame.font.SysFont(None, 22)
clock = pygame.time.Clock()
btn_w, btn_h, gap = 520, 96, 28
total = len(MODES) * btn_h + (len(MODES) - 1) * gap
start_y = (HEIGHT - total) // 2 + 40
rects = [pygame.Rect((WIDTH - btn_w) // 2, start_y + i * (btn_h + gap), btn_w, btn_h)
for i in range(len(MODES))]
while True:
mouse = pygame.mouse.get_pos()
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
if e.type == pygame.KEYDOWN:
if e.key in (pygame.K_1, pygame.K_KP1):
return MODES[0][2]()
if e.key in (pygame.K_2, pygame.K_KP2):
return MODES[1][2]()
if e.key in (pygame.K_3, pygame.K_KP3):
return MODES[2][2]()
if e.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if e.type == pygame.MOUSEBUTTONDOWN and e.button == 1:
for i, rect in enumerate(rects):
if rect.collidepoint(e.pos):
return MODES[i][2]()
screen.fill(BG)
title = title_font.render("BOMBERMAN", True, TEXT)
screen.blit(title, title.get_rect(center=(WIDTH // 2, start_y - 90)))
subtitle = desc_font.render("Wybierz tryb gry", True, MUTED)
screen.blit(subtitle, subtitle.get_rect(center=(WIDTH // 2, start_y - 48)))
for i, (label, desc, _) in enumerate(MODES):
rect = rects[i]
hovered = rect.collidepoint(mouse)
pygame.draw.rect(screen, PANEL_HOVER if hovered else PANEL, rect, border_radius=12)
pygame.draw.rect(screen, ACCENT if hovered else PANEL, rect, width=2, border_radius=12)
num = option_font.render(str(i + 1), True, ACCENT)
screen.blit(num, num.get_rect(midleft=(rect.x + 24, rect.centery)))
name = option_font.render(label, True, TEXT)
screen.blit(name, name.get_rect(midleft=(rect.x + 70, rect.centery - 12)))
info = desc_font.render(desc, True, MUTED)
screen.blit(info, info.get_rect(midleft=(rect.x + 70, rect.centery + 18)))
hint = hint_font.render("Klawisze 1/2/3 lub klik myszka | ESC = wyjscie", True, MUTED)
screen.blit(hint, hint.get_rect(center=(WIDTH // 2, HEIGHT - 32)))
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bomberman 1v1 AI")
while True:
playerA, playerB = choose_mode(screen)
run(playerA, playerB)