Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Location
    Dallas, TX
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with the ever popular "skier" program... NoneType object not iterable

    I'm just starting out, going through an easy book. I'm having a problem with a "skier" program that it had in the book. When I run the provided file, it works fine, but when I run the copy that I typed out, I get an error. I can't find any differences between the two versions of code in or around the lines the Traceback mentions. Any idea where else I should look? Thanks for any help you can offer.

    Here's the error that I'm getting:

    Code:
    Traceback (most recent call last):
      File "/Users/jacobdanks/Desktop/eBooks/skier/skier game.py", line 111, in <module>
        obstacles = updateObstacleGroup(map0, map1)
      File "/Users/jacobdanks/Desktop/eBooks/skier/skier game.py", line 94, in updateObstacleGroup
        for ob in map0: obstacles.add(ob)
    TypeError: 'NoneType' object is not iterable
    Here's the code for the program up to where I'm getting the error. For reference, lines 94 and 111 are in red:

    Code:
    import pygame, sys, random
    
    skier_images = ["skier_down.png", "skier_right1.png",
                    "skier_right2.png", "skier_left2.png",
                    "skier_left1.png"]
    
    
    #This creates the skier:
    
    class SkierClass(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load("skier_down.png")
            self.rect = self.image.get_rect()
            self.rect.center = [320, 100]
            self.angle = 0
    
    
    #This turns the skier:
    
    def turn(self, direction):
        if self.angle < -2:  self.angle = -2
        if self.angle > 2: self.angle = 2
        center = self.rect.center
        self.image = pygame.image.load(skier_images[self.angle])
        self.rect = self.image.get_rect()
        self.rect.center = center
        speed = [self.angle, 6 - abs(self.angle) * 2]
        return speed
    
    
    #This moves the skier left and right:
    
    def move(self, speed):
        self.rect.centerx = self.rect.centerx + speed[0]
        if self.rect.centerx < 20:  self.rect.centerx = 20
        if self.rect.centerx > 620: self.rect.centerx = 620
    
    
    #This creates trees and flags:
    
    class ObstacleClass(pygame.sprite.Sprite):
        def __init__(self, image_file, location, type):
            pygame.sprite.Sprite.__init__(self)
            self.image_file = image_file
            self.image = pygame.image.load(image_file)
            self.location = location
            self.rect = self.image.get_rect()
            self.rect.center = location
            self.type = type
            self.passed = False
    
    
    #This makes the scenery scroll up:
    
        def scroll(self, t_ptr):
            self.rect.centery = self.location[1] - t_ptr
    
    
    #This creates one window of random trees and flags:ArithmeticError
    
    def create_map(start, end):
        obstacles = pygame.sprite.Group()
        gates = pygame.sprite.Group()
        locations = []
        for i in range (10):
            row = random.randint(start, end)
            col = random.randint(0, 9)
            location = [col * 64 + 20, row * 64 + 20]
            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)
    
    
    #This redraws the screen when the things move:
    
    def animate():
        screen.fill([255, 255, 255])
        pygame.display.update(obstacles.draw(screen))
        screen.blit(skier.image, skier.rect)
        screen.blit(score_text, [10, 10])
        pygame.display.flip()
    
    
    
    #This changes to the next screen of scenery:
    
    def updateObstacleGroup(map0, map1):
        obstacles = pygame.sprite.Group()
        for ob in map0: obstacles.add(ob)
        for ob in map1: obstacles.add(ob)
        return obstacles
    
    
    #This gets everything ready:
    
    pygame.init()
    screen = pygame.display.set_mode([640, 640])
    clock = pygame.time.Clock()
    skier = SkierClass()
    speed = [0, 6]
    map_position = 0
    points = 0
    map0 = create_map(20, 29)
    map1 = create_map(10, 19)
    activeMap = 0
    obstacles = updateObstacleGroup(map0, map1)
    font = pygame.font.Font(None, 50)
    Last edited by Danksalot; 01-27-2010 at 03:45 PM.

  • #2
    New to the CF scene
    Join Date
    Jan 2010
    Location
    Dallas, TX
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I found it!

    I forgot to "return obstacles" at the end of the createMap function.

    I'll let you know if I run into any more snags I can't get out of.

    Danksalot

  • #3
    New to the CF scene
    Join Date
    Jan 2010
    Location
    Dallas, TX
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I got all of the bugs out, and my game works! Hurray for a boring skier game!!!

    I learned so much debugging this thing. Notice the "turn" and "move" functions in the "skierClass" weren't indented in the code above, that one took a while to figure out. That process was way more fun than playing the game when it was done.

    Danksalot
    Last edited by Danksalot; 01-28-2010 at 08:07 AM.

  • #4
    New Coder
    Join Date
    Dec 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Also, if you're using any version of unix, you can use the 'diff' bash command, or maybe it's in windows too. That would've solved your problem.

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    thank you, exactly what I'm looking for!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •