Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math, random, sys, time
- import pygame
- from pygame.locals import *
- from pygame import gfxdraw
- # exit the program
- def quit():
- for event in pygame.event.get():
- if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
- return True
- return False
- # define display surface
- W, H = 1280, 720
- HW, HH = W / 2, H / 2
- # initialise display
- pygame.init()
- FONT = pygame.font.SysFont(None, 72)
- DS = pygame.display.set_mode((W, H))
- pygame.display.set_caption("Tetris Clone")
- FPS = 120
- SPF = 1.00 / FPS
- # define some colors
- FUCHSIA = (255, 0, 255)
- PURPLE = (128, 0, 128)
- TEAL = ( 0, 128, 128)
- LIME = ( 0, 255, 0)
- GREEN = ( 0, 128, 0)
- OLIVE = (128, 128, 0)
- YELLOW = (255, 255, 0)
- ORANGE = (255, 165, 0)
- RED = (255, 0, 0)
- MAROON = (128, 0, 0)
- SILVER = (192, 192, 192)
- GRAY = (128, 128, 128)
- BLUE = ( 0, 0, 255)
- NAVY = ( 0, 0, 128)
- AQUA = ( 0, 255, 255)
- WHITE = (255, 255, 255)
- BLACK = ( 0, 0, 0)
- COLORS = [BLACK, WHITE, RED, GREEN, BLUE, PURPLE, YELLOW, ORANGE, GRAY, SILVER, AQUA, FUCHSIA, LIME, MAROON, NAVY, OLIVE, TEAL]
- # the areas of the display that need updating
- AREAS = [(0, 0, W, H)]
- # start FPS monitoring
- FPSTime = time.time()
- # PI = 3.1415926
- # PI * 2 = 6.2831852
- BIRD_SHAPE = [0, 150, 210]
- BIRD_SIZE = 10
- BIRD_SIGHT_RADIUS = 100
- BIRD_BLIND_SPOT_SIZE = 50 # is actually double
- VECTOR_TABLE = [[math.cos(math.radians(degrees)), math.sin(math.radians(degrees))] for degrees in range(360)]
- MAX_AVOIDANCE_SPEED = 3
- AVOIDANCE_THRESHOLD = 2.0 * MAX_AVOIDANCE_SPEED / (BIRD_SIGHT_RADIUS * (BIRD_SIGHT_RADIUS - 1))
- AVOIDANCE_SPEED_TABLE = []
- a = 0
- for index in range(BIRD_SIGHT_RADIUS):
- AVOIDANCE_SPEED_TABLE.append(a)
- a += AVOIDANCE_THRESHOLD * index
- AVOIDANCE_SPEED_TABLE.reverse()
- class bird:
- def __init__(s):
- global W, H
- s.x, s.y = random.randint(20, W - 40), random.randint(20, H - 40)
- s.angle = float(random.randint(0, 360))
- s.seen = False
- s.focus = False
- def draw(s):
- global DS
- global WHITE, RED
- global BIRD_SHAPE, BIRD_SIZE
- polygon = []
- for point_degrees in BIRD_SHAPE:
- angle = math.radians(s.angle + point_degrees)
- px = int(s.x + math.cos(angle) * BIRD_SIZE)
- py = int(s.y + math.sin(angle) * BIRD_SIZE)
- polygon.append([px, py])
- if s.focus:
- pygame.gfxdraw.filled_polygon(DS, polygon, RED)
- else:
- pygame.gfxdraw.filled_polygon(DS, polygon, WHITE)
- def move(s):
- global W, H
- global VECTOR_TABLE
- angle = int(s.angle)
- s.x += VECTOR_TABLE[angle][0]
- if s.x > W: s.x = 0
- if s.x < 0: s.x = W
- s.y += VECTOR_TABLE[angle][1]
- if s.y > H: s.y = 0
- if s.y < 0: s.y = H
- class birds:
- def __init__(s):
- s.aviary = list()
- def add(s, n):
- global bird
- for i in range(n):
- s.aviary.append(bird())
- def avoid(s):
- global BIRD_SIGHT_RADIUS, BIRD_BLIND_SPOT_SIZE
- for b in s.aviary:
- bbsal = (b.angle + 180 - BIRD_BLIND_SPOT_SIZE) % 360
- bbsah = (b.angle + 180 + BIRD_BLIND_SPOT_SIZE) % 360
- for tb in s.aviary:
- dist = math.hypot(b.x - tb.x, b.y - tb.y)
- if dist > BIRD_SIGHT_RADIUS: continue
- if b == tb: continue
- angle = math.degrees(math.atan2(b.y - tb.y, b.x - tb.x)) % 360
- if bbsal > bbsah:
- if angle > bbsal or angle < bbsah:
- continue
- elif angle < bbsah and angle > bbsal:
- continue
- if b.angle > angle:
- b.angle -= AVOIDANCE_SPEED_TABLE[int(dist)]
- else:
- b.angle += AVOIDANCE_SPEED_TABLE[int(dist)]
- b.angle %= 360
- #print polarity
- def do(s):
- for b in s.aviary:
- b.draw()
- b.move()
- s.avoid()
- b = birds()
- b.add(40)
- b.aviary[20].focus = True
- # main loop
- while not quit():
- frameTime = time.time() - FPSTime
- if frameTime < SPF: continue
- FPSTime += frameTime
- b.do()
- pygame.display.update(AREAS)
- DS.fill(BLACK)
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement