1

im attempting to load single digit numbers line by line from file in python this code makes an error saying:

line 27, in <module>
environment.environment1.load_map(environmentVector)
TypeError: load_map() takes 1 positional argument but 2 were given

heres the source code: (main.py)

environmentVector = []
environment.environment1 = environment.environment(160, 100, 32, 32)
environment.environment1.load_map(environmentVector)

environment.py:

    def load_map(environmentVector):
        string = ''
        with open('map.txt', 'r') as f:
            for line in f:
                string = f.readline()
                row = []
                for character in string:
                    if character == '0':
                        pass
                    elif character == '1':
                        environmentVector.append(environment)
travisp4
  • 27
  • 5
  • Do you have an example of this map.txt data? –  Nov 27 '18 at 05:07
  • 0 0 0 0 0 0 0 0 1 – travisp4 Nov 27 '18 at 05:08
  • zeroes and ones with 1 space between is whats in the map.txt theres about 10 lines of them – travisp4 Nov 27 '18 at 05:08
  • What's your desired output? –  Nov 27 '18 at 05:10
  • i want to store environment objects in a list (each environment having x,y coordinates) to make a map with boundaries where if the player runs into environment the collision will prevent them from walking through the rectangle – travisp4 Nov 27 '18 at 05:14
  • https://stackoverflow.com/questions/14676265/how-to-read-a-text-file-into-a-list-or-an-array-with-python see here, looks similar. –  Nov 27 '18 at 05:18

1 Answers1

1

First things first, if load_map is a method of a class, the first argument it needs to take is self. Either add self, or mark load_map as a static method with @staticmethod See this.

About your load_map function: Instead of iterating through the string, split it. It also makes sense to store a map like this to a 2D list. Try this:

def load_map(self, environmentVector):
    with open('map.txt', 'r') as f:
        for line in f:
            nums = list(map(int, line.split()))
            environmentVector.append(nums)
iz_
  • 15,923
  • 3
  • 25
  • 40