Exemple #1
0
    def __init__(self, game):
        self.game = game

# TODO: Not ready yet
#        self.world = world.make_default_world()
        
        self.entities = []

        player = entity.make_lich(self, 5 * 32, 35 * 32)
        self.entities.append(player)
        self.player = entity.Player(self, player)
        self.player_speed = 150

        self.cdx, self.cdy = 0, 0
        self.cross = pygame.sprite.Sprite()
        self.cross.image = resourcemanager.images["crosshair"]
        self.cross.rect = self.cross.image.rect.move(self.game.width / 2, self.game.height / 2)

        self.hud = text.Text(x=10, y=10, font=game.font_s, text="HUD MESSAGE!",
                             color=(255, 255, 255), background=(0, 0, 255), alpha=96)

#        floors, evil floors, good floors
        self.map = tilemap.TileMap(resourcemanager.tiles, "grass", 80, 60, 32, 32)
        self.map.random_fill(resourcemanager.tilesets["generation"])
        self.map.add_circle(60, 10, 10, "grass")
        self.map.add_circle(50, 20, 10, "grass")
        self.map.add_circle(70, 20, 10, "grass")
        self.map.add_filled_box(60, 0, 20, 20, "castle floor")
        self.map.add_circle(10, 45, 10, "dark floor")
        self.map.add_circle(10, 45, 5, "cracked floor")
        self.map.add_circle(5, 35, 5, "cracked floor")
        self.map.add_line(60, 59, 40, 30, "brick wall 0000")
        self.map.add_line(61, 59, 41, 30, "brick wall 0000")
        self.map.add_line(0, 20, 30, 50, "brick wall 0000")
        self.map.add_line(1, 20, 31, 50, "brick wall 0000")
        self.map.add_edge_wall("brick wall 0000")
        self.map.add_line(60, 10, 70, 10, "castle wall 0000")
        self.map.add_box(60, 0, 20, 20, "castle wall 0000")
        self.map.add_filled_box(60, 17, 3, 3, "castle floor")
        self.map.fix_brick_walls()

        self.entities.append(entity.make_knight(self, 32 * 62, 32 * 5))
        for _ in range(5):
            self.entities.append(entity.make_peasant(self, 32 * 60, 32 * 25))
        for _ in range(5):
            self.entities.append(entity.make_peasant(self, 32 * 50, 32 * 20))
        for _ in range(5):
            self.entities.append(entity.make_peasant(self, 32 * 50, 32 * 10))

        for i in range(4):
            self.entities.append(entity.make_imp(self, 32 * (10 + i), 32 * 45))
            
        self.camera = camera.Camera(self.game.width, self.game.height,
                                    self.map.w * self.map.tile_w,
                                    self.map.h * self.map.tile_h)
Exemple #2
0
    def handle_event(self, event):
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                resourcemanager.sounds["select"].play()
                self.game.set_state(TitleState(self.game))
            elif event.key == K_w:
                self.player.dy -= self.player_speed
            elif event.key == K_a:
                self.player.dx -= self.player_speed
            elif event.key == K_s:
                self.player.dy += self.player_speed
            elif event.key == K_d:
                self.player.dx += self.player_speed
            elif event.key == K_p:
                resourcemanager.sounds["select"].play()
                self.game.set_state(PauseState(self.game, self))
            elif event.key == K_F12:
                for i in range(200):
                    filename = 'screenshot{}.png'.format(i)
                    if not os.path.isfile(filename):
                        pygame.image.save(self.game.screen, filename)
                        break
        elif event.type == KEYUP:
            if event.key == K_w:
                self.player.dy += self.player_speed
            elif event.key == K_a:
                self.player.dx += self.player_speed
            elif event.key == K_s:
                self.player.dy -= self.player_speed
            elif event.key == K_d:
                self.player.dx -= self.player_speed
        elif event.type == JOYAXISMOTION:
            if event.axis == 0:
                if abs(event.value) > 0.2:
                    self.player.dx = event.value * self.player_speed
                else:
                    self.player.dx = 0
            elif event.axis == 1:
                if abs(event.value) > 0.2:
                    self.player.dy = event.value * self.player_speed
                else:
                    self.player.dy = 0
            elif event.axis == 3:
                if abs(event.value) > 0.2:
                    self.cdx = event.value * 300
                else:
                    self.cdx = 0
            elif event.axis == 4:
                if abs(event.value) > 0.2:
                    self.cdy = event.value * 300
                else:
                    self.cdy = 0
            else:
                print "Movement on {} axis.".format(event.axis)
        elif event.type == pygame.JOYBUTTONDOWN:
            if event.button == 5:
                select = random.randint(0,3)
                if select == 0:
                    self.entities.append(entity.make_spider(self,
                                                            self.player.entity.pos.x,
                                                            self.player.entity.pos.y))

                elif select == 1:
                    self.entities.append(entity.make_knight(self,
                                                            self.player.entity.pos.x,
                                                            self.player.entity.pos.y))
                elif select == 2:
                    self.entities.append(entity.make_peasant(self,
                                                            self.player.entity.pos.x,
                                                            self.player.entity.pos.y))
                else:
                    self.entities.append(entity.make_imp(self,
                                                            self.player.entity.pos.x,
                                                            self.player.entity.pos.y))
            elif event.button == 6:
                resourcemanager.sounds["select"].play()
                self.game.set_state(PauseState(self.game, self))
            else:
                tx = self.camera.pos.x + self.cross.rect.x - self.player.entity.pos.x
                ty = self.camera.pos.y + self.cross.rect.y - self.player.entity.pos.y
                if tx == 0 and ty == 0: tx = 1
                
                self.entities.append(entity.make_fireball(self,
                                                          self.player.entity.pos.x,
                                                          self.player.entity.pos.y,
                                                          tx, ty))
        elif event.type == pygame.JOYBUTTONUP:
            print "Joystick button released."
        elif event.type == JOYBALLMOTION:
            print "JOYBALLMOTION"
        elif event.type == JOYHATMOTION:
            print "JOYHATMOTION"