-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (51 loc) · 2.26 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
import pygame
import numpy as np
from InitDraw import screen, grid, CellWidth, CellHight, Margin, myfont, DrawStartingBoard
from MovingOpponet import RandomMovment
from WinIdint import checkwin
won = False
def mainGame():
global grid
global won
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
scale = 3
grid = np.empty(shape=(scale, scale))
won = False
DrawStartingBoard(screen)
elif event.type == pygame.MOUSEBUTTONDOWN:
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = int(pos[0] // (CellWidth + Margin))
row = int(pos[1] // (CellHight + Margin))
if grid[row][column] == 0 and won == False:
# Set that location to one
#pygame.draw.rect(screen, (255,0,0),[(((Margin + CellWidth) * column) + Margin),(Margin + CellHight) * row + Margin, CellWidth, CellHight])
grid[row][column] = 1
RandomMovment(grid)
if(checkwin(grid) != False):
print(checkwin(grid))
won = True
EndGame(checkwin(grid))
elif np.count_nonzero(grid)==9:
EndGame(0)
for column in range(len(grid)):
for row in range(len(grid[column])):
if grid[row,column] == 1:
pygame.draw.rect(screen, (0,0,255),[(((Margin + CellWidth) * column) + Margin),(Margin + CellHight) * row + Margin, CellWidth, CellHight])
if grid[row,column] == 2:
pygame.draw.rect(screen, (255,0,0),[(((Margin + CellWidth) * column) + Margin),(Margin + CellHight) * row + Margin, CellWidth, CellHight])
pygame.display.flip()
def EndGame(winner):
pygame.draw.rect(screen,(255,255,255),pygame.Rect(0, 600, 600, 100))
Players = ["CAT ","You ", "The Computer " ]
message = str(Players[int(winner)]+"won the game!")
textsurface = myfont.render(message, False, (0, 0, 0))
screen.blit(textsurface,(20,600))
textsurface = myfont.render("Press 'Space' To Play Again", False, (0, 0, 0))
screen.blit(textsurface,(20,650))
mainGame()