Advertisement
Pymonster

radarSim

Oct 13th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.03 KB | None | 0 0
  1. import pygame
  2. import random
  3. import time
  4.  
  5. pygame.init()
  6. display_width = 700
  7. display_height = 700
  8. clock = pygame.time.Clock()
  9. FPS = 30
  10.  
  11. gameDisplay = pygame.display.set_mode((display_width, display_height))
  12. pygame.display.set_caption("radarSim")
  13. font = pygame.font.SysFont(None, 25)
  14.  
  15. white = pygame.Color(255, 255, 255)
  16. black = pygame.Color(0, 0, 0)
  17. red = pygame.Color(255, 0, 0)
  18. green = pygame.Color(0, 255, 0)
  19. blue = pygame.Color(0, 0, 255)
  20. grey = pygame.Color(200, 200, 200)
  21.  
  22. detection_area = [100, 100]
  23. detection_size = [500, 500]
  24. speed = [1]
  25. pulsing = False
  26. self_radius = [10]
  27. priority_loc = [554, 94]
  28. speed_loc = [435, 94, 470, 94]
  29. targets_loc = [165, 94, 240, 94]
  30. targets = [0]
  31. grid_color = [white, grey, black]
  32. foreign_loc = [0, 0]
  33. foreign_color = [red]
  34. target_set = False
  35. foreign_size = [20]
  36.  
  37.  
  38. def text_objects(text, color, text_x, text_y):
  39.     textSurface = font.render(text, True, color)
  40.     return textSurface, textSurface.get_rect()
  41.  
  42.  
  43. def message_to_screen(msg, color, text_x, text_y):
  44.     textSurf, textRect = text_objects(msg, color, text_x, text_y)
  45.     textRect.center = (text_x, text_y)
  46.     gameDisplay.blit(textSurf, textRect)
  47.  
  48.  
  49. class Pulses:
  50.     def __init__(self, x, y, radius=10, size=1, color=red):
  51.         self.x = x
  52.         self.y = y
  53.         self.size = size
  54.         self.color = color
  55.         self.speed = speed
  56.  
  57.     def draw(self):
  58.         pygame.draw.rect(gameDisplay, grid_color[0], (detection_area[0], detection_area[1], detection_size[0],                                      detection_size[1]), 1)
  59.         pygame.draw.line(gameDisplay, grid_color[1], (355, 100), (355, 600), 1)
  60.         pygame.draw.line(gameDisplay, grid_color[1], (100, 350), (600, 350), 1)
  61.         pygame.draw.circle(gameDisplay, white, (self.x, self.y), self_radius[0], self.size)
  62.         Pulses.pulse(self)
  63.  
  64.     def pulse(self):
  65.         pygame.draw.circle(gameDisplay, white, (self.x, self.y), 10, self.size)
  66.         if pulsing:
  67.             if self_radius[0] < 245:
  68.                 circleRect = pygame.draw.circle(gameDisplay, self.color, (self.x, self.y), self_radius[0], self.size)
  69.                 self_radius[0] += speed[0]
  70.                 for circle in circleRect:
  71.                     if circleRect[0] < foreign_loc[0]:
  72.                         if circleRect[1] > foreign_loc[1]:
  73.                             if circleRect[2] > foreign_loc[0]:
  74.                                 if circleRect[3] < foreign_loc[1]:
  75.                                     print("possible collision:", circleRect)
  76.             else:
  77.                 self_radius[0] = 10
  78.                 pygame.draw.circle(gameDisplay, self.color, (self.x, self.y), self_radius[0], self.size)
  79.                 self_radius[0] += speed[0]
  80.         else:
  81.             self_radius[0] = 10
  82.  
  83.     def change_color(self):
  84.         if self_radius[0] == 0:
  85.             message_to_screen("Priority = 0", white, priority_loc[0], priority_loc[1])
  86.         elif self_radius[0] <= 85:
  87.             message_to_screen("Priority = 0", white, priority_loc[0], priority_loc[1])
  88.             self.color = red
  89.         elif self_radius[0] <= 170:
  90.             message_to_screen("Priority = 1", white, priority_loc[0], priority_loc[1])
  91.             self.color = green
  92.         elif self_radius[0] <= 250:
  93.             message_to_screen("Priority = 2", white, priority_loc[0], priority_loc[1])
  94.             self.color = white
  95.         elif self_radius[0] >= 250:
  96.             message_to_screen("Priority = 2", white, priority_loc[0], priority_loc[1])
  97.             self.color = white
  98.  
  99.  
  100. emitter1 = Pulses(355, 350)
  101.  
  102.  
  103. def event_(mod):
  104.     global pulsing
  105.     if mod == "k_up":
  106.         if speed[0] >= 5:
  107.             pass
  108.         else:
  109.             speed[0] += 1
  110.     elif mod == "k_down":
  111.         if speed[0] <= 1:
  112.             speed[0] = 1
  113.             pulsing = False
  114.         else:
  115.             speed[0] -= 1
  116.     elif mod == "toggle":
  117.         pulsing = not pulsing
  118.         speed[0] = 1
  119.     elif mod == "grid":
  120.         if grid_color[0] == white:
  121.             grid_color[0] = black
  122.             grid_color[1] = black
  123.         else:
  124.             grid_color[0] = white
  125.             grid_color[1] = grey
  126.     elif mod == "target_vis":
  127.         if foreign_color[0] == black:
  128.             foreign_color[0] = red
  129.         else:
  130.             foreign_color[0] = black
  131.  
  132.  
  133. def display_text():
  134.     message_to_screen("Targets Located:", white, targets_loc[0], targets_loc[1])
  135.     message_to_screen(str(targets[0]), white, targets_loc[2], targets_loc[3])
  136.     message_to_screen("Speed:", white, speed_loc[0], speed_loc[1])
  137.     message_to_screen(str(speed[0]), white, speed_loc[2], speed_loc[3])
  138.  
  139.  
  140. def foreign_object():
  141.     global target_set
  142.     foreign_loc[0] = 200
  143.     foreign_loc[1] = 400
  144.     pygame.draw.rect(gameDisplay, foreign_color[0], (foreign_loc[0], foreign_loc[1],
  145.                                                      foreign_size[0], foreign_size[0]), 1)
  146.     # pygame.draw.rect(gameDisplay, foreign_color[0], (foreign_loc[0], foreign_loc[1],
  147.     # foreign_size[0], foreign_size[0]), 1)
  148.     # if not target_set:
  149.     #     foreign_loc[0] = random.randrange(100, 501)
  150.     #     foreign_loc[1] = random.randrange(100, 501)
  151.     #     target_set = True
  152.     # else:
  153.     #     pass
  154.  
  155.  
  156. def game_loop():
  157.     for event in pygame.event.get():
  158.         if event.type == pygame.QUIT:
  159.             pygame.quit()
  160.             quit()
  161.         elif event.type == pygame.KEYDOWN:
  162.             if event.key == pygame.K_UP:
  163.                 event_("k_up")
  164.             elif event.key == pygame.K_DOWN:
  165.                 event_("k_down")
  166.             elif event.key == pygame.K_SPACE:
  167.                 event_("toggle")
  168.             elif event.key == pygame.K_g:
  169.                 event_("grid")
  170.             elif event.key == pygame.K_t:
  171.                 event_("target_vis")
  172.         else:
  173.             pass
  174.  
  175.     gameDisplay.fill(black)
  176.     foreign_object()
  177.     emitter1.draw()
  178.     emitter1.change_color()
  179.     display_text()
  180.     pygame.display.update()
  181.     clock.tick(FPS)
  182.  
  183.  
  184. still_playing = True
  185. while still_playing:
  186.     game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement