Пример #1
0
    def __init__(self):

        pygame.display.set_caption('Platformer')

        self.clock = pygame.time.Clock()
        self.last_tick = pygame.time.get_ticks()
        self.screen_res = [750, 500]

        self.font = pygame.font.SysFont("Impact", 55)
        self.screen = pygame.display.set_mode(self.screen_res,
                                              pygame.HWSURFACE, 32)

        self.entities = pygame.sprite.Group()
        self.solids = pygame.sprite.Group()
        self.maploader = MapLoader(self)
        self.maploader.load(2)
        self.player = self.maploader.player
        self.camera = self.maploader.camera

        self.entities.add(self.solids)
        self.entities.add(self.player)

        self.clock.tick(60)
        while 1:
            self.Loop()
Пример #2
0
    def __init__(self):

        pygame.display.set_caption('Platformer')

        self.clock = pygame.time.Clock()
        self.last_tick = pygame.time.get_ticks()
        self.screen_res = [750, 500]

        self.font = pygame.font.SysFont("Impact", 55)
        self.screen = pygame.display.set_mode(self.screen_res, pygame.HWSURFACE, 32)

        self.entities = pygame.sprite.Group()
        self.solids = pygame.sprite.Group()
        self.maploader = MapLoader(self)
        self.maploader.load(1)
        self.player = self.maploader.player
        self.camera = self.maploader.camera
        
        self.entities.add(self.solids)
        self.entities.add(self.player)

        self.clock.tick(60)
        while 1:
            self.Loop()
Пример #3
0
class Game():
    def __init__(self):

        pygame.display.set_caption('Platformer')

        self.clock = pygame.time.Clock()
        self.last_tick = pygame.time.get_ticks()
        self.screen_res = [750, 500]

        self.font = pygame.font.SysFont("Impact", 55)
        self.screen = pygame.display.set_mode(self.screen_res,
                                              pygame.HWSURFACE, 32)

        self.entities = pygame.sprite.Group()
        self.solids = pygame.sprite.Group()
        self.maploader = MapLoader(self)
        self.maploader.load(2)
        self.player = self.maploader.player
        self.camera = self.maploader.camera

        self.entities.add(self.solids)
        self.entities.add(self.player)

        self.clock.tick(60)
        while 1:
            self.Loop()

    def Loop(self):
        # main game loop
        self.eventLoop()

        self.Tick()
        self.Draw()
        pygame.display.update()

    def eventLoop(self):
        # the main event loop, detects keypresse
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_RETURN:
                    self.reset()
                    self.maploader.load(2)

                if event.key == K_SPACE:
                    self.player.jump()

    def Tick(self):
        self.ttime = self.clock.tick()
        self.mpos = pygame.mouse.get_pos()
        self.keys_pressed = pygame.key.get_pressed()

    def reset(self):
        self.maploader.layers = []
        player = self.maploader.player
        self.entities.empty()
        self.solids.empty()
        self.player = player

        self.entities.add(self.player)

    def Draw(self):
        self.screen.fill((150, 150, 150))

        for l in self.maploader.layers[::
                                       -1]:  #Update layers (have to reverse list to blit properly)
            self.screen.blit(l.image, self.camera.apply_layer(l))

        self.player.update(self.ttime / 1000.)
        self.camera.update(self.player)

        for e in self.entities:  #update blocks etc.
            self.screen.blit(e.image, self.camera.apply(e))
Пример #4
0
    def run(self):
        pygame.init()
        flags = 0
        if len(sys.argv) > 1:
            if sys.argv[1] == 'f':
                flags = pygame.FULLSCREEN
        screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), flags)
        icon = pygame.image.load("icon.png").convert()
        pygame.display.set_icon(icon)
        pygame.display.set_caption("La Trappe")
        clock = pygame.time.Clock()

        self.olutta = 100
        self.kaljaa = 50
        self.meat = BarAmount(0)
        self.grain = BarAmount(0)
        self.beer = BarAmount(0)

        ResourceFactory.resource_created_subscribers.append(self)
        ResourceFactory.resource_destroyed_subscribers.append(self)

        self.CreateTestVillage()
        self.randomizeNpcLocations()
        maploader = MapLoader()
        self.map = maploader.load_map("level")
        self.tiledisplay = TileDisplay(screen, self.city, self.map)
        self.statdisplay = Display(screen)
        self.initStatDisplay(self.statdisplay)
        self.window = self.tiledisplay
        #self.camera_movement = (0,0)
        self.time = 0
        self.player = self.city.get_controlled_player()

        Npc.defaultFoodConsumption = 0 #no food problems :)
        #self.testBars()
        while 1:
            screen.fill((0,0,0))
            self.window.reset()
            self.testText()
            
            #self.testNPC()
            self.statdisplay.visualizeNPCs(self.city.npcs)

            #self.UpdateVillage()
            self.moveNpcs()

            Scheduler.instance().advance(5)

            for npc in self.city.npcs:
                npc.advance(5)

            for player in self.city.players:
                player.advance(5)

            for animal in self.city.animals:
                animal.advance(5)

            #self.moveCamera()
            self.window.advance(self.time)
            self.window.draw()
            pygame.display.update()

            msElapsed = clock.tick(30)
            self.time += 30
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if (event.key == K_ESCAPE):
                        pygame.quit()
                        sys.exit()
                    if (event.key == K_1):
                        print 'Switching to tile display.'
                        self.window = self.tiledisplay
                        # Show tile display
                        pass
                    if (event.key == K_2):
                        print 'Switching to statistics display.'
                        self.window = self.statdisplay
                        # Show statistics display
                        pass
                    if (event.key == K_RIGHT):
                        self.player.move(2, 0)

                    if (event.key == K_LEFT):
                        self.player.move(-2, 0)

                    if (event.key == K_UP):
                        self.player.move(0, -2)

                    if (event.key == K_DOWN):
                        self.player.move(0, 2)

                if event.type == KEYUP:
                    self.player.move(0, 0)
Пример #5
0
    def start(self):
        pygame.init()
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        icon = pygame.image.load("icon.png").convert()
        pygame.display.set_icon(icon)
        pygame.display.set_caption("La Trappe - Level editor")
        self.TILE_WIDTH = 32
        self.TILE_HEIGHT = 32
        self.selectedTile = 1
        self.filename = "level"
        # self.city = City()
        self.maploader = MapLoader()
        self.map = self.maploader.load_map("level")
        loader = Loader()
        self.city = loader.load_city("city.txt")
        self.display = TileDisplay(self.screen, self.city, self.map)
        self.cursor = (0, 0)
        # print str(self.map)

        self.editmode = "CURSOR"
        while 1:
            self.screen.fill((0, 0, 0))

            for event in pygame.event.get():
                # Keyboard handling
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        pygame.quit()
                        sys.exit()
                    if event.key == K_1:
                        self.display.draw_background = not self.display.draw_background
                    if event.key == K_2:
                        self.display.draw_foreground = not self.display.draw_foreground
                    if event.key == K_3:
                        self.display.draw_objects = not self.display.draw_objects

                    if event.key == K_RIGHT:
                        self.display.move_camera(192, 0)

                    if event.key == K_LEFT:
                        self.display.move_camera(-192, 0)

                    if event.key == K_UP:
                        self.display.move_camera(0, -192)

                    if event.key == K_DOWN:
                        self.display.move_camera(0, 192)

                    if event.key == K_SPACE:
                        self.do_action()

                    if event.key == K_t:
                        print "Add tile mode"
                        self.editmode = "ADD_TILE"
                        # self.choose_tile()

                    if event.key == K_f:
                        print "Add foreground mode"
                        self.editmode = "ADD_FOREGROUND"

                    if event.key == K_s:
                        print "Saving map"
                        self.save_map()

                    if event.key == K_PAGEUP:
                        # print "Selecting next tile"
                        tiles = self.display.MAP_CACHE[self.display.tileset]
                        tile = self.selectedTile + 1
                        if tile > (len(tiles) - 1):
                            tile = 0

                        self.selectedTile = tile
                        print "Selected tile: " + str(tile)

                    if event.key == K_PAGEDOWN:
                        # print "Selecting next tile"
                        tiles = self.display.MAP_CACHE[self.display.tileset]
                        tile = self.selectedTile - 1
                        if tile < 0:
                            tile = len(tiles) - 1

                        self.selectedTile = tile
                        print "Selected tile: " + str(tile)

                # Mouse handling
                elif event.type == MOUSEBUTTONUP:
                    mousex, mousey = pygame.mouse.get_pos()
                    print "Mouse clicked: " + str(mousex) + "," + str(mousey)
                    self.do_action()

            self.display.draw()
            self.draw_overlays()
            pygame.display.update()
Пример #6
0
class LevelEditor:

    editmodes = ["ADD_TILE", "ADD_OBJECT", "CURSOR"]

    def start(self):
        pygame.init()
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        icon = pygame.image.load("icon.png").convert()
        pygame.display.set_icon(icon)
        pygame.display.set_caption("La Trappe - Level editor")
        self.TILE_WIDTH = 32
        self.TILE_HEIGHT = 32
        self.selectedTile = 1
        self.filename = "level"
        # self.city = City()
        self.maploader = MapLoader()
        self.map = self.maploader.load_map("level")
        loader = Loader()
        self.city = loader.load_city("city.txt")
        self.display = TileDisplay(self.screen, self.city, self.map)
        self.cursor = (0, 0)
        # print str(self.map)

        self.editmode = "CURSOR"
        while 1:
            self.screen.fill((0, 0, 0))

            for event in pygame.event.get():
                # Keyboard handling
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        pygame.quit()
                        sys.exit()
                    if event.key == K_1:
                        self.display.draw_background = not self.display.draw_background
                    if event.key == K_2:
                        self.display.draw_foreground = not self.display.draw_foreground
                    if event.key == K_3:
                        self.display.draw_objects = not self.display.draw_objects

                    if event.key == K_RIGHT:
                        self.display.move_camera(192, 0)

                    if event.key == K_LEFT:
                        self.display.move_camera(-192, 0)

                    if event.key == K_UP:
                        self.display.move_camera(0, -192)

                    if event.key == K_DOWN:
                        self.display.move_camera(0, 192)

                    if event.key == K_SPACE:
                        self.do_action()

                    if event.key == K_t:
                        print "Add tile mode"
                        self.editmode = "ADD_TILE"
                        # self.choose_tile()

                    if event.key == K_f:
                        print "Add foreground mode"
                        self.editmode = "ADD_FOREGROUND"

                    if event.key == K_s:
                        print "Saving map"
                        self.save_map()

                    if event.key == K_PAGEUP:
                        # print "Selecting next tile"
                        tiles = self.display.MAP_CACHE[self.display.tileset]
                        tile = self.selectedTile + 1
                        if tile > (len(tiles) - 1):
                            tile = 0

                        self.selectedTile = tile
                        print "Selected tile: " + str(tile)

                    if event.key == K_PAGEDOWN:
                        # print "Selecting next tile"
                        tiles = self.display.MAP_CACHE[self.display.tileset]
                        tile = self.selectedTile - 1
                        if tile < 0:
                            tile = len(tiles) - 1

                        self.selectedTile = tile
                        print "Selected tile: " + str(tile)

                # Mouse handling
                elif event.type == MOUSEBUTTONUP:
                    mousex, mousey = pygame.mouse.get_pos()
                    print "Mouse clicked: " + str(mousex) + "," + str(mousey)
                    self.do_action()

            self.display.draw()
            self.draw_overlays()
            pygame.display.update()

    def save_map(self):
        w = len(self.map)
        h = len(self.map[0])
        self.maploader.save_map(self.map, self.filename, w, h)

    def draw_overlays(self):
        # Draw cursor
        pygame.draw.rect(self.screen, CURSOR_COLOR, Rect(self.cursor, (self.TILE_WIDTH, self.TILE_HEIGHT)), 1)

        # Draw current tile
        tiles = self.display.MAP_CACHE[self.display.tileset]
        tile_image = tiles[self.selectedTile]
        self.screen.blit(tile_image, (0, 0))
        pygame.draw.rect(self.screen, CURSOR_COLOR, Rect((0, 0), (self.TILE_WIDTH, self.TILE_HEIGHT)), 1)

    def do_action(self):
        print "Doing action: " + self.editmode
        if self.editmode == "CURSOR":
            self.select()

        elif self.editmode == "ADD_TILE":
            self.add_tile()

    def choose_tile(self):
        pass

    def add_tile(self):
        self.select()
        tilex, tiley = self.get_nearest_tile(
            self.cursor[0] + self.display.camerax, self.cursor[1] + self.display.cameray
        )
        if (tilex > len(self.map)) or (tiley > len(self.map[0])):
            print "Cursor out of map bounds!"
            return

        self.map[tilex][tiley] = self.selectedTile
        # print str(self.map[tiley])
        # self.map[tiley] = self.map[tiley][:tilex] + "w" + self.map[tiley][tilex+1:]

    def select(self):
        mousex, mousey = pygame.mouse.get_pos()
        tilex, tiley = self.get_nearest_tile(mousex, mousey)
        self.cursor = (tilex * self.TILE_WIDTH, tiley * self.TILE_HEIGHT)

    def get_nearest_tile(self, x, y):
        x = x / self.TILE_WIDTH
        y = y / self.TILE_HEIGHT
        return (x, y)
Пример #7
0
class Game():
    def __init__(self):

        pygame.display.set_caption('Platformer')

        self.clock = pygame.time.Clock()
        self.last_tick = pygame.time.get_ticks()
        self.screen_res = [750, 500]

        self.font = pygame.font.SysFont("Impact", 55)
        self.screen = pygame.display.set_mode(self.screen_res, pygame.HWSURFACE, 32)

        self.entities = pygame.sprite.Group()
        self.solids = pygame.sprite.Group()
        self.maploader = MapLoader(self)
        self.maploader.load(1)
        self.player = self.maploader.player
        self.camera = self.maploader.camera
        
        self.entities.add(self.solids)
        self.entities.add(self.player)

        self.clock.tick(60)
        while 1:
            self.Loop()
            
    def Loop(self):
        # main game loop
        self.eventLoop()
        
        self.Tick()
        self.Draw()
        pygame.display.update()


    def eventLoop(self):
        # the main event loop, detects keypresses
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_RETURN:
                    self.reset()
                    self.maploader.load(2)
                    
                if event.key == K_SPACE:
                    self.player.jump()

    def Tick(self):
        self.ttime = self.clock.tick()
        self.mpos = pygame.mouse.get_pos()
        self.keys_pressed = pygame.key.get_pressed()

    def reset(self):
        self.maploader.layers = []
        player = self.maploader.player
        self.entities.empty()
        self.solids.empty()
        self.player = player

        self.entities.add(self.player)

    def Draw(self):
        self.screen.fill((150,150,150))

        for l in self.maploader.layers[::-1]:#Update layers (have to reverse list to blit properly)
            self.screen.blit(l.image, self.camera.apply_layer(l))                

        self.player.update(self.ttime / 1000.)
        self.camera.update(self.player)
        
        for e in self.entities: #update blocks etc.
            self.screen.blit(e.image, self.camera.apply(e))