当たり判定を作ってみた。
ブログのように
def x_collision(self):
width = self.rect.width
height = self.rect.height
# 移動先の矩形と座標を求めます。
newx = self.fpx+self.fpvx
newrect =Rect(newx,self.fpy,width,height)
for block in self.blocks:
collide = newrect.colliderect(block.rect)
if collide:
if self.fpvx > 0:
self.fpx = block.rect.left - width
self.fpvx = 0
elif self.fpvx 0:
self.fpy = block.rect.top - height
self.fpvy = 0
elif self.fpvy < 0:
self.fpy = block.rect.bottom
self.fpvy = 0
break
else:
self.fpy = newy
以下の画像は、上記のミスをして実行した場面。
追記:naohiroさんのご指摘に従い、パックマン風ゲームにしました。