1

I want to make a little snake game with Pygame. I've got a apple class but when I want to create the apple object I get that error: "TypeError: 'module' object is not callable"

I have got absolutely no idea how I can fix it

Class:

class Apple: 
    def __init__(self, surface, row, colum, scaller):
        self.surface = surface
        self.scaller = scaller
        self.x = int(map1d(row, 0, self.scaller, 0, width))
        self.y = int(map1d(colum, 0, self.scaller, 0, height))

    def show(self):
        pygame.draw.rect(self.surface, (255, 0, 0), (self.x, self.y), width / self.scaller / 2, height / self.scaler / 2)

map1d function:

def map1d(value, min_, max_, scalledMin, scalledMax):
    scaller = interp1d([min_, max_], [scalledMin, scalledMax])
    return scaller(value)

Main:

if __name__ == "__main__":
    width = 700
    height = 700
    scaller = 35

    win = pygame.display.set_mode((width, height))

    #here I'm getting the error
    apple = Apple(win, random(scaller - 1), random(scaller - 1),                               scaller)
Traceback (most recent call last):
  File "sketch.py", line 48, in <module>
    apple = Apple(win, random(scaller - 1), random(scaller - 1), scaller)
TypeError: 'module' object is not callable
  • 1
    I assume that you have imported the `random` module like so: `import random`. If that is the case, to generate a random number up to `scaller - 1` you will need `random.random(scaller - 1)` – Hoog Sep 12 '19 at 19:26
  • 1
    then I get this error: " File "sketch.py", line 48, in apple = Apple(win, random.random(scaller - 1), random.random(scaller - 1), scaller) TypeError: random() takes no arguments (1 given)" – Gian Laager Sep 12 '19 at 19:42
  • Getting closer! This one is my fault, I forgot exactly how to random. IF you are looking for a random integer from 0 to `scaller-1` then use `randint`: `random.randint(0,scaller-1)`. IF you are looking for any real number between 0 and `scaller-1` use `random()` without arguments to get a number in [0,1] and multiply by `scaller-1` like so: `random.random()*(scaller-1)`. – Hoog Sep 12 '19 at 19:49
  • 1
    Thanks for helping now it's working – Gian Laager Sep 12 '19 at 19:54

0 Answers0