The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

This topic is READ ONLY
EG527 (4) [Avatar] Offline
#1
Hi

In listing 10.1 I've typed all the code out, and slowly removed all the typos etc. but it still isn't working on either IDLE or SPE.

I try to run the program, but it says:
Traceback (most recent call last):
File "/Users/Jasper/Documents/Programming/Hello World!/SKIING!!/skiing game.py", line 93, in <module>
obstacles = updateObstacleGroup(map0, map1)
NameError: name 'updateObstacleGroup' is not defined

I've gone back and checked all the previous mentions of 'updateObstacleGroup' but it all seems correct:

Here i my code:


import pygame, sys, random

skier_images = ["skier_down.png", "skier_right1.png", "skier_right2.png", "skier_left2.png", "skier_left1.png"]

#Creates 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

#Turns skier
def turn(self, direction):
self.angle = self.angle + 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

#Moves 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

#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

#Makes scenery scroll up
def scroll(self, t_ptr):
self.rect.centery = self.locatoin[1] - t_ptr

#Creates one window of random trees and flags
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)
return obstacles

#Redraws screen when 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()

#Changes to 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

#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)


while True: #Starts main loop
clock.tick(30) #Updates graphics 30 times per second
#Checks for keypresses or window close
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed = skier.turn(-1)
elif event.key == pygame.K_RIGHT:
speed = skier.turn(1)
skier.move(speed) #Moves skier
map_position += speed[1]

#Changes from one window of scenery to the next
if map_position >= 640 and activeMap == 0:
activeMap = 1
map0 = create_Map(20, 29)
obstacles = updateObstacleGroup(map0, map1)
if map_position >= 1280 and activeMAp == 1:
activeMap = 0
for ob in map0:
ob.location[1] = ob.location[1] - 1280
map_position - 1280
map1 = create_map(10, 19)
obstacles = updateObstacleGroup (map0, map1)

for obstacle in obstacles:
obstacle.scroll(map_position)

#Checks for hitting trees and getting flags
hit = pygame.sprite.spritecollide(skier, obstacles, False)
if hit:
if hit[0].type == "tree" and not hit[0].passed:
points = points - 100
skier.image = pygame.image.load("skier_crash.png")
animate()
pygame.time.delay(1000)
skier.image = pygame.image.load("skier_down.png")
skier.angle = 0
speed = [0, 6]
hit[0].passed = True
elif hit[0].type == "flag" and not hit[0].passed:
points += 10
obstacles.remove(hit[0])

#Displays score
score_text = font.render("Score: " +str(points), 1, (0, 0, 0))
animate()

am thankful for any help
warren.sande (211) [Avatar] Offline
#2
Re: Chapter 10 Skiing Game Problem
Unfortunately, this forum deletes the whitespace, so I can't see how your code is indented. I suspect it might be an indenting problem.

If you send your code to cp4khelp@yahoo.com, I'll have a look and see if I can find the problem.

Regards,
Warren Sande
riverasq (1) [Avatar] Offline
#3
Re: Chapter 10 Skiing Game Problem
Can not get program run who I can I email my code to get explanation, with print screens.