Plotting terrain pixels with PyGame based on random NumPy array












3












$begingroup$


I am experimenting with Perlin Noise and random map generation. I have a 2D numpy ndarray full of 16-bit floats called map_list that I call from the singleton ST. It has 900 rows with 1600 elements each. I am iterating through it to display different colored pixels to represent terrain at different points in the map. Array values in different ranges produce pixels of different colors. The total possible range is [0, 1] because I am normalizing the values.



My question is, is there a way to do this faster than I am capable of now?



"""

This file holds functions that modify pyGame surfaces.

"""

from __future__ import division
from singleton import ST
from pygame import gfxdraw


def display_map(surface):
"""
This takes in a pyGame surface, and draws colored tiles on it according to
the values in ST.map_list. The higher the value, the lighter the shade of
the tile.

:param surface: A pyGame surface.
"""

x_pos = 0
y_pos = 0

for y in range(len(ST.map_list)):

for x in range(len(ST.map_list[y])):
noise_value = ST.map_list[y][x]
shade = int(noise_value * 255)
color = __color_tiles(noise_value, shade)

gfxdraw.pixel(surface, x_pos, y_pos, color)
x_pos += ST.TILE_SIZE

x_pos = 0
y_pos += ST.TILE_SIZE


def __color_tiles(noise_value, shade):
"""
Treat this function as private. It should only be called by functions and
methods within this file. It returns a 3-element 1D tuple that represents
the rgb color values to display the tile as.

:param noise_value: The noise value at a specific point in ST.map_list.
:param shade: How dark or light the tile should be.
:return: tuple
"""

if noise_value < ST.WATER_LEVEL:
rgb = (shade, shade, 255)
elif noise_value > ST.MOUNTAIN_LEVEL:
rgb = (shade, shade, shade)
else:
rgb = (shade, 255, shade)

return rgb


What it generates



enter image description here










share|improve this question











$endgroup$

















    3












    $begingroup$


    I am experimenting with Perlin Noise and random map generation. I have a 2D numpy ndarray full of 16-bit floats called map_list that I call from the singleton ST. It has 900 rows with 1600 elements each. I am iterating through it to display different colored pixels to represent terrain at different points in the map. Array values in different ranges produce pixels of different colors. The total possible range is [0, 1] because I am normalizing the values.



    My question is, is there a way to do this faster than I am capable of now?



    """

    This file holds functions that modify pyGame surfaces.

    """

    from __future__ import division
    from singleton import ST
    from pygame import gfxdraw


    def display_map(surface):
    """
    This takes in a pyGame surface, and draws colored tiles on it according to
    the values in ST.map_list. The higher the value, the lighter the shade of
    the tile.

    :param surface: A pyGame surface.
    """

    x_pos = 0
    y_pos = 0

    for y in range(len(ST.map_list)):

    for x in range(len(ST.map_list[y])):
    noise_value = ST.map_list[y][x]
    shade = int(noise_value * 255)
    color = __color_tiles(noise_value, shade)

    gfxdraw.pixel(surface, x_pos, y_pos, color)
    x_pos += ST.TILE_SIZE

    x_pos = 0
    y_pos += ST.TILE_SIZE


    def __color_tiles(noise_value, shade):
    """
    Treat this function as private. It should only be called by functions and
    methods within this file. It returns a 3-element 1D tuple that represents
    the rgb color values to display the tile as.

    :param noise_value: The noise value at a specific point in ST.map_list.
    :param shade: How dark or light the tile should be.
    :return: tuple
    """

    if noise_value < ST.WATER_LEVEL:
    rgb = (shade, shade, 255)
    elif noise_value > ST.MOUNTAIN_LEVEL:
    rgb = (shade, shade, shade)
    else:
    rgb = (shade, 255, shade)

    return rgb


    What it generates



    enter image description here










    share|improve this question











    $endgroup$















      3












      3








      3





      $begingroup$


      I am experimenting with Perlin Noise and random map generation. I have a 2D numpy ndarray full of 16-bit floats called map_list that I call from the singleton ST. It has 900 rows with 1600 elements each. I am iterating through it to display different colored pixels to represent terrain at different points in the map. Array values in different ranges produce pixels of different colors. The total possible range is [0, 1] because I am normalizing the values.



      My question is, is there a way to do this faster than I am capable of now?



      """

      This file holds functions that modify pyGame surfaces.

      """

      from __future__ import division
      from singleton import ST
      from pygame import gfxdraw


      def display_map(surface):
      """
      This takes in a pyGame surface, and draws colored tiles on it according to
      the values in ST.map_list. The higher the value, the lighter the shade of
      the tile.

      :param surface: A pyGame surface.
      """

      x_pos = 0
      y_pos = 0

      for y in range(len(ST.map_list)):

      for x in range(len(ST.map_list[y])):
      noise_value = ST.map_list[y][x]
      shade = int(noise_value * 255)
      color = __color_tiles(noise_value, shade)

      gfxdraw.pixel(surface, x_pos, y_pos, color)
      x_pos += ST.TILE_SIZE

      x_pos = 0
      y_pos += ST.TILE_SIZE


      def __color_tiles(noise_value, shade):
      """
      Treat this function as private. It should only be called by functions and
      methods within this file. It returns a 3-element 1D tuple that represents
      the rgb color values to display the tile as.

      :param noise_value: The noise value at a specific point in ST.map_list.
      :param shade: How dark or light the tile should be.
      :return: tuple
      """

      if noise_value < ST.WATER_LEVEL:
      rgb = (shade, shade, 255)
      elif noise_value > ST.MOUNTAIN_LEVEL:
      rgb = (shade, shade, shade)
      else:
      rgb = (shade, 255, shade)

      return rgb


      What it generates



      enter image description here










      share|improve this question











      $endgroup$




      I am experimenting with Perlin Noise and random map generation. I have a 2D numpy ndarray full of 16-bit floats called map_list that I call from the singleton ST. It has 900 rows with 1600 elements each. I am iterating through it to display different colored pixels to represent terrain at different points in the map. Array values in different ranges produce pixels of different colors. The total possible range is [0, 1] because I am normalizing the values.



      My question is, is there a way to do this faster than I am capable of now?



      """

      This file holds functions that modify pyGame surfaces.

      """

      from __future__ import division
      from singleton import ST
      from pygame import gfxdraw


      def display_map(surface):
      """
      This takes in a pyGame surface, and draws colored tiles on it according to
      the values in ST.map_list. The higher the value, the lighter the shade of
      the tile.

      :param surface: A pyGame surface.
      """

      x_pos = 0
      y_pos = 0

      for y in range(len(ST.map_list)):

      for x in range(len(ST.map_list[y])):
      noise_value = ST.map_list[y][x]
      shade = int(noise_value * 255)
      color = __color_tiles(noise_value, shade)

      gfxdraw.pixel(surface, x_pos, y_pos, color)
      x_pos += ST.TILE_SIZE

      x_pos = 0
      y_pos += ST.TILE_SIZE


      def __color_tiles(noise_value, shade):
      """
      Treat this function as private. It should only be called by functions and
      methods within this file. It returns a 3-element 1D tuple that represents
      the rgb color values to display the tile as.

      :param noise_value: The noise value at a specific point in ST.map_list.
      :param shade: How dark or light the tile should be.
      :return: tuple
      """

      if noise_value < ST.WATER_LEVEL:
      rgb = (shade, shade, 255)
      elif noise_value > ST.MOUNTAIN_LEVEL:
      rgb = (shade, shade, shade)
      else:
      rgb = (shade, 255, shade)

      return rgb


      What it generates



      enter image description here







      python performance python-2.x numpy pygame






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 29 mins ago









      200_success

      130k17153419




      130k17153419










      asked 8 hours ago









      LuminousNutriaLuminousNutria

      1415




      1415






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215575%2fplotting-terrain-pixels-with-pygame-based-on-random-numpy-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215575%2fplotting-terrain-pixels-with-pygame-based-on-random-numpy-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin