Reflective Shield feedback

I started reflective shield and started by writing this code, which succeeds in reflecting the projectiles, but it sends them much to high (screenshot 1).

while True:
    nalfar = hero.findNearestEnemy()
    dir = Vector.subtract(nalfar.pos, hero.pos)
    dir = Vector.normalize(dir)
    hero.reflect(dir)

screenshot 1:
Screen Shot 2021-07-03 at 12.12.58
Then, I tried to do change the dir.z property in the same way as it’s done in the code example, and it threw up this error (screenshot 2):

while True:
    nalfar = hero.findNearestEnemy()
    dir = Vector.subtract(nalfar.pos, hero.pos)
    dir = Vector.normalize(dir)
    dir.z = 0.3
    hero.reflect(dir)

screenshot 2:


I managed to complete the level by entering the z value in a dictionary with x and y as dir.x and dir.y. Also the z value does require fine tuning to 0.1 increments to get the right angle to deal damage to Nalfar.
The reflect ability does obviously work, but I think the reflect ability documentation should be changed so that the code doesn’t cause an error.
Does anyone else see this in this level, or any other levels when using relfect()?

1 Like

For vectors, there is also a z(height) along with x and y. You need to specify a z direction.

The example is just one way you can reflect a projectile. It’s on you to modify the code so it works. If the answer was in the example code, it wouldn’t be interesting.

No, but that’s the problem. It’s not one way you can reflect a projectile because it doesn’t work. Obviously I wouldn’t expect it to be the answer to the level, but I would expect it to give, as you said, an example of something that would reflect something in some way.

My bad, I misinterpreted your post. I’ll take a look and change the example code.

Hi, Deadpool198 how do you enter the z value in a dictionary with x and y as dir.x and dir.y because i am stuck on the level too, thanks

Welcome to the forum! :smiley:
We hope you have a lovely, enjoyable, and fun time here on the CodeCombat Discourse.
This forum is a homely place, where you can share ideas, share made levels, report bugs, get help on code, and so much more!
We hope you review this topic for all the rules of the forum!
Thanks and enjoy your time here!!!
:partying_face:

Hi and welcome! I think you need to make a new topic. You need help on the level. Deadpool was giving feedback. Thanks and I hope you pass this level!

Hi, i already make a new topic and RangerGrant9307 already gave me some advice but i cannot complete this level, thanks.

1 Like

No Illia, probably will never buy her, so tested reflect in kelvintaph burgler using another user session:


delete his code (he didn’t post his solution) and paste the code below:

dirZ = 0.2
minDistance = 10
while True:
    # projectile = hero.findNearest(hero.findEnemyMissiles())
    projectile = hero.findNearest(hero.findByType("plasma-ball"))
    if projectile and projectile.pos.distance(hero.pos) < minDistance: 
        dir = Vector.subtract(projectile.pos, hero.pos)
        hero.reflect(Vector(dir.x, dir.y, dirZ) )
    if projectile and projectile.velocity.x < 0:
        hero.move(Vector(76, 14))

this code somehow works, the 2 robots are killed, I use only vectors and I think this is the normal way to use reflect. But dirZ variable is taken from thin air…


using

projectile = hero.findNearest(hero.findEnemyMissiles())

results in only one robot drstroyed.
I think dir vector can be found using only the missile velocity, not the position => code:

dirZ = 0.2
minDistance = 10
while True:
    projectile = hero.findNearest(hero.findByType("plasma-ball"))
    if projectile and projectile.pos.distance(hero.pos) < minDistance: 
        dir = Vector.subtract( hero.pos, projectile.velocity)
        hero.reflect(Vector(dir.x, dir.y, dirZ) )
    if projectile and projectile.velocity.x < 0:
        hero.move(Vector(76, 14))


but again only one robot is destroyed.
I’m not good with velocity, so everyone with completed Kelvintaph Burgler can take a look, no need to be subscriber
( sorry @Hydrobolic , can you?)

I’m not quite sure what your question is.

How to reflect the plasma balls perfectly? Negate their velocity.

while True:
    ball = hero.findNearest(hero.findByType("plasma-ball"))
    if ball and hero.distanceTo(ball) < 5:
        hero.reflect(Vector().subtract(ball.velocity))

Kill the robots? You’ll have to reflect such that each robot hits the other one, e.g.

robots = hero.findByType("robot-walker")
ballOwners = {}

while True:
    balls = hero.findByType("plasma-ball")
    for ball in balls:
        if ball.id not in ballOwners:
            ballOwners[ball.id] = ball.findNearest(robots)
    
    ball = hero.findNearest(balls)
    if ball and hero.distanceTo(ball) < 5:
        robot = hero.findNearest([r for r in robots if r != ballOwners[ball.id]])
        hero.reflect(Vector.subtract(robot.pos, hero.pos))
1 Like

The code not only works but it’s also very beautiful :+1:

1 Like

im not subsciriber so sad