Beispiel #1
0
    def draw(self):
        # Game Loop - draw
        self.screen.fill(black)
        #self.draw_relative_to_camera(self.screen, self.level)
        self.BG_sprites.draw(self.screen)

        for (x, y) in self.level.path:
            x, y = tools.ScreenFromIngame(x, y)
            x = x - int(self.camera_x)
            y = y - int(self.camera_y)
            #tmp_rect = pg.draw.circle(self.screen, settings.white, (x,y) , 10)
            #pg.display.update(tmp_rect)

        for enemy_ in self.FG_sprites:
            self.draw_relative_to_camera(self.screen, enemy_)

        for tower in self.player.towers_sprites:
            if tower.type == 1:
                for proj in tower.projectiles:
                    self.draw_relative_to_camera(self.screen, proj)
            self.draw_relative_to_camera(self.screen, tower)

        self.draw_relative_to_camera(self.screen, self.player)

        self.draw_hud()
        #self.FG_sprites.draw(self.screen)
        #self.player.projectiles.draw(self.screen)
        pg.display.update()
Beispiel #2
0
    def __init__(self, spawn_x, spawn_y):
        super(tesla_coil, self).__init__()
        self.x_pos = spawn_x * 1.
        self.y_pos = spawn_y * 1.
        self.animation_frame = 0
        self.spritesheet = []

        self.spritesheet.append(
            pg.image.load(
                '../Gameart/towers/teslacoil/teslacoil0.png').convert_alpha())
        self.spritesheet.append(
            pg.image.load(
                '../Gameart/towers/teslacoil/teslacoil1.png').convert_alpha())

        self.image = self.spritesheet[0]

        #self.image = pg.Surface((tower_width, tower_height))
        #self.image.set_colorkey(settings.black)
        #self.image.blit(self.spritesheet[0], (0,0), (0, 0, tower_width, tower_height))

        #self.image = self.image.convert()
        self.rect = self.image.get_rect()

        self.collision_rect = pg.Rect(self.x_pos, self.y_pos, 100, 100)
        self.rect.x, self.rect.y = tools.ScreenFromIngame(
            self.x_pos, self.y_pos)

        self.attack_CD = 0

        self.attack_radius = 300

        self.attack_rect = pg.Rect(self.x_pos - self.attack_radius,
                                   self.y_pos - self.attack_radius,
                                   2 * self.attack_radius,
                                   2 * self.attack_radius)

        self.target = None

        self.projectiles = pg.sprite.Group()

        self.type = 1

        self.teslahit_sound = pg.mixer.Sound(
            '../Gameart/Sounds/Tower/Shoot/Teslahit.wav')
    def __init__(self, spawn_x, spawn_y):
        super(rail_gun, self).__init__()
        self.x_pos = spawn_x * 1.
        self.y_pos = spawn_y * 1.
        self.animation_frame = 0
        self.spritesheet = []

        self.spritesheet.append(
            pg.image.load(
                '../Gameart/towers/railgun/railgun0.png').convert_alpha())
        self.spritesheet.append(
            pg.image.load(
                '../Gameart/towers/railgun/railgun1.png').convert_alpha())
        self.spritesheet.append(
            pg.image.load(
                '../Gameart/towers/railgun/railgun2.png').convert_alpha())

        self.image = self.spritesheet[0]

        #self.image = pg.Surface((tower_width, tower_height))
        #self.image.set_colorkey(settings.black)
        #self.image.blit(self.spritesheet[0], (0,0), (0, 0, tower_width, tower_height))

        #self.image = self.image.convert()
        self.rect = self.image.get_rect()

        self.rect.x, self.rect.y = tools.ScreenFromIngame(
            self.x_pos, self.y_pos)

        self.collision_rect = pg.Rect(self.x_pos, self.y_pos, 100, 100)

        self.attack_CD = 0

        self.attack_radius = 100

        self.attack_rect = pg.Rect(self.x_pos - self.attack_radius,
                                   self.y_pos - self.attack_radius,
                                   2 * self.attack_radius,
                                   2 * self.attack_radius)

        self.target = None

        self.type = 0
Beispiel #4
0
    def update(self):
        # Game Loop - update
        if self.camera_x != self.prev_camera_x or self.camera_y != self.prev_camera_y:
            self.level.update(self.camera_x, self.camera_y)
        self.player.update()
        self.FG_sprites.update()
        self.update_camera(
            tools.ScreenFromIngame(self.player.x_pos, self.player.y_pos))

        for tower in self.player.towers_sprites:
            tower.update()

        for enemy in self.FG_sprites:
            if self.player.collision_rect.colliderect(enemy.collision_rect):
                self.player.take_damage(10)
            if self.player.hp <= 0:
                self.dead = True
        if self.dead:
            self.playing = False
    def update(self):

        if not self.target is None:

            target_x, target_y = self.target.x_pos, self.target.y_pos
            dist_x = target_x - self.x_pos
            dist_y = target_y - self.y_pos
            self.direction = np.asarray([dist_x, dist_y])
            if np.linalg.norm(self.direction) < 30:
                if not self.finished:
                    self.target.hp -= 40
                self.finished = True
            if np.any(self.direction):
                self.direction /= np.linalg.norm(self.direction)
            self.x_pos += self.direction[0] * 5
            self.y_pos += self.direction[1] * 5

            self.collision_rect.x = self.x_pos - 10
            self.collision_rect.y = self.y_pos - 10

            self.rect.x, self.rect.y = tools.ScreenFromIngame(
                self.x_pos, self.y_pos)
            self.rect.x -= 50
            self.rect.y -= 50
    def update(self):

        # TICK STUFF
        self.prev_x = self.x_pos
        self.prev_y = self.y_pos
        if self.invincible_frames > 0:
            self.invincible_frames -= 1

        # ANIMATE
        if self.walking:
            length = 2
            for i in range(8):
                if self.animation_frame in range(i*length, (i+1)*length):
                    if self.facing is 'left':
                        self.image = self.spritesheet_left[i]
                    else:
                        self.image = self.spritesheet_right[i]
                    self.animation_frame += 1
                    break
            if self.animation_frame == 8*length:
                self.animation_frame = 0
        else:
            if self.facing is 'left':
                self.image = self.spritesheet_left[0]
            else:
                self.image = self.spritesheet_right[0]

        # MOVE
        keys = pg.key.get_pressed()
        speed = 3.
        if keys[loc.K_RIGHT]:
            self.x_pos += speed
            self.y_pos -= speed
            self.walking = True
            self.facing = 'right'
        elif keys[loc.K_LEFT]:
            self.x_pos -= speed
            self.y_pos += speed
            self.walking = True
            self.facing = 'left'
        if keys[loc.K_UP]:
            self.x_pos -= speed
            self.y_pos -= speed
            self.walking = True
        elif keys[loc.K_DOWN]:
            self.x_pos += speed
            self.y_pos += speed
            self.walking = True
        self.dir = np.asarray([self.x_pos - self.prev_x, self.y_pos - self.prev_y])
        self.rect.x, self.rect.y = tools.ScreenFromIngame(self.x_pos, self.y_pos)
        self.collision_rect.x = self.x_pos
        self.collision_rect.y = self.y_pos

        if not np.any([keys[i] for i in [loc.K_RIGHT, loc.K_LEFT, loc.K_UP, loc.K_DOWN]]):
            self.walking = False

        if keys[loc.K_SPACE]:
            print self.x_pos, self.y_pos
            #self.projectiles.add(projectile((1,1)))

        # place rail_gun
        if keys[loc.K_1] and self.check4towers(0):
            self.towers_sprites.add(rail_gun(self.x_pos, self.y_pos))

        # place tesla_coil
        if keys[loc.K_2] and self.check4towers(50):
            self.towers_sprites.add(tesla_coil(self.x_pos, self.y_pos))
    def update(self):

        # TICK STUFF
        self.prev_x = self.x_pos
        self.prev_y = self.y_pos
        if self.invincible_frames > 0:
            self.invincible_frames -= 1
        if self.attacking_CD > 0:
            self.attacking_CD -= 1

        if self.build_CD > 0:
            self.build_CD -= 1

        self.attack_collision_rect.x = self.x_pos - self.attack_radius
        self.attack_collision_rect.y = self.y_pos - self.attack_radius

        # ANIMATE
        if self.walking:
            self.attacking = False
            length = 2
            for i in range(8):
                if self.animation_frame in range(i * length, (i + 1) * length):
                    if self.facing is 'left':
                        self.image = self.spritesheet_left[i]
                    else:
                        self.image = self.spritesheet_right[i]
                    self.animation_frame += 1
                    break
            if self.animation_frame == 8 * length:
                self.animation_frame = 0
                if self.facing is 'left':
                    self.image = self.spritesheet_left[0]
                else:
                    self.image = self.spritesheet_right[0]
        elif self.attacking:
            self.rect.x -= 50
            self.rect.y -= 50
            self.walking = False
            length = 4
            for i in range(4):
                if self.animation_frame in range(i * length, (i + 1) * length):
                    if self.facing is 'left':
                        pass
                        self.image = self.sword_left[i]
                    else:
                        self.image = self.sword_right[i]
                    self.animation_frame += 1
                    break
            if self.animation_frame == 4 * length:
                self.animation_frame = 0
                self.attacking = False
                if self.facing is 'left':
                    self.image = self.spritesheet_left[0]
                else:
                    self.image = self.spritesheet_right[0]

        else:
            if self.facing is 'left':
                self.image = self.spritesheet_left[0]
            else:
                self.image = self.spritesheet_right[0]

        # MOVE
        keys = pg.key.get_pressed()
        speed = 3.

        if keys[loc.K_RIGHT]:
            self.x_pos += speed
            self.y_pos -= speed
            self.walking = True
            self.facing = 'right'
        elif keys[loc.K_LEFT]:
            self.x_pos -= speed
            self.y_pos += speed
            self.walking = True
            self.facing = 'left'
        if keys[loc.K_UP]:
            self.x_pos -= speed
            self.y_pos -= speed
            self.walking = True
        elif keys[loc.K_DOWN]:
            self.x_pos += speed
            self.y_pos += speed
            self.walking = True

        new_dir = np.asarray(
            [self.x_pos - self.prev_x, self.y_pos - self.prev_y])
        if np.any(new_dir):
            self.dir = new_dir
            self.dir /= np.linalg.norm(self.dir)
        self.rect.x, self.rect.y = tools.ScreenFromIngame(
            self.x_pos, self.y_pos)

        #if self.attacking:
        #    self.rect.x -= 50
        #    self.rect.y -= 50

        self.collision_rect.x = self.x_pos - self.collision_radius
        self.collision_rect.y = self.y_pos - self.collision_radius

        if not np.any(
            [keys[i]
             for i in [loc.K_RIGHT, loc.K_LEFT, loc.K_UP, loc.K_DOWN]]):
            self.walking = False

        if keys[loc.K_SPACE] and self.attacking_CD == 0:
            self.attacking = True
            self.animation_frame = 0
            self.attacking_CD = 20
            print self.x_pos, self.y_pos

        # place rail_gun
        if keys[loc.K_1] and self.build_CD == 0:
            if self.check4towers(0) and self.gold >= 40:
                self.towers_sprites.add(rail_gun(self.x_pos, self.y_pos))
                self.gold -= 40
                print "Gold: %d" % self.gold
                self.build_sound.play()
                self.build_CD = 40
            else:
                self.nobuild_sound.play()

        # place tesla_coil
        if keys[loc.K_2] and self.build_CD == 0:
            if self.check4towers(50) and self.gold >= 80:
                self.towers_sprites.add(tesla_coil(self.x_pos, self.y_pos))
                self.gold -= 80
                print "Gold: %d" % self.gold
                self.build_sound.play()
                self.build_CD = 40
            else:
                self.nobuild_sound.play()
Beispiel #8
0
    def update(self):

        if self.ticks == 200:
            self.music.play(loops=-1)

        self.coin_animation_frame += 1
        self.heart_animation_frame += 1

        # COIN ANIM
        anim_speed = 8

        for i in range(8):

            if self.coin_animation_frame == anim_speed * i:
                self.coin_image = self.coin_images[i]

        if self.coin_animation_frame == 8 * anim_speed:
            self.coin_image = self.coin_images[0]
        if self.coin_animation_frame == 12 * anim_speed:
            self.coin_animation_frame = 0

        # HEART ANIM
        anim_speed = 50
        for i in range(2):

            if self.heart_animation_frame == anim_speed * i:
                self.heart_image = self.hearts[i]

        if self.heart_animation_frame == 2 * anim_speed:
            self.heart_animation_frame = -1

        # Game Loop - update
        if self.camera_x != self.prev_camera_x or self.camera_y != self.prev_camera_y:
            self.level.update(self.camera_x, self.camera_y)
        self.player.update()
        #self.FG_sprites.update()
        self.update_camera(
            tools.ScreenFromIngame(self.player.x_pos, self.player.y_pos))

        erects = [enemy_.collision_rect for enemy_ in self.FG_sprites]

        spawn_x, spawn_y = (1485., 1085.)

        # ============ SPAWN ENEMIES

        if self.ticks % 2000 == 0 and self.ticks > 0:
            self.FG_sprites.add(enemy(spawn_x, spawn_y, 1))
        elif self.ticks % 200 == 0:
            self.FG_sprites.add(enemy(spawn_x, spawn_y, 0))

        for tower in self.player.towers_sprites:
            tower.update()
            if tower.attack_CD == 0:
                idx = tower.attack_rect.collidelist(erects)
                if idx != -1:
                    tower.target = self.FG_sprites.sprites()[idx]
                    if tower.type == 1:
                        tower.projectiles.add(
                            projectile(tower.x_pos, tower.y_pos, tower.target))
                        self.tesla_sound.play()
                    elif tower.type == 0:
                        tower.target.hp -= 10
                        self.mg_sound.play()

        hitsoundvals = [0, 0]
        for enemy_ in self.FG_sprites:
            if self.player.attacking and self.player.attacking_CD == 20:
                if self.player.attack_collision_rect.colliderect(
                        enemy_.collision_rect):
                    enemy_.hp -= 30
                    hitsoundvals[0] += 1
                else:
                    hitsoundvals[1] += 1
            enemy_.update()
            if self.player.collision_rect.colliderect(enemy_.collision_rect):
                if self.player.invincible_frames == 0:
                    self.player.take_damage(enemy_.playerdmg)
                    direc = np.asarray([
                        self.player.x_pos - enemy_.x_pos,
                        self.player.y_pos - enemy_.y_pos
                    ])
                    direc /= np.linalg.norm(direc)
                    self.player.x_pos += 100 * direc[0]
                    self.player.y_pos += 100 * direc[1]
            if self.player.hp <= 0:
                self.die_anim = True
            if enemy_.destroy_flag:
                if enemy_.remove_path_hp:
                    self.path_HP -= enemy_.path_dmg
                    self.etru_sound.play()
                else:
                    self.player.gold += 10
                    print "Gold: %d" % self.player.gold
                self.FG_sprites.remove(enemy_)
        if hitsoundvals[0]:
            self.player.sword_nohit.play()
            self.player.sword_hit.play()
        elif hitsoundvals[1]:
            self.player.sword_nohit.play()

        if self.die_anim or self.path_HP <= 0:
            self.playing = False