python - Colission testing with numpy -
let's have cell-matrix , coordinates x
, y
denote top-left cell of tetronimo
, matrix b
corresponding tetris well:
t = [[2,2,2], [2,0,0], [0,0,0]] y,x = (1,0) b = [[0,0,0,0,0], [0,0,0,0,0], [1,0,0,0,0],]
currently i'm using simple comparison find collision:
def testcollision(x,y, t, b): dx in xrange(3): dy in xrange(3): if t[dy][dx] == 0: continue else: if b[y+dy][x + dx] != 0: return false return true
can speed use of numpy?
if not mistaken, of form should work
return (b[x:x+3,y:y+3] * t).sum() == 0
you should test correctness; in case algorithmically optimal, within numpy paradigm. note however, operations on small arrays such these not terribly efficient either. still lot better c-style iteration, overhead of array abstraction noticeable. is, insofar performance issue in first place, when comes tetris. ;)
return (b[x:,y:][:3,:3] * t).sum() == 0
btw, think cleaner, more readable indexing these kind of operations; though marginally (o(1)) slower since view constructed.
Comments
Post a Comment