コード例 #1
0
ファイル: game.py プロジェクト: Landric/SpaceAdventure
class Game(object):
    def __init__(self):
        self.player = Player()
        self.station = Station()

    def play(self):

        while True:
            self.station.levels[self.player.floor].rooms[
                self.player.room].describe()

            for enemy in self.station.levels[self.player.floor].rooms[
                    self.player.room].entities:

                if isinstance(enemy, Enemy):
                    while self.player.health > 0 and enemy.health > 0:
                        self.player.attack(enemy)
                        if enemy.health > 0:
                            enemy.attack(self.player)
                    self.station.levels[self.player.floor].rooms[
                        self.player.room].entities.remove(enemy)

            # Lose condition
            if self.player.health <= 0:
                print "Oh no, you've died!"
                break

            self.take_turn()

            # Win condition
            if self.player.floor >= len(self.station.levels):
                print "You've escaped!"
                break

    def take_turn(self):
        # move
        # item
        # map?
        self.move()

    def move(self):
        input = raw_input("Which door would you like to enter?")
        visible_doors = self.station.levels[self.player.floor].rooms[
            self.player.room].doors
        door = next((door for door in visible_doors if door.label == input),
                    None)
        if door is None:
            print "There is no door labeled {0} here!".format(input)
        else:
            self.player.room = door.enter(self.player.room)
            if self.player.room == -1:
                self.player.floor += 1
                self.player.room = 0
コード例 #2
0
ファイル: main.py プロジェクト: calzoneman/ld27
    def play():
        keys = defaultdict(lambda: False)
        clock = pygame.time.Clock()
        paused = False

        world = World((50, 50), default=Tile.GRASS)
        px, py = world.random_position()
        player = Player((px, py), loadimage("player.png"))
        world.add_entity(player)
        MOVE_SPEED = 3
        for i in range(1):
            world.spawn_clock()
        for i in range(3):
            world.spawn_fastenemy()
        for i in range(5):
            world.spawn_slowenemy()

        ticks = 0
        started = False

        while True:
            for ev in pygame.event.get():
                if ev.type == QUIT:
                    pygame.quit()
                    sys.exit(0)
                elif ev.type == KEYDOWN:
                    keys[ev.key] = True
                    if ev.key == K_SPACE:
                        if not started:
                            started = True
                        else:
                            paused = not paused
                    if ev.key in [K_UP, K_LEFT, K_DOWN, K_RIGHT]:
                        slash.play()
                    if ev.key == K_e and player.bombs > 0:
                        world.bomb((player.x, player.y))
                        player.bombs -= 1
                elif ev.type == KEYUP:
                    keys[ev.key] = False

            if keys[K_ESCAPE]:
                pygame.quit()
                sys.exit(0)

            if started and not paused:
                if keys[K_w]:
                    player.move((player.x           , player.y-MOVE_SPEED))
                if keys[K_s]:
                    player.move((player.x           , player.y+MOVE_SPEED))
                if keys[K_a]:
                    player.move((player.x-MOVE_SPEED, player.y           ))
                if keys[K_d]:
                    player.move((player.x+MOVE_SPEED, player.y           ))

                if keys[K_UP]:
                    player.attack("UP")
                elif keys[K_LEFT]:
                    player.attack("LEFT")
                elif keys[K_DOWN]:
                    player.attack("DOWN")
                elif keys[K_RIGHT]:
                    player.attack("RIGHT")

                world.tick()

            if player.health <= 0 or world.timer <= 0:
                return player.score, ticks

            px, py = player.x, player.y
            xo = SWIDTH/2 - player.x
            yo = SHEIGHT/2 - player.y

            #screen.fill(BLACK)
            screen.blit(bg, (0, 0))

            # Draw world
            world.render(screen, (xo, yo), (TWIDTH, THEIGHT))

            # Draw HUD
            for e in world.entities:
                if isinstance(e, Clock):
                    ex = e.x + xo
                    ey = e.y + yo
                    if (ex < 0 or ex > SWIDTH or
                        ey < 0 or ey > SHEIGHT):
                        ang = math.atan2(ey - SHEIGHT/2, ex - SWIDTH/2)
                        icon = pygame.transform.rotate(arrow, -ang*180/math.pi)
                        w = icon.get_width()/2
                        h = icon.get_height()/2
                        dx = math.cos(ang) * 50 - h
                        dy = math.sin(ang) * 50 - w
                        screen.blit(icon, (SWIDTH/2 + dx, SHEIGHT/2 + dy))

            msg = regfont.render("{} fps".format(int(clock.get_fps())), 1,
                                 WHITE, BLACK)
            blitfont(screen, msg, (0, 0))
            msg = regfont.render("{:.1f} seconds".format(world.timer / 60.0), 1,
                                 WHITE, BLACK)
            blitfont(screen, msg, (SWIDTH-1, 0), align="RIGHT")
            msg = regfont.render("Bombs: {}".format(player.bombs), 1,
                                 WHITE, BLACK)
            blitfont(screen, msg, (0, 1*msg.get_height()))
            healthcol = GREEN
            if player.health < 3:
                healthcol = RED
            elif player.health < 6:
                healthcol = YELLOW
            msg = regfont.render("Health: {}".format(player.health), 1,
                                 healthcol, BLACK)
            blitfont(screen, msg, (0, 2*msg.get_height()))
            msg = regfont.render("Score: {}".format(player.score), 1,
                                 WHITE, BLACK)
            blitfont(screen, msg, (0, 3*msg.get_height()))

            if world.timer < 180 and ticks % 40 == 0:
                timewarn.play()

            if world.timer < 180 and (ticks / 20) % 2 == 0:
                msg = regfont.render("Time's running out!", 1, WHITE, BLACK)
                blitfont(screen, msg, (SWIDTH/2, msg.get_height()/2),
                                       align="CENTER")


            if not started:
                show_intro(screen, regfont)
            elif paused:
                msg = regfont.render("Paused", 1, WHITE, BLACK)
                blitfont(screen, msg, (SWIDTH/2, SHEIGHT/2), align="CENTER")
            pygame.display.flip()

            ticks += 1
            clock.tick(60)
コード例 #3
0
    def play():
        keys = defaultdict(lambda: False)
        clock = pygame.time.Clock()
        paused = False

        world = World((50, 50), default=Tile.GRASS)
        px, py = world.random_position()
        player = Player((px, py), loadimage("player.png"))
        world.add_entity(player)
        MOVE_SPEED = 3
        for i in range(1):
            world.spawn_clock()
        for i in range(3):
            world.spawn_fastenemy()
        for i in range(5):
            world.spawn_slowenemy()

        ticks = 0
        started = False

        while True:
            for ev in pygame.event.get():
                if ev.type == QUIT:
                    pygame.quit()
                    sys.exit(0)
                elif ev.type == KEYDOWN:
                    keys[ev.key] = True
                    if ev.key == K_SPACE:
                        if not started:
                            started = True
                        else:
                            paused = not paused
                    if ev.key in [K_UP, K_LEFT, K_DOWN, K_RIGHT]:
                        slash.play()
                    if ev.key == K_e and player.bombs > 0:
                        world.bomb((player.x, player.y))
                        player.bombs -= 1
                elif ev.type == KEYUP:
                    keys[ev.key] = False

            if keys[K_ESCAPE]:
                pygame.quit()
                sys.exit(0)

            if started and not paused:
                if keys[K_w]:
                    player.move((player.x, player.y - MOVE_SPEED))
                if keys[K_s]:
                    player.move((player.x, player.y + MOVE_SPEED))
                if keys[K_a]:
                    player.move((player.x - MOVE_SPEED, player.y))
                if keys[K_d]:
                    player.move((player.x + MOVE_SPEED, player.y))

                if keys[K_UP]:
                    player.attack("UP")
                elif keys[K_LEFT]:
                    player.attack("LEFT")
                elif keys[K_DOWN]:
                    player.attack("DOWN")
                elif keys[K_RIGHT]:
                    player.attack("RIGHT")

                world.tick()

            if player.health <= 0 or world.timer <= 0:
                return player.score, ticks

            px, py = player.x, player.y
            xo = SWIDTH / 2 - player.x
            yo = SHEIGHT / 2 - player.y

            #screen.fill(BLACK)
            screen.blit(bg, (0, 0))

            # Draw world
            world.render(screen, (xo, yo), (TWIDTH, THEIGHT))

            # Draw HUD
            for e in world.entities:
                if isinstance(e, Clock):
                    ex = e.x + xo
                    ey = e.y + yo
                    if (ex < 0 or ex > SWIDTH or ey < 0 or ey > SHEIGHT):
                        ang = math.atan2(ey - SHEIGHT / 2, ex - SWIDTH / 2)
                        icon = pygame.transform.rotate(arrow,
                                                       -ang * 180 / math.pi)
                        w = icon.get_width() / 2
                        h = icon.get_height() / 2
                        dx = math.cos(ang) * 50 - h
                        dy = math.sin(ang) * 50 - w
                        screen.blit(icon, (SWIDTH / 2 + dx, SHEIGHT / 2 + dy))

            msg = regfont.render("{} fps".format(int(clock.get_fps())), 1,
                                 WHITE, BLACK)
            blitfont(screen, msg, (0, 0))
            msg = regfont.render("{:.1f} seconds".format(world.timer / 60.0),
                                 1, WHITE, BLACK)
            blitfont(screen, msg, (SWIDTH - 1, 0), align="RIGHT")
            msg = regfont.render("Bombs: {}".format(player.bombs), 1, WHITE,
                                 BLACK)
            blitfont(screen, msg, (0, 1 * msg.get_height()))
            healthcol = GREEN
            if player.health < 3:
                healthcol = RED
            elif player.health < 6:
                healthcol = YELLOW
            msg = regfont.render("Health: {}".format(player.health), 1,
                                 healthcol, BLACK)
            blitfont(screen, msg, (0, 2 * msg.get_height()))
            msg = regfont.render("Score: {}".format(player.score), 1, WHITE,
                                 BLACK)
            blitfont(screen, msg, (0, 3 * msg.get_height()))

            if world.timer < 180 and ticks % 40 == 0:
                timewarn.play()

            if world.timer < 180 and (ticks / 20) % 2 == 0:
                msg = regfont.render("Time's running out!", 1, WHITE, BLACK)
                blitfont(screen,
                         msg, (SWIDTH / 2, msg.get_height() / 2),
                         align="CENTER")

            if not started:
                show_intro(screen, regfont)
            elif paused:
                msg = regfont.render("Paused", 1, WHITE, BLACK)
                blitfont(screen,
                         msg, (SWIDTH / 2, SHEIGHT / 2),
                         align="CENTER")
            pygame.display.flip()

            ticks += 1
            clock.tick(60)