Advertisement
snappyink

Untitled

Nov 29th, 2022
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.92 KB | Gaming | 0 0
  1. import pygame
  2. import socket
  3. import time
  4. import math
  5.  
  6.  
  7. # classes
  8. class Player(pygame.sprite.Sprite):
  9.     def __init__(self):
  10.         super().__init__()
  11.         self.origin_image = pygame.transform.scale(pygame.image.load("assets/player2.png"), (25, 25))
  12.         self.image = self.origin_image
  13.         self.rect = self.image.get_rect()
  14.         self.rect.center = (960/2, 540/2)
  15.         self.coords = [0, 0]
  16.         self.angle = 0
  17.         self.speed = 5
  18.  
  19.     def move_up(self):
  20.         self.coords[1] += self.speed
  21.  
  22.     def move_down(self):
  23.         self.coords[1] -= self.speed
  24.  
  25.     def move_left(self):
  26.         self.coords[0] += self.speed
  27.  
  28.     def move_right(self):
  29.         self.coords[0] -= self.speed
  30.  
  31.     def set_coords(self, new_coords: tuple or list):
  32.         self.coords = list(new_coords)
  33.  
  34.     def rotate_to_mouse(self):
  35.         player_rect = self.origin_image.get_rect(center=self.rect.center)
  36.         self.angle = rotate_to_point(pygame.mouse.get_pos(), 0, player_rect)
  37.  
  38.  
  39.  
  40. class OtherPlayer(pygame.sprite.Sprite):
  41.     def __init__(self, coordinates: tuple, angle: float or int, pseudo: str):
  42.         super().__init__()
  43.         self.pseudo = pseudo
  44.         self.rect = pygame.rect.Rect(0, 0, 25, 25)
  45.         self.rect.center = coordinates
  46.         self.angle = angle
  47.         self.speed = 5
  48.  
  49.     def move_up(self):
  50.         self.rect.centery += self.speed
  51.  
  52.     def move_down(self):
  53.         self.rect.centery -= self.speed
  54.  
  55.     def move_left(self):
  56.         self.rect.centerx += self.speed
  57.  
  58.     def move_right(self):
  59.         self.rect.centerx -= self.speed
  60.  
  61.  
  62. class Asteroid(pygame.sprite.Sprite):
  63.     def __init__(self, coordinates: tuple or list):
  64.         super().__init__()
  65.         self.rect = game.asteroid_image.get_rect()
  66.         self.rect.center = coordinates
  67.         self.speed = 5
  68.  
  69.     def move_up(self):
  70.         self.rect.centery += self.speed
  71.  
  72.     def move_down(self):
  73.         self.rect.centery -= self.speed
  74.  
  75.     def move_left(self):
  76.         self.rect.centerx += self.speed
  77.  
  78.     def move_right(self):
  79.         self.rect.centerx -= self.speed
  80.  
  81.  
  82. class Game:
  83.     def __init__(self):
  84.         self.player = Player()
  85.         self.all_other_players = pygame.sprite.Group()
  86.         self.all_asteroids = pygame.sprite.Group()
  87.         self.asteroid_image = pygame.image.load('assets/asteroid.png')
  88.  
  89.     def add_a_player(self, coordinates, angle, pseudo):
  90.         self.all_other_players.add(OtherPlayer(coordinates, angle, pseudo))
  91.  
  92.     def clear_all_players(self):
  93.         self.all_other_players.empty()
  94.  
  95.     def add_asteroid(self, coordinates: tuple or list):
  96.         self.all_asteroids.add(Asteroid(coordinates))
  97.  
  98.     def clear_all_asteroids(self):
  99.         self.all_asteroids.empty()
  100.  
  101. # classes
  102.  
  103.  
  104. # socket stuff
  105. user_name = str(input("What is your username: "))
  106. header = 64
  107. port = 5050
  108. FORMAT = 'utf-8'
  109. disconnect_message = "!DISCONNECT"
  110. pos_update_message = "!POS_UPDATE"
  111. received_message = "RECEIVED"
  112. get_all_player_info_message = "!GET_PLAYERS"
  113. get_near_asteroids_message = "!GET_ASTEROIDS"
  114. server = "127.0.0.1"
  115. addr = (server, port)
  116. server_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  117. server_client.connect(addr)
  118. # socket stuff
  119.  
  120. # create pygame window
  121. pygame.init()
  122. pygame.display.set_caption("")
  123. screen = pygame.display.set_mode((960, 540))
  124. clock = pygame.time.Clock()
  125. max_fps = 60
  126. screen_x, screen_y = screen.get_size()
  127. font = pygame.font.Font('assets/SMME.ttf', 15)
  128. # create pygame window
  129.  
  130. # variables
  131. game = Game()
  132. frame_count = 0
  133. # variables
  134.  
  135.  
  136. # functions
  137. def send(msg):
  138.     """Sends a socket request to the server and
  139.     gets a response from the server and converts
  140.      it to it's form"""
  141.     server_client.send(msg.encode(FORMAT))  # Sends a socket request to the server
  142.     answer = server_client.recv(4096*8).decode(FORMAT)
  143.     if answer == received_message:
  144.         return
  145.     return eval(answer)  # Gets a response from the server and converts it to it's form
  146.  
  147.  
  148. def rotate_to_point(point, correction_angle, rect):
  149.     dx, dy = point[0] - rect.centerx, rect.centery - point[1]
  150.     angle = math.degrees(math.atan2(dy, dx)) - correction_angle
  151.     return angle
  152.  
  153.  
  154. def rot_center(image, angle, xy):
  155.     rotated_image = pygame.transform.rotate(image, angle)
  156.     new_rect = rotated_image.get_rect(center=image.get_rect(center=xy).center)
  157.     return rotated_image, new_rect
  158. # functions
  159.  
  160.  
  161.  
  162. send(user_name)
  163. player_infos = send(get_all_player_info_message)
  164. game.player.set_coords(player_infos[user_name]['pos'])
  165. running = True
  166. while running:
  167.  
  168.     # calculations
  169.     frame_count += 1
  170.     if frame_count % 30 == 0:
  171.         frame_count = 0
  172.         msg_to_send = {"pos": game.player.coords, "angle": game.player.angle}
  173.         send(f"{pos_update_message}:{msg_to_send}")
  174.         player_infos = send(get_all_player_info_message)
  175.         game.clear_all_players()
  176.         for player in player_infos:
  177.             if not player == user_name:
  178.                 x = game.player.rect.centerx + game.player.coords[0] - player_infos[player]['pos'][0]
  179.                 y = game.player.rect.centery + game.player.coords[1] - player_infos[player]['pos'][1]
  180.                 game.add_a_player((x, y), player_infos[player]['angle'], player)
  181.  
  182.         near_asteroids = send(f"{get_near_asteroids_message}:{msg_to_send}")
  183.         game.clear_all_asteroids()
  184.         for asteroid in near_asteroids:
  185.             x = game.player.rect.centerx + game.player.coords[0] - asteroid[0]
  186.             y = game.player.rect.centery + game.player.coords[1] - asteroid[1]
  187.             game.add_asteroid((x, y))
  188.  
  189.     game.player.rotate_to_mouse()
  190.  
  191.     keys = pygame.key.get_pressed()
  192.  
  193.     if keys[pygame.K_z]:
  194.         game.player.move_up()
  195.         for asteroid in game.all_asteroids:
  196.             asteroid.move_up()
  197.         for player in game.all_other_players:
  198.             player.move_up()
  199.     if keys[pygame.K_s]:
  200.         game.player.move_down()
  201.         for asteroid in game.all_asteroids:
  202.             asteroid.move_down()
  203.         for player in game.all_other_players:
  204.             player.move_down()
  205.     if keys[pygame.K_q]:
  206.         game.player.move_left()
  207.         for asteroid in game.all_asteroids:
  208.             asteroid.move_left()
  209.         for player in game.all_other_players:
  210.             player.move_left()
  211.     if keys[pygame.K_d]:
  212.         game.player.move_right()
  213.         for asteroid in game.all_asteroids:
  214.             asteroid.move_right()
  215.         for player in game.all_other_players:
  216.             player.move_right()
  217.  
  218.  
  219.     # displaying
  220.  
  221.     screen.fill((0, 0, 0))
  222.     screen.blit(rot_center(game.player.origin_image, game.player.angle, game.player.rect.center)[0],
  223.                 rot_center(game.player.origin_image, game.player.angle, game.player.rect.center)[1])
  224.     for player in game.all_other_players:
  225.         pseudo_render = font.render(str(player.pseudo), True, (255, 255, 255))
  226.         pos_to_display_pseudo = (player.rect.centerx - pseudo_render.get_size()[0]/2, player.rect.centery - player.rect.size[1] - 10)
  227.         screen.blit(pseudo_render, pos_to_display_pseudo)
  228.         screen.blit(rot_center(game.player.origin_image, player.angle, player.rect.center)[0],
  229.                     rot_center(game.player.origin_image, player.angle, player.rect.center)[1])
  230.  
  231.     pseudo_render = font.render(str('You'), True, (255, 255, 255))
  232.     pos_to_display_pseudo = (game.player.rect.centerx - pseudo_render.get_size()[0] / 2, game.player.rect.centery - game.player.rect.size[1] - 10)
  233.     screen.blit(pseudo_render, pos_to_display_pseudo)
  234.  
  235.  
  236.     for asteroid in game.all_asteroids:
  237.         screen.blit(game.asteroid_image, asteroid.rect)
  238.  
  239.  
  240.  
  241.     screen.blit(font.render(str(game.player.coords), True, (255, 255, 255)), (5, 5))
  242.  
  243.  
  244.     # display
  245.     pygame.display.flip()
  246.     clock.tick(max_fps)
  247.     # events
  248.     for event in pygame.event.get():
  249.         if event.type == pygame.QUIT:
  250.             send(disconnect_message)
  251.             time.sleep(0.2)
  252.             running = False
  253.  
Tags: Gaming
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement