Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- size_block = 100
- margin = 10
- width = heigth = size_block * 3 + margin * 4
- size_window = (width, heigth)
- screen = pygame.display.set_mode(size_window)
- mtr = []
- for i in range(3):
- m = ['a'] * 3
- mtr.append(m)
- #print(mtr)
- #
- def check():
- for i in range(3):
- if mtr[i][0] == mtr[i][1] == mtr[i][2]:
- if mtr[i][0] == 'x':
- return 'x'
- elif mtr[i][0] == '0':
- return '0'
- for j in range(3):
- if mtr[0][j] == mtr[1][j] == mtr[2][j]:
- if mtr[0][j] == 'x':
- return 'x'
- elif mtr[0][j] == '0':
- return '0'
- if mtr[0][0] == mtr[1][1] == mtr[2][2]:
- if mtr[0][0] == 'x':
- return 'x'
- elif mtr[0][0] == '0':
- return '0'
- if mtr[0][2] == mtr[1][1] == mtr[2][0]:
- if mtr[0][2] == 'x':
- return 'x'
- elif mtr[0][2] == '0':
- return '0'
- draw = True
- for i in range(3):
- for j in range(3):
- if mtr[i][j] == 'a':
- draw = False
- if draw:
- return 'draw'
- return 'a'
- cnt = 0
- over = False
- while True:
- for event in pygame.event.get():
- """if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
- mtr = []
- for i in range(3):
- m = ['a'] * 3
- mtr.append(m)
- over = False"""
- if event.type == pygame.QUIT:
- pygame.quit()
- elif over:
- pygame.quit()
- elif event.type == pygame.MOUSEBUTTONDOWN:
- x, y = pygame.mouse.get_pos()
- #print("x y = ", x, y)
- col = x // (size_block + margin)
- row = y // (size_block + margin)
- print("cnt = ", cnt)
- if mtr[col][row] == 'a':
- if cnt == 0:
- mtr[col][row] = 'x'
- else:
- mtr[col][row] = '0'
- #print("col row = ", col, row)
- cnt = (cnt + 1) % 2
- #print(mtr)
- for i in range(3):
- for j in range(3):
- x = i * size_block + (i + 1) * margin
- y = j * size_block + (j + 1) * margin
- color = WHITE
- if mtr[i][j] == '0':
- color = BLUE
- elif mtr[i][j] == 'x':
- color = RED
- pygame.draw.rect(screen, color, (x, y, size_block, size_block))
- if color == RED:
- pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
- pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
- elif color == BLUE:
- pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
- pygame.display.update()
- res = check()
- final = ""
- if res == 'x':
- final = "Cross wins"
- elif res == '0':
- final = "Zero wins"
- elif res == 'draw':
- final = "DRAW"
- if final != "":
- over = True
- screen.fill(BLACK)
- font = pygame.font.SysFont('stxingkai', 80)
- text1 = font.render(final, True, WHITE)
- text_rect = text1.get_rect()
- text_x = screen.get_width() / 2 - text_rect.width / 2
- text_y = screen.get_height() / 2 - text_rect.height / 2
- screen.blit(text1, [text_x, text_y])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement