rishabbansal21

Day-13

Jan 22nd, 2021 (edited)
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. #client.py
  2. import pygame
  3.  
  4. width = 500
  5. height = 500
  6. screen = pygame.display.set_mode((width, height))
  7.  
  8. clients_num = 0
  9.  
  10. class Player:
  11.     def __init__(self, x, y, width, height, color):
  12.         self.x = x
  13.         self.y = y
  14.         self.width = width
  15.         self.height = height
  16.         self.color = color
  17.         self.rect = (x, y, width, height)
  18.         self.vel = 5
  19.  
  20.     def draw(self, screen):
  21.         pygame.draw.rect(screen, self.color, self.rect)
  22.  
  23.     def move(self):
  24.         keys = pygame.key.get_pressed()
  25.         if keys[pygame.K_LEFT]: self.x -= self.vel
  26.         if keys[pygame.K_RIGHT]: self.x += self.vel
  27.         if keys[pygame.K_UP]: self.y -= self.vel
  28.         if keys[pygame.K_DOWN]: self.y += self.vel
  29.         self.rect = (self.x, self.y, self.width, self.height)
  30.    
  31. def redrawWindow(screen, player):
  32.     screen.fill((255,255,255))
  33.     player.draw(screen)
  34.     pygame.display.update()
  35.  
  36. def main():
  37.     run = True
  38.     p = Player(50,50,100,100, (0,255,0))
  39.     clock = pygame.time.Clock()
  40.     while run:
  41.         for event in pygame.event.get():
  42.             if event.type == pygame.QUIT:
  43.                 run = False
  44.                 pygame.quit()
  45.  
  46.         p.move()
  47.         clock.tick(20)
  48.         redrawWindow(screen, p)
  49.  
  50. main()
  51.  
  52.  
  53. ------------------------------------------------------------------------------------
  54. #server.py
  55.  
  56.  
  57. import socket
  58. from _thread import *
  59.  
  60. server = "192.168.1.4"
  61. port = 5555
  62.  
  63. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    #ipv4, #stream
  64.  
  65. try:
  66.     s.bind((server,port))
  67.  
  68. except socket.error as e:
  69.     print(str(e))
  70.  
  71. s.listen(2)  #number of clients
  72. print("WAITING FOR CONNECTION")
  73.  
  74. def threaded_client(conn):
  75.     conn.send(str.encode("CONNECTED!"))
  76.     reply = ""
  77.     while True:
  78.         try:
  79.             data = conn.recv(2048) #amount of bits
  80.             reply = data.decode("utf-8")
  81.  
  82.             if not data:
  83.                 print("DISCONNECTED!")
  84.                 break
  85.            
  86.             else:
  87.                 print("RECEIVED: " + str(reply))
  88.                 print("SENDING: OKAY")
  89.  
  90.             conn.sendall(str.encode("OKAY"))
  91.  
  92.         except:
  93.             break
  94.     print("LOST CONNECTION")
  95.     conn.close()
  96.  
  97. while True:
  98.     conn, addr = s.accept()   #obj, ip
  99.     print("Connected To: " + str(addr))
  100.  
  101.     start_new_thread(threaded_client, (conn,))
  102.  
  103.  
  104.  
  105.  
  106. -------------------------------------------------------------------------------------------
  107.  
  108. #network.py
  109. import socket
  110.  
  111. class Network:
  112.     def __init__(self):
  113.         self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  114.         self.server = "192.168.1.4"
  115.         self.port = 5555
  116.         self.addr = (self.server, self.port)
  117.         self.id = self.connect()
  118.         print(self.id)
  119.  
  120.     #CONNECT HO JAO AND CONNE T HOTE HI JO BH IMILE WO RETURN KARDO
  121.     def connect(self):
  122.         try:
  123.             self.client.connect(self.addr)
  124.             return self.client.recv(2048).decode()
  125.         except:
  126.             pass
  127.  
  128.     def send(self, data):
  129.         try:
  130.             self.client.send(str.encode(data))
  131.             return self.client.recv(2048).decode()
  132.         except socket.error as e:
  133.             print(e)
  134.  
  135. n = Network()
  136. print(n.send("Rishab"))
  137. #print(n.send("Bansal"))
  138.  
Add Comment
Please, Sign In to add comment