[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Making a character jump



Hi, I edited the aliens.py game but still can't get the character to jump.
Any suggestions? Here's my code:

class Player(pygame.sprite.Sprite):
   speed = 10
   bounce = 1
   images = []
   def __init__(self):
       pygame.sprite.Sprite.__init__(self, self.containers)
       self.image = self.images[0]
       self.rect = self.image.get_rect(midbottom=SCREENRECT.midbottom)
       self.reloading = 0
       self.origtop = self.rect.top
       self.facing = -1

def move(self, direction):
if direction: self.facing = direction
self.rect.move_ip(direction*self.speed, 0)
self.rect = self.rect.clamp(SCREENRECT)
if direction < 0:
self.image = self.images[0]
elif direction > 0:
self.image = self.images[1]
self.rect.top = self.origtop - (self.rect.left/self.bounce%2)



def main(winstyle = 0): # Initialize pygame pygame.init() if pygame.mixer and not pygame.mixer.get_init(): print 'Warning, no sound' pygame.mixer = None

   # Set the display mode
   winstyle = 0  # |FULLSCREEN
   bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32)
   screen = pygame.display.set_mode(SCREENRECT.size, winstyle, bestdepth)

   #Load images, assign to sprite classes
   #(do this before the classes are used, after screen setup)
   img = load_image('images/shared/largetux-walk-left-1.png')
   Player.images = [img, pygame.transform.flip(img, 1, 0)]

   #decorate the game window
   #icon = pygame.transform.scale(Alien.images[0], (32, 32))
   #pygame.display.set_icon(icon)
   pygame.display.set_caption('CCN 2 Month Game')
   pygame.mouse.set_visible(1)

   #create the background, tile the bgd image
   bgdtile = load_image('background.gif')
   background = pygame.Surface(SCREENRECT.size)
   for x in range(0, SCREENRECT.width, bgdtile.get_width()):
       background.blit(bgdtile, (x, 0))
   screen.blit(background, (0,0))
   pygame.display.flip()

   #load the sound effects
   if pygame.mixer:
       music = os.path.join('data', 'house_lo.wav')
       pygame.mixer.music.load(music)
       pygame.mixer.music.play(-1)

   # Initialize Game Groups
   shots = pygame.sprite.Group()
   all = pygame.sprite.RenderUpdates()

   #assign default groups to each sprite class
   Player.containers = all
   #Shot.containers = shots, all

   #Create Some Starting Values
   clock = pygame.time.Clock()

   #initialize our starting sprites
   player = Player()
   while player.alive():

       #get input
       for event in pygame.event.get():
           if event.type == QUIT or \
               (event.type == KEYDOWN and event.key == K_ESCAPE):
                   return
       keystate = pygame.key.get_pressed()

       # clear/erase the last drawn sprites
       all.clear(screen, background)

       #update all the sprites
       all.update()
       direction = ''

#handle player input
direction = keystate[K_RIGHT] - keystate[K_LEFT]
player.move(direction)
# time.sleep(.05)
#draw the scene
dirty = all.draw(screen)
pygame.display.update(dirty)


       #cap the framerate
       clock.tick(40)

   if pygame.mixer:
       pygame.mixer.music.fadeout(1000)
   pygame.time.wait(1000)



#call the "main" function if running this script
if __name__ == '__main__': main()