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 intersects()

    Pythonista
    3
    4
    3285
    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

      I created bounds for the game, and they seem to be moving and working as they should. But the player intersects the line it shouldn't go past it. For some of the boundtop line, it woks fine - but it also goes through. This happens for all the lines. Could someone please point out what I did wrong? I tried changing the variables used in making the line to a limit number but it wouldn't work and this is my last option.

      Here's the code:

      from scene import *
      from random import *
      
      class Particle(object):
      	def __init__(self, wh):
      		self.w = wh.w
      		self.h = wh.h
      		self.x = randint(self.w*-1, self.w)
      		self.y = randint(self.h*-1, self.h)
      		self.colour = Color(random(), random(), random())
      		
      		self.cells=Rect(self.x, self.y, 5, 5)
      		
      		self.lw=self.w*-1
      		self.rw=self.w
      		self.bh=self.h*-1
      		self.th=self.h
      		self.boundleft=Rect(self.lw, self.bh, self.lw, self.th)
      		self.boundtop=Rect(self.lw, self.th, self.rw, self.th)
      		self.boundright=Rect(self.rw, self.bh, self.rw, self.th)
      		self.boundbottom=Rect(self.lw, self.bh, self.rw, self.bh)
      
      	def update(self):
      		self.cells=Rect(self.x, self.y, 5, 5)
      		self.boundleft=Rect(self.lw, self.bh, self.lw, self.th)
      		self.boundtop=Rect(self.lw, self.th, self.rw, self.th)
      		self.boundright=Rect(self.rw, self.bh, self.rw, self.th)
      		self.boundbottom=Rect(self.lw, self.bh, self.rw, self.bh)
      
      	def draw(self):
      		stroke(0,0,0)
      		stroke_weight(4)
      		line(*self.boundleft)
      		line(*self.boundtop)
      		line(*self.boundright)
      		line(*self.boundbottom)
      		fill(*self.colour)
      		stroke(*self.colour)
      		ellipse(*self.cells)
      
      class Intro(Scene):
      	def setup(self):
      		global count
      		count=0
      		global psize
      		psize=8
      		plocx=240
      		plocy=160
      		self.player = Rect(plocx, plocy, 20, 20)
      		self.colour = Color(random(), random(), random())
      		self.particles = []
      		for p in xrange(10):
      			self.particles.append(Particle(self.size))
      
      	def touch_began(self, touch):
      		global x1
      		global y1
      		x1=touch.location.x
      		y1=touch.location.y
      
      
      	def touch_moved(self, touch):
      		global x
      		global y
      		x=touch.location.x
      		y=touch.location.y
      		global count
      		count = 1
      
      	def movecells(self):
      		global x
      		global x1
      		global y
      		global y1
      		for p in self.particles:
      			if x1 > x and not p.boundleft.intersects(self.player):
      				p.x += 1.5
      				p.lw += 1.5
      				p.rw += 1.5
      			if x1 < x and not p.boundright.intersects(self.player):
      				p.x += -1.5
      				p.lw += -1.5
      				p.rw += -1.5
      			if y1 > y and not p.boundbottom.intersects(self.player):
      				p.y += 1.5
      				p.bh += 1.5
      				p.th += 1.5
      			if y1 < y and not p.boundtop.intersects(self.player):
      				p.y += -1.5
      				p.bh += -1.5
      				p.th += -1.5
      
      	def draw(self):
      		global count
      		if count == 1:
      			self.movecells()
      		global psize
      		background(0.00, 0.05, 0.20)
      		for p in self.particles:
      			p.update()
      			p.draw()
      			plocx=p.w/2-psize/2
      			plocy=p.h/2-psize/2
      			if self.player.intersects(p.cells):
      				self.newpsize=psize+0.2*10
      				psize=self.newpsize
      				self.particles.remove(p)
      		self.player = Rect(plocx, plocy, psize, psize)
      		ellipse(*self.player)
      
      run(Intro(), LANDSCAPE)
      

      Thanks in advance!

      P.s I know I'm not supposed to post this onto this forum but on stack overflow, etc - no one was able to help me, probably because they don't have pythonista or don't use it as much as the people on this forum do. Just bear with me for this post.
      @ccc @Cethric @dgelessus @Webmaster4o

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

        Some advice if you want to get help on this or any other forum.

        1. dont post giant blocks of code on the forums, repeatedly. It makes it hard to help you, since we cannot refernece line numbers, etc. the app has a built in way to post code as a gist. use it.

        2. The forums are not your only option.... debugging is your first and best option.

        3. Rather than posting "I have a problem somewhere, where is it?", show us that you have thought critically about the problem. Since you have already added print statements befor posting to the forums, before and after the lines that you think might be the problem, share those print statements and their results, and explain why the results are confusing to you. For this problem, you can find the problem with one or two print statements before, and one or two print statements after the region of interest.

        1 Reply Last reply Reply Quote 3
        • Cethric
          Cethric last edited by

          Please have a look at this while I cannot say I always follow it myself it is something good to keep in mind when writing questions
          As to the bounding issue, follow what @JonB has said and then if you still can't get it (make sure to post your findings), I am sure someone has found the answer and will be willing to help

          Also make this project avaliable on Github, that way you can constantly update it to reflect your work and make it easier for us to help you find bugs in the code.

          1 Reply Last reply Reply Quote 1
          • JonB
            JonB last edited by

            In this particular case, it might be good to read the documentation of Rect....

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