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