Exemplo n.º 1
0
    def __init__(self):
        #self.objects = []
        #self.objects_map = defaultdict(list)
        self.objectives = []
        self.messages = []
        self.state = STATE_NORMAL

        pygame.key.set_repeat(1, 50)

        self.quitting = False
        self.renderer = Renderer()
        self.clock = pygame.time.Clock()
        self.framerates = []

        self.font = pygame.font.SysFont('Sans', 18)

        self.player = Player(name='you', level=None, description='The Player')
        self.level = TestLevel(self)
        self.player.level = self.level
        self.player.location = (self.level.width/2, self.level.height/2)
        mq = MainQuest(self)
Exemplo n.º 2
0
    def __init__(self, config, level_name):
        self.config = config
        self.physics_manager = PhysicsManager()
        self.material_manager = MaterialManager(config["material_file"])
        self.transmutation_manager = TransmutationManager(self.material_manager)
        self.transmutation_manager.blow_key = "stone"
        self.level = Level("{0}/{1}.lvl".format(config["levels_dir"], level_name), self.physics_manager, self.material_manager)

        self.main_char = Player.genMainCharacter()
        self.main_char.physical.position = [25, 10]
        self.level.actors.append(self.main_char)

        self.viewport = Viewport(config["width"], config["height"], self.main_char, self.level, 100)
        self.picking_handler = PickingHandler(self.viewport, self.transmutation_manager, self.physics_manager)

        self.ui_overlay = UIOverlay(config["font_file"])
        self.ui_overlay.text_elements["score"] = TextElement((20, 20), 20, (0, 0, 0), "0 pts")

        self.physics_manager.add_actor(self.main_char)
        self._highlight_actors = False

        self.sound_manager = SoundManager()
        self.sound_manager.actors.append(self.main_char)
Exemplo n.º 3
0
class World:
    def __init__(self):
        #self.objects = []
        #self.objects_map = defaultdict(list)
        self.objectives = []
        self.messages = []
        self.state = STATE_NORMAL

        pygame.key.set_repeat(1, 50)

        self.quitting = False
        self.renderer = Renderer()
        self.clock = pygame.time.Clock()
        self.framerates = []

        self.font = pygame.font.SysFont('Sans', 18)

        self.player = Player(name='you', level=None, description='The Player')
        self.level = TestLevel(self)
        self.player.level = self.level
        self.player.location = (self.level.width/2, self.level.height/2)
        mq = MainQuest(self)

    #def add_object(self, obj):
    #   if obj in self.objects:
    #       return

    #   self.objects.append(obj)
    #   self.objects_map[obj.location].append(obj)

    #   obj.world = self
    #
    #def remove_object(self, obj):
    #   if obj not in self.objects:
    #       return

    #   self.objects.remove(obj)
    #   self.objects_map[obj.location].remove(obj)
    #   obj.destroy()

    def add_objective(self, objective):
        self.objectives.append(objective)

    def get_objects_at(self, location, test=None):
        #First item at a location is always terrain
        if test is None:
            return self.level[location][1:]
        else:
            return [obj for obj in self.level[location][1:] if test(obj)]

    def get_object_by_name(self, name):
        #TODO this should not just work on the current level
        for obj in self.level.objects:
            if obj.name == name:
                return obj

    def can_move_to(self, obj, location):
        if [1 for x in self.level[location] if x.block_move]:
            return False
        return True

    def win(self):
        self.describe("You win!")
        self.quitting = True

    def main_loop(self, screen):
        self.update()
        while not self.quitting:
            delay = self.clock.tick(15)
            self.framerates.insert(0, 1000.0/delay)
            self.framerates = self.framerates[:50]
            framerate = sum(self.framerates)/50.0
            self.process_events()
            self.renderer.render(self, screen)
            screen.blit(self.font.render('%d fps' % framerate, True, (255,255,255)),
                        (1, 1))
            pygame.display.flip()

    def process_events(self):
        took_turn = False
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.quitting = True
                return

            if e.type == pygame.KEYDOWN:
                took_turn = self.keypressed(e)
            elif e.type == pygame.MOUSEBUTTONUP:
                took_turn = self.clicked(e)

        if took_turn:
            self.update()

    def keypressed(self, e):
        took_turn = False
        if self.state == STATE_PICK:
            if e.key == pygame.K_LEFT or e.key == pygame.K_h:
                self.pick_location[0] -= 1
            elif e.key == pygame.K_RIGHT or e.key == pygame.K_l:
                self.pick_location[0] += 1
            elif e.key == pygame.K_UP or e.key == pygame.K_k:
                self.pick_location[1] -= 1
            elif e.key == pygame.K_DOWN or e.key == pygame.K_j:
                self.pick_location[1] += 1
            elif e.key == pygame.K_PERIOD or e.key == pygame.K_RETURN:
                self.pick_done()
        elif self.state == STATE_DIALOGUE:
            self.do_dialogue(e)
        else:
            newloc = list(self.player.location)
            if e.key == pygame.K_LEFT or e.key == pygame.K_h:
                newloc[0] -= 1
            elif e.key == pygame.K_RIGHT or e.key == pygame.K_l:
                newloc[0] += 1
            elif e.key == pygame.K_UP or e.key == pygame.K_k:
                newloc[1] -= 1
            elif e.key == pygame.K_DOWN or e.key == pygame.K_j:
                newloc[1] += 1
            newloc = tuple(newloc)

            if e.key == pygame.K_COMMA:
                for obj in self.get_objects_at(self.player.location):
                    if self.player.add(obj):
                        self.describe('You pick up %s' % obj.indefinite())
                        took_turn = True

            elif e.key == pygame.K_d:
                for obj in self.player.contained:
                    if self.player.remove(obj):
                        self.describe('You drop %s' % obj.indefinite())
                        took_turn = True

            elif e.key == pygame.K_r:
                loc = self.player.location
                self.player.map_memory = {}
                self.level.setup()
                self.level.add_object(self.player)
                self.player.level = self.level
                self.player.location = loc
                #self.player.update_fov()
                self.renderer.render_level(self)
                self.messages = []

            elif e.key == pygame.K_0:
                self.renderer.tiles.scale = 1
                self.renderer.render_level(self)

            elif e.key == pygame.K_9:
                self.renderer.tiles.scale = 1/4.0
                self.renderer.render_level(self)

            elif e.key == pygame.K_t:
                self.pick_target(self.talk)

            elif newloc != self.player.location:
                if self.can_move_to(self.player, newloc):
                    self.player.location = newloc
                    took_turn = True
                else:
                    for thing in self.level[newloc]:
                        if thing.bumped(self.player):
                            took_turn = True
                            break
        return took_turn

    def clicked(self, e):
        if e.button == 1:
            pass
        if e.button == 4:
            self.renderer.tiles.scale *= 1.1
            self.renderer.render_level(self)
        elif e.button == 5:
            self.renderer.tiles.scale *= 0.9
            self.renderer.render_level(self)

        return False

    def pick_target(self, handler):
        """Enter targeting mode"""
        self.state = STATE_PICK
        self.pick_handler = handler
        self.pick_location = list(self.player.location)

    def pick_done(self):
        """Targeting mode over, do something at the target"""
        self.state = STATE_NORMAL
        self.pick_handler(self.pick_location)

    def talk(self, target):
        """Try and initiate a conversation with someone"""
        if (abs(target[0] - self.player.location[0]) > 1 or
            abs(target[0] - self.player.location[0]) > 1):
            self.describe('Too far away')
            return

        targets = self.get_objects_at(target)
        valid_targets = [t for t in targets if hasattr(t, 'ask')]
        if not valid_targets:
            if targets:
                self.describe('You get no response from %s' % (targets[-1].definite()))
            else:
                self.describe('There\'s no-one there to talk to')
            return

        self.state = STATE_DIALOGUE
        self.conversation = []
        self.talking_to = valid_targets[-1]
        self.input_text = ''

    def do_dialogue(self, event):
        """Handle having a conversation"""
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                self.state = STATE_NORMAL
            elif event.key == pygame.K_RETURN:
                self.conversation.append('> %s' % (self.input_text))
                self.conversation.append(self.talking_to.ask(self.player, self.input_text))
                self.input_text = ''
            elif event.key == pygame.K_BACKSPACE:
                if self.input_text:
                    self.input_text = self.input_text[:-1]
            elif event.unicode:
                self.input_text += event.unicode

    def describe(self, text):
        self.messages.append(text[0].upper() + text[1:])

    def update(self):
        self.player.update_fov()
        self.renderer.render_level(self)
Exemplo n.º 4
0
# set window title
pygame.display.set_caption("Space Invaders")
# set window icon
# see flaticon.com  spaceship 32x32,  arcade space 64x64
icon = pygame.image.load("images/ufo.png")
pygame.display.set_icon(icon)

# a pressed key is repeated, with 1ms delay and 20ms interval
#pygame.key.set_repeat(5, 20)

# load a background image
background = pygame.image.load("images/background.png")

# initiate player
player = Player("images/player1.png", screen, 200, 480, changeX=8, changeY=0)

# initiate score and pause
score_value = 0
score_font = pygame.font.Font('freesansbold.ttf', 32)
pause_font = pygame.font.Font('freesansbold.ttf', 64)
pause_text = pause_font.render("Pause", True, (250, 250, 0))

# initiate sound
volume_increment = 0.1
sound_volume = 0.0
mixer.music.load("sound/background.wav")
mixer.music.set_volume(sound_volume)
mixer.music.play(-1)
laser_sound = mixer.Sound("sound/laser.wav")
laser_sound.set_volume(sound_volume)
Exemplo n.º 5
0
class Basement:
    MOVEMENT = {
        ord('.'): (0, 0),
        ord('h'): (-1, 0),
        curses.KEY_LEFT: (-1, 0),
        ord('j'): (0, 1),
        curses.KEY_DOWN: (0, 1),
        ord('k'): (0, -1),
        curses.KEY_UP: (0, -1),
        ord('l'): (1, 0),
        curses.KEY_RIGHT: (1, 0),
    }

    def __init__(self, stdscr):
        self.stdscr = stdscr
        self.level_memory = InfiniteMatrix(10, lambda x, y: ' ')

        caverns = generate.caverns(24, 500)
        self.player = Player(caverns[0, 0])

    def act(self):
        pass

    def ask(self, question):
        prevcursor = self.stdscr.getyx()
        self.stdscr.move(self.msgcursor[0], self.msgcursor[1])
        self.stdscr.addstr(question + " ")
        c = chr(self.stdscr.getch())
        self.stdscr.addstr(c)
        self.msgcursor = self.stdscr.getyx()
        self.stdscr.move(*prevcursor)
        return c

    def message(self, msg):
        prevcursor = self.stdscr.getyx()
        self.stdscr.move(self.msgcursor[0], self.msgcursor[1])
        self.stdscr.addstr(msg + "\n")
        self.msgcursor = self.stdscr.getyx()
        self.stdscr.move(*prevcursor)

    def draw(self):
        height, width = self.stdscr.getmaxyx()
        self.stdscr.clear()
        self.stdscr.addstr(self.level_memory.view(self.player.x - width // 2, self.player.y - height // 2, self.player.x + width // 2, self.player.y + height // 2))
        self.msgcursor = (0, 0)
        if self.player.tile.items:
            self.message("You see here:")
            for item in self.player.tile.items:
                self.message("    " + item.name)
        self.stdscr.move(height // 2, width // 2)

    def see(self, x, y):
        dx = abs(self.player.x - x)
        dy = abs(self.player.y - y)
        if dx * dx + dy * dy < 10 * 10:
            self.level_memory[x, y] = self.player.level[x, y].symbol

    def run(self):
        actcall = self.player.level.act()
        while True:
            fov.fieldOfView(self.player.x, self.player.y, 10, self.see, lambda x, y: not self.player.level[x, y].is_passable)
            self.draw()
            key = self.stdscr.getch()
            if key == ord('q'):
                if self.ask("Really quit? [yN]").lower() == 'y':
                    return
            elif key in self.MOVEMENT:
                delta = self.MOVEMENT[key]
                try:
                    self.player.move_by(*delta)
                except MovementObstructed:
                    pass
                next(actcall)
Exemplo n.º 6
0
    def __init__(self, stdscr):
        self.stdscr = stdscr
        self.level_memory = InfiniteMatrix(10, lambda x, y: ' ')

        caverns = generate.caverns(24, 500)
        self.player = Player(caverns[0, 0])