def __init__(self): super(Level,self).__init__() # set up image dictionary self.picbox = readimagesfile('imagekeys.txt') self.image = self.picbox['foggyforest'] # get width and height from background image self.rect = self.image.get_rect() # background image centered with screen self.rect.x = (WINW - self.rect.width) / 2 self.rect.y = (WINH - self.rect.height) / 2 self.xoriginal = self.rect.x self.yoriginal = self.rect.y # background velocity self.dx = 0 self.dy = 0 # size of actual map # different from screen # may be smaller than image self.truew = 2560 self.trueh = 1440 # sprite lists self.platformlist = pygame.sprite.Group() self.spritelist = pygame.sprite.Group() # platform set up # x,y,w,h platformsprites = ( self.topboundary = platform(0,-50,2560,50), self.leftboundary = platform(-50,-50,50,1540), self.rightboundary = platform(0,1440,2560,50), self.bottomboundary = platform(2560,0,50,1540) ) for p in platformsprites: p.havesthour(self.truew,self.trueh) self.platformlist.add(p) self.spritelist.add(p)
class Baddy(pygame.sprite.Sprite): #### basic attribute declarations #### # track direction baddy is facing direction = 'R' rightface = [] leftface = [] # track distance to player x2prey = 0 y2prey = 0 # some baddies are faster than others (horizontal) baddyspeed = 1 # some baddies... can fly! wings = False # running out of image dictionary names iselfies = readimagesfile('imagekeys.txt') #### class functions #### def __init__(self, player): super(Player, self).__init__() # set up image image = self.iselfies['Batbaddy'] self.rightface.append(image) image = pygame.transform.flip(image, True, False) self.leftface.append(image) self.image = self.rightface[0] # set up rect obj and attributes self.rect = self.image.get_rect() self.rect.w = self.image.get_width() self.rect.h = self.image.get_height() # find your prey self.player = player def update(self): x2prey = self.rect.x - self.player.rect.x y2prey = self.rect.y - self.player.rect.y # in the presence of player if abs(x2prey) < 300: if abs(y2prey) < 200: # to the left to the left if x2prey < 0: self.direction = 'L' self.image = self.leftface[0] if abs(x2prey) > 10: self.rect.x -= baddyspeed # to the right to the right elif x2prey > 0: self.direction = 'R' self.image = self.rightface[0] if abs(x2prey) > 10: self.rect.x += baddyspeed # I can fly if self.wings == True: # in place if abs(y2prey) < 1: self.rect.y = self.player.rect.y # up in the sky elif y2prey < 0: self.rect.y -= 1 # burrowing with wings elif y2prey > 0: self.rect.y += 1
def main(): #### pre-loop preparations #### # activate pygame pygame.init() # activate screen and define dimensions screen = pygame.display.set_mode(WINS) # set game screen caption pygame.display.set_caption('Platformer') # set up image library and images ifolder = readimagesfile('imagekeys.txt') # set up player player = Player(ifolder['ball']) # create each level levellist = [] levellist.append(levels.Level01(player)) levellist.append(levels.Level02(player)) # set current level lvlnum = 0 lvlnow = levellist[lvlnum] nextlevel = 0 # create sprite groups for managing sprites spritesunited = pygame.sprite.Group() # pass in current level class to player player.level = lvlnow # set player position player.rect.x = HWINW player.rect.y = HWINH - player.rect.h # then add player to sprite list spritesunited.add(player) # create main loop bool done = False # set up a clock to manage screen refresh rate clock = pygame.time.Clock() #### main loop ################################################ while not done: #### event handling loop #### # retrieve user actions/events for event in pygame.event.get(): # if window close button clicked if event.type == pygame.QUIT: done = True # if a key is pressed if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: done = True if event.key == pygame.K_LEFT: player.leftmotion() if event.key == pygame.K_RIGHT: player.rightmotion() if event.key == pygame.K_UP: player.jump() if event.key == pygame.K_DOWN: nextlevel = player.useportal() # if a key is released if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT and player.dx < 0: player.stop() if event.key == pygame.K_RIGHT and player.dx > 0: player.stop() # update sprites spritesunited.update() # update level lvlnow.update() # for the following, rgap and lgap are from wallcon if not lvlnow.rlimit: # move background left when player approaches right edge if player.rect.right >= rgap and player.dx > 0: player.rect.right = rgap lvlnow.worldrevolution(-xbackspeed,0) if not lvlnow.llimit: # move background right when player approaches left edge if player.rect.left <= lgap and player.dx < 0: player.rect.left = lgap lvlnow.worldrevolution(xbackspeed,0) # for the following, tgap and bgap are from wallcon if not lvlnow.tlimit: # move background up when player goes up high if player.rect.top <= tgap and player.dy < 0: player.rect.top = tgap lvlnow.worldrevolution(0,ymovespeed/2) if not lvlnow.blimit: # move background down when player goes down if player.rect.bottom >= bgap and player.dy > 0: player.rect.bottom = bgap lvlnow.worldrevolution(0,ymovespeed / 2 * -1) #### moving between levels ################################ if nextlevel != 0: lvlnum = nextlevel - 1 lvlnow = levellist[lvlnum] player.level = lvlnow player.rect.x = player.level.xplaypos player.rect.y = player.level.yplaypos nextlevel = 0 ''' # upon reaching the right edge if lvlnow.worldxshift < lvlnow.xshiftmax: # reposition player.rect.x = 360 # move to next level if lvlnow < len(levellist) - 1: lvlnum += 1 lvlnow = levellist[lvlnum] player.level = lvlnow ''' #### draw to screen object################################# # draw background of level first lvlnow.draw(screen) # then draw sprites to screen spritesunited.draw(screen) #### clock ################################################ clock.tick(FPS) #### refresh actual screen ################################ pygame.display.update() # exit pygame pygame.quit()