Advertisement
Korotkodul

client game

Sep 15th, 2022 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. import socket
  2.  
  3. sock = socket.socket()
  4. sock.connect(('10.8.128.183',9991))
  5. #sock.send(b'hello, world!')
  6.  
  7. data = sock.recv(1024)
  8. data = data.decode('utf-8')
  9. me=data
  10.  
  11.  
  12.  
  13. import pygame
  14.  
  15. pygame.init()
  16.  
  17. RED = (255, 0, 0)
  18. GREEN = (0, 255, 0)
  19. BLUE = (0, 0, 255)
  20. WHITE = (255, 255, 255)
  21. BLACK = (0, 0, 0)
  22.  
  23. size_block = 100
  24. margin = 10
  25. width = heigth = size_block * 3 + margin * 4
  26.  
  27. size_window = (width, heigth)
  28.  
  29. screen = pygame.display.set_mode(size_window)
  30.  
  31. mtr = []
  32. for i in range(3):
  33.     m = ['a'] * 3
  34.     mtr.append(m)
  35. #print(mtr)
  36. #
  37.  
  38. def check():
  39.     for i in range(3):
  40.         if mtr[i][0] == mtr[i][1] == mtr[i][2]:
  41.             if mtr[i][0] == 'x':
  42.                 return 'x'
  43.             elif mtr[i][0] == '0':
  44.                 return '0'
  45.  
  46.     for j in range(3):
  47.         if mtr[0][j] == mtr[1][j] == mtr[2][j]:
  48.             if mtr[0][j] == 'x':
  49.                 return 'x'
  50.             elif mtr[0][j] == '0':
  51.                 return '0'
  52.  
  53.     if mtr[0][0] == mtr[1][1] == mtr[2][2]:
  54.         if mtr[0][0] == 'x':
  55.             return 'x'
  56.         elif mtr[0][0] == '0':
  57.             return '0'
  58.  
  59.     if mtr[0][2] == mtr[1][1] == mtr[2][0]:
  60.         if mtr[0][2] == 'x':
  61.             return 'x'
  62.         elif mtr[0][2] == '0':
  63.             return '0'
  64.  
  65.     draw = True
  66.     for i in range(3):
  67.         for j in range(3):
  68.             if mtr[i][j] == 'a':
  69.                 draw = False
  70.     if draw:
  71.         return 'draw'
  72.  
  73.     return 'a'
  74.  
  75.  
  76.  
  77. def play(act, x , y):
  78.     #act = enemy: ход сделан не нами
  79.     #act = me: ход сделан НАМИ
  80.     over = False
  81.     if act == "enemy":
  82.         if me == '0':
  83.             cnt = 0
  84.         else:
  85.             cnt = 1
  86.     else:
  87.         if me == '0':
  88.             cnt = 1
  89.         else:
  90.             cnt = 0
  91.  
  92.     for event in pygame.event.get():
  93.         """if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  94.            mtr = []
  95.            for i in range(3):
  96.                m = ['a'] * 3
  97.                mtr.append(m)
  98.            over = False"""
  99.         if event.type == pygame.QUIT:
  100.             pygame.quit()
  101.         elif over:
  102.             pygame.quit()
  103.         elif act == "me" and event.type == pygame.MOUSEBUTTONDOWN:
  104.             x, y = pygame.mouse.get_pos()#  в проиивном случае x и y заданы!
  105.  
  106.         #print("x y = ", x, y)
  107.         col = x // (size_block + margin)
  108.         row = y // (size_block + margin)
  109.         print("cnt = ", cnt)
  110.  
  111.         if mtr[col][row] == 'a':
  112.             if cnt == 0:
  113.                 mtr[col][row] = 'x'
  114.             else:
  115.                 mtr[col][row] = '0'
  116.         #print("col row = ", col, row)
  117.         #cnt = (cnt + 1) % 2
  118.  
  119.  
  120.         #print(mtr)
  121.  
  122.     for i in range(3):
  123.         for j in range(3):
  124.             x = i * size_block + (i + 1) * margin
  125.             y = j * size_block + (j + 1) * margin
  126.             color = WHITE
  127.             if mtr[i][j] == '0':
  128.                 color = BLUE
  129.             elif mtr[i][j] == 'x':
  130.                 color = RED
  131.             pygame.draw.rect(screen, color, (x, y, size_block, size_block))
  132.             if color == RED:
  133.                 pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
  134.                 pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
  135.             elif color == BLUE:
  136.                 pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
  137.             pygame.display.update()
  138.  
  139.             res = check()
  140.             final = ""
  141.             if res == 'x':
  142.                 final = "Cross wins"
  143.             elif res == '0':
  144.                 final = "Zero wins"
  145.             elif res == 'draw':
  146.                 final = "DRAW"
  147.  
  148.  
  149.             if final != "":
  150.                 over = True
  151.                 screen.fill(BLACK)
  152.                 font = pygame.font.SysFont('stxingkai', 80)
  153.                 text1 = font.render(final, True, WHITE)
  154.                 text_rect = text1.get_rect()
  155.                 text_x = screen.get_width() / 2 - text_rect.width / 2
  156.                 text_y = screen.get_height() / 2 - text_rect.height / 2
  157.                 screen.blit(text1, [text_x, text_y])
  158.  
  159.  
  160.  
  161. for i in range(3):
  162.     for j in range(3):
  163.         x = i * size_block + (i + 1) * margin
  164.         y = j * size_block + (j + 1) * margin
  165.         color = WHITE
  166.         if mtr[i][j] == '0':
  167.             color = BLUE
  168.         elif mtr[i][j] == 'x':
  169.             color = RED
  170.         pygame.draw.rect(screen, color, (x, y, size_block, size_block))
  171.         if color == RED:
  172.             pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
  173.             pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
  174.         elif color == BLUE:
  175.             pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
  176.         pygame.display.update()
  177.  
  178.  
  179.  
  180.  
  181. while True:
  182.     data = sock.recv(1024)
  183.     if not data:
  184.         break
  185.     elif data == b"end":
  186.         break
  187.     x, y = data.decode('utf-8').split()
  188.     x = int(x)
  189.     y = int(y)
  190.     play("enemy", x, y)
  191.     message = bytes(input(), 'utf-8')
  192.     sock.send(message)
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. sock.close()
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement