示例#1
0
    def __init__(self):
        ''' Constructor '''

        # Call the parents constructor
        pygame.sprite.Sprite.__init__(self)

        spriteSheet = spritesheet.SpriteSheet("resources/images/player.png")

        # Load the static images

        self.standImgR = spriteSheet.get_image(0, 0, 27, 62)
        self.jumpImgR = spriteSheet.get_image(113, 0, 30, 69)
        self.fallImgR = spriteSheet.get_image(143, 0, 35, 67)
        self.attackImgR = spriteSheet.get_image(178, 0, 33, 62)

        self.standImgL = pygame.transform.flip(self.standImgR, True, False)
        self.jumpImgL = pygame.transform.flip(self.jumpImgR, True, False)
        self.fallImgL = pygame.transform.flip(self.fallImgR, True, False)
        self.attackImgL = pygame.transform.flip(self.attackImgR, True, False)

        # Load all the right-facing images into the list
        image = spriteSheet.get_image(0, 0, 27, 62)
        self.walkingFramesR.append(image)
        image = spriteSheet.get_image(27, 0, 31, 62)
        self.walkingFramesR.append(image)
        image = spriteSheet.get_image(58, 0, 27, 62)
        self.walkingFramesR.append(image)
        image = spriteSheet.get_image(85, 0, 28, 62)
        self.walkingFramesR.append(image)

        # Do the same, but flip them to left
        for frame in self.walkingFramesR:
            image = pygame.transform.flip(frame, True, False)
            self.walkingFramesL.append(image)

        # Set the starting image
        self.image = self.standImgR

        # Set a reference to the image rect
        self.rect = self.image.get_rect()

        # Load sounds
        self.jumpSound = pygame.mixer.Sound("resources/sounds/Jump Sound.wav")
        self.deathSound = pygame.mixer.Sound(
            "resources/sounds/Death Sound.wav")
        self.attackSound = pygame.mixer.Sound(
            "resources/sounds/Attack Sound.wav")
        self.jumpSound.set_volume(0.25)
        self.deathSound.set_volume(0.25)
        self.attackSound.set_volume(0.25)

        # Initiate the hearts
        self.heartList = pygame.sprite.Group()

        for n in range(int(constants.PLAYER_HEALTH / 2)):
            newHeart = heart.Heart(n)
            newHeart.player = self
            self.heartList.add(newHeart)
示例#2
0
def run(host, port, ns_host, ns_port, name):
    """

    Runs the server.

    @param host The host for the server

    @param port The port for the server

    """
    hb = heart.Heart(name, host, port, ns_host, ns_port, 1)
    while True:
        try:
            hb.register()
            break
        except:
            pass
    hb.start()
    config.app.run(host=host, port=int(port), debug=True, use_reloader=False)
示例#3
0
    def __init__(self, file_path):
        self.starting_blocks = []
        self.starting_enemies = []
        self.starting_coins = []
        self.starting_powerups = []
        self.starting_flag = []
        self.starting_fires = []

        self.blocks = pygame.sprite.Group()
        self.enemies = pygame.sprite.Group()
        self.coins = pygame.sprite.Group()
        self.powerups = pygame.sprite.Group()
        self.flag = pygame.sprite.Group()
        self.fires = pygame.sprite.Group()

        self.active_sprites = pygame.sprite.Group()
        self.inactive_sprites = pygame.sprite.Group()

        with open(file_path, 'r') as f:
            data = f.read()

        map_data = json.loads(data)

        self.width = map_data['width'] * game.GRID_SIZE
        self.height = map_data['height'] * game.GRID_SIZE

        self.start_x = map_data['start'][0] * game.GRID_SIZE
        self.start_y = map_data['start'][1] * game.GRID_SIZE

        for item in map_data['blocks']:
            x, y = item[0] * game.GRID_SIZE, item[1] * game.GRID_SIZE
            img = game.block_images[item[2]]
            self.starting_blocks.append(block.Block(x, y, img))

        for item in map_data['fires']:
            x, y = item[0] * game.GRID_SIZE, item[1] * game.GRID_SIZE
            self.starting_fires.append(fire.Fire(x, y, game.fire_img))

        for item in map_data['bears']:
            x, y = item[0] * game.GRID_SIZE, item[1] * game.GRID_SIZE
            self.starting_enemies.append(bear.Bear(x, y, game.bear_images))

        for item in map_data['monsters']:
            x, y = item[0] * game.GRID_SIZE, item[1] * game.GRID_SIZE
            self.starting_enemies.append(
                monster.Monster(x, y, game.monster_images))

        for item in map_data['coins']:
            x, y = item[0] * game.GRID_SIZE, item[1] * game.GRID_SIZE
            self.starting_coins.append(coin.Coin(x, y, game.coin_img))

        for item in map_data['oneups']:
            x, y = item[0] * game.GRID_SIZE, item[1] * game.GRID_SIZE
            self.starting_powerups.append(oneUp.OneUp(x, y, game.oneup_img))

        for item in map_data['hearts']:
            x, y = item[0] * game.GRID_SIZE, item[1] * game.GRID_SIZE
            self.starting_powerups.append(heart.Heart(x, y, game.heart_img))

        for i, item in enumerate(map_data['flag']):
            x, y = item[0] * game.GRID_SIZE, item[1] * game.GRID_SIZE

            if i == 0:
                img = game.flag_img
            else:
                img = game.flagpole_img

            self.starting_flag.append(flag.Flag(x, y, img))

        self.background_layer = pygame.Surface([self.width, self.height],
                                               pygame.SRCALPHA, 32)
        self.scenery_layer = pygame.Surface([self.width, self.height],
                                            pygame.SRCALPHA, 32)
        self.inactive_layer = pygame.Surface([self.width, self.height],
                                             pygame.SRCALPHA, 32)
        self.active_layer = pygame.Surface([self.width, self.height],
                                           pygame.SRCALPHA, 32)

        if map_data['background-color'] != "":
            self.background_layer.fill(map_data['background-color'])

        if map_data['background-img'] != "":
            background_img = pygame.image.load(
                map_data['background-img']).convert_alpha()

            if map_data['background-fill-y']:
                h = background_img.get_height()
                w = int(background_img.get_width() * game.HEIGHT / h)
                background_img = pygame.transform.scale(
                    background_img, (w, game.HEIGHT))

            if "top" in map_data['background-position']:
                start_y = 0
            elif "bottom" in map_data['background-position']:
                start_y = self.height - background_img.get_height()

            if map_data['background-repeat-x']:
                for x in range(0, self.width, background_img.get_width()):
                    self.background_layer.blit(background_img, [x, start_y])
            else:
                self.background_layer.blit(background_img, [0, start_y])

        if map_data['scenery-img'] != "":
            scenery_img = pygame.image.load(
                map_data['scenery-img']).convert_alpha()

            if map_data['scenery-fill-y']:
                h = scenery_img.get_height()
                w = int(scenery_img.get_width() * game.HEIGHT / h)
                scenery_img = pygame.transform.scale(scenery_img,
                                                     (w, game.HEIGHT))

            if "top" in map_data['scenery-position']:
                start_y = 0
            elif "bottom" in map_data['scenery-position']:
                start_y = self.height - scenery_img.get_height()

            if map_data['scenery-repeat-x']:
                for x in range(0, self.width, scenery_img.get_width()):
                    self.scenery_layer.blit(scenery_img, [x, start_y])
            else:
                self.scenery_layer.blit(scenery_img, [0, start_y])

        pygame.mixer.music.load(map_data['music'])

        self.gravity = map_data['gravity']
        self.terminal_velocity = map_data['terminal-velocity']

        self.completed = False

        self.blocks.add(self.starting_blocks)
        self.enemies.add(self.starting_enemies)
        self.coins.add(self.starting_coins)
        self.powerups.add(self.starting_powerups)
        self.flag.add(self.starting_flag)
        self.fires.add(self.starting_fires)

        self.active_sprites.add(self.coins, self.enemies, self.powerups,
                                self.fires)
        self.inactive_sprites.add(self.blocks, self.flag)

        # with this speed up blitting on slower computers?
        for s in self.active_sprites:
            s.image.convert()

        for s in self.inactive_sprites:
            s.image.convert()

        self.inactive_sprites.draw(self.inactive_layer)

        # is converting layers helpful at all?
        self.background_layer.convert()
        self.scenery_layer.convert()
        self.inactive_layer.convert()
        self.active_layer.convert()
示例#4
0
    def __init__(self, player):
        """ Create level 2. """

        # Player start position
        self.posx = 290
        self.posy = 580

        # Call the parent constructor
        Level.__init__(self, player)

        # Door
        block = door.Door()
        block.rect.x = 522
        block.rect.y = 0
        block.player = self.player
        self.door_list.add(block)

        # Walls
        self.walls = [[wall.Wall(0), 58, 0], [wall.Wall(1), 116, 0],
                      [wall.Wall(1), 174, 0], [wall.Wall(1), 232, 0],
                      [wall.Wall(1), 290, 0], [wall.Wall(1), 348, 0],
                      [wall.Wall(1), 406, 0], [wall.Wall(1), 464, 0]]
        for w in self.walls:
            block = w[0]
            block.rect.x = w[1]
            block.rect.y = w[2]
            block.player = self.player
            self.walls_list.add(block)

        # Chest
        self.hearts_needed = 4
        block = chest.Chest(4)
        block.rect.x = 58
        block.rect.y = 348
        block.player = self.player
        self.chest_list.add(block)

        # Hearts
        self.hearts = [[heart.Heart(2), 116, 116], [heart.Heart(0), 580, 116],
                       [heart.Heart(0), 58, 638], [heart.Heart(0), 464, 522]]
        for h in self.hearts:
            block = h[0]
            block.rect.x = h[1]
            block.rect.y = h[2]
            block.player = self.player
            self.hearts_list.add(block)

        # Dragon
        #block = snake.Snake()
        #block.rect.x = 406
        #block.rect.y = 348
        #block.player = self.player
        #self.enemy_list.add(block)

        # Array with type of artifacts, and x, y location of them.
        level = [[artifact.Artifact("Assets/Rock.png", self), 348, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 406, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 464, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 348, 116],
                 [artifact.Artifact("Assets/Rock.png", self), 406, 116],
                 [artifact.Artifact("Assets/Rock.png", self), 464, 116],
                 [artifact.Artifact("Assets/Rock.png", self), 348, 464],
                 [artifact.Artifact("Assets/Rock.png", self), 348, 522],
                 [artifact.Artifact("Assets/Rock.png", self), 406, 464],
                 [artifact.Artifact("Assets/Rock.png", self), 406, 522],
                 [artifact.Artifact("Assets/Tree.png", self), 174, 348],
                 [artifact.Artifact("Assets/Tree.png", self), 174, 406],
                 [artifact.Artifact("Assets/Tree.png", self), 232, 348],
                 [artifact.Artifact("Assets/Tree.png", self), 232, 406],
                 [artifact.Artifact("Assets/Tree.png", self), 464, 406],
                 [artifact.Artifact("Assets/Tree.png", self), 522, 406],
                 [artifact.Artifact("Assets/Tree.png", self), 464, 464],
                 [artifact.Artifact("Assets/Tree.png", self), 522, 464],
                 [artifact.Artifact("Assets/Tree.png", self), 406, 174],
                 [artifact.Artifact("Assets/Tree.png", self), 464, 174]]
        for a in level:
            block = a[0]
            block.rect.x = a[1]
            block.rect.y = a[2]
            block.player = self.player
            self.artifact_list.add(block)

        # Green blocks
        block = artifact.Artifact("Assets/Block.png", self, True)
        block.rect.x = 232  #406
        block.rect.y = 580
        block.player = self.player
        self.block_list.add(block)

        waters = [[water.Water(), 58, 232], [water.Water(), 116, 232],
                  [water.Water(), 174, 232], [water.Water(), 232, 232],
                  [water.Water(), 58, 290], [water.Water(), 116, 290],
                  [water.Water(), 174, 290], [water.Water(), 232, 290],
                  [water.Water(), 348, 232], [water.Water(), 406, 232],
                  [water.Water(), 464, 232], [water.Water(), 522, 232],
                  [water.Water(), 348, 290], [water.Water(), 406, 290],
                  [water.Water(), 464, 290], [water.Water(), 522, 290],
                  [water.Water(), 638, 232], [water.Water(), 638, 290],
                  [water.Water(), 638, 348], [water.Water(), 638, 406],
                  [water.Water(), 638, 464], [water.Water(), 638, 522],
                  [water.Water(), 638, 580], [water.Water(), 638, 638],
                  [water.Water(), 580, 638], [water.Water(), 522, 638],
                  [water.Water(), 464, 638], [water.Water(), 406, 638]]

        for w in waters:
            block = w[0]
            block.rect.x = w[1]
            block.rect.y = w[2]
            block.player = self.player
            self.water_list.add(block)

        bridges = [[bridge.Bridge("VERTICAL"), 290, 232],
                   [bridge.Bridge("VERTICAL"), 290, 290],
                   [bridge.Bridge("VERTICAL"), 580, 232],
                   [bridge.Bridge("VERTICAL"), 580, 290]]

        for b in bridges:
            block = b[0]
            block.rect.x = b[1]
            block.rect.y = b[2]
            block.player = self.player
            self.bridge_list.add(block)
示例#5
0
    def __init__(self, player):
        """ Create level 1. """

        # Player start position
        self.posx = 58
        self.posy = 232

        # Call the parent constructor
        Level.__init__(self, player)

        # Door
        block = door.Door()
        block.rect.x = 348
        block.rect.y = 0
        block.player = self.player
        self.door_list.add(block)

        # Walls
        self.walls = [[wall.Wall(0), 58, 0], [wall.Wall(1), 116, 0],
                      [wall.Wall(1), 174, 0], [wall.Wall(1), 232, 0],
                      [wall.Wall(1), 290, 0], [wall.Wall(1), 522, 0],
                      [wall.Wall(1), 580, 0], [wall.Wall(1), 638, 0]]
        for w in self.walls:
            block = w[0]
            block.rect.x = w[1]
            block.rect.y = w[2]
            block.player = self.player
            self.walls_list.add(block)

        # Chest
        self.hearts_needed = 2
        block = chest.Chest(2)
        block.rect.x = 290
        block.rect.y = 580
        block.player = self.player
        self.chest_list.add(block)

        # Hearts
        self.hearts = [[heart.Heart(0), 290, 116], [heart.Heart(2), 638, 290]]
        for h in self.hearts:
            block = h[0]
            block.rect.x = h[1]
            block.rect.y = h[2]
            block.player = self.player
            self.hearts_list.add(block)

        # Snake
        block = snake.Snake(self.player, self)
        block.rect.x = 406
        block.rect.y = 348
        self.enemy_list.add(block)

        # Array with type of artifacts, and x, y location of them.
        level = [[artifact.Artifact("Assets/Rock.png", self), 58, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 58, 116],
                 [artifact.Artifact("Assets/Rock.png", self), 116, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 174, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 232, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 290, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 348, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 232, 116],
                 [artifact.Artifact("Assets/Rock.png", self), 232, 174],
                 [artifact.Artifact("Assets/Rock.png", self), 290, 174],
                 [artifact.Artifact("Assets/Rock.png", self), 348, 174],
                 [artifact.Artifact("Assets/Rock.png", self), 290, 232],
                 [artifact.Artifact("Assets/Rock.png", self), 348, 232],
                 [artifact.Artifact("Assets/Rock.png", self), 290, 290],
                 [artifact.Artifact("Assets/Rock.png", self), 348, 290],
                 [artifact.Artifact("Assets/Rock.png", self), 464, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 464, 116],
                 [artifact.Artifact("Assets/Rock.png", self), 464, 174],
                 [artifact.Artifact("Assets/Rock.png", self), 464, 232],
                 [artifact.Artifact("Assets/Rock.png", self), 464, 290],
                 [artifact.Artifact("Assets/Rock.png", self), 522, 58],
                 [artifact.Artifact("Assets/Rock.png", self), 522, 116],
                 [artifact.Artifact("Assets/Rock.png", self), 522, 174],
                 [artifact.Artifact("Assets/Rock.png", self), 522, 232],
                 [artifact.Artifact("Assets/Rock.png", self), 522, 290],
                 [artifact.Artifact("Assets/Rock.png", self), 522, 348],
                 [artifact.Artifact("Assets/Rock.png", self), 580, 174],
                 [artifact.Artifact("Assets/Rock.png", self), 580, 232],
                 [artifact.Artifact("Assets/Rock.png", self), 58, 580],
                 [artifact.Artifact("Assets/Rock.png", self), 232, 580],
                 [artifact.Artifact("Assets/Rock.png", self), 58, 638],
                 [artifact.Artifact("Assets/Rock.png", self), 116, 638],
                 [
                     artifact.Artifact("Assets/Rock.png", self),
                     174,
                     638,
                 ], [artifact.Artifact("Assets/Rock.png", self), 232, 638],
                 [artifact.Artifact("Assets/Rock.png", self), 290, 638],
                 [artifact.Artifact("Assets/Rock.png", self), 348, 638],
                 [artifact.Artifact("Assets/Tree.png", self), 58, 464],
                 [artifact.Artifact("Assets/Tree.png", self), 58, 522],
                 [artifact.Artifact("Assets/Tree.png", self), 116, 116],
                 [artifact.Artifact("Assets/Tree.png", self), 116, 174],
                 [artifact.Artifact("Assets/Tree.png", self), 116, 406],
                 [artifact.Artifact("Assets/Tree.png", self), 116, 464],
                 [artifact.Artifact("Assets/Tree.png", self), 116, 522],
                 [artifact.Artifact("Assets/Tree.png", self), 116, 580],
                 [artifact.Artifact("Assets/Tree.png", self), 174, 116],
                 [artifact.Artifact("Assets/Tree.png", self), 174, 174],
                 [artifact.Artifact("Assets/Tree.png", self), 174, 232],
                 [artifact.Artifact("Assets/Tree.png", self), 174, 406],
                 [artifact.Artifact("Assets/Tree.png", self), 174, 464],
                 [artifact.Artifact("Assets/Tree.png", self), 174, 522],
                 [artifact.Artifact("Assets/Tree.png", self), 174, 580],
                 [artifact.Artifact("Assets/Tree.png", self), 232, 232],
                 [artifact.Artifact("Assets/Tree.png", self), 232, 464],
                 [artifact.Artifact("Assets/Tree.png", self), 232, 522],
                 [artifact.Artifact("Assets/Tree.png", self), 464, 464],
                 [artifact.Artifact("Assets/Tree.png", self), 464, 522],
                 [artifact.Artifact("Assets/Tree.png", self), 522, 464],
                 [artifact.Artifact("Assets/Tree.png", self), 522, 522],
                 [artifact.Artifact("Assets/Tree.png", self), 522, 580],
                 [artifact.Artifact("Assets/Tree.png", self), 580, 58],
                 [artifact.Artifact("Assets/Tree.png", self), 580, 116],
                 [artifact.Artifact("Assets/Tree.png", self), 580, 290],
                 [artifact.Artifact("Assets/Tree.png", self), 580, 522],
                 [artifact.Artifact("Assets/Tree.png", self), 580, 580],
                 [artifact.Artifact("Assets/Tree.png", self), 638, 58],
                 [artifact.Artifact("Assets/Tree.png", self), 638, 116],
                 [artifact.Artifact("Assets/Tree.png", self), 638, 174],
                 [artifact.Artifact("Assets/Tree.png", self), 638, 232]]
        for a in level:
            block = a[0]
            block.rect.x = a[1]
            block.rect.y = a[2]
            block.player = self.player
            self.artifact_list.add(block)
import time
from neopixel_animation_helpers import Timer, Spiral, apply_overlay
from board import A1
import heart
import neopixel
import twinkle

pixel_pin = A1
num_pixels = 64
rows = 8
cols = 8

animation = heart.Heart()
overlay = twinkle.Twinkle(1, (50, 50, 50), 8, 8)
spiral = Spiral(8, 8)

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, auto_write=False)


def animation_callback():
    animation.calc_next_frame()
    overlay.calc_next_overlay()
    apply_overlay(animation.frame, overlay.overlay)
    for x in range(0, rows):
        for y in range(0, cols):
            pixels[spiral.convert_xy_to_index(x, y)] = animation.frame[x][y]
    pixels.show()


timer = Timer(10, animation_callback)