main function

Started by
3 comments, last by Kylotan 17 years, 6 months ago
....this is pretty raw im new to programming read the absolute beginners guide to programming....third time through i wanted to just be able to get the pitcher to shoot the ball...no animation yet...just tryin to figure out how things work right now but...when i run this program a message says game function not defined...ive tried taking the game function out and mainlooping it instead but it wont fire the ball....can somebody help me out with this....or give me some material to read to improve my coding or anything im so confused on this

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

[Edit: Added [source lang="python"][/source] tags - Oluseyi] [Edited by - Oluseyi on September 27, 2006 2:10:02 PM]
Advertisement
Your most immediate problem is that, because it is indented one level, your main function is called inside the body of the Game class. Dedent it.

You have a few other weird bits of code, like whenever you call init_sprite(screen = screen, x = x, y = y, image = ...). What on earth is the portion in bold supposed to do?
You may also need to create a Game object at global scope, and call the main() function from that, though it's hard to see whether that function should be part of the Game class or not.

Oluseyi, my guess is that init_sprite can take some keyword arguments, but it does look somewhat redundant.

It's certainly interesting how many people are using this livewires package.
thanks for the help however now my pitcher sprite will not show up on the screen...also i am afraid that when he does he will not shoot the ball as intended...any ideas? thanks for the help before
Putting that if-check in the __init__ function is not going to work, as the __init__ function only gets called when the Pitcher is created.

Also, you misspell 'lifetime' in one place, which will stop that from working.

I can't comment much further because you haven't shown your changed code, and because it's not clear how livewires works.

This topic is closed to new replies.

Advertisement