def renderBackground(self): floor = league.Spritesheet(self.map.floor[0], 16, self.map.floor[1]) wall = league.Spritesheet(self.map.walls[0], 16, self.map.walls[1]) decoration = league.Spritesheet(self.map.decoration[0], 16, self.map.decoration[1]) decoration2 = league.Spritesheet(self.map.decoration2[0], 16, self.map.decoration2[1]) floorLayer = league.Tilemap(self.map.floor[2], floor, layer=0) wallLayer = league.Tilemap(self.map.walls[2], wall, layer=1) decoLayer = league.Tilemap(self.map.decoration[2], decoration, layer=2) deco2Layer = league.Tilemap(self.map.decoration2[2], decoration2, layer=3) world_size = (floorLayer.wide * league.Settings.tile_size, floorLayer.high * league.Settings.tile_size) self.engine.mapDrawables.add(floorLayer.passable.sprites()) self.engine.mapDrawables.add(wallLayer.impassable.sprites()) self.engine.mapDrawables.add(decoLayer.impassable.sprites()) self.engine.mapDrawables.add(deco2Layer.impassable.sprites()) # list of all impassable objects that are only used in game.py for collisions return world_size
def renderForeGround(self): ceiling = league.Spritesheet(self.map.ceiling[0], 16, self.map.ceiling[1]) ceilingLayer = league.Tilemap(self.map.ceiling[2], ceiling, layer=4) self.engine.mapDrawables.add(ceilingLayer.impassable.sprites()) self.all_impassables.append(ceilingLayer.impassable.sprites())
def init_map(engine): """Create map and background""" league.Settings.tile_size = 16 league.Settings.fill_color = (31, 38, 84) sprites = league.Spritesheet('./assets/tileset-collapsed.png', league.Settings.tile_size, 14) level1 = league.Tilemap('./assets/level1.lvl', sprites, layer = 1) world_size = (level1.wide*league.Settings.tile_size, level1.high*league.Settings.tile_size) engine.drawables.add(level1.passable.sprites()) background = Background('./assets/near-buildings-bg.png') engine.drawables.add(background)
def __parse(self): """This function begins the process of (attempting) to parse a level file. The structure of the file is described above. """ #default image for replacing tiles with blank images defaultImg = league.Spritesheet( '../assets/map assets/sprite sheets/Hospital Tiles/TileA5_PHC_Interior-Hospital.png', 16, 1) with open(self.path, 'r') as f: reader = csv.reader(f) contents = list(reader) # How many tiles wide is our world? self.wide = int(contents[0][0]) # And how tall? self.high = int(contents[1][0]) # Sprite numbers for all tiles are in the # multidimensional list "world". self.world = contents[2:] a = 0 skipVal = 555 for i in self.world: b = 0 for j in i: x = b * self.spritesheet.tile_size y = a * self.spritesheet.tile_size num = int(j) if (num == skipVal): base_sprite = defaultImg.sprites[25] else: if num < 0: base_sprite = self.spritesheet.sprites[abs(num)] else: base_sprite = self.spritesheet.sprites[num] sprite = Drawable(self.layer) sprite.image = base_sprite.image # Set rectangle coords (using top-left coords here) rect = sprite.image.get_rect() rect.x = x rect.y = y sprite.x = x sprite.y = y sprite.rect = rect if num < 0: self.impassable.add(sprite) else: self.passable.add(sprite) b = b + 1 a = a + 1
def __init__(self, x, y): """ Inits the victory item. sets its location and sets won to false. param - x,y: the coordinates for the victory item """ super().__init__(self) self._layer = 10 self.x = x self.y = y self.won = False self.spritesheet = league.Spritesheet( './assets/18_midnight_spritesheet.png', 100, 8) self.image = pygame.Surface((100, 100)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.image_index_x = 0 self.image_index_y = 0
def __init__(self, player): super().__init__(self) self._layer = 1000 self.player = player self.image = pygame.Surface([480, 480]).convert_alpha() self.image.fill((255, 255, 255, 0)) self.rect = self.image.get_rect() self.x = 100 self.y = 10 self.rect.x = 100 self.rect.y = 10 self.static = True # set up any text display stuffs self.font = pygame.font.Font('freesansbold.ttf', 32) self.text = self.font.render( str(self.player.health) + " 4 lives", True, (255, 255, 255)) # set up action meter for user interaction self.action_meter_sheet = league.Spritesheet( "../assets/player/action_meter.png", 16, 3) self.action_meter_sprites = self.action_meter_sheet.sprites self.action_meter_index = 0 self.action_meter_len = len(self.action_meter_sprites) - 2 self.action_meter_animation_speed = self.player.interaction_timer // self.action_meter_len + 3 self.action_meter_animation_counter = self.action_meter_animation_speed # set up inventory gui stuffs self.ui_box = pygame.image.load("../assets/overlay assets/ui_box.png") self.item_icons = [ pygame.image.load('../assets/icons/beartrap.png').convert_alpha(), pygame.image.load( '../assets/game objects/lantern.png').convert_alpha(), pygame.image.load( '../assets/icons/rancidmeat.png').convert_alpha() ]
def main(): gamin = True #reset indicator while (gamin): gamin = False e = league.Engine("Go Nutts!") e.init_pygame() ##sprites = league.Spritesheet('../assets/base_chip_pipo.png', league.Settings.tile_size, 8) backgroundMaterial = league.Spritesheet( '../assets/woodlandMaterials.png', league.Settings.tile_size, 5) directionalInfo = league.Spritesheet('../assets/directions.png', league.Settings.tile_size, 3) t = league.Tilemap('../assets/woodland.lvl', backgroundMaterial, layer=1) ##d = league.Tilemap('../assets/directions.lvl', directionalInfo, layer = 2) b = league.Tilemap('../assets/background.lvl', backgroundMaterial, layer=0) world_size = (t.wide * league.Settings.tile_size, t.high * league.Settings.tile_size) e.drawables.add(b.passable.sprites()) e.drawables.add(t.passable.sprites()) ##e.drawables.add(d.passable.sprites()) m = SoundManager() m.bgm_start('Song_For_Someone.wav') p = Player(2, 420, 120) o = Overlay(p) bu = MusicButton() p.blocks.add(t.impassable) p.world_size = world_size p.rect = p.image.get_rect() s = Spider(10, 2810, 2527, 210, "v") s.blocks.add(t.impassable) s.world_size = world_size s.rect = s.image.get_rect() s2 = Spider(10, 1590, 630, 200, "v") s2.blocks.add(t.impassable) s2.world_size = world_size s2.rect = s2.image.get_rect() s3 = Spider(10, 3090, 700, 230, "v") s3.blocks.add(t.impassable) s3.world_size = world_size s3.rect = s3.image.get_rect() s4 = Spider(10, 3530, 957, 200, "fL") s4.blocks.add(t.impassable) s4.world_size = world_size s4.rect = s4.image.get_rect() b = Bee(10, 3482, 3134, 220, "v") b.blocks.add(t.impassable) b.world_size = world_size b.rect = b.image.get_rect() b2 = Bee(10, 2486, 790, 200, "s") b2.blocks.add(t.impassable) b2.world_size = world_size b2.rect = b2.image.get_rect() b3 = Bee(10, 5940, 1078, 170, "fL") b3.blocks.add(t.impassable) b3.world_size = world_size b3.rect = b3.image.get_rect() b4 = Bee(10, 6185, 1509, 250, "s") b4.blocks.add(t.impassable) b4.world_size = world_size b4.rect = b4.image.get_rect() ac = Acorn(10, 7000, 3300) ac.blocks.add(t.impassable) ac.world_size = world_size ac.rect = ac.image.get_rect() l = Leafbug(10, 2213, 3103, 500, "h") l.blocks.add(t.impassable) l.world_size = world_size l.rect = l.image.get_rect() l2 = Leafbug(10, 1230, 167, 400, "h") l2.blocks.add(t.impassable) l2.world_size = world_size l2.rect = l2.image.get_rect() l3 = Leafbug(10, 4259, 30, 150, "s") l3.blocks.add(t.impassable) l3.world_size = world_size l3.rect = l3.image.get_rect() l4 = Leafbug(10, 6559, 1215, 250, "h") l4.blocks.add(t.impassable) l4.world_size = world_size l4.rect = l4.image.get_rect() resetL = Overlay_Button(200, 250, False, " Reset", (209, 45, 25), (0, 0, 0), (255, 255, 255), e) quL = Overlay_Button(375, 250, False, " Quit", (209, 45, 25), (0, 0, 0), (255, 255, 255), e) resetW = Overlay_Button(200, 250, False, " Reset", (53, 50, 150), (209, 45, 25), (255, 255, 255), e) quW = Overlay_Button(375, 250, False, " Quit", (53, 50, 150), (209, 45, 25), (255, 255, 255), e) lose = Lose_Overlay(p, resetL, quL, e) w = Win_Overlay(p, resetW, quW, e) c = league.LessDumbCamera(800, 400, p, e.drawables, world_size) e.objects.append(p) e.objects.append(b) e.objects.append(b2) e.objects.append(b3) e.objects.append(b4) e.objects.append(ac) e.objects.append(l) e.objects.append(l2) e.objects.append(l3) e.objects.append(l4) e.objects.append(s) e.objects.append(s2) e.objects.append(s3) e.objects.append(s4) #Adding Drawables e.drawables.add(p) e.drawables.add(s) e.drawables.add(b) e.drawables.add(b2) e.drawables.add(b3) e.drawables.add(b4) e.drawables.add(ac) e.drawables.add(l) e.drawables.add(l2) e.drawables.add(l3) e.drawables.add(l4) e.drawables.add(s) e.drawables.add(s2) e.drawables.add(s3) e.drawables.add(s4) e.drawables.add(o) e.drawables.add(bu) e.objects.append(resetL) e.objects.append(quL) e.objects.append(resetW) e.objects.append(quW) e.objects.append(lose) e.objects.append(c) e.objects.append(o) e.objects.append(w) e.objects.append(bu) e.drawables.add(quL) e.drawables.add(resetL) e.drawables.add(quW) e.drawables.add(resetW) e.drawables.add(lose) e.drawables.add(w) e.collisions[(p, p.ouch)] = [s, s2, s3, s4, b, b2, b3, b4, l, l2, l3, l4] e.collisions[(p, p.win)] = [ac] #Bees pygame.time.set_timer(pygame.USEREVENT + 1, 100 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 2, 100 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 3, 100 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 4, 100 // league.Settings.gameTimeFactor) #Leafbugs pygame.time.set_timer(pygame.USEREVENT + 10, 100 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 11, 100 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 12, 100 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 13, 100 // league.Settings.gameTimeFactor) #Spiders pygame.time.set_timer(pygame.USEREVENT + 20, 100 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 21, 100 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 22, 100 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 23, 100 // league.Settings.gameTimeFactor) #Nutthaniel pygame.time.set_timer(pygame.USEREVENT + 30, 10 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 31, 5 // league.Settings.gameTimeFactor) #Acorn pygame.time.set_timer(pygame.USEREVENT + 5, 100 // league.Settings.gameTimeFactor) e.add_key(pygame.K_a, p.move_left) e.add_key(pygame.K_d, p.move_right) e.add_key(pygame.K_w, p.move_up) e.add_key(pygame.K_s, p.move_down) e.add_key(pygame.K_q, p.print_place) e.add_key(pygame.K_b, p.climb_on) e.add_key(pygame.K_SPACE, p.jump) e.events[pygame.MOUSEBUTTONDOWN] = bu.mouse_click e.events[pygame.USEREVENT + 1] = b.move e.events[pygame.USEREVENT + 2] = b2.move e.events[pygame.USEREVENT + 3] = b3.move e.events[pygame.USEREVENT + 4] = b4.move e.events[pygame.USEREVENT + 5] = ac.move e.events[pygame.USEREVENT + 10] = l.move e.events[pygame.USEREVENT + 11] = l2.move e.events[pygame.USEREVENT + 12] = l3.move e.events[pygame.USEREVENT + 13] = l4.move e.events[pygame.USEREVENT + 20] = s.move e.events[pygame.USEREVENT + 21] = s2.move e.events[pygame.USEREVENT + 22] = s3.move e.events[pygame.USEREVENT + 23] = s4.move e.events[pygame.USEREVENT + 30] = p.move_down_gravity e.events[pygame.USEREVENT + 31] = p.update_jump e.events[pygame.QUIT] = e.stop e.run() if e.reset: gamin = True
def main(): e = league.Engine("The Lone Ranger") e.init_pygame() sprites = league.Spritesheet('./assets/base_chip_pipo.png', league.Settings.tile_size, 8) t = league.Tilemap('./assets/world.lvl', sprites, layer=1) b = league.Tilemap('./assets/background.lvl', sprites, layer=0) world_size = (t.wide * league.Settings.tile_size, t.high * league.Settings.tile_size) e.drawables.add(t.passable.sprites()) e.drawables.add(b.passable.sprites()) p = Player(2, 400, 300) o = Overlay(p) p.blocks.add(t.impassable) p.world_size = world_size p.rect = p.image.get_rect() e.drawables.add(p) e.drawables.add(o) c = league.LessDumbCamera(800, 600, p, e.drawables, world_size) e.objects.append(c) e.objects.append(o) wave = 1 zombieCount = 0 #Register events? pygame.time.set_timer(pygame.USEREVENT + 0, 250 // league.Settings.gameTimeFactor) pygame.time.set_timer(pygame.USEREVENT + 1, 250 // league.Settings.gameTimeFactor) #Player movements zombies = [] def shoot(time): now = pygame.time.get_ticks() if now - p.lastShot > 500: bullet = Bullet(p.direction, 10, p.x, p.y) e.objects.append(bullet) e.drawables.add(bullet) for z in zombies: e.collisions[z] = (bullet, z.ouch) p.lastShot = now ''' Spawns Zombies with a 25% freqency every 250ms ''' def spwanZombies(time): if p.zombieCount < p.wave * 5: if random.randint(1, 100) > 75: z = Zombie(p, 10, p.x, p.y) e.drawables.add(z) e.objects.append(z) e.collisions[z] = (p, p.ouch) p.zombieCount = p.zombieCount + 1 zombies.append(z) def moveAndShoot(time): keys = pygame.key.get_pressed() if keys[pygame.K_w] or keys[pygame.K_a] or keys[pygame.K_s] or keys[ pygame.K_d]: p.move(time, pygame.key.get_pressed()) if keys[pygame.K_SPACE]: shoot(time) ''' Moves zombies every 250ms ''' def updateZoms(time): for i in e.objects: if isinstance(i, Zombie): if random.randint(1, 100) > 25: i.move_towards_player(time) if i.health <= 0: e.objects.remove(i) e.drawables.remove(i) if isinstance(i, Bullet): if i.ttl > 10: i.kill() e.objects.remove(c) e.objects.append(c) e.key_events[pygame.K_a] = moveAndShoot e.key_events[pygame.K_d] = moveAndShoot e.key_events[pygame.K_w] = moveAndShoot e.key_events[pygame.K_s] = moveAndShoot e.key_events[pygame.K_SPACE] = moveAndShoot #Register zombie movements in event list e.events[pygame.USEREVENT] = updateZoms e.events[pygame.USEREVENT + 1] = spwanZombies #Sets up exit button to quit game e.events[pygame.QUIT] = e.stop #Runs the main game loop e.run()
def init_map(engine, player, gravity): """ Loads up all the assets for the game map and background sprites. Updates actors to hold world data. Does an inital render of all game objects. param - engine: The engine being used for this game instance param - player: The player being used for this game instance param - enemy_list: A list of enemey objects that are in this game instance. """ league.Settings.tile_size = 16 league.Settings.fill_color = (0, 0, 60) # league.Settings.tile_scale = 1.7 sprites = league.Spritesheet('./assets/tileset-collapsed.png', league.Settings.tile_size, 14) level1 = league.Tilemap('./assets/level1.lvl', sprites, layer=2) world_size = (level1.wide * league.Settings.tile_size, level1.high * league.Settings.tile_size) # initialize camera cam = CameraUpdates(player, world_size) engine.objects.append(cam) engine.drawables = cam # allow camera to override draw() # add in background and level1 index = 0 while index < world_size[0]: full_background = Background('./assets/skyline-a.png', x=index, layer=0) background = Background('./assets/buildings-bg.png', x=index, layer=1) engine.drawables.add(full_background) engine.drawables.add(background) index += league.Settings.width engine.drawables.add(level1.passable.sprites()) # Gravity must be appended first engine.objects.append(gravity) player.world_size = world_size player.blocks.add(level1.impassable) engine.collisions[player] = [] engine.objects.append(player) engine.drawables.add(player) place_random_items(engine, world_size, player) flag = Flag(world_size[0] - (league.Settings.tile_size * 10), world_size[1]) engine.collisions[player].append((flag, flag.win)) engine.objects.append(flag) engine.drawables.add(flag) # add background music with map creation ### MUSIC IS BROKEN WON' REPEAT # pygame.mixer.music.load('assets/Blazer Rail.wav') # pygame.mixer.music.play(-1, 0.0) for enemy in engine.enemy_list: enemy.world_size = world_size enemy.rect = enemy.image.get_rect() enemy.blocks.add(level1.impassable) engine.objects.append(enemy) engine.drawables.add(enemy) engine.collisions[player].append((enemy, player.take_dmg))
def __init__(self, user, g_engine): """Calls the first game screen and creates data structures to pass to update. user is the player class. g_engine is the league engine. door_x and door_y set the player position when moving between screens. drawables holds all the objects that will be placed on top of the tilemap. collisions holds tuples of objects and their corresponding method call blocking_object adds sprites to the player's block group. change_flag changes the screen in update() when true. add_flag adds sprites to overlay some added blocking sprite in update() (not being used). move_player_to_door moves the player to the last used door in update(). last_drawables stores a list of objects draw from a screen to remove when changing. last_collisions stores a list of collision from a screen to remove when changing last_blocking_object list of blocking objects to remove now is the current time last_switch is the time the screen last changed sprites_utu is a sprite sheet from ProjectUtumno_full.png sprites_base is a sprite sheet from base_chip_pipo.png south_door_image is a flipped image used for all south doors background is a tilemap for covering white show through terrain is a tilemap for the basic terrain/full covering sprites detail is a tilemap for additional sprites that show whats under them world_size is how many tiles the .lvl says it has times the size league.Settings says the tiles are. Currently only used to set the player boundary and is set once set_screen_beach loads the first screen""" self.engine = g_engine self.user = user self.enemy = Enemy(self.engine, self.user, "../assets/imp - walk - vanilla.png", "../assets/imp - attack - vanilla.png", 100, 10, 200, 200) self.engine.drawables.add(self.user) self.door_x = 350 self.door_y = 250 self.drawables = [] self.collisions = [] self.objects = [] self.blocking_object = pygame.sprite.Group() self.change_flag = True self.add_flag = False self.move_player_to_door = True self.terrain_switch_flag = False self.boss_door_east_flag = False self.boss_door_west_flag = False self.last_drawables = [] self.last_collisions = [] self.last_objects = [] self.last_blocking_objects = pygame.sprite.Group() self.switching_objects = [] self.switching_collision_list = [] self.last_switching_collision_list = [] self.enemy_list = [] self.now = pygame.time.get_ticks() self.last_switch = pygame.time.get_ticks() + 1000 self.next_terrain_switch_time = 0 self.switch_interval = 0 self.path_count = 0 self.current_path = 0 self.last_path = -1 self.sprites_utu = league.Spritesheet('../assets/ProjectUtumno_full.png', league.Settings.tile_size, 64) self.sprites_base = league.Spritesheet('../assets/base_chip_pipo.png', league.Settings.tile_size, 8) self.south_door_image = pygame.transform.flip(self.sprites_utu.sprites[1094].image, 0, 1) self.background = league.Tilemap('../assets/blank.lvl', self.sprites_utu, layer=1) self.terrain = league.Tilemap('../assets/blank.lvl', self.sprites_utu, layer = 2) self.details = league.Tilemap('../assets/blank.lvl', self.sprites_utu, layer = 3) self.world_size = (self.terrain.wide*league.Settings.tile_size, self.terrain.high * league.Settings.tile_size) self.user.world_size = self.world_size self.set_screen_beach()
def all_done(self): print("Peace out ya'll.") pygame.quit() sys.exit() league.Settings.width = 768 league.Settings.height = 768 e = league.Engine("Really sucky PvZ") e.init_pygame() e.events[pygame.QUIT] = all_done sprites = league.Spritesheet('./assets/base_chip_pipo.png', league.Settings.tile_size, 8) t = league.Tilemap('./assets/defense.lvl', sprites, layer=0) scs = [] scs_b = [] sc = sprites.sprites[155] sc_b = sprites.sprites[163] for i in range(6): scs.append(league.Drawable()) scs_b.append(league.Drawable()) scs[i].image = sc.image scs_b[i].image = sc_b.image scs[i].rect = scs[i].image.get_rect() scs_b[i].rect = scs_b[i].image.get_rect() scs[i].rect.x = 64 scs_b[i].rect.x = 64