I need to know how to use collision detection and how to give an object opacity. Could someone please post some code???
Thx
—Webmaster4o
Forum Archive
Collision detection and opacity
Webmaster4o
Feb 01, 2014 - 11:40
ccc
Jan 26, 2014 - 00:20
Look at the gists in this thread for code that does collision detection:
http://omz-forums.appspot.com/pythonista/post/4695137988902912
Changing a scene.Layer.alpha value will change that Layer's transparency / opacity.
alpha = 0.0 means the Layer is fully transparent
alpha = 1.0 means the Layer is fully opaque
import scene
class MyScene(scene.Scene):
def __init__(self):
scene.run(self)
def setup(self):
center = self.bounds.center()
self.layer = scene.Layer(scene.Rect(center.x - 64, center.y - 64, 128, 128))
self.layer.alpha = 0 # 0.0 = fully transparent, 1.0 = fully opaque
self.layer.background = scene.Color(1, 0, 0)
self.layer.image = 'Snake'
self.add_layer(self.layer)
def draw(self):
scene.background(0, 0, 0)
self.root_layer.update(self.dt)
self.root_layer.draw()
self.layer.alpha += .01 # cycle the layer's alpha value
if self.layer.alpha > 1:
self.layer.alpha = 0
MyScene()
scene.Color objects also have an alpha value that you can mess with: scene.Color(red, green, blue, alpha)
Gerzer
Feb 01, 2014 - 11:11
If you want collision detection for Rect objects, use the Rect.intersects(other_rect) method. It returns a bool value. An example:
from scene import *
class MyScene(Scene):
def draw(self):
fill(1, 0, 0)
rect(0, 0, 50, 75)
r1 = Rect(0, 0, 50, 75)
rect(0, 0, 50, 25)
r2 = Rect(0, 0, 50, 25)
if r1.intersects(r2):
print "R1 intersects R2"
run(MyScene()) # Please note that this script will continuously produce output in the console, so stop the script quickly.
This only works for Rect objects.
I hope that helps!
Sebastian
Feb 01, 2014 - 11:40
This works with circles:
from scene import *
def centered_ellipse(x, y, w, h):
ellipse(x-w/2, y-h/2, w, h)
class MyScene (Scene):
def setup(self):
self.pos = Point(self.size.w/2, self.size.h/2)
self.pos2 = Point(0, 0)
def draw(self):
background(0, 0, 0)
centered_ellipse(self.pos.x, self.pos.y, 100, 100)
centered_ellipse(self.pos2.x, self.pos2.y, 100, 100)
for touch in self.touches.values():
self.pos2 = touch.location
if self.pos.distance(self.pos2) < 100:
fill(1, 0, 0)
else:
fill(1, 1, 1)
run(MyScene())