S
S
Staffic2020-08-17 21:46:14
Python
Staffic, 2020-08-17 21:46:14

Problems in Python code?

def create_map():
    global obstacles
    locations = []
    for i in range(10):
        row = random.randint(0, 9)
        col = random.randint(0, 9)
        location = [col * 64 + 20, row * 64 + 20 + 640]
        if not (location in locations):
            locations.append(location)
            type = random.choice(["tree", "flag"])
            if type == "tree": img = ("d:\Python\skier_tree.png")
            elif type == "flag": img = ("d:\Python\skier_flag.png")
            obstacle = ObstacleClass(img, location, type)
            obstacle.add(obstacle)

Part of the code from the book "Hello, World!", the game Skier. Gives an error message -
Traceback (most recent call last):
  File "D:\Game Skier - 1.py", line 76, in <module>
    create_map()
  File "D:\Game Skier - 1.py", line 59, in create_map
    obstacle.add(obstacle)
  File "D:\Python\lib\site-packages\pygame\sprite.py", line 142, in add
    self.add(*group)
TypeError: add() argument after * must be an iterable, not ObstacleClass

How to deal with it? And where do the pictures come from if you enter - pygame.image.load # This is just an example
Thank you so much in advance for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery Mamontov, 2020-08-18
@vmamontov

Staffic Good day!
First, indent your code.
And then you made a mistake on the last line. Compare your code and code from the book.

def create_map():
    global obstacles
    locations = []
    for i in range(10):
    row = random.randint(0, 9)
    col = random.randint(0, 9)
    location = [col * 64 + 20, row * 64 + 20 + 640]
    if not (location in locations):
        locations.append(location)
        type = random.choice(["tree", "flag"])
        if type == "tree": img = "skier_tree.png"
        elif type == "flag": img = "skier_flag.png"
        obstacle = ObstacleClass(img, location, type)
        obstacles.add(obstacle)

Here is the code from the book and all the files.

S
Staffic, 2020-08-18
@Staffic

Thank you
so much!) And another question, where to store the images that you write in the code?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question