Forum Archive

How to draw lines using touch_began/touch_moved/touch_ended

Pythonistapro777

I'm working on a new little project and so far, I have the 2 different objects and their intersection to work fine.
Now I'm trying to get it so that if the players draws a line, it will cause the balls to bounce off the line.
I would just like help on drawing the line using touch_began and touch_ended - then try to work on the bounce by myself...
I used to know how to make this but I forgot and I lost the code I made before which implemented this.
@webmaster4o @ccc @Cethric @dgelessus @JonB @Phuket2
I know that i will have to start with touch_began and then use append to add the touch_moved/touch_ended but I just don't remember how to do it :(
Here's what I have so far:

import random, scene
from scene import *

class Particle(object):
    def __init__(self, wh):
        self.w = wh.w
        self.h = wh.h
        self.xblue = random.randint(0, self.w)
        self.yblue = random.randint(0, self.h)

        self.vxblue = 10
        self.vyblue = 10

        self.xred = random.randint(0, self.w)
        self.yred = random.randint(0, self.h)

        self.vxred = 10
        self.vyred = 10

        self.blueball = Rect(self.xblue, self.yblue, self.w/6, self.w/6)
        self.redball = Rect(self.xred, self.yred, self.w/6, self.w/6)


    def update(self):
        self.xblue += self.vxblue
        self.yblue += self.vyblue

        if self.xblue > self.w-100:
            self.xblue = self.w-100
            self.vxblue *= -1
        if self.xblue < 0:
            self.xblue = 0
            self.vxblue *= -1
        if self.yblue > self.h-100:
            self.yblue = self.h-100
            self.vyblue *= -1
        if self.yblue < 0:
            self.yblue = 0
            self.vyblue *= -1



        self.xred += self.vxred
        self.yred += self.vyred

        if self.xred > self.w-100:
            self.xred = self.w-100
            self.vxred *= -1
        if self.xred < 0:
            self.xred = 0
            self.vxred *= -1
        if self.yred > self.h-100:
            self.yred = self.h-100
            self.vyred *= -1
        if self.yred < 0:
            self.yred = 0
            self.vyred *= -1


        self.blueball = Rect(self.xblue, self.yblue, self.w/6, self.w/6)
        self.redball = Rect(self.xred, self.yred, self.w/6, self.w/6)


    def draw(self):
        fill('#3547ff')
        ellipse(*self.blueball)
        fill('#ff0b0b')
        ellipse(*self.redball)
        if self.blueball.intersects(self.redball):
                print('yay')





class MyScene(Scene):
    def setup(self):
        self.particles = []
        self.particles.append(Particle(self.size))
    def draw(self):
        background(0, 0, 0)
        for p in self.particles:
            p.update()
            p.draw()


run(MyScene(), PORTRAIT)

At the moment I'm just using old codes I've made to help me since I've become a scrub xD

Pythonistapro777

Also, just an additional question while I'm here:
If I decided that I want to take THIS code and compile it into an API. Do you think it would be better to use UI?
And if so, do you think turning it into a UI will be easy when I'm done or is it better to start over and write the code as a UI.

Thanks in advance!

Phuket2

@Pythonistapro777 , I am really bad at this sort of stuff. But I a, pretty sure you after ui.Path and its associated methods for drawing shapes, lines etc. if you search ui.Path in docs, all the class methods are underneath. This is what you use in ui. I am pretty sure the same is used in scene

Phuket2

Or try this search , just searches the forums with Google