美文网首页
day18 作业 弹幕

day18 作业 弹幕

作者: HavenYoung | 来源:发表于2018-08-08 19:39 被阅读0次

    服务端

    import socket
    from threading import Thread
    import pygame
    from random import randint
    
    
    class ConversationThread(Thread):
        """在子线程中处理不同的客户端会话"""
        """
        python中可以在函数参数后面加一个冒号,来对参数的类型进行说明
        """
        def __init__(self, conversation: socket.socket, address):
            super().__init__()
            self.conversation = conversation
            self.address = address
            # self.screen = screen
    
        def run(self):
            while True:
                self.conversation.send('hello'.encode())
                barrage = self.conversation.recv(1024).decode(encoding='utf-8')
                word = Barrage(barrage)
                barrages.append(word)
    
    
    class ChatLoop(Thread):
        def __init__(self, server):
            super().__init__()
            self.server = server
    
        def run(self):
            while True:
                conversation, address = self.server.accept()
    
                t = ConversationThread(conversation, address)
                t.start()
    
    
    barrages = []
    
    
    class Barrage:
        def __init__(self, content):
            self.x = 800
            self.y = randint(1,450)
            self.x_speed = randint(-20, -10)
            self.color = (randint(1, 255), randint(1, 255), randint(1, 255))
            self.content = content
    
    
    if __name__ == '__main__':
        server = socket.socket()
        server.bind(('10.7.181.51', 8080))
        server.listen(50)
    
        pygame.init()
        screen = pygame.display.set_mode((800, 450))
        screen.fill((255, 255, 255))
    
        pygame.display.flip()
        chat = ChatLoop(server)
        chat.start()
    
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit(0)
    
            screen.fill((255, 255, 255))
    
            for barrage in barrages:
                font = pygame.font.Font('./aa.ttf', 30)
                surface = font.render(barrage.content, True, barrage.color)
                screen.blit(surface, (barrage.x, barrage.y))
                barrage.x += barrage.x_speed
                if barrage.x <= 0:
                    barrages.remove(barrage)
    
            pygame.display.update()
            pygame.time.delay(100)
    
    

    客户端

    import socket
    
    
    if __name__ == '__main__':
        # 1.创建套接字对象
        client = socket.socket()
    
        # 2.连接服务器
        client.connect(('10.7.181.51', 8080))
    
        # 聊天循环
        while True:
            # 接收消息
            message_data = client.recv(1024)
            print(message_data.decode(encoding='utf-8'))
    
            # 发送消息
            message = input('>>>>')
            client.send(message.encode())
    

    相关文章

      网友评论

          本文标题:day18 作业 弹幕

          本文链接:https://www.haomeiwen.com/subject/rdaxbftx.html