Advertisement
Guest User

Circle Game

a guest
Feb 24th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.05 KB | None | 0 0
  1. from random import seed
  2. from random import randint
  3. import datetime
  4. import pygame
  5. import sys
  6. import time
  7. import math
  8. import os
  9. from pygame.locals import *
  10.  
  11. seed(time)
  12.  
  13. def DrawText(text, font, surface_menu, x, y, selected=False):
  14.     textobj = font.render(text, 1, font_color)
  15.     textrect = textobj.get_rect()
  16.     textrect.center = (x, y)
  17.     windowSurface.blit(textobj, textrect)
  18.  
  19. pygame.init()
  20.  
  21. GOLDEN = (255, 255, 153)
  22. WHITE = (255, 255, 255)
  23. GRAY = (51, 51, 51)
  24. RED = (255, 0, 0)
  25. GREEN = (0, 255, 0)
  26. BLUE = (0, 0, 255)
  27. BLACK = (0, 0, 0)
  28. CYAN = (0, 255, 255)
  29. MAGENTA = (255, 0, 255)
  30. AZURE = (240, 255, 255)
  31. OLIVE = (128, 128, 0)
  32.  
  33. colors = [RED, GREEN, BLUE, CYAN, MAGENTA, AZURE, OLIVE, GOLDEN]
  34.  
  35. WINDOW_WIDTH = 1920
  36. WINDOW_HEIGHT = 1200
  37. font_color = GOLDEN
  38. font = pygame.font.SysFont("comicsansms", 72)
  39. windowSurface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), pygame.FULLSCREEN, 0, 32)
  40. pygame.display.set_caption('Circle Game')
  41. fps = pygame.time.Clock()
  42.  
  43. CIRCLE_RADIUS = 75
  44. PLAYER_SIZE = 50
  45. CIRCLES_NUM = 5
  46. MOVESPEED = 5
  47. THICKNESS = 15
  48. MIN_DISTANCE = 200
  49.  
  50. score = 0
  51. gameover = False
  52.  
  53. class Circle(object):
  54.     def __init__(self, x, y, radius, color, duration, direction):
  55.         self.x = x
  56.         self.y = y
  57.         self.radius = radius
  58.         self.color = color
  59.         self.duration = duration
  60.         self.direction = direction
  61.  
  62.     def intersects(self, circle):
  63.         return ((self.x - circle.x)**2 + (self.y - circle.y)**2 <
  64.                 (self.radius + circle.radius)**2)
  65.  
  66.     def check_min_distance(self, circle, min_distance):
  67.         return ((self.x - circle.x)**2 + (self.y - circle.y)**2 <
  68.                 (self.radius + circle.radius + min_distance)**2)
  69.  
  70.     def make_move(self):
  71.         if self.duration == 0:
  72.             self.set_random_dir()
  73.         while (self.x + DX[self.direction] + self.radius >= WINDOW_WIDTH or
  74.                self.x + DX[self.direction] - self.radius <= 0 or
  75.                self.y + DY[self.direction] + self.radius >= WINDOW_HEIGHT or
  76.                self.y + DY[self.direction] - self.radius <= 0):
  77.             self.set_random_dir()
  78.         self.x += DX[self.direction]
  79.         self.y += DY[self.direction]
  80.         self.duration -= 1
  81.  
  82.     def set_random_dir(self):
  83.         self.duration = randint(50, 150)
  84.         check = self.direction
  85.         self.direction = randint(0, 7)
  86.         while check == self.direction:
  87.             self.direction = randint(0, 7)
  88.  
  89. def update_leaderboard():
  90.     buffer = []
  91.     timestamp = datetime.datetime.now()
  92.     now = timestamp.strftime("%Y-%m-%d %H:%M:%S")
  93.     scoreboard = open("scoreboard.txt", "a")
  94.     scoreboard.write(f"{score} | {now}")
  95.     scoreboard.close()
  96.     with open("scoreboard.txt", "r") as scoreboard:
  97.         x = scoreboard.readlines()
  98.         for i in range(len(x)):
  99.             currentline = x[i]
  100.             element = currentline.strip().split(' | ')
  101.             buffer.append((int(element[0]), element[1]))
  102.     print(buffer)
  103.     buffer.sort(reverse = True)
  104.     print(buffer)
  105.     file = open("scoreboard.txt", "w")
  106.     file.close()
  107.     scoreboard = open("scoreboard.txt", "a")
  108.     for i in range(len(buffer)):
  109.         scoreboard.write(str(buffer[i][0]) + " | " + buffer[i][1] + '\n')
  110.     scoreboard.close()
  111.  
  112. def generate_circle_random_pos():
  113.     x = randint(CIRCLE_RADIUS, WINDOW_WIDTH-CIRCLE_RADIUS)
  114.     y = randint(CIRCLE_RADIUS, WINDOW_HEIGHT-CIRCLE_RADIUS)
  115.     return Circle(x, y, CIRCLE_RADIUS, colors[randint(0, len(colors)-1)], 0, 0)
  116.  
  117.  
  118. def generate_circles(num_circles):
  119.     circles = []
  120.     cnt = 0
  121.     MAX_CNT = 500
  122.     while len(circles) < num_circles:
  123.         new_circle = generate_circle_random_pos()
  124.         if player.check_min_distance(new_circle, MIN_DISTANCE):
  125.             continue
  126.         has_intersection = False
  127.         for c in circles:
  128.             if c.intersects(new_circle):
  129.                 has_intersection = True
  130.                 break
  131.         if not has_intersection:
  132.             circles.append(new_circle)
  133.         cnt += 1
  134.         if cnt == MAX_CNT:
  135.             circles.clear()
  136.     return circles
  137.  
  138.  
  139. player = Circle(round(WINDOW_WIDTH/2), round(WINDOW_HEIGHT/2),
  140.                 PLAYER_SIZE, GREEN, 0, 0,)
  141.  
  142. circles = generate_circles(CIRCLES_NUM)
  143.  
  144. DX = [-MOVESPEED, 0, MOVESPEED, 0, -MOVESPEED, MOVESPEED, MOVESPEED, -MOVESPEED]
  145. DY = [0, -MOVESPEED, 0, MOVESPEED, -MOVESPEED, -MOVESPEED, MOVESPEED, MOVESPEED]
  146.  
  147. moveLeft = False
  148. moveRight = False
  149. moveUp = False
  150. moveDown = False
  151.  
  152. while not gameover:
  153.     score += 1
  154.     if score % 20 == 0:
  155.         player.radius += 1
  156.     for event in pygame.event.get():
  157.         if event.type == QUIT:
  158.             pygame.quit()
  159.             sys.exit()
  160.         elif event.type == KEYDOWN:
  161.             if event.key == K_w or event.key == K_UP:
  162.                 moveDown = False
  163.                 moveUp = True
  164.             elif event.key == K_a or event.key == K_LEFT:
  165.                 moveRight = False
  166.                 moveLeft = True
  167.             elif event.key == K_s or event.key == K_DOWN:
  168.                 moveUp = False
  169.                 moveDown = True
  170.             elif event.key == K_d or event.key == K_RIGHT:
  171.                 moveLeft = False
  172.                 moveRight = True
  173.         elif event.type == KEYUP:
  174.             if event.key == K_w or event.key == K_UP:
  175.                 moveUp = False
  176.             elif event.key == K_a or event.key == K_LEFT:
  177.                 moveLeft = False
  178.             elif event.key == K_s or event.key == K_DOWN:
  179.                 moveDown = False
  180.             elif event.key == K_d or event.key == K_RIGHT:
  181.                 moveRight = False
  182.  
  183.     windowSurface.fill(GRAY)
  184.  
  185.     DrawText(f'Score: {score}', font, windowSurface, round(WINDOW_WIDTH/2), round(WINDOW_HEIGHT/10), True)
  186.  
  187.     if moveDown and player.y + player.radius < WINDOW_HEIGHT:
  188.         player.y += MOVESPEED*2
  189.     if moveUp and player.y - player.radius > 0:
  190.         player.y -= MOVESPEED*2
  191.     if moveLeft and player.x - player.radius > 0:
  192.         player.x -= MOVESPEED*2
  193.     if moveRight and player.x + player.radius < WINDOW_WIDTH:
  194.         player.x += MOVESPEED*2
  195.  
  196.     for c in circles:
  197.         if c.intersects(player):
  198.             gameover = True
  199.             update_leaderboard()
  200.             break
  201.  
  202.     new_circles = []
  203.  
  204.     while True:
  205.         global_ok = True
  206.         for c in circles:
  207.             cand = Circle(c.x, c.y, CIRCLE_RADIUS, c.color,
  208.                           c.duration, c.direction)
  209.             chance = randint(1, 100)
  210.             if chance >= 99:
  211.                 cand.set_random_dir()
  212.             cand.make_move()
  213.             ok = True
  214.             for c2 in new_circles:
  215.                 if c2.intersects(cand):
  216.                     ok = False
  217.                     break
  218.             for c2 in circles:
  219.                 if c2 != c and c2.intersects(cand):
  220.                     ok = False
  221.                     break
  222.             cnt = 0
  223.             MAX_CNT = 50
  224.             while not ok:
  225.                 cand = Circle(c.x, c.y, CIRCLE_RADIUS, c.color,
  226.                               c.duration, c.direction)
  227.                 cand.set_random_dir()
  228.                 cand.make_move()
  229.                 ok = True
  230.                 for c2 in new_circles:
  231.                     if c2.intersects(cand):
  232.                         ok = False
  233.                         break
  234.                 for c2 in circles:
  235.                     if c2 != c and c2.intersects(cand):
  236.                         ok = False
  237.                         break
  238.                 cnt += 1
  239.                 if cnt == MAX_CNT:
  240.                     break
  241.             if cnt == MAX_CNT:
  242.                 new_circles.clear()
  243.                 global_ok = False
  244.                 break
  245.             new_circles.append(cand)
  246.         if global_ok:
  247.             break
  248.     circles = new_circles
  249.     for c in circles:
  250.         pygame.draw.circle(windowSurface, c.color,
  251.                            (c.x, c.y), c.radius, THICKNESS)
  252.     pygame.draw.circle(windowSurface, player.color,
  253.                        (player.x, player.y), player.radius, 0)
  254.     pygame.display.update()
  255.     fps.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement