import socket
import pygame, sys, random
from pygame.locals import *
import time
# Создаем сокет
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', 2000)) # Устанавливаем IP-адрес и порт для прослушивания
server_socket.listen()
print("Сервер запущен и ожидает подключения...")
# Ожидаем подключения клиента
client_socket, client_address = server_socket.accept()
print(f"Подключен клиент: {client_address}")
# Получаем данные от клиента
data = client_socket.recv(1024).decode()
print(f"Получено от клиента: {data}")
# Отправляем ответ клиенту
client_socket.send("Сообщение получено".encode())
pygame.init()
mainClock = pygame.time.Clock()
WINDOWWIDTH = 400
WINDOWHEIGHT = 400
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Hungry Square')
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
foodCounter = 0
NEWFOOD = 40
FOODSIZE = 20
player = pygame.Rect(300, 100, 50, 50)
foods = []
for i in range(20):
foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH - FOODSIZE), random.randint(0, WINDOWHEIGHT - FOODSIZE), FOODSIZE, FOODSIZE))
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 6
foodSend = []
for food in foods:
foodSend.append(food.x)
foodSend.append(food.y)
print(str(foodSend))
client_socket.send(str(foodSend).encode('utf-8'))
abc = 0
for i in range(100):
abc += 1
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == K_a:
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == K_d:
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == K_w:
moveDown = False
moveUp = True
if event.key == K_DOWN or event.key == K_s:
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == K_a:
moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
moveRight = False
if event.key == K_UP or event.key == K_w:
moveUp = False
if event.key == K_DOWN or event.key == K_s:
moveDown = False
if event.key == K_x:
player.top = random.randint(0, WINDOWHEIGHT - player.height)
player.left = random.randint(0, WINDOWWIDTH - player.width)
if event.type == MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0], event.pos[1], FOODSIZE, FOODSIZE))
foodCounter += 1
if foodCounter >= NEWFOOD:
foodCounter = 0
foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH - FOODSIZE), random.randint(0, WINDOWHEIGHT-FOODSIZE), FOODSIZE, FOODSIZE))
windowSurface.fill(WHITE)
if moveDown and player.bottom < WINDOWHEIGHT:
player.top += MOVESPEED
if moveUp and player.top > 0:
player.top -= MOVESPEED
if moveLeft and player.left > 0:
player.left -= MOVESPEED
if moveRight and player.right < WINDOWWIDTH:
player.right += MOVESPEED
pygame.draw.rect(windowSurface, BLACK, player)
for food in foods[:]:
if player.colliderect(food):
foods.remove(food)
if(abc >=100):
getData = client_socket.recv(1024).decode('utf-8').split(' ')
a = 0
if(getData != ''):
for i in range (int(len(getData) / 2)):
for food in foods[:]:
if food.x == getData[i] and food.y == getData[i+1]:
foods.remove(food)
for i in range(len(foods)):
pygame.draw.rect(windowSurface, GREEN, foods[i])
pygame.display.update()
foodSend = []
for food in foods:
foodSend.append(food.x)
foodSend.append(food.y)
client_socket.send(str(foodSend).encode('utf-8'))
time.sleep(0.2)
mainClock.tick(40)
# Закрываем соединение
client_socket.close()
server_socket.close()
time.sleep(3)
pygame.quit()
sys.exit()
import socket
import pygame, sys, random
from pygame.locals import *
import time
pygame.init()
mainClock = pygame.time.Clock()
# Создаем сокет
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('xxx.xxx.xxx.xxx', 2000)) # Замените IP-адрес на IP вашего сервера
# Отправляем данные серверу
message = "Привет, сервер!"
client_socket.send(message.encode())
# Получаем ответ от сервера
response = client_socket.recv(1024).decode()
print(f"Ответ от сервера: {response}")
WINDOWWIDTH = 400
WINDOWHEIGHT = 400
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Hungry Square')
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
foodCounter = 0
NEWFOOD = 40
FOODSIZE = 20
player = pygame.Rect(300, 100, 50, 50)
foods = []
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 6
response = client_socket.recv(4096).decode('utf-8')
foodSend = (response[1:-1]).split(", ")
a = 0
for i in range(20):
foods.append(pygame.Rect(int(foodSend[a]), int(foodSend[a+1]), FOODSIZE, FOODSIZE))
a +=2
sendTo = ''
client_socket.send(sendTo.encode('utf-8'))
for i in range(100):
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == K_a:
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == K_d:
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == K_w:
moveDown = False
moveUp = True
if event.key == K_DOWN or event.key == K_s:
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == K_a:
moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
moveRight = False
if event.key == K_UP or event.key == K_w:
moveUp = False
if event.key == K_DOWN or event.key == K_s:
moveDown = False
if event.key == K_x:
player.top = random.randint(0, WINDOWHEIGHT - player.height)
player.left = random.randint(0, WINDOWWIDTH - player.width)
if event.type == MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0], event.pos[1], FOODSIZE, FOODSIZE))
windowSurface.fill(WHITE)
if moveDown and player.bottom < WINDOWHEIGHT:
player.top += MOVESPEED
if moveUp and player.top > 0:
player.top -= MOVESPEED
if moveLeft and player.left > 0:
player.left -= MOVESPEED
if moveRight and player.right < WINDOWWIDTH:
player.right += MOVESPEED
pygame.draw.rect(windowSurface, BLACK, player)
sendTo = ''
for food in foods[:]:
if player.colliderect(food):
sendTo += str(food.x)
sendTo += ' '
sendTo += str(food.y)
sendTo += ' '
client_socket.send(sendTo.encode('utf-8'))
response = 0
response = client_socket.recv(8192).decode('utf-8')
foodSend = (response[1:-1]).split(", ")
a = 0
foods = []
for i in range(int(len(foodSend)/ 2)):
foods.append(pygame.Rect(int(foodSend[a]), int(foodSend[a+1]), FOODSIZE, FOODSIZE))
a +=2
for i in range(len(foods)):
pygame.draw.rect(windowSurface, GREEN, foods[i])
pygame.display.update()
mainClock.tick(40 )
client_socket.close()
pygame.quit()
sys.exit()
getData = client_socket.recv(1024).decode('utf-8').split(' ')