0
votes

My 10 year old son is trying to implement new feature a weather system in this 2d game he made base off tutorial with pygame but is running into a IdententationError issue.

I been helping him but I am still learning also, I think it is the random and the display but I don't know how to fix it since is one of his first approach to pygame.

He is try to have it generate random weather to send across the screen. He also wants to add collision detection with player down the line but he still learning that subject.

Here is his code: all files I also included the original finished untouched the full edited one

Problem area 1:

Getting IdententationError line 259 which is this one

randomNumber = random.randint(0,20)

4 lines down from here

#a list of resources
WEATHER = [CLOUD,RAIN,THUNDER,TORNADO]

#loop through each weather type and choose weather type for it to be
        #pick a random number between 0 and 20
        randomNumber = random.randint(0,20)
        #if a zero, then the weather is TORNADO
        if randomNumber == 0:
            WEATHER = TORNADO
        #water if the random number is a 1 or a 2
        elif randomNumber in [1,2]:
            WEATHER = THUNDER
        elif randomNumber in [3,4,5,6,7,8]:
            WEATHER = RAIN
        else:
            WEATHER = CLOUD

Problem area 2:

#display the weather
DISPLAYSURF.blit(textures[WEATHER].convert_alpha(),(weatherx,weathery))
#move the cloud to the left slightly
weatherx+=1
#if the weather has moved past the map
if weatherx > MAPWIDTH*TILESIZE:
    #pick a new position to place the weather
    weathery = random.randint(0,(MAPHEIGHT*TILESIZE) - 150)
    weatherx = -200

Below is the original cloud only system before he tried to add weather

#commented out the original cloud only weather system 
#display the cloud
DISPLAYSURF.blit(textures[CLOUD].convert_alpha(),(cloudx,cloudy))
#move the cloud to the left slightly
cloudx+=1
if the cloud has moved past the map
if cloudx > MAPWIDTH*TILESIZE:
    #pick a new position to place the cloud
    cloudy = random.randint(0,(MAPHEIGHT*TILESIZE) - 150)
    cloudx = -200

Other Parts Of the Code

But not the full code below we believe to be correct

I import in pygame and the random and set clock

import pygame, sys, random
from pygame.locals import *

fpsClock = pygame.time.Clock()

I load in the weather off screen

#weather position
weatherx = -200
weathery = 0

The Choices of Weather Types which I can add more down the line ex snow, etc

   #the number of each weather type that we have
weather =   {
                CLOUD    : 0,
                RAIN     : 0,
                THUNDER  : 0,
                TORNADO  : 0
                         }

This is how I add the texture or png images I left out the many others

DIRT    : pygame.image.load('C:\\Users\\Daddy\\Documents\\python\\working_34\\mincraft2d\\dirt.png'),

Some times in order to get it to work I have to show full path like above

    #a dictionary linking resources to textures
    textures =   {
                    DIRT    : pygame.image.load('dirt.png'),
                    GRASS   : pygame.image.load('grass.png'),
....

                    CLOUD   : pygame.image.load('cloud.png'),
                    RAIN    : pygame.image.load('rain.png'),
                    THUNDER : pygame.image.load('thunder.png'),
                    TORNADO : pygame.image.load('tornado.png')
             }

I get error at line 261 which begins with if randomNumber == 0:

    #pick a random number between 0 and 20
    randomNumber = random.randint(0,20)
    #if a zero, then the weather is TORNADO
    if randomNumber == 0:
        WEATHER = TORNADO
    #water if the random number is a 1 or a 2
    elif randomNumber in [1,2]:
        WEATHER = THUNDER
    elif randomNumber in [3,4,5,6,7,8]:
        WEATHER = RAIN
    else:
        WEATHER = CLOUD
1
Please describe what the problem you're having is - Foon
I get a indent error on line 261 I will add to above - john taylor
If you're getting an "IndentationError" . . . did you try checking the indentation? - imallett
I did try moving it around and testing it I think the problem goes further than that - john taylor
Make sure that there are no tabs, replace all tabs in the code with 4 spaces. - demented hedgehog

1 Answers

1
votes

Here's what the code looks like to me.. (for the first problem, line 259).

    #a list of resources
WEATHER = [CLOUD,RAIN,THUNDER,TORNADO]  # <-- problem here.

    #loop through each weather type and choose weather type for it to be
    #pick a random number between 0 and 20
    randomNumber = random.randint(0,20)
        #if a zero, then the weather is TORNADO
        if yada yada ...

You need to indent the line starting WEATHER or dedent the line starting randomNumber. Indentation is used in python for blocks of code. There's nothing between the WEATHER and randomNumber line (like an if statement or a while statement) that would mean the randomNumber line should be indented relative to the WEATHER line. Same with all the other indent problems you've got. There's a similar problem with the if line that follows randomNumber assignment. Get all your lines with the same depth of indentation unless there's a reason not to (e.g. the body of an if statement, but not the if statement itself).

To be clear.. what it should look like is this (all indented 4 spaces):

    #a list of resources
    WEATHER = [CLOUD,RAIN,THUNDER,TORNADO]  

    #loop through each weather type and choose weather type for it to be
    #pick a random number between 0 and 20
    randomNumber = random.randint(0,20)
    #if a zero, then the weather is TORNADO
    if yada yada ...

This will give you a little background http://www.python-course.eu/python3_blocks.php Also choosing a decent editor will often help find these bugs for you. Here's a list http://pedrokroger.net/choosing-best-python-ide/

Also good on you for taking the time to do this with your kid.