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)