Hello, this is my first post here - I’m new to Python and Pythonista. As I learn I’ve hit a strange issue with drawing paths I cannot work out.
I’ve been trying to draw a line between 2 points - a fixed point at the centre of the screen and a touched point on the screen - and whilst I can get nodes drawing on screen OK, the line itself is not in the right position. I’ve posted my code below to help replicate the issue.
It’s not in my code below, but I’ve also tried numerous workarounds such as rotating the node, changing anchor points dynamically and calculating the vector between the points, all of which would work in Swift SpriteKit (which I have years of experience with) but don’t seem to work here - the nodes just end up in seemingly random positions with no pattern I can discern.
Any pointers in the right direction would be very much appreciated - many thanks.
import ui
from scene import *
class MyScene (Scene):
def setup(self):
self.background_color = '#00006b'
self.centre = Point(self.size.w/2, self.size.h/2)
def update(self):
pass
def touch_began(self, touch):
# Place dots in scene to test touch and centre points are correct
dotA = Dot(location = self.centre)
self.add_child(dotA)
dotB = Dot(location = touch.location)
self.add_child(dotB)
# The issue: line is right length but never right position or angle
# Draw line between the touched point and centre of the screen
path = ui.Path()
path.line_width = 4.0
path.move_to(self.centre.x, self.centre.y)
path.line_to(touch.location.x, touch.location.y)
line = ShapeNode()
line.path = path
line.position = (self.centre.x, self.centre.y)
line.anchor_point = (0, 0)
line.stroke_color = 'white'
self.add_child(line)
class Dot(ShapeNode):
def __init__(self, *, location, **kwargs):
circle = ui.Path.oval(0,0,20,20)
super().__init__(circle, fill_color='white', **kwargs)
self.position = (location.x, location.y)
run(MyScene())

