在 Python 中,我收到一个错误,指出找不到全局变量,但在 codeskupltor 中 - 代码工作正常

In Python I get an error that a global variable is not found, but in codeskupltor - the code works fine

我在计算机上从 codeskulptor 转移到 python 的本地安装。因此我需要使用 simpleguitk 中的 simplegui。这基本上有效(声音除外,但这是另一个问题,我认为不是这个问题的一部分)。 当我使用这段在 codeskulptor 中运行良好的特定代码并将其传输到我的本地 python(相同版本 2.7 以防万一有人觉得这可能是个问题)- 我突然发现找不到全局变量。代码很长 - 但问题出在函数 draw:

def draw(canvas):
    global time, started
    global lives, score
    global my_ship, rock_group, rock_rand_limit, missile_group, explosion_group

    # animiate background
    time += 1
    wtime = (time / 4) % WIDTH
    center = debris_info.get_center()
    size = debris_info.get_size()
    canvas.draw_image(nebula_image, nebula_info.get_center(), nebula_info.get_size(), [WIDTH / 2, HEIGHT / 2], [WIDTH, HEIGHT])
    canvas.draw_image(debris_image, center, size, (wtime - WIDTH / 2, HEIGHT / 2), (WIDTH, HEIGHT))
    canvas.draw_image(debris_image, center, size, (wtime + WIDTH / 2, HEIGHT / 2), (WIDTH, HEIGHT))

    # draw ship and sprites
    my_ship.draw(canvas)

    # update ship and sprites
    my_ship.update()

    if group_collide(rock_group, my_ship) == True:
        lives -= 1    

    score += group_group_collide(rock_group, missile_group)

    if lives == 0:
        started = False
        copy_of_rock_group = rock_group.copy()
        copy_of_missile_group = missile_group.copy()
        copy_of_explosion_group = explosion_group.copy()
        rock_group.difference_update(copy_of_rock_group)
        missile_group.difference_update(copy_of_missile_group)
        explosion_group.difference_update(copy_of_explosion_group)
        soundtrack.stop()
        timer.stop()
        soundtrack_timer.stop()
        new_game()

表示全局变量:my_ship 在行中:

#画船和精灵 my_ship.draw(canvas)

未定义。它还声明许多其他全局变量未定义。我一定做错了什么,但我找不到。

如果有人能找到错误 - 我会非常高兴听到我做错了什么。

我现在包括整个程序 - 很长,对不起。这是我几个月前交的一门课程的作业。由于我进行了搜索并在网上找到了其他人的解决方案,因此我认为我可以在此处 post 解决这个问题。请注意,这不会 运行 在 codeskulptor 中,因为它是为 simpleguitk 设计的,并且声音的工作方式不同等。所以这不是我的作业的精确副本 - 这正是问题所在...... 谢谢, 杰夫

# program template for Spaceship
# To be upgraded to RiceRocks for final mini-project
# (Keep in mind to check the template for RiceRocks for 
#  differences in the merge)
import simpleguitk as simplegui
import math
import random
import pygame

# globals for user interface
WIDTH = 800
HEIGHT = 600
FONT_SIZE = 20
LT_TEXT = 50
RT_TEXT = WIDTH - 100
TOP_TEXT = 30
BOT_TEXT = TOP_TEXT + 30
MAX_LIVES = 3
ZERO_SCORE = 0
score = ZERO_SCORE
lives = MAX_LIVES
time = 0
#time = 0.5

started = False

angle_update = 0.06
friction = 0.03
thrust_const = 0.25
missile_vel_factor = 10
missile_lifespan = 40
explosion_lifespan = 24
asteroid_crash_lifespan = 5

max_rocks = 6
ROCK_RAND_LIMIT_INIT = 0.95
rock_rand_limit = ROCK_RAND_LIMIT_INIT
ROCK_SHIP_BUFFER = 20


class ImageInfo:
    def __init__(self, center, size, radius = 0, lifespan = None, animated = False):
        self.center = center
        self.size = size
        self.radius = radius
        if lifespan:
            self.lifespan = lifespan
        else:
            self.lifespan = float('inf')
        self.animated = animated

    def get_center(self):
        return self.center

    def get_size(self):
        return self.size

    def get_radius(self):
        return self.radius

    def get_lifespan(self):
        return self.lifespan

    def get_animated(self):
        return self.animated


# art assets created by Kim Lathrop, may be freely re-used in non-commercial projects, please credit Kim

# debris images - debris1_brown.png, debris2_brown.png, debris3_brown.png, debris4_brown.png
#                 debris1_blue.png, debris2_blue.png, debris3_blue.png, debris4_blue.png, debris_blend.png
debris_info = ImageInfo([320, 240], [640, 480])
debris_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/debris2_blue.png")

# nebula images - nebula_brown.png, nebula_blue.png
nebula_info = ImageInfo([400, 300], [800, 600])
nebula_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/nebula_blue.f2014.png")

# splash image
splash_info = ImageInfo([200, 150], [400, 300])
splash_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/splash.png")

# ship image
ship_info = ImageInfo([45, 45], [90, 90], 35)
ship_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/double_ship.png")

# missile image - shot1.png, shot2.png, shot3.png
#missile_info = ImageInfo([5,5], [10, 10], 3, 50) original value of lifespan is 50
missile_info = ImageInfo([5,5], [10, 10], 3, missile_lifespan)
missile_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/shot2.png")

# asteroid images - asteroid_blue.png, asteroid_brown.png, asteroid_blend.png
asteroid_info = ImageInfo([45, 45], [90, 90], 40)
asteroid_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/asteroid_blend.png")
# asteroid-ship crash explosion
asteroid_crash_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/asteroid_blue.png")
asteroid_crash_info = ImageInfo([45, 45], [90, 90], 40, asteroid_crash_lifespan, False)
# animated explosion - explosion_orange.png, explosion_blue.png, explosion_blue2.png, explosion_alpha.png
explosion_info = ImageInfo([64, 64], [128, 128], 17, explosion_lifespan, True)
explosion_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/explosion_alpha.png")

pygame.mixer.init()

# sound assets purchased from sounddogs.com, please do not redistribute
#soundtrack = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/soundtrack.mp3")
soundtrack = pygame.mixer.Sound('c:\Users\jdanowit\Downloads\AnacondaPython\soundtrack.mp3')
#missile_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/missile.mp3")
missile_sound = pygame.mixer.Sound('c:\Users\jdanowit\Downloads\AnacondaPython\missile.mp3')
missile_sound.set_volume(.5)
#ship_thrust_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/thrust.mp3")
ship_thrust_sound = pygame.mixer.Sound('c:\Users\jdanowit\Downloads\AnacondaPython\thrust.mp3')
#explosion_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/explosion.mp3")
explosion_sound = pygame.mixer.Sound('c:\Users\jdanowit\Downloads\AnacondaPython\explosion.mp3')
##missile_sound = explosion_sound
# helper functions to handle transformations
def angle_to_vector(ang):
    return [math.cos(ang), math.sin(ang)]

def dist(p,q):
    return math.sqrt((p[0] - q[0]) ** 2+(p[1] - q[1]) ** 2)

def ranInRange(a,b):
    range_width = b - a
    return random.random() * range_width + a

# Ship class
class Ship:
    def __init__(self, pos, vel, angle, image, info):
        self.pos = [pos[0],pos[1]]
        self.vel = [vel[0],vel[1]]
        self.thrust = False
        self.thrust_amount = 0
        self.angle = angle
        self.angle_vel = 0
        self.image = image
        self.image_center = info.get_center()
        self.image_size = info.get_size()
        self.radius = info.get_radius()
        self.forward = angle_to_vector(self.angle)

    def draw(self,canvas):
        image_center = list(self.image_center);
        if(self.thrust == True):
            image_center[0] += self.image_size[0]

        canvas.draw_image(self.image, image_center, self.image_size, self.pos, self.image_size, self.angle)

    def update(self):     
        self.vel[0] *= (1 - friction)
        self.vel[1] *= (1 - friction)

        self.pos[0] += self.vel[0]
        self.pos[1] += self.vel[1]

        self.pos[0] =  self.pos[0] % WIDTH
        self.pos[1] =  self.pos[1] % HEIGHT

        self.angle += self.angle_vel

        self.forward = angle_to_vector(self.angle)

        self.vel[0] += self.thrust_amount*self.forward[0]
        self.vel[1] += self.thrust_amount*self.forward[1]

    def update_angle_vel(self, delta):
        self.angle_vel += delta

    def stop_angle_vel(self):
        self.angle_vel = 0

    def update_thrust(self, doThrust): 
        self.thrust = doThrust
        if(self.thrust == True):
            #ship_thrust_sound.rewind()
            ship_thrust_sound.play()
            self.thrust_amount = thrust_const
        else:
            ship_thrust_sound.stop()
            self.thrust_amount = 0

    def shoot(self):
        #global a_missile
        global missle_group
        #calculate the position and velocity of the missile: missile_vel_factor
        missile_x = self.pos[0] + self.forward[0]*self.radius
        missile_y = self.pos[1] + self.forward[1]*self.radius
        missile_vx = self.vel[0] + self.forward[0]*missile_vel_factor
        missile_vy = self.vel[1] + self.forward[1]*missile_vel_factor
        missile_vel = [missile_vx, missile_vy]
        #print [cx + self.radius, cy]
        new_missile = Sprite([missile_x, missile_y], missile_vel, self.angle, 0, missile_image, missile_info, missile_sound)
        missile_group.add(new_missile)

    def get_position(self):
        return self.pos

    def get_radius(self):
        return self.radius

# Sprite class
class Sprite:
    def __init__(self, pos, vel, ang, ang_vel, image, info, sound = None):
        self.pos = [pos[0],pos[1]]
        self.vel = [vel[0],vel[1]]
        self.angle = ang
        self.angle_vel = ang_vel
        self.image = image
        self.image_center = info.get_center()
        self.image_size = info.get_size()
        self.radius = info.get_radius()
        self.lifespan = info.get_lifespan()
        self.animated = info.get_animated()
        self.age = 0
        if sound:
            #sound.rewind()
            sound.play()

    def draw(self, canvas):   
        if self.animated == False:
            canvas.draw_image(self.image, self.image_center, self.image_size, self.pos, self.image_size, self.angle)
        else:
            age = self.age
            im_cen_x = self.image_center[0] + age*self.image_size[0]
            im_cen_y = self.image_center[1]
            im_cen = [im_cen_x, im_cen_y]
            canvas.draw_image(explosion_image, im_cen, self.image_size, self.pos, self.image_size, self.angle)

    def update(self):
        self.angle += self.angle_vel

        self.pos[0] += self.vel[0]
        self.pos[0] = self.pos[0] % WIDTH

        self.pos[1] += self.vel[1]
        self.pos[1] = self.pos[1] % HEIGHT

        self.age += 1
        if self.age > self.lifespan:
            return True #remove me

        return False #keep me

    def get_position(self):
        return self.pos

    def get_radius(self):
        return self.radius

    def collide(self, other_object):
        pos_other = other_object.get_position()
        rad_other = other_object.get_radius()

        object_distance = dist(self.pos, pos_other)

        if object_distance <= (rad_other + self.radius):
            return True

        return False

def new_game():
    global my_ship, rock_group, rock_rand_limit, missile_group, explosion_group
    global timer
    global soundtrack_timer

    my_ship = Ship([WIDTH / 2, HEIGHT / 2], [0, 0], 0, ship_image, ship_info)
    rock_group = set([])
    missile_group = set([])
    explosion_group = set([])
    rock_rand_limit = ROCK_RAND_LIMIT_INIT

def process_sprite_group(current_set, canvas):
    remove_set = set([])
    for sprite in current_set:
        sprite.draw(canvas)
        remove_me = sprite.update()
        if remove_me == True:
            remove_set.add(sprite)

    current_set.difference_update(remove_set)    

def group_collide(group, other_object): 
    global explosion_group
    '''group is a set of sprites'''
    '''other_object is a single object like "ship" '''
    remove_set = set([])
    for element in group:
        if element.collide(other_object):
            if isinstance(other_object, Ship) == True:
                new_explosion = Sprite(element.get_position(), [0, 0], 0, 0, asteroid_crash_image, asteroid_crash_info, explosion_sound)
            else:    
                new_explosion = Sprite(element.get_position(), [0, 0], 0, 0, explosion_image, explosion_info, explosion_sound)

            explosion_group.add(new_explosion)
            remove_set.add(element)

    crashes = len(remove_set)
    group.difference_update(remove_set)

    if crashes > 0:
        return True

    return False

def group_group_collide(group1, group2):
    global explosion_group
    remove_set = set([])
    for g2 in group2:
        if group_collide(group1, g2) == True:
            #new_explosion = Sprite(g2.get_position(), [0, 0], 0, 0, explosion_image, explosion_info, explosion_sound)
            #explosion_group.add(new_explosion)
            remove_set.add(g2)

    number_of_collisions = len(remove_set)
    group2.difference_update(remove_set)

    return number_of_collisions


# mouseclick handlers that reset UI and conditions whether splash image is drawn
def click(pos):
    global started
    global score, lives
    global timer, soundtrack_timer
    if started == False:
        center = [WIDTH / 2, HEIGHT / 2]
        size = splash_info.get_size()
        inwidth = (center[0] - size[0] / 2) < pos[0] < (center[0] + size[0] / 2)
        inheight = (center[1] - size[1] / 2) < pos[1] < (center[1] + size[1] / 2)
        score = ZERO_SCORE
        lives = MAX_LIVES
        if (not started) and inwidth and inheight:
            started = True
            #soundtrack.rewind()
            soundtrack.play()
            timer.start()
            soundtrack_timer.start()

def draw(canvas):
    global time, started
    global lives, score
    global my_ship, rock_group, rock_rand_limit, missile_group, explosion_group

    # animiate background
    time += 1
    wtime = (time / 4) % WIDTH
    center = debris_info.get_center()
    size = debris_info.get_size()
    canvas.draw_image(nebula_image, nebula_info.get_center(), nebula_info.get_size(), [WIDTH / 2, HEIGHT / 2], [WIDTH, HEIGHT])
    canvas.draw_image(debris_image, center, size, (wtime - WIDTH / 2, HEIGHT / 2), (WIDTH, HEIGHT))
    canvas.draw_image(debris_image, center, size, (wtime + WIDTH / 2, HEIGHT / 2), (WIDTH, HEIGHT))

    # draw ship and sprites
    my_ship.draw(canvas)

    # update ship and sprites
    my_ship.update()

    if group_collide(rock_group, my_ship) == True:
        lives -= 1    

    score += group_group_collide(rock_group, missile_group)

    if lives == 0:
        started = False
        copy_of_rock_group = rock_group.copy()
        copy_of_missile_group = missile_group.copy()
        copy_of_explosion_group = explosion_group.copy()
        rock_group.difference_update(copy_of_rock_group)
        missile_group.difference_update(copy_of_missile_group)
        explosion_group.difference_update(copy_of_explosion_group)
        soundtrack.stop()
        timer.stop()
        soundtrack_timer.stop()
        new_game()

    process_sprite_group(rock_group, canvas)
    process_sprite_group(missile_group, canvas)
    process_sprite_group(explosion_group, canvas)

     # draw splash screen if not started
    if not started:
        canvas.draw_image(splash_image, splash_info.get_center(), 
                          splash_info.get_size(), [WIDTH / 2, HEIGHT / 2], 
                          splash_info.get_size())

    #lives and score
    canvas.draw_text("Lives",    [LT_TEXT, TOP_TEXT], FONT_SIZE, "white", "monospace")
    canvas.draw_text("Score",    [RT_TEXT, TOP_TEXT], FONT_SIZE, "white", "monospace")
    canvas.draw_text(str(lives), [LT_TEXT, BOT_TEXT], FONT_SIZE, "white", "monospace")
    canvas.draw_text(str(score), [RT_TEXT, BOT_TEXT], FONT_SIZE, "white", "monospace")

def keydown(key):
    global my_ship
    if started == True:
          if key==simplegui.KEY_MAP["up"]:    
             my_ship.update_thrust(True)

          if key==simplegui.KEY_MAP["left"]:
             my_ship.update_angle_vel(-angle_update)

          if key==simplegui.KEY_MAP["right"]:
             my_ship.update_angle_vel(angle_update)

          if key==simplegui.KEY_MAP["space"]:
             my_ship.shoot()

def keyup(key):   
      global my_ship
      if started == True:
          if key==simplegui.KEY_MAP["up"]:
              my_ship.update_thrust(False)

          if key==simplegui.KEY_MAP["left"] or key==simplegui.KEY_MAP["right"]: 
              my_ship.stop_angle_vel()

# timer handler that spawns a rock    
def rock_spawner():
    global rock_group, rock_rand_limit, my_ship

    if started == True:
        if len(rock_group) < max_rocks :
            #print "adding"
            pos_x = random.randint(0, WIDTH -1)
            pos_y = random.randint(0, HEIGHT-1)
            rock_ship_distance = dist(my_ship.get_position(), [pos_x, pos_y])
            if rock_ship_distance > (asteroid_info.get_radius() + my_ship.get_radius() + ROCK_SHIP_BUFFER):                          
                vel_x = ranInRange(-rock_rand_limit, rock_rand_limit)
                vel_y = ranInRange(-rock_rand_limit, rock_rand_limit)
                rock_rand_limit = min(rock_rand_limit + 0.001, 1.5)  
                print rock_rand_limit
                ang_vel = ranInRange(-0.05, 0.05)
                asteroid = Sprite([pos_x, pos_y], [vel_x, vel_y], 0, ang_vel, asteroid_image, asteroid_info)
                rock_group.add(asteroid)



def soundtrack_restart():
    print "restarting soundtrack"
    #soundtrack.rewind()
    soundtrack.play()

# initialize frame
frame = simplegui.create_frame("Asteroids", WIDTH, HEIGHT)

# register handlers
frame.set_keyup_handler(keyup)
frame.set_keydown_handler(keydown)
frame.set_mouseclick_handler(click)
frame.set_draw_handler(draw)

timer = simplegui.create_timer(1000.0, rock_spawner)
soundtrack_timer = simplegui.create_timer(180000.0, soundtrack_restart)

# get things rolling
frame.start()

# initialize game
new_game()

问题出在你的最后四行代码中——frame.start() 在你的游戏结束之前不会退出,但在那之后你有 new_game(),所以全局变量永远不会被初始化。