Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Python Problem  (Read 934 times)

Eagle_eye

  • Bay Watcher
    • View Profile
Python Problem
« on: August 10, 2011, 09:36:39 pm »

Could any programmers more experienced than myself help me with an issue? I've got an array holding 1's and 0's, to represent water and land tiles, for the moment. I have a function that will read the value in the array, and return the proper color to make the block. unfortunately, after a certain point, it ceases to return values properly, and simply paints it all one color.

color getting function:
Code: [Select]

def getblock(block):
    returnvalue = -1
    if block == 0:
        returnvalue = (0,255,0)
    elif block == 1:
        returnvalue = (0,0,255)
    return returnvalue

generation:
Code: [Select]
import pygame
import random
import numpy
import blocks

       
       

class Plane:
    def __init__(self,size):
        self.block = numpy.zeros((size,size),numpy.int32)
        for y in range(len(self.block)):
            for x in range(len(self.block)):
                self.block[x][y] = random.randint(0,1)
        print (self.block)

    def display(self,screen):
        xpos = 0
        ypos = 0
        for y in range(len(self.block)):
            for x in range(len(self.block)):
                pygame.draw.rect(screen, (blocks.getblock(self.block[x][y])),(xpos,ypos,xpos+16,ypos+16))
                xpos = xpos + 16
            ypos = ypos + 16
            xpos = 0
Logged

nogoodnames

  • Bay Watcher
    • View Profile
Re: Python Problem
« Reply #1 on: August 11, 2011, 11:20:53 pm »

I don't use numpy or pygame but if I understand what it's doing then I see no problems with your code.

I made some quick and messy modifications for me to test and it seems to be working fine.

The only causes I can think of are a quirk in numpy or pygame I'm not aware of or just random numbers being random. Try running it a few times.

Also, at what point does it stop alternating colours and what colour does it paint everything afterward? That could be important.
Logged
Life is, in a word, volcanoes.
                        - Random human lord

Chris_24

  • Bay Watcher
    • View Profile
Re: Python Problem
« Reply #2 on: August 12, 2011, 01:45:06 am »

Correct me if I'm wrong, but wouldn't randint(0, 1) always produce a 0? Shouldn't it be randint(0, 2)?
Logged

Kay12

  • Bay Watcher
  • Fighting for Elite Liberal values since 2009!
    • View Profile
Re: Python Problem
« Reply #3 on: August 12, 2011, 07:01:48 am »

No. randint from Python is inclusive in both ways - a <= randint(a, b) <= b so randint(0, 1) can produce either 0 or 1.

According to the official documentation at least.
Logged
Try Liberal Crime Squad, an excellent Liberal Crime adventure game by Toady One and the open source community!
LCS in SourceForge - LCS Wiki - Forum thread for 4.04

olemars

  • Bay Watcher
    • View Profile
Re: Python Problem
« Reply #4 on: August 12, 2011, 07:16:10 am »

Some example output from the print function and a screenshot of the result would be interesting here. Not very familiar with python but I Can't see anything glaringly wrong in the code either.
Logged

Eagle_eye

  • Bay Watcher
    • View Profile
Re: Python Problem
« Reply #5 on: August 12, 2011, 09:28:47 am »

well,I won't have access to the computer with python on it for about a week, but the upper left corner is always entirely randomized, whereas the upper right and bottom left follow the same pattern, but each row is the same color all the way through, and the bottom right is a single block of color.  The colors are as random as you would expect.
Logged

olemars

  • Bay Watcher
    • View Profile
Re: Python Problem
« Reply #6 on: August 12, 2011, 09:55:26 am »

Looked a little bit in pygame docs, and pygame.Rect has the arguments (top left x, top left y, width, height), not (top left x, top left y, bottom right x, bottom right y).

So
Code: [Select]
pygame.draw.rect(screen, (blocks.getblock(self.block[x][y])),(xpos,ypos,xpos+16,ypos+16))should be
Code: [Select]
pygame.draw.rect(screen, (blocks.getblock(self.block[x][y])),(xpos,ypos,16,16))
Note also that you're technically drawing your array in transposed order, although in this case it's only cosmetic.
« Last Edit: August 12, 2011, 10:01:32 am by olemars »
Logged