-
Notifications
You must be signed in to change notification settings - Fork 0
/
mian.py
101 lines (85 loc) · 3.76 KB
/
mian.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
import sys
from ui import Game
import pygame
# pygame Setup
pygame.init()
def main():
final = False
# a variable that check if we are in the welcome screen or not
welcome = True
# a variable check if we are in the end of a round or not
end_round = False
# init a game class it's showing the ui and controls all the other class (AI and GameBrain)
game = Game()
while True:
if game.brain.running:
if welcome:
# showing welcome screen and ask user to choose the game mode
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_g:
game.brain.game_mode = "ai"
game.draw_lines()
welcome = False
elif event.key == pygame.K_p:
game.brain.game_mode = "pvp"
game.draw_lines()
welcome = False
elif event.key == pygame.K_n:
game.brain.ai.level = 0
game.brain.game_mode = "ai"
game.draw_lines()
welcome = False
if not end_round:
if not welcome:
# clear the screen from welcome text and draw a game board and waiting for
# user to click and fill the board with user choose also check iof game is over or not
for event in pygame.event.get():
if game.brain.is_full():
end_round = True
if game.brain.final_state()[0] != 0:
end_round = True
if event.type == pygame.MOUSEBUTTONDOWN:
game.check_click(event)
pygame.display.update()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if end_round and not final:
# showing the winner or Draw if draw and after a while showing a users current scores
pygame.display.update()
if game.brain.final_state()[0] != 0:
game.draw_winner_status()
pygame.display.update()
game.draw_score()
pygame.display.update()
if not game.brain.check_final():
if game.brain.player == 2:
game.ai_turn()
pygame.display.update()
end_round = False
if game.brain.check_final():
# if any of users reach the 5 score in game there is a winner, and we show the winner
final = True
game.draw_final_score()
for event in pygame.event.get():
# after showing winner asking from user want to play again or quit
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
end_round = False
final = False
welcome = True
game.rest_game()
print(game.brain.running)
elif event.key == pygame.K_q:
pygame.quit()
sys.exit()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
if __name__ == '__main__':
main()