Advertisement
ziyadzer

Agar.io made with python

Oct 21st, 2019
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.39 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import random
  4. # import os
  5. # import time
  6.  
  7. pygame.init()
  8. pygame.display.set_caption("Agar.io")
  9.  
  10. name = input("Player name: ")
  11. line_draw = input("do you want to draw a line? ")
  12.  
  13.  
  14. width = 600
  15. height = 600
  16. vel = 0.5
  17. rows = 20
  18. w = 500
  19.  
  20. screen = pygame.display.set_mode((height, width))
  21.  
  22. red = (255, 0, 0)
  23. dim_red = (55, 0, 0)
  24. green = (0, 255, 0)
  25. yellow = (255, 255, 0)
  26. blue = (0, 0, 255)
  27. dim_blue = (0, 0, 55)
  28. cyan = (0, 255, 255)
  29. white = (255, 255, 255)
  30. magenta = (255, 0, 255)
  31. black = (0, 0, 0)
  32.  
  33. background_color = green
  34.  
  35. player_position = [265, 265]
  36. player_color = [0, 0, 255]
  37. player_size = 20
  38.  
  39. food_position = [random.randint(1, height), random.randint(1, width)]
  40. food_sizes_list = [7, 7, 7, 7, 7, 7, 8, 8, 8, 9, 10, 10, 11, 12, 13, 14]
  41. food_size = random.choice(food_sizes_list)
  42. food_colors = [red, blue, white, cyan, dim_blue, dim_red, yellow, magenta]
  43. food_color = random.choice(food_colors)
  44. food_list = [food_position]
  45.  
  46. myFont = pygame.font.SysFont("arial", 30)
  47. my_name = pygame.font.SysFont("arial", 30)
  48.  
  49.  
  50. def detect_collision(player_pos, food_pos):
  51.     p_x = player_pos[0]
  52.     p_y = player_pos[1]
  53.  
  54.     e_x = food_pos[0]
  55.     e_y = food_pos[1]
  56.  
  57.     if ((e_x >= p_x) and e_x < (p_x + player_size)) or ((p_x >= e_x) and p_x < (e_x + food_size)):
  58.         if ((e_y >= p_y)and e_y < (p_y + player_size)) or ((p_y >= e_y) and p_y < (e_y + food_size)):
  59.             return True
  60.     return False
  61.  
  62.  
  63. game_over = False
  64.  
  65. while not game_over:
  66.     class Player:
  67.         def draw_player(self):
  68.             pygame.draw.rect(screen, player_color, (player_position[0], player_position[1], player_size, player_size))
  69.  
  70.     if detect_collision(player_position, food_position):
  71.         player_size += food_size / 2
  72.  
  73.         food_position = [random.randint(1, height), random.randint(1, width)]
  74.         food_size = random.choice(food_sizes_list)
  75.         food_color = random.choice(food_colors)
  76.  
  77.  
  78.     class Food:
  79.         def draw_food(self):
  80.             pygame.draw.rect(screen, food_color, (food_position[0], food_position[1], food_size, food_size))
  81.  
  82.     x = food_position[0]
  83.     y = food_position[1]
  84.     mouse_pos = pygame.mouse.get_pos()
  85.     keys = pygame.key.get_pressed()
  86.  
  87.     if keys[pygame.K_LEFT]:  # and food_position[0] > 0:
  88.         x += vel
  89.  
  90.     elif keys[pygame.K_RIGHT]:  # and food_position[0] < width - food_size:
  91.         x -= vel
  92.  
  93.     elif keys[pygame.K_UP]:  # and food_position[1] > 0:
  94.         y += vel
  95.  
  96.     elif keys[pygame.K_DOWN]:  # and food_position[1] < height - food_size:
  97.         y -= vel
  98.  
  99.     elif keys[pygame.K_x]:
  100.         sys.exit()
  101.  
  102.     food_position = [x, y]
  103.  
  104.  
  105.     class Line:
  106.         def draw_line(self):
  107.             pygame.draw.line(screen, white, player_position, mouse_pos)
  108.  
  109.  
  110.     for event in pygame.event.get():
  111.         if event.type == pygame.QUIT:
  112.             sys.exit()
  113.     screen.fill(background_color)
  114.     pygame.display.update()
  115.  
  116.     text = "Player size: " + str(player_size)
  117.     label = myFont.render(text, 1, black)
  118.     screen.blit(label, (width - 600, height - 40))
  119.  
  120.     name_label = name
  121.     label_name = my_name.render(name_label, 1, black)
  122.     screen.blit(label_name, (player_position[0], player_position[1] - 50))
  123.  
  124.     player = Player()
  125.     player.draw_player()
  126.  
  127.     food = Food()
  128.     food.draw_food()
  129.  
  130.     if line_draw.lower() == "yes":
  131.         line = Line()
  132.         line.draw_line()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement