> Tutorial
We have already used several functions and methods (setWeapon, moveToward, etc.). You can also create your own custom functions, with the following syntax:
def add(x, y):
return x + y
def keywordadd( x, y ): character then, below it and indented, the block of instructionsThe optional return keyword makes the function return a value. In the previous example, the function takes the parameters x and y and returns their sum: x + y.
In Python, a function must be defined before it is used. So we put our functions at the top of the file, before the main code.
canFinish(enemy) functionOur previous code for the "finish" condition on the enemy is rather long and hard to reuse, so we'll put it in a function.
We'll create a custom function canFinish(enemy), which takes an enemy and returns a boolean telling whether we can finish it off or not.
We copy the finisher code into a function, written like this:
def canFinish(enemy):
effects = me.weapon.features # list of features
total = 0 # We'll add up all the damage
for effect in effects: # we iterate over all the features
if effect.type == Effect.DAMAGE: # if it's damage
min_damage = effect.minValue # minimum damage of the weapon
boosted_damage = min_damage * (1 + me.strength / 100) # increased by strength
final_damage = boosted_damage * (1 - enemy.relativeShield / 100) - enemy.absoluteShield # reduced by shields
total += final_damage
total_damage = total * (me.tp // me.weapon.cost) # number of possible uses (TP / cost of the weapon)
return total_damage >= enemy.life
Now we can use our canFinish function in our main code, which makes it much simpler to write:
# [...]
if not canFinish(enemy):
# We can't finish it :( Boosts, shield and heal
# [...]
pass
Did you know? Moving code that appears several times into a function is called factoring.
getClosestCellToUseWeapon(enemy) functionYou may have noticed that to use the Machine Gun or, later on, the Laser, you have to be in line with the enemy.
So you'll need to go to a cell from which you can use your weapon, instead of simply moving toward the enemy as with the Pistol.
For this, the me.weaponCells(enemy) method returns all the cells from which you can use your weapon on a target. You then have to pick one: the closest!
So we'll create a getClosestCellToUseWeapon function and use a for in loop to find the cell with the shortest distance to us:
def getClosestCellToUseWeapon(enemy):
closest = None
min_distance = 9999
for cell in me.weaponCells(enemy):
distance = me.cell.distance(cell)
if distance is not None and distance < min_distance:
closest = cell
min_distance = distance
return closest
We use it to replace the movement:
# We move toward the nearest cell from which we can use the weapon
cell = getClosestCellToUseWeapon(enemy)
me.moveToward(cell)
To get a better view of the terrain and the cells, feel free to use tactical mode by pressing T during a fight.
In the next chapters, we'll tackle harder topics, so take the time to play around with your functions and the concepts we've covered: they're already enough to climb the rankings!
Which function correctly returns the square of a number?
def square(x): return x * x def square(x): x * x def square(): return x * x function square(x): return x * x
Which function calculates the maximum of a list?
def maximum(my_list): maxi = -99999 for e in my_list: if e > maxi: maxi = e return maxi def maximum(my_list): maxi = 99999 for e in my_list: if e < maxi: maxi = e return maxi def maximum(my_list): maxi = 99999 for e in my_list: if e > maxi: maxi = e return maxi def maximum(my_list): maxi = -99999 for e in my_list: if e < maxi: maxi = e return maxi
me = Fight.me
# In Python, functions must be defined before they are used.
def canFinish(enemy):
effects = me.weapon.features # list of features
total = 0 # We'll add up all the damage
for effect in effects: # we iterate over all the features
if effect.type == Effect.DAMAGE: # if it's damage
min_damage = effect.minValue # minimum damage of the weapon
boosted_damage = min_damage * (1 + me.strength / 100) # increased by strength
final_damage = boosted_damage * (1 - enemy.relativeShield / 100) - enemy.absoluteShield # reduced by shields
total += final_damage
total_damage = total * (me.tp // me.weapon.cost) # number of possible uses (TP / cost of the weapon)
return total_damage >= enemy.life
def getClosestCellToUseWeapon(enemy):
closest = None
min_distance = 9999
for cell in me.weaponCells(enemy):
distance = me.cell.distance(cell)
if distance is not None and distance < min_distance:
closest = cell
min_distance = distance
return closest
# Main program
# We take the machine gun if it isn't equipped
if me.weapon is None:
me.setWeapon(Weapon.machineGun)
# We get the nearest enemy
enemy = Fight.getNearestEnemy()
# We greet our opponent
if Fight.turn == 1:
me.say("Hi " + enemy.name + "!")
if not canFinish(enemy):
# We can't finish it :( Boosts, shield and heal
me.useChip(Chip.motivation)
me.useChip(Chip.protein)
me.useChip(Chip.helmet)
# We heal ourselves if our life is below our max life.
if me.life < me.maxLife:
me.useChip(Chip.bandage)
# We move toward the nearest cell from which we can use the weapon
cell = getClosestCellToUseWeapon(enemy)
me.moveToward(cell)
# We try to shoot at it 4 times!
for i in range(4):
me.useWeapon(enemy)
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.