Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
How to use a unicode character as a movable object in scene?
-
I am trying to make chess, I can draw the image of the unicode king chess piece with this code:
from scene import * class example(Scene): def draw(self): image(render_text(str(unichr(9812)), font_size = 80)[0], examplex, exampley)
When I run this, it draws the king chess piece at the inputted x and y, although I am trying to create a touch event where the king moves to where the player taps, that is what I need and I can do the logic part with testing if it is a legal move, although, how can I move the king without re-running the draw function. Please help!
-
As I mentioned in other post use ShapeNode and LabelNode
https://forum.omz-software.com/topic/3610/scene_drawing-help -
@abcabc I'm sorry, I understand what you mean, but I just can't figure it out. Here is my code for a label node:
from scene import * class test(Scene): def setup(self): self.background_color = 'beige' self.test = LabelNode(unichr(9812), 500, 500) run(test())
And when I run it, I get the strangest error, typeerror expect a texture or image. Please help!
-
This works for me in Python 2.7
from scene import * class test(Scene): def setup(self): self.background_color = 'beige' self.test = LabelNode(unichr(9812), position=(500, 500), color='black', parent=self) run(test())
-
@abcabc oh lol, oops. Thank you for your help!!
-
@amharder If you want to use Unicode characters in string literals, there are a few better options than
unichr
. For all of them, you should useunicode
strings (u"..."
, with au
before the string) because Python 2's oldstr
strings don't work properly with Unicode.First, you can use a Unicode hex escape like
u"\u2654"
, this is simple but not much nicer thanunichr
. There are also Unicode name escapes likeu"\N{WHITE CHESS KING}"
, this inserts the Unicode character with that name into the string.The third option is to copy and paste the characters you need directly into the Unicode string. If you do this, you also need to add the following line to the start of your script:
# -*- coding: utf-8 -*-
This comment tells Python how Unicode characters are stored in the file, so it knows how to read them. Then you can type or paste any Unicode character into your
u"..."
string and it will appear correctly.By the way, Python 3 is much better at handling Unicode than Python 2 is. Python 3's
str
supports Unicode correctly, so you don't have to useunicode
andu"..."
strings by hand, and you can type Unicode characters directly into your file without adding acoding
line at the top. There are lots of other great features in Python 3 too. If you're already using Pythonista 3, you can try switching your Python version to 3.5 and see if your script runs with that.