Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- from random import randint
- DW = DH = 400 # DisplayWidth, DisplayHeight
- HDW = int(DW / 2) # HalfDisplayWidth
- HDH = int(DH / 2) # HalfDisplayHeight
- #FPS = 60
- # colors
- RED = [255, 0, 0]
- class point:
- def __init__(s, x, y):
- s.x = x
- s.y = y
- def pos(s):
- return [s.x, s.y]
- pygame.init()
- PD = pygame.display.set_mode((DW, DH)) # PD = Primary Display
- pygame.display.set_caption("Worley Noise")
- # generate points
- NUMBER_OF_POINTS = 25
- points = [point(randint(0, DW), randint(0, DH)) for count in range(NUMBER_OF_POINTS)]
- # draw
- PA = pygame.PixelArray(PD) # get a pixel array of the primary display
- for x in range(DW):
- for y in range(DH): # go through the all the pixels starting at 0, 0 and ending at DW, DH
- distances = [] # this will store the distances between the points and x, y from the loops above
- for index in range(NUMBER_OF_POINTS): # index will reference the points in the points list
- p = points[index]
- distance = math.hypot(x - p.x, y - p.y) # get the distance
- distances.append(distance) # add the distance to the list
- n = 0 # this value selects which distance to use to render the noise value. Remember index 0 in the distances list is the closet point to x, y
- distances.sort() # sort the distances so the closest is at index 0
- noise = int((distances[n] / (HDW / 2)) * 255) # hard to explain this but it's trying to keep the value noise less than 255
- if noise > 255: noise = 255 # if the noise value is greater than 255 then limit it to 255
- PA[x][y] = PD.map_rgb(noise, noise, noise) # apply the noise value to the pixel at x, y
- PA.close() # close the pixel array
- del PA # and delete (something about memory bleed)
- # display the points
- for p in points:
- pygame.draw.circle(PD, RED, p.pos(), 2) # 2 is the radius
- pygame.display.update()
- # press escape to get out!
- while True:
- e = pygame.event.get()
- if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement