day24-弹幕
实现多客户弹弹幕 server_barrage.py
import socket
from threading import Thread
import pygame
from random import randintglobal texts
texts = []class Text_barrage:
def __init__(self, title):
self.speed = 1
self.pox_x = 700
self.pox_y = randint(10, 160)
self.title = title
self.color = (randint(100, 255), randint(100, 255), randint(100, 255))class ClientThread(Thread):
def __init__(self, c, addr):
super().__init__()
self.c = c
self.addr = addrdef run(self):
while True:
try:
data = https://www.it610.com/article/self.c.recv(1024).decode()
text_barrage = Text_barrage(data)
texts.append(text_barrage)
print(str(self.addr)+'>>>'+data)
except ConnectionResetError:
print(str(self.addr)+' close connection')
breakclass ServerThread(Thread):
def __init__(self, ip, port):
super().__init__()
self.ip = ip
self.port = portdef run(self):
server = socket.socket()
server.bind((self.ip, self.port))
server.listen(512)
while True:
c, addr = server.accept()
client = ClientThread(c, addr)
client.start()if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600, 200))
pygame.display.set_caption('barrage')
font = pygame.font.Font('./aa.ttf', 30)
ServerThread('10.7.181.82', 8080).start()
while True:
screen.fill((0, 0, 0))
if len(texts) > 0:
for t in texts[:]:
if t.pox_x < 0-len(t.title)*40:
texts.remove(t)
else:
title = font.render(t.title, True, t.color)
screen.blit(title, (t.pox_x, t.pox_y)
t.pox_x -= t.speed
pygame.time.delay(5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.display.flip()
client_barrage.py
import socketif __name__ == '__main__':
client = socket.socket()
client.connect(('10.7.181.82', 8080))while True:
message = input('>>>')
client.send(message.encode())
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 人脸识别|【人脸识别系列】| 实现自动化妆
- paddle|动手从头实现LSTM
- pytorch|使用pytorch从头实现多层LSTM