omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    Help with intersetion between line and rect

    Pythonista
    noob line game rect intersect
    3
    3
    3277
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Pythonistapro777
      Pythonistapro777 last edited by Pythonistapro777

      So, I'm working on my new project which is a game that there are two balls. One red, and one blue. You must draw a line to prevent them from touching.
      I have gotten the balls, and bouncing to work and I just got the lines to work.
      I just need help or ideas on how to get the balls bounce off the line.
      I can use a similar code to the one I used for the edges of the screen but I'm thinking there might be issues since the line wont be horizontal or vertical. They might be diagonal.
      Any ideas are welcome!

      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))
      		self.line = []
      		
      	def touch_began(self, touch):
      		self.pposx = touch.location.x
      		self.pposy = touch.location.y
      
      	def touch_moved(self, touch):
      		self.x = touch.location.x
      		self.y = touch.location.y
      		x = touch.location.x
      		y = touch.location.y
      		self.line = Rect(self.pposx, self.pposy, self.x, self.y)
      		
      		
      	def draw(self):
      		background(0, 0, 0)
      		for p in self.particles:
      			p.update()
      			p.draw()
      		stroke(1, 1, 1)
      		stroke_weight(7)
      		line(*self.line)
      	
      		
      run(MyScene(), PORTRAIT)
      
      1 Reply Last reply Reply Quote 0
      • Webmaster4o
        Webmaster4o last edited by

        Could you not mention a bunch of people in your posts unless you need to? Thanks.

        1 Reply Last reply Reply Quote 0
        • Oscar
          Oscar last edited by

          A quick google search found me this:
          Detection between a circle and a line

          Should be a good start.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors