[Tutor] Help with a game

Gregor Lingl glingl at aon.at
Tue Nov 11 03:47:06 EST 2003


Hi Jeff!

If I wrote a program of more than 100 lines and then tried
it out, it very probably won't run! For programming beginners
I would suggest that you develop your programs incrementally,
i. e. step by step.  Break up your program in many tiny tasks
and implement them on after the other. After having completed
a task test it. Only if it works continue with the next task.
(Don't they tell you things like that in your  programming
class?)

This way you will find much more easily your mistakes,
because there are only the recently added lines where
your mistake has to be located. Moreover a long program may
contain a lot of mistakes (which possibly interact in a
complicated way) and of course it's harder to locate them all
than to find only one or two in a small portion of your program.

(Of course I've simplified things a bit, because the reason why
some portion of added code doesn't work properly may lie in a
design flaw of the rest of program ... . But even in this case
you easily find out which small part of the code is in conflict
with the rest ...  and finally Python gives you it's error-massages
which you definitly should not only read but also try to
understand.)

Concerning YOUR PROGRAM the problem with main(), observed by Lee,
would have been found much earlier if you had tried to test it after
you had written the first 10 to 15 lines.

I observed  - additionally to what Lee mentioned - that in main()
you call the constructor of some not existing class New whereas
you define a class ship() which is never used. (And please note,
that we cannot try to run your program as we don't have the
necessary bitmap files.)

Regards,
Gregor

j j schrieb:

> Hey guys, i'm in a programmng class and i need some help.  I made a 
> game based off of the pygame example "Chimp" and it won't run.  Maybe 
> a little look over to see if i've missed something or if i've made a 
> mistake.  Thank you for your time guys.  Later.
>
> import os, pygame
> from pygame.locals import *
> if not pygame.font:print 'Warning, fonts disabled'
>  
> def load_image(name, colorkey=None):
>         fullname = os.path.join('data', name)
>         try:
>             image = pygame.image.load(fullname)
>         except pygame.error, message:
>             print 'Cannot load image:', fullname
>             raise SystemExit, message
>         image = image.convert()
>         if colorkey is not None:
>             if colorkey is -1:
>                 colorkey = image.get_at((0,0))
>             image.set_colorkey(colorkey, RLEACCEL)
>         return image, image.get_rect()
>
> class Gun(pygame.sprite.Sprite):
>     def __init__(self):
>         pygame.sprite.Sprite.__init__(self)
>         self.image, self.rect = load_image('gun.bmp', -1)
>         self.punching = 0
>     def update(self):
>         pos = pygame.mouse.get_pos()
>         self.rect.midtop = pos
>         if self.punching:
>             self.rect.move_ip(5, 10)
>     def shoot(self, target):
>         if not self.punching:
>             self.punching = 1
>             hitbox = self.rect.inflate(-5,-5)
>             return hitbox.colliderect(target.rect)
>     def unshoot(self):
>         self.punching = 0
>        
> class ship(pygame.sprite.Sprite):
>     def __init__(self):
>         pygame.sprite.Sprite.__init__(self)
>         self.image, self.rect = load_image('new.bmp',-1)
>         screen = pygame.display.get_suface()
>         self.area = screen.get_rect()
>         self.rect.topleft = 10, 10
>         self.move = 9
>         self.dizzy = 0
>     def update(self):
>         if self.dizzy:
>             self._spin()
>         else:
>             self._walk()
>     def _walk(self):
>         change = self.rect.move((self.move, 0))
>         if self.rect.left< self.area.left or \
>            self.rect.right > self.area.right:
>             self.move = -self.move
>             change = self.rect.move((self.move, 0))
>             self.image = pygame.transform.flip(self.image, 1, 0)
>         self.rect = change           
>     def _change(self):
>         center = self.rect.center
>         self.dizzy = self.dizzy + 12
>         if self.dizzy >= 360:
>             self.dizzy = 0
>             self.image = self.original
>         else:
>             rotate = pygame.transform.rotate
>             self.image = rotate(self.original, slef.dizzy)
>         self.rect = self.image.get_rect()
>         self.rect.center = center
>     def done(self):
>         if not self.dizzy:
>             self.dizzy = 1
>             self.original = self.image
>     def main():
>         pygame.init()
>         screen = pygame.display.set_mode((480, 60))
>         pygame.display.set_caption('Battleship')
>         pygame.mouse.set_visible(0)
>         backround = pygame.Surface(screen.get_size())
>         backround = backround.convert()
>         backround.fill((250, 250, 250))
>
>         screen.blit(backround, (0, 0))
>         pygame.display.flip()
>         clock = pygame.time.Clock()
>         new = New()
>         gun = Gun()
>         allsprites = pygame.sprite.RenderPlain((gun, new))
>         while 1:
>             clock.tick(60)
>             for  event in pygame.event.get():
>                 if event.type == QUIT:
>                     return
>                 elif event.type == KEYDOWN and event.key == K_ESCAPE:
>                     return
>                 elif event.type == MOUSEBUTTONDOWN:
>                     if fist.punch(chimp):
>                         chimp.punched()
>                 elif event.type == MOUSEBUTTONUP:
>                     fist.unpunch()
>                 allsprites.update()
>                 screen.blit(backround, (0, 0))
>                 allsprites.draw(screen)
>                 pygame.display.flip()
>               
>                
>         
> ------------------------------------------------------------------------
> Do you Yahoo!?
> Protect your identity with Yahoo! Mail AddressGuard 
> <http://antispam.yahoo.com/whatsnewfree>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>  
>




More information about the Tutor mailing list