Advertisement
Guest User

owo

a guest
Jan 26th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. from noise import pnoise2
  2. import pygame
  3. from random import randint
  4.  
  5. def generate2dperlin(offsetX, offsetY,x,y,octaves):
  6. noiseMap = []
  7. for i in range(offsetX,x+offsetX):
  8. n = []
  9. for j in range(offsetY,y+offsetY):
  10. n.append((pnoise2((i+offsetX)/x,(j+offsetY)/y,octaves, 0.5, 2) + 1) )
  11. noiseMap.append(n)
  12.  
  13. return noiseMap
  14.  
  15.  
  16. x = 100
  17. y = 100
  18.  
  19. octaves = 8
  20. offsetX = randint(-100000,100000)
  21. offsetY = randint(-100000,100000)
  22. print(offsetX, offsetY)
  23.  
  24. offsetValues = [163, 152, 140, 137, 126, 10] #list of color values
  25. currentOffsetValue = 0
  26.  
  27. noiseMap = generate2dperlin(offsetX, offsetY,x,y,octaves)
  28.  
  29. white = (255,255,255)
  30. green = (58,95,11)
  31. blue = (0, 119, 190)
  32. yellow = (237, 201, 175)
  33. grey = (155, 155, 155)
  34. dark_blue = (0, 80, 120)
  35.  
  36. #pgm
  37.  
  38. screen_x = 600
  39. screen_y = 600
  40.  
  41. maxX = 0#how much of the screen will be ofsetted
  42.  
  43.  
  44. rectSize = screen_x//x
  45.  
  46. window = pygame.display.set_mode((screen_x, screen_y))
  47. pygame.display.set_caption('perlin')
  48.  
  49. screen = pygame.Surface((screen_x, screen_y))
  50.  
  51. done = True
  52. while done:
  53. keys = pygame.key.get_pressed()
  54. if keys[pygame.K_RIGHT]:
  55. offsetX += 1
  56. if keys[pygame.K_LEFT]:
  57. offsetX -= 1
  58. if keys[pygame.K_UP]:
  59. offsetY -= 1
  60. if keys[pygame.K_DOWN]:
  61. offsetY += 1
  62. noiseMap = generate2dperlin(int(offsetX), int(offsetY),x,y,octaves)
  63.  
  64. for e in pygame.event.get():
  65. if e.type == pygame.QUIT:
  66. done = False
  67. if e.type == pygame.KEYDOWN:
  68. if e.key == pygame.K_w:
  69. octaves += 1
  70. if e.key == pygame.K_s:
  71. octaves -= 1 * (octaves > 1)
  72.  
  73. if e.key == pygame.K_d:
  74. currentOffsetValue += 1 * (currentOffsetValue < len(offsetValues))
  75. print('Ты на ',currentOffsetValue, 'элементе')
  76. if e.key == pygame.K_a:
  77. currentOffsetValue -= 1 * (currentOffsetValue > 0)
  78. print('Ты на ',currentOffsetValue, 'элементе')
  79.  
  80.  
  81.  
  82. noiseMap = generate2dperlin(int(offsetX), int(offsetY),x,y,octaves)
  83. #print(octaves)
  84.  
  85. if e.type == pygame.MOUSEBUTTONDOWN:
  86. if e.button == 4:
  87. offsetValues[currentOffsetValue] += 0.5
  88. if e.button == 5:
  89. offsetValues[currentOffsetValue] -= 0.5
  90.  
  91. noiseMap = generate2dperlin(offsetX, offsetY,x,y,octaves)
  92. print(offsetValues)
  93.  
  94. screen.fill(white)
  95.  
  96.  
  97.  
  98. for i in range (0, x-maxX):
  99. for j in range (0, y):
  100. sColor = noiseMap[i][j] * 137.5
  101.  
  102. if (sColor > offsetValues[0]):
  103. color = white
  104. elif (sColor > offsetValues[1]):
  105. color = grey
  106. elif (sColor > offsetValues[2]):
  107. color = green
  108. elif (sColor > offsetValues[3]):
  109. color = yellow
  110. elif (sColor > offsetValues[4]):
  111. color = blue
  112. elif (sColor > offsetValues[5]):
  113. color = dark_blue
  114. else:
  115. color = white
  116.  
  117. pygame.draw.rect(screen, color, (i*rectSize, j*rectSize, rectSize, rectSize))
  118.  
  119.  
  120.  
  121. window.blit(screen, (0, 0))
  122. pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement