Advertisement
Guest User

123

a guest
Feb 24th, 2020
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.64 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), 0, 32)
  40. pygame.display.set_caption('Circle Game')
  41. fps = pygame.time.Clock()
  42.  
  43. CIRCLE_RADIUS = 100
  44. PLAYER_SIZE = 50
  45. CIRCLES_NUM = 7
  46. MOVESPEED = 5
  47. THICKNESS = 15
  48. MIN_DISTANCE = 200
  49. TICK = 60
  50.  
  51. background_image = pygame.image.load("bg2.png").convert()
  52. background_image = pygame.transform.scale(background_image, (1920, 1200))
  53.  
  54. player_image = pygame.image.load('3.png').convert_alpha()
  55.  
  56. minions = [pygame.image.load('minion_1.png').convert_alpha(), pygame.image.load('minion_2.png').convert_alpha(), pygame.image.load('minion_3.png').convert_alpha(), pygame.image.load('minion_4.png').convert_alpha(), pygame.image.load('minion_5.png').convert_alpha(), pygame.image.load('minion_6.png').convert_alpha()]
  57.  
  58. score = 0
  59. gameover = False
  60.  
  61. class Circle(object):
  62.     def __init__(self, x, y, radius, color, duration, direction, sprite):
  63.         self.x = x
  64.         self.y = y
  65.         self.radius = radius
  66.         self.color = color
  67.         self.duration = duration
  68.         self.direction = direction
  69.         self.sprite = sprite
  70.  
  71.     def intersects(self, circle):
  72.         return ((self.x - circle.x)**2 + (self.y - circle.y)**2 <
  73.                 (self.radius + circle.radius)**2)
  74.  
  75.     def check_min_distance(self, circle, min_distance):
  76.         return ((self.x - circle.x)**2 + (self.y - circle.y)**2 <
  77.                 (self.radius + circle.radius + min_distance)**2)
  78.  
  79.     def make_move(self):
  80.         if self.duration == 0:
  81.             self.set_random_dir()
  82.         while (self.x + DX[self.direction] + self.radius >= WINDOW_WIDTH or
  83.                self.x + DX[self.direction] - self.radius <= 0 or
  84.                self.y + DY[self.direction] + self.radius >= WINDOW_HEIGHT or
  85.                self.y + DY[self.direction] - self.radius <= 0):
  86.             self.set_random_dir()
  87.         self.x += DX[self.direction]
  88.         self.y += DY[self.direction]
  89.         self.duration -= 1
  90.  
  91.     def set_random_dir(self):
  92.         self.duration = randint(50, 150)
  93.         check = self.direction
  94.         self.direction = randint(0, 7)
  95.         while check == self.direction:
  96.             self.direction = randint(0, 7)
  97.  
  98. def update_leaderboard():
  99.     buffer = []
  100.     timestamp = datetime.datetime.now()
  101.     now = timestamp.strftime("%Y-%m-%d %H:%M:%S")
  102.     scoreboard = open("scoreboard.txt", "a")
  103.     scoreboard.write(f"{score} | {now}")
  104.     scoreboard.close()
  105.     with open("scoreboard.txt", "r") as scoreboard:
  106.         x = scoreboard.readlines()
  107.         for i in range(len(x)):
  108.             currentline = x[i]
  109.             element = currentline.strip().split(' | ')
  110.             buffer.append((int(element[0]), element[1]))
  111.     print(buffer)
  112.     buffer.sort(reverse = True)
  113.     print(buffer)
  114.     file = open("scoreboard.txt", "w")
  115.     file.close()
  116.     scoreboard = open("scoreboard.txt", "a")
  117.     for i in range(len(buffer)):
  118.         scoreboard.write(str(buffer[i][0]) + " | " + buffer[i][1] + '\n')
  119.     scoreboard.close()
  120.  
  121. def generate_circle_random_pos():
  122.     x = randint(CIRCLE_RADIUS, WINDOW_WIDTH-CIRCLE_RADIUS)
  123.     y = randint(CIRCLE_RADIUS, WINDOW_HEIGHT-CIRCLE_RADIUS)
  124.     return Circle(x, y, CIRCLE_RADIUS, colors[randint(0, len(colors)-1)], 0, 0, randint(0, 5))
  125.  
  126.  
  127. def generate_circles(num_circles):
  128.     circles = []
  129.     cnt = 0
  130.     MAX_CNT = 500
  131.     while len(circles) < num_circles:
  132.         new_circle = generate_circle_random_pos()
  133.         if player.check_min_distance(new_circle, MIN_DISTANCE):
  134.             continue
  135.         has_intersection = False
  136.         for c in circles:
  137.             if c.intersects(new_circle):
  138.                 has_intersection = True
  139.                 break
  140.         if not has_intersection:
  141.             circles.append(new_circle)
  142.         cnt += 1
  143.         if cnt == MAX_CNT:
  144.             circles.clear()
  145.     return circles
  146.  
  147.  
  148. player = Circle(round(WINDOW_WIDTH/2), round(WINDOW_HEIGHT/2),
  149.                 PLAYER_SIZE, GREEN, 0, 0, 0)
  150.  
  151. circles = generate_circles(CIRCLES_NUM)
  152.  
  153. DX = [-MOVESPEED, 0, MOVESPEED, 0, -MOVESPEED, MOVESPEED, MOVESPEED, -MOVESPEED]
  154. DY = [0, -MOVESPEED, 0, MOVESPEED, -MOVESPEED, -MOVESPEED, MOVESPEED, MOVESPEED]
  155.  
  156. moveLeft = False
  157. moveRight = False
  158. moveUp = False
  159. moveDown = False
  160.  
  161. pause = False
  162.  
  163. while not gameover:
  164.     for event in pygame.event.get():    
  165.         if event.type == QUIT:
  166.             pygame.quit()
  167.             sys.exit()
  168.  
  169.         elif event.type == KEYDOWN:
  170.             if event.key == K_w or event.key == K_UP:
  171.                 moveDown = False
  172.                 moveUp = True
  173.             elif event.key == K_a or event.key == K_LEFT:
  174.                 moveRight = False
  175.                 moveLeft = True
  176.             elif event.key == K_s or event.key == K_DOWN:
  177.                 moveUp = False
  178.                 moveDown = True
  179.             elif event.key == K_d or event.key == K_RIGHT:
  180.                 moveLeft = False
  181.                 moveRight = True
  182.  
  183.         elif event.type == KEYUP:
  184.             if event.key == K_w or event.key == K_UP:
  185.                 moveUp = False
  186.             elif event.key == K_a or event.key == K_LEFT:
  187.                 moveLeft = False
  188.             elif event.key == K_s or event.key == K_DOWN:
  189.                 moveDown = False
  190.             elif event.key == K_d or event.key == K_RIGHT:
  191.                 moveRight = False
  192.             elif event.key==K_SPACE:
  193.                 pause = True
  194.                 moveLeft = False
  195.                 moveRight = False
  196.                 moveUp = False
  197.                 moveDown = False
  198.  
  199.     while pause == True:
  200.         for event in pygame.event.get():
  201.             if event.type==KEYUP:
  202.                 if event.key==K_SPACE:
  203.                     pause = False
  204.  
  205.     score += 1
  206.     if score % 20 == 0:
  207.         player.radius += 1
  208.  
  209.     #windowSurface.fill(GRAY)
  210.    
  211.     windowSurface.blit(background_image, [0, 0])
  212.     player_stretched_image = pygame.transform.scale(player_image, (player.radius*2, player.radius*2))
  213.     playerpos = pygame.Rect(0, 0, player.radius*2, player.radius*2)
  214.     playerpos.center = (player.x, player.y)
  215.  
  216.     DrawText(f'Score: {score}', font, windowSurface, round(WINDOW_WIDTH/2), round(WINDOW_HEIGHT/10), True)
  217.  
  218.     if moveDown and player.y + player.radius < WINDOW_HEIGHT:
  219.         player.y += MOVESPEED*2
  220.     if moveUp and player.y - player.radius > 0:
  221.         player.y -= MOVESPEED*2
  222.     if moveLeft and player.x - player.radius > 0:
  223.         player.x -= MOVESPEED*2
  224.     if moveRight and player.x + player.radius < WINDOW_WIDTH:
  225.         player.x += MOVESPEED*2
  226.  
  227.     for c in circles:
  228.         if c.intersects(player):
  229.             gameover = True
  230.             update_leaderboard()
  231.             break
  232.  
  233.     new_circles = []
  234.  
  235.     while True:
  236.         global_ok = True
  237.         for c in circles:
  238.             cand = Circle(c.x, c.y, CIRCLE_RADIUS, c.color,
  239.                           c.duration, c.direction , c.sprite)
  240.             chance = randint(1, 100)
  241.             if chance >= 99:
  242.                 cand.set_random_dir()
  243.             cand.make_move()
  244.             ok = True
  245.             for c2 in new_circles:
  246.                 if c2.intersects(cand):
  247.                     ok = False
  248.                     break
  249.             for c2 in circles:
  250.                 if c2 != c and c2.intersects(cand):
  251.                     ok = False
  252.                     break
  253.             cnt = 0
  254.             MAX_CNT = 50
  255.             while not ok:
  256.                 cand = Circle(c.x, c.y, CIRCLE_RADIUS, c.color,
  257.                               c.duration, c.direction, c.sprite)
  258.                 cand.set_random_dir()
  259.                 cand.make_move()
  260.                 ok = True
  261.                 for c2 in new_circles:
  262.                     if c2.intersects(cand):
  263.                         ok = False
  264.                         break
  265.                 for c2 in circles:
  266.                     if c2 != c and c2.intersects(cand):
  267.                         ok = False
  268.                         break
  269.                 cnt += 1
  270.                 if cnt == MAX_CNT:
  271.                     break
  272.             if cnt == MAX_CNT:
  273.                 new_circles.clear()
  274.                 global_ok = False
  275.                 break
  276.             new_circles.append(cand)
  277.         if global_ok:
  278.             break
  279.     circles = new_circles
  280.     for c in circles:
  281.         #pygame.draw.circle(windowSurface, c.color,
  282.                            #(c.x, c.y), c.radius, THICKNESS)
  283.         minion_stretched_image = pygame.transform.scale(minions[c.sprite], (CIRCLE_RADIUS*2, CIRCLE_RADIUS*2))
  284.         minionpos = pygame.Rect(0, 0, CIRCLE_RADIUS*2, CIRCLE_RADIUS*2)
  285.         minionpos.center = (c.x, c.y)
  286.         windowSurface.blit(minion_stretched_image, minionpos)
  287.     #pygame.draw.circle(windowSurface, player.color,
  288.                        #(player.x, player.y), player.radius, 0)
  289.     windowSurface.blit(player_stretched_image, playerpos)
  290.     pygame.display.update()
  291.     fps.tick(TICK)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement