I already learned that Python is not real Python here and support of classes is still work in progress.
I tried a little and found, that the basics of Python classes is working.
Now I am trying to pass a non-numerical object of class (C2) to a method M of another class (C1) and get an error, regardless if the object is constructed by the official literal {‘param1’: param1, ‘param2’: param2…} or by using another helper class.
When I move the method M out of the class C1 and make it global, everything works.
Is this use case still in development or is it supposed to work?
If the latter is the case, I’m happy to provide code-examples, otherwise I would be very interested in a workaround…
Curious to know what from python classes is working? Can you post some code with explanation what you’re attempting to achieve. I’m a python noob ( and a javascript also, only a little bit less ) but despite it I will try to help.
I don’t think this worked 2 years ago in CoCo:
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age() {
let date = new Date();
return date.getFullYear() - this.year;
}
}
let myCar = new Car("Toyota", 2014);
console.log("My car is " + myCar.age() + " years old.");
but now the output in the console is:
My car is 7 years old.
or this:
class A {
constructor() {
let Obj = new B(this); // Make object of class B
}
methodA() // Method to test
{
console.log("Hello!");
}
}
class B {
constructor(a) //receive class A here in class B
{
a.methodA(); // call method of the instance class A
}
}
let a = new A();
Wow, but this is javascript I guess.
I more and more think about resetting my CC account (i.e. delete it…) and restart with JS - but I’m too deep in Py for now - and the goal was basically to learn Py…
I cannot get all of my example code to run satisfactorily… I’ll post it as soon as it is ok, but now it’s a little late here - I have to go to bed…
Maybe my fault is that I too often try real Python code? It often seems to work at first glance but then drops out…
Thanks anyway for the first.
You don’t need to restart with JS.Only change the language to JS and comment the python code. And be aware the default code is written in some JS syntax from Jurassic era. You can try writing ES6 JS - it’s just now I found that this is possible - thank you!
@Archipelago-Gold can you help “translating” the above code to CoCo python?
I tried again, using “Coinucopia” from “Backwoods Forest” with jump-boots to set up an example and … it works…
I don’t know what I did wrong when posting this, unfortunately I deleted the original code - but, yeah, in the end the problem had apparently been sitting in front of the computer.
I’m very sorry.
Here the working example:
class CoinsInfo:
def __init__(self, coins):
self.coins = coins
self.nearest = None
self.farthest = None
self.GetInfo()
def GetInfo(self):
nearest = None
farthest = None
minDist = 9999
maxDist = 0
for coin in self.coins:
dist = hero.distanceTo(coin)
if dist < minDist:
nearest = coin
minDist = dist
if dist > maxDist:
farthest = coin
maxDist = dist
self.nearest = nearest
self.farthest = farthest
class JumpManager:
def __init__(self, initTime):
self.jumptime = initTime
self.cooldown = 8
def JumpIfPossible(self, coinInfo):
if self.jumptime <= 0 or hero.time - self.jumptime >= self.cooldown:
if coinInfo.farthest:
pet.say("jump")
self.jumptime = hero.time
hero.jumpTo(coinInfo.farthest.pos)
return True
return False
#class test
jumpManager = JumpManager(0)
while True:
coinsInfo = CoinsInfo(hero.findByType("coin"))
if not jumpManager.JumpIfPossible(coinsInfo):
if coinsInfo.nearest:
hero.move(coinsInfo.nearest.pos)
OMG so we have here real javascript and python classes!!! I never thought this was possible! @Archipelago-Gold talk to @PhiPaVa - i’m a python noob, when starting codecombat more than 3 years ago I have several months learning to find it is not supported fully here and ditched it.
I’ve also been wondering myself if CoCo will ever introduce classes in their actual levels. Perhaps we can try implementing them for the Game Dev levels? Classes is definitely a can-do for Game Dev as the whole point of these levels is to have players “build” their level from scratch. Classes could be used to construct units and such. I’ll forward this thread to Nick to see if he’s interested.