Advertisement
Korotkodul

client_new

Sep 18th, 2022 (edited)
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. import socket
  2.  
  3. sock = socket.socket()
  4. sock.connect(('192.168.1.171',9995))
  5. #sock.send(b'hello, world!')
  6.  
  7. data = sock.recv(1024)
  8. data = data.decode('utf-8')
  9. print(data)
  10. me=data
  11. enemy = 'x'
  12. if enemy == me:
  13.     enemy = '0'
  14.  
  15. import pygame
  16. import sys
  17.  
  18. pygame.init()
  19.  
  20. RED = (255, 0, 0)
  21. GREEN = (0, 255, 0)
  22. BLUE = (0, 0, 255)
  23. WHITE = (255, 255, 255)
  24. BLACK = (0, 0, 0)
  25.  
  26. size_block = 100
  27. margin = 10
  28. width = heigth = size_block * 3 + margin * 4
  29.  
  30. size_window = (width, heigth)
  31.  
  32. screen = pygame.display.set_mode(size_window)
  33.  
  34. mtr = []
  35. for i in range(3):
  36.     m = ['a'] * 3
  37.     mtr.append(m)
  38.  
  39. cnt_a = 9
  40.  
  41. def check():
  42.     for i in range(3):
  43.         if mtr[i][0] == mtr[i][1] == mtr[i][2]:
  44.             if mtr[i][0] == 'x':
  45.                 return 'x'
  46.             elif mtr[i][0] == '0':
  47.                 return '0'
  48.  
  49.     for j in range(3):
  50.         if mtr[0][j] == mtr[1][j] == mtr[2][j]:
  51.             if mtr[0][j] == 'x':
  52.                 return 'x'
  53.             elif mtr[0][j] == '0':
  54.                 return '0'
  55.  
  56.     if mtr[0][0] == mtr[1][1] == mtr[2][2]:
  57.         if mtr[0][0] == 'x':
  58.             return 'x'
  59.         elif mtr[0][0] == '0':
  60.             return '0'
  61.  
  62.     if mtr[0][2] == mtr[1][1] == mtr[2][0]:
  63.         if mtr[0][2] == 'x':
  64.             return 'x'
  65.         elif mtr[0][2] == '0':
  66.             return '0'
  67.  
  68.     draw = True
  69.     for i in range(3):
  70.         for j in range(3):
  71.             if mtr[i][j] == 'a':
  72.                 draw = False
  73.     if draw:
  74.         return 'draw'
  75.  
  76.     return 'a'
  77.  
  78.  
  79. #clock = pygame.time.Clock()
  80.  
  81. while check() == 'a':
  82.     #clock.tick(120)
  83.  
  84.     for event in pygame.event.get():
  85.         if event.type == pygame.QUIT:
  86.             sys.exit()
  87.  
  88.     for i in range(3):
  89.         for j in range(3):
  90.             x = i * size_block + (i + 1) * margin
  91.             y = j * size_block + (j + 1) * margin
  92.             color = WHITE
  93.             if mtr[i][j] == '0':
  94.                 color = BLUE
  95.             elif mtr[i][j] == 'x':
  96.                 color = RED
  97.             pygame.draw.rect(screen, color, (x, y, size_block, size_block))
  98.             if color == RED:
  99.                 pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
  100.                 pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
  101.             elif color == BLUE:
  102.                 pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
  103.             pygame.display.update()
  104.  
  105.     data = sock.recv(1024)
  106.     #print("DATA = ", data.decode('utf-8'))
  107.     if data == b"move":
  108.         #print("WE MOVE")
  109.         old_cnt_a = cnt_a
  110.         while cnt_a == old_cnt_a:#против перезакрашивания клетки
  111.             x, y = pygame.mouse.get_pos()
  112.             # print("x y = ", x, y)
  113.             col = x // (size_block + margin)
  114.             row = y // (size_block + margin)
  115.             # print("cnt = ", cnt)
  116.  
  117.             if mtr[col][row] == 'a':
  118.                 mtr[col][row] = me
  119.                 cnt_a -= 1
  120.         to_server = (str(col) + ' ' + str(row)).encode('utf-8')
  121.         sock.send(to_server)
  122.     else:
  123.         col, row = data.decode('utf-8').split()
  124.         col = int(col)
  125.         row = int(row)
  126.         #print("col row = ", col, row, type(col), type(row))
  127.         mtr[col][row] = enemy
  128.  
  129.     for i in range(3):
  130.         for j in range(3):
  131.             x = i * size_block + (i + 1) * margin
  132.             y = j * size_block + (j + 1) * margin
  133.             color = WHITE
  134.             if mtr[i][j] == '0':
  135.                 color = BLUE
  136.             elif mtr[i][j] == 'x':
  137.                 color = RED
  138.             pygame.draw.rect(screen, color, (x, y, size_block, size_block))
  139.             if color == RED:
  140.                 pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
  141.                 pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
  142.             elif color == BLUE:
  143.                 pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
  144.             pygame.display.update()
  145.  
  146. print("END A")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement