pygame problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dynamo
    New Member
    • Apr 2007
    • 51

    pygame problems

    Hi guys i have come again with more problems.This time it has to do with pygame.The following code does not give any error messages but it does not do what it is supposed to do either.the code is a bit long but it's straightforward .Please help
    Code:
    import pygame
    from pygame.locals import *
    pygame.init()
    screen=pygame.display.set_mode((900,900))
    
    
    class Hero(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image=pygame.image.load('C:/Python24/r3.PNG')
            self.image=self.image.convert()
            self.rect=self.image.get_rect()
            self.rect.topleft=0,500
            self.a=screen.get_rect()
        def forward(self):
            a=1
        def back(self):
            a=2
        def original(self):
            self.a.topleft=0,500
        def moveup(self):
            a=3  
        def movedown(self):
            a=4    
        def update(self):
            if a==0:
                self.original()
            if a==1:
                if self.rect.left<=self.a.right :
                    self.rect.move_ip(5,0)
                else:
                    self.original()
            if a==2:
                if self.rect.left!=0:
                    self.rect.move_ip(-5,0)
                else:
                    self.original()
            if a==3:
                if self.rect.top!=self.a.top:
                    self.rect.move_ip(0,5)
                else:
                    self.rect.move_ip(0,-5)
            if a==4:
                if self.rect.bottom!=0:
                    self.rect.move_ip(0,-5)
                else:
                    self.rect.move_ip(0,5)
    class Villian(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image=pygame.image.load('C:/Python24/toba-pyweek3/toba-pyweek3/data/cards/1.PNG')
            self.rect=self.image.convert()
            self.rect=self.image.get_rect()
            self.rect.topleft=400,500
            a=0
        def forward(self):
            self.rect.move_ip(0,5)
            
        def update(self):
            if self.rect.top!=900:
                self.forward()
            else:
                self.original()
        def original(self):
            self.rect.topleft=400,500
    
    imig=pygame.image.load('C:/Python24/Blue hills.GIF')
    background=pygame.Surface(screen.get_size())
    background=background.convert()
    background.blit(imig,(0,0))
    screen.blit(background,(0,0))
    hero=Hero()
    villian=Villian()
    allsprites=pygame.sprite.RenderPlain((hero,villian))
    allsprites.draw(screen)
    pygame.display.flip()
    
    clock=pygame.time.Clock()
    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type==KEYDOWN and event.key==K_UP:
                hero.moveup()
            if event.type==KEYDOWN and event.key==K_DOWN:
                hero.movedown()
            if event.type==KEYDOWN and event.key==K_RIGHT:
                hero.forward()
            if event.type==KEYDOWN and event.key==K_LEFT:
                hero.back()
            if event.type==QUIT:
                pygame.quit()
                raise SystemExit()
    allsprites.update()
    screen.blit(background,(0,0))
    allsprites.draw(screen)
    pygame.display.flip()
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by dynamo
    Hi guys i have come again with more problems.This time it has to do with pygame.The following code does not give any error messages but it does not do what it is supposed to do either.the code is a bit long but it's straightforward .Please help
    [CODE=python]
    clock=pygame.ti me.Clock()
    while True:
    clock.tick(60)
    for event in pygame.event.ge t():
    if event.type==KEY DOWN and event.key==K_UP :
    hero.moveup()
    if event.type==KEY DOWN and event.key==K_DO WN:
    hero.movedown()
    if event.type==KEY DOWN and event.key==K_RI GHT:
    hero.forward()
    if event.type==KEY DOWN and event.key==K_LE FT:
    hero.back()
    if event.type==QUI T:
    pygame.quit()
    raise SystemExit()
    allsprites.upda te()
    screen.blit(bac kground,(0,0))
    allsprites.draw (screen)
    pygame.display. flip()[/CODE]
    Hi, I'm not sure you're indenting is correct. In your while loop you don't draw anything or update the screen. You should put the updating, drawing, and blitting in every frame. The way you have it, it is only going to do something when you close the window and only once.Your while loop should look like this:
    [code=python]
    while True:
    clock.tick(60)
    for event in pygame.event.ge t():
    if event.type==KEY DOWN and event.key==K_UP :
    hero.moveup()
    if event.type==KEY DOWN and event.key==K_DO WN:
    hero.movedown()
    if event.type==KEY DOWN and event.key==K_RI GHT:
    hero.forward()
    if event.type==KEY DOWN and event.key==K_LE FT:
    hero.back()
    if event.type==QUI T:
    pygame.quit()
    raise SystemExit()
    allsprites.upda te()
    screen.blit(bac kground,(0,0))
    allsprites.draw (screen)
    pygame.display. flip()
    [/code]

    Comment

    • dynamo
      New Member
      • Apr 2007
      • 51

      #3
      Originally posted by ilikepython
      Hi, I'm not sure you're indenting is correct. In your while loop you don't draw anything or update the screen. You should put the updating, drawing, and blitting in every frame. The way you have it, it is only going to do something when you close the window and only once.Your while loop should look like this:
      [code=python]
      while True:
      clock.tick(60)
      for event in pygame.event.ge t():
      if event.type==KEY DOWN and event.key==K_UP :
      hero.moveup()
      if event.type==KEY DOWN and event.key==K_DO WN:
      hero.movedown()
      if event.type==KEY DOWN and event.key==K_RI GHT:
      hero.forward()
      if event.type==KEY DOWN and event.key==K_LE FT:
      hero.back()
      if event.type==QUI T:
      pygame.quit()
      raise SystemExit()
      allsprites.upda te()
      screen.blit(bac kground,(0,0))
      allsprites.draw (screen)
      pygame.display. flip()
      [/code]
      Thanks for replying.I think what your saying is i should include the blitting and drawing into the while loop,makes sense.How did you get to this level in pygame,any good tutorials?

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by dynamo
        Thanks for replying.I think what your saying is i should include the blitting and drawing into the while loop,makes sense.How did you get to this level in pygame,any good tutorials?
        The pygame website has some good tutorials and is a good reference.
        Here are some links to help you out:



        Also, you could download some sample games from the website so you can see how they are written and learn from them.

        Comment

        • dynamo
          New Member
          • Apr 2007
          • 51

          #5
          Originally posted by ilikepython
          The pygame website has some good tutorials and is a good reference.
          Here are some links to help you out:



          Also, you could download some sample games from the website so you can see how they are written and learn from them.
          Thanks for the links.Do you you know how to make a part of your image move.For instance to make your hero kick or to shoot.

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by dynamo
            Thanks for the links.Do you you know how to make a part of your image move.For instance to make your hero kick or to shoot.
            Do you mean animation? You can't make part of an image move, you need to make seperate pygame.Surfaces . If you want to do animation like walking, running, or shooting you can have a sprite sheet all in one BMP file showing all the possible frames of animation of a charatcer. You can blit the part of the image that you need for the current frame to represent an animation. Is that what you need?

            Comment

            • dynamo
              New Member
              • Apr 2007
              • 51

              #7
              Originally posted by ilikepython
              Do you mean animation? You can't make part of an image move, you need to make seperate pygame.Surfaces . If you want to do animation like walking, running, or shooting you can have a sprite sheet all in one BMP file showing all the possible frames of animation of a charatcer. You can blit the part of the image that you need for the current frame to represent an animation. Is that what you need?
              I did not think of that thats actually brilliant and a lot easier the creating little seperate images.Thanks

              Comment

              • ilikepython
                Recognized Expert Contributor
                • Feb 2007
                • 844

                #8
                Originally posted by dynamo
                I did not think of that thats actually brilliant and a lot easier the creating little seperate images.Thanks
                No problem. (stupid limit)

                Comment

                • dynamo
                  New Member
                  • Apr 2007
                  • 51

                  #9
                  Originally posted by ilikepython
                  No problem. (stupid limit)
                  what do you mean by stupid limit.

                  Comment

                  • ilikepython
                    Recognized Expert Contributor
                    • Feb 2007
                    • 844

                    #10
                    Originally posted by dynamo
                    what do you mean by stupid limit.
                    Lol, you need to enter at least 20 characters in a message. Used to be 10.

                    Comment

                    • dynamo
                      New Member
                      • Apr 2007
                      • 51

                      #11
                      Originally posted by ilikepython
                      Lol, you need to enter at least 20 characters in a message. Used to be 10.
                      Oh,lol.Thanks again.

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Originally posted by ilikepython
                        Lol, you need to enter at least 20 characters in a message. Used to be 10.
                        Or !

                        Comment

                        Working...