Runtime error when starting a game.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GKRAFT
    New Member
    • Nov 2008
    • 9

    Runtime error when starting a game.

    All i wanna do at this point is to test the (soon to be) game and see that the correct sprites are were they are supposed to be.

    When i run the game the background is black instead of the green color of my field.png, no other sprites are visible and after a while i get a Runtime error and the game crashes.

    Any ideas?

    Code:
    import pygame, math, sys
    from pygame.locals import *
    from livewires import games
    
    # FPS
    clock = pygame.time.Clock()
    FRAMES_PER_SECOND = 30
    deltat = clock.tick(FRAMES_PER_SECOND)
    
    class Endzone(pygame.sprite.Sprite):
        endzone_no_td = pygame.image.load('endzone.png')
        endzone_td = pygame.image.load('endzone_td.png')
    
        def __init__(self, position):
            pygame.sprite.Sprite.__init__(self)
            self.rect = pygame.Rect(self.endzone_no_td.get_rect())
            self.rect.center = position
    
    class Player(pygame.sprite.Sprite):
        player_image = pygame.image.load('49er.gif')
        def __init__(self,position,team,pos_in_team):
    
            if team == 'offense':
                player_image = pygame.image.load('49er.gif')
            else:
                player_image = pygame.image.load('defense.gif')
    
            """ if pos_in_team... """
            
            pygame.sprite.Sprite.__init__(self)
            self.rect = pygame.Rect(self.player_image.get_rect())
            self.rect.center = position
    
    def game():
        my_screen = games.Screen (width=1024, height=768)
        bg = games.load_image("field.png")
        my_screen.set_background(bg)
        def_safety = Player((400,400),'defense','safety')
        off_tight_end = Player((500,500),'offense','tigth_end')
        my_screen.mainloop()
    
    game()
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Is your background solid green? Livewires may be converting the green pixels to transparent. Try this:
    bg = games.load_imag e("field.png" , transparent = False)
    If that works, we'll have to work on your sprites next. I don't think loading the player images with pygame is a good idea. If you're going to be using livewires, you might as well let it do that stuff for you. Or you could just use pygame without livewires.
    Good luck with the game.

    Comment

    • GKRAFT
      New Member
      • Nov 2008
      • 9

      #3
      Thanks for the tip, it took care of the runtime error.
      I've played around a bit more and now i have another problem.

      I get the following error:

      Traceback (most recent call last):
      File "...", line 54, in <module>
      game()
      File "...", line 47, in game
      def_safety = Player('defense ',(40, 40))
      File "...", line 36, in __init__
      screen.blit(pla yer_image, self.position)
      TypeError: invalid destination position for blit



      And here is the code:
      Code:
      import pygame, math, sys
      from pygame.locals import *
      from livewires import games
      
      # FPS
      clock = pygame.time.Clock()
      FRAMES_PER_SECOND = 30
      deltat = clock.tick(FRAMES_PER_SECOND)
      
      screen = pygame.display.set_mode((1024, 768))
      
      
      class Player(pygame.sprite.Sprite):
          player_image = pygame.image.load('49er.gif')
          def __init__(self,team,position):
              self.team = None
              self.position = None
      
              if self.team == 'offense':
                  player_image = pygame.image.load('49er.gif')
              else:
                  player_image = pygame.image.load('defense.gif')
      
              screen.blit(player_image, self.position)
      
      def game():
          bg = pygame.image.load("field.png")
          screen.blit(bg, (0,0))
      
          #DEFENSE
          def_safety = Player('defense',(40, 40))
          def_safety.__init__()
      
          # OFFENSE
          off_tight_end = Player('offense',(100, 100))
          def_safety.__init()
      
      game()
      I have no idea why the "coordinate s" doesn't work...

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Your init function does nothing with the arguments you pass it, and assigns the members to None. It should be
        Code:
            def __init__(self,team,position):
                self.team = team
                self.position = position
        I hope this helps.

        Comment

        • GKRAFT
          New Member
          • Nov 2008
          • 9

          #5
          Ah, that was kinda obvious :)
          Thanks alot!

          Comment

          Working...