Sign in to follow this  
boxerbaseball24

name error

Recommended Posts

returns error game not defined....need help or material to read to improve my coding
# Baseball game 2
# get pitcher to throw
# kyle cantrell 9/6/06

from livewires import games

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
THE_SCREEN = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT)

class Pitcher(games.Sprite):
    """Pitcher who throws the ball on spacebar command"""
    image = games.load_image("pitch2.bmp")

    def __init__(self, screen, x, y):
        self.init_sprite(screen = screen, x = x, y = y, image = Pitcher.image)
        self.pitch_wait = 0

        if self.screen.is_pressed(games.K_SPACE):
            Pitch(self.screen, self.get_xpos(), self.get_ypos(), self.get_angle())

class Pitch(games.Sprite):
    image = games.load_image("ball.bmp")
    BUFFER = 40
    VELOCITY_FACTOR = 7
    LIFETIME = 40

    def __init__(self, screen, pitcher_x, pitcher_y, pitcher_angle):
        angle = pitcher_angle * math.pi / 180

        buffer_x = Pitch.BUFFER * math.sin(angle)
        buffer_y = -Pitch.BUFFER * math.cos(angle)
        x = pitcher_x + buffer_x
        y = pitcher_y + buffer_y

        dx = Pitch.VELOCITY_FACTOR * math.sin(angle)
        dy = -Pitch.VELOCITY_FACTOR * math.cos(angle)

        self.init_sprite(screen = screen, x = x, y = y,
                         dx = dx, dy = dy, image = Pitch.image)
        self.liftime = Pitch.LIFETIME

    def moved(self):
        self.lifetime -= 1
        if not self.lifetime:
            self.destroy()

class Game(object):

    def create_pitcher(screen, x, y):
        Game.pitcher = Pitcher(screen = screen, x = x, y = y)

    create_pitcher = staticmethod(create_pitcher)

    def main():
        my_screen = THE_SCREEN
        field_image = games.load_image("field.bmp")
        my_screen.set_background(field_image)

        Game.create_pitcher(screen = my_screen, x = SCREEN_WIDTH / 2, y = SCREEN_HEIGHT / 2)

    main()

[Edited by - Oluseyi on September 27, 2006 5:57:13 PM]

Share this post


Link to post
Share on other sites
Well, I have no idea what language this is (useful information), and I can only guess that the error occurs near the bottom on the line "Game.create_pitcher(...", but my guess is that your problem exists because you never created an instance of the Game type. A type only describes how an instance would work, whereas an instance actually exists and thus does work. Perhaps you need a line such as "my_game = Game()"?

Share this post


Link to post
Share on other sites
Quote:
Original post by Agony
Well, I have no idea what language this is (useful information), and I can only guess that the error occurs near the bottom on the line "Game.create_pitcher(...", but my guess is that your problem exists because you never created an instance of the Game type. A type only describes how an instance would work, whereas an instance actually exists and thus does work. Perhaps you need a line such as "my_game = Game()"?




Its python code. And you damned near hit the answer

You have the line of code:
games.load_image("field.bmp")

But I dont see anywhere you assigned games, aka, games = SomeObj()

What object is it that has the load_image() in it?

Share this post


Link to post
Share on other sites
Before you can improve your coding, you must improve your posting.

- State clearly what language you're using (I can tell it's Python, but lots of people won't recognize any given language immediately; we don't all know all of them, or even all the popular ones).

- Use [code][/code] or [source][/source] tags for posting code, in order to preserve the indentation (especially important for Python ;) )

- We don't care about your real name; although you're free to leave such comments in if you like, it's not always the best idea on the Internet.

- Post *exact* errors (copy and paste them) and explain how far you got in the process when the error occurred.

- Ask an actual question.

- See also.

Share this post


Link to post
Share on other sites
You still have the same error as you did in your previous post: your main() function is called inside your Game class. Dedent it (remove the tabs/spaces at the start of the line) so that it comprises a new code block. Remember, whitespace defines scope in Python, and a blank line does not create a new block.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by Serapth
You have the line of code:
games.load_image("field.bmp")

But I dont see anywhere you assigned games, aka, games = SomeObj()

from livewires import games



GargGHH!!! I really need to get used to reading code in a 3 inch window :(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this