3
3
3d_Max2020-06-17 19:18:06
Python
3d_Max, 2020-06-17 19:18:06

Help with agar.io parody?

when a character eats a color (66, 254, 71) he should reduce but no

import random
import socket
from threading import Thread

colors_cells = [
    (80, 252, 54), (36, 244, 255), (243, 31, 46),
    (4, 39, 243), (254, 6, 178), (255, 211, 7),
    (216, 6, 254), (145, 255, 7), (7, 255, 182), (255,6, 86),(147, 7, 255),(66, 254, 71)
]

dots = {j: {'x': random.randint(20, 1980), 'y': random.randint(20, 1980),
            'color': colors_cells[random.randint(0, len(colors_cells)-1)]} for j in range(2000)}
all_users = {}

user = {}

def upd_eaten(dots, all_users):
    eaten_dots_ids = []
    for i, dot in dots.items():
        for name, user in all_users.items():
            if ((dot['x'] - user['x']) ** 2 + (dot['y'] - user['y']) ** 2) ** 0.5 <= user['mass'] / 2:
                if dot == colors_cells[-1]:
                    all_users[name]['mass'] -= 0.5
                    eaten_dots_ids.append(i)
                else:
                    all_users[name]['mass'] += 1
                    eaten_dots_ids.append(i)
                    return eaten_dots_ids
    for dot in eaten_dots_ids:
        del dots[dot]
    return eaten_dots_ids

def on_new_client(clientsocket, addr):
    while True:
        msg = clientsocket.recv(1024)
        print(addr, ' >> ', msg)
        if msg == b'close':
            break
        if msg == b'spawn':
            msg = bytes(str(dots), encoding='UTF-8')
            clientsocket.sendall(msg)
            print('sent dots')
        else:
            user = eval(msg.decode('UTF-8'))
            all_users[user['name']] = user
            eaten_dots_ids = upd_eaten(dots, all_users)
            resp = {'user': all_users[user['name']], 'eaten_dots_ids': eaten_dots_ids}
            msg = bytes(str(resp), encoding='UTF-8')
            clientsocket.send(msg)
    clientsocket.close()


host = 'localhost'
port = 34325

SERVER_ADDRESS = (host, port)
s = socket.socket()
s.bind(SERVER_ADDRESS)
s.listen(10)
print('Server started!')
print('Waiting for clients...')
try:
    while True:
        c, addr = s.accept()
        print ('Got connection from', addr)
        thread = Thread(target=on_new_client, args=(c, addr))
        thread.start()
except KeyboardInterrupt:
    s.close()
    print('server closed')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-06-17
@3d_Max

if dot == colors_cells[-1]:
    all_users[name]['mass'] -= 0.5

I understand that this line is responsible for this? You are comparing two different data types - dict and tuple with each other. Lead to one

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question