Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import seed
- from random import randint
- import datetime
- import pygame
- import sys
- import time
- import math
- import os
- from pygame.locals import *
- seed(time)
- def DrawText(text, font, surface_menu, x, y, selected=False):
- textobj = font.render(text, 1, font_color)
- textrect = textobj.get_rect()
- textrect.center = (x, y)
- windowSurface.blit(textobj, textrect)
- pygame.init()
- GOLDEN = (255, 255, 153)
- WHITE = (255, 255, 255)
- GRAY = (51, 51, 51)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- BLACK = (0, 0, 0)
- CYAN = (0, 255, 255)
- MAGENTA = (255, 0, 255)
- AZURE = (240, 255, 255)
- OLIVE = (128, 128, 0)
- colors = [RED, GREEN, BLUE, CYAN, MAGENTA, AZURE, OLIVE, GOLDEN]
- WINDOW_WIDTH = 1920
- WINDOW_HEIGHT = 1200
- font_color = GOLDEN
- font = pygame.font.SysFont("comicsansms", 72)
- windowSurface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), pygame.FULLSCREEN, 0, 32)
- pygame.display.set_caption('Circle Game')
- fps = pygame.time.Clock()
- CIRCLE_RADIUS = 75
- PLAYER_SIZE = 50
- CIRCLES_NUM = 5
- MOVESPEED = 5
- THICKNESS = 15
- MIN_DISTANCE = 200
- score = 0
- gameover = False
- class Circle(object):
- def __init__(self, x, y, radius, color, duration, direction):
- self.x = x
- self.y = y
- self.radius = radius
- self.color = color
- self.duration = duration
- self.direction = direction
- def intersects(self, circle):
- return ((self.x - circle.x)**2 + (self.y - circle.y)**2 <
- (self.radius + circle.radius)**2)
- def check_min_distance(self, circle, min_distance):
- return ((self.x - circle.x)**2 + (self.y - circle.y)**2 <
- (self.radius + circle.radius + min_distance)**2)
- def make_move(self):
- if self.duration == 0:
- self.set_random_dir()
- while (self.x + DX[self.direction] + self.radius >= WINDOW_WIDTH or
- self.x + DX[self.direction] - self.radius <= 0 or
- self.y + DY[self.direction] + self.radius >= WINDOW_HEIGHT or
- self.y + DY[self.direction] - self.radius <= 0):
- self.set_random_dir()
- self.x += DX[self.direction]
- self.y += DY[self.direction]
- self.duration -= 1
- def set_random_dir(self):
- self.duration = randint(50, 150)
- check = self.direction
- self.direction = randint(0, 7)
- while check == self.direction:
- self.direction = randint(0, 7)
- def update_leaderboard():
- buffer = []
- timestamp = datetime.datetime.now()
- now = timestamp.strftime("%Y-%m-%d %H:%M:%S")
- scoreboard = open("scoreboard.txt", "a")
- scoreboard.write(f"{score} | {now}")
- scoreboard.close()
- with open("scoreboard.txt", "r") as scoreboard:
- x = scoreboard.readlines()
- for i in range(len(x)):
- currentline = x[i]
- element = currentline.strip().split(' | ')
- buffer.append((int(element[0]), element[1]))
- print(buffer)
- buffer.sort(reverse = True)
- print(buffer)
- file = open("scoreboard.txt", "w")
- file.close()
- scoreboard = open("scoreboard.txt", "a")
- for i in range(len(buffer)):
- scoreboard.write(str(buffer[i][0]) + " | " + buffer[i][1] + '\n')
- scoreboard.close()
- def generate_circle_random_pos():
- x = randint(CIRCLE_RADIUS, WINDOW_WIDTH-CIRCLE_RADIUS)
- y = randint(CIRCLE_RADIUS, WINDOW_HEIGHT-CIRCLE_RADIUS)
- return Circle(x, y, CIRCLE_RADIUS, colors[randint(0, len(colors)-1)], 0, 0)
- def generate_circles(num_circles):
- circles = []
- cnt = 0
- MAX_CNT = 500
- while len(circles) < num_circles:
- new_circle = generate_circle_random_pos()
- if player.check_min_distance(new_circle, MIN_DISTANCE):
- continue
- has_intersection = False
- for c in circles:
- if c.intersects(new_circle):
- has_intersection = True
- break
- if not has_intersection:
- circles.append(new_circle)
- cnt += 1
- if cnt == MAX_CNT:
- circles.clear()
- return circles
- player = Circle(round(WINDOW_WIDTH/2), round(WINDOW_HEIGHT/2),
- PLAYER_SIZE, GREEN, 0, 0,)
- circles = generate_circles(CIRCLES_NUM)
- DX = [-MOVESPEED, 0, MOVESPEED, 0, -MOVESPEED, MOVESPEED, MOVESPEED, -MOVESPEED]
- DY = [0, -MOVESPEED, 0, MOVESPEED, -MOVESPEED, -MOVESPEED, MOVESPEED, MOVESPEED]
- moveLeft = False
- moveRight = False
- moveUp = False
- moveDown = False
- while not gameover:
- score += 1
- if score % 20 == 0:
- player.radius += 1
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == KEYDOWN:
- if event.key == K_w or event.key == K_UP:
- moveDown = False
- moveUp = True
- elif event.key == K_a or event.key == K_LEFT:
- moveRight = False
- moveLeft = True
- elif event.key == K_s or event.key == K_DOWN:
- moveUp = False
- moveDown = True
- elif event.key == K_d or event.key == K_RIGHT:
- moveLeft = False
- moveRight = True
- elif event.type == KEYUP:
- if event.key == K_w or event.key == K_UP:
- moveUp = False
- elif event.key == K_a or event.key == K_LEFT:
- moveLeft = False
- elif event.key == K_s or event.key == K_DOWN:
- moveDown = False
- elif event.key == K_d or event.key == K_RIGHT:
- moveRight = False
- windowSurface.fill(GRAY)
- DrawText(f'Score: {score}', font, windowSurface, round(WINDOW_WIDTH/2), round(WINDOW_HEIGHT/10), True)
- if moveDown and player.y + player.radius < WINDOW_HEIGHT:
- player.y += MOVESPEED*2
- if moveUp and player.y - player.radius > 0:
- player.y -= MOVESPEED*2
- if moveLeft and player.x - player.radius > 0:
- player.x -= MOVESPEED*2
- if moveRight and player.x + player.radius < WINDOW_WIDTH:
- player.x += MOVESPEED*2
- for c in circles:
- if c.intersects(player):
- gameover = True
- update_leaderboard()
- break
- new_circles = []
- while True:
- global_ok = True
- for c in circles:
- cand = Circle(c.x, c.y, CIRCLE_RADIUS, c.color,
- c.duration, c.direction)
- chance = randint(1, 100)
- if chance >= 99:
- cand.set_random_dir()
- cand.make_move()
- ok = True
- for c2 in new_circles:
- if c2.intersects(cand):
- ok = False
- break
- for c2 in circles:
- if c2 != c and c2.intersects(cand):
- ok = False
- break
- cnt = 0
- MAX_CNT = 50
- while not ok:
- cand = Circle(c.x, c.y, CIRCLE_RADIUS, c.color,
- c.duration, c.direction)
- cand.set_random_dir()
- cand.make_move()
- ok = True
- for c2 in new_circles:
- if c2.intersects(cand):
- ok = False
- break
- for c2 in circles:
- if c2 != c and c2.intersects(cand):
- ok = False
- break
- cnt += 1
- if cnt == MAX_CNT:
- break
- if cnt == MAX_CNT:
- new_circles.clear()
- global_ok = False
- break
- new_circles.append(cand)
- if global_ok:
- break
- circles = new_circles
- for c in circles:
- pygame.draw.circle(windowSurface, c.color,
- (c.x, c.y), c.radius, THICKNESS)
- pygame.draw.circle(windowSurface, player.color,
- (player.x, player.y), player.radius, 0)
- pygame.display.update()
- fps.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement