By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Runtime error when starting a game.

P: 9
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?

Expand|Select|Wrap|Line Numbers
  1. import pygame, math, sys
  2. from pygame.locals import *
  3. from livewires import games
  4.  
  5. # FPS
  6. clock = pygame.time.Clock()
  7. FRAMES_PER_SECOND = 30
  8. deltat = clock.tick(FRAMES_PER_SECOND)
  9.  
  10. class Endzone(pygame.sprite.Sprite):
  11.     endzone_no_td = pygame.image.load('endzone.png')
  12.     endzone_td = pygame.image.load('endzone_td.png')
  13.  
  14.     def __init__(self, position):
  15.         pygame.sprite.Sprite.__init__(self)
  16.         self.rect = pygame.Rect(self.endzone_no_td.get_rect())
  17.         self.rect.center = position
  18.  
  19. class Player(pygame.sprite.Sprite):
  20.     player_image = pygame.image.load('49er.gif')
  21.     def __init__(self,position,team,pos_in_team):
  22.  
  23.         if team == 'offense':
  24.             player_image = pygame.image.load('49er.gif')
  25.         else:
  26.             player_image = pygame.image.load('defense.gif')
  27.  
  28.         """ if pos_in_team... """
  29.  
  30.         pygame.sprite.Sprite.__init__(self)
  31.         self.rect = pygame.Rect(self.player_image.get_rect())
  32.         self.rect.center = position
  33.  
  34. def game():
  35.     my_screen = games.Screen (width=1024, height=768)
  36.     bg = games.load_image("field.png")
  37.     my_screen.set_background(bg)
  38.     def_safety = Player((400,400),'defense','safety')
  39.     off_tight_end = Player((500,500),'offense','tigth_end')
  40.     my_screen.mainloop()
  41.  
  42. game()
  43.  
Mar 2 '09 #1
Share this Question
Share on Google+
4 Replies


boxfish
Expert 100+
P: 469
Is your background solid green? Livewires may be converting the green pixels to transparent. Try this:
bg = games.load_image("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.
Mar 2 '09 #2

P: 9
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(player_image, self.position)
TypeError: invalid destination position for blit



And here is the code:
Expand|Select|Wrap|Line Numbers
  1. import pygame, math, sys
  2. from pygame.locals import *
  3. from livewires import games
  4.  
  5. # FPS
  6. clock = pygame.time.Clock()
  7. FRAMES_PER_SECOND = 30
  8. deltat = clock.tick(FRAMES_PER_SECOND)
  9.  
  10. screen = pygame.display.set_mode((1024, 768))
  11.  
  12.  
  13. class Player(pygame.sprite.Sprite):
  14.     player_image = pygame.image.load('49er.gif')
  15.     def __init__(self,team,position):
  16.         self.team = None
  17.         self.position = None
  18.  
  19.         if self.team == 'offense':
  20.             player_image = pygame.image.load('49er.gif')
  21.         else:
  22.             player_image = pygame.image.load('defense.gif')
  23.  
  24.         screen.blit(player_image, self.position)
  25.  
  26. def game():
  27.     bg = pygame.image.load("field.png")
  28.     screen.blit(bg, (0,0))
  29.  
  30.     #DEFENSE
  31.     def_safety = Player('defense',(40, 40))
  32.     def_safety.__init__()
  33.  
  34.     # OFFENSE
  35.     off_tight_end = Player('offense',(100, 100))
  36.     def_safety.__init()
  37.  
  38. game()
  39.  
I have no idea why the "coordinates" doesn't work...
Mar 3 '09 #3

boxfish
Expert 100+
P: 469
Your init function does nothing with the arguments you pass it, and assigns the members to None. It should be
Expand|Select|Wrap|Line Numbers
  1.     def __init__(self,team,position):
  2.         self.team = team
  3.         self.position = position
I hope this helps.
Mar 3 '09 #4

P: 9
Ah, that was kinda obvious :)
Thanks alot!
Mar 3 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.