Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2010, 07:56 AM   PM User | #1
Danksalot
New to the CF scene

 
Join Date: Jan 2010
Location: Dallas, TX
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Danksalot is an unknown quantity at this point
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..
Danksalot is offline   Reply With Quote
Old 01-28-2010, 05:58 AM   PM User | #2
Danksalot
New to the CF scene

 
Join Date: Jan 2010
Location: Dallas, TX
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Danksalot is an unknown quantity at this point
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
Danksalot is offline   Reply With Quote
Old 01-28-2010, 08:05 AM   PM User | #3
Danksalot
New to the CF scene

 
Join Date: Jan 2010
Location: Dallas, TX
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Danksalot is an unknown quantity at this point
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..
Danksalot is offline   Reply With Quote
Old 05-09-2010, 07:35 AM   PM User | #4
pippin418
New Coder

 
Join Date: Dec 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
pippin418 is an unknown quantity at this point
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.
pippin418 is offline   Reply With Quote
Old 09-06-2011, 10:34 AM   PM User | #5
arven77
New to the CF scene

 
Join Date: Sep 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
arven77 is an unknown quantity at this point
Hi,
thank you, exactly what I'm looking for!
arven77 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:27 AM.


Advertisement
Log in to turn off these ads.