Collision issue on Python snake game -
have issue game of snake in python im making collision check isnt working. have written function check collision between snake , food itself. when collides doesnt anything, have written undraw if collides using function, put in print function see if function working if using , saw no print.
def collide(block1,block2): if math.sqrt(((block2.getcenterx() - block1.getcenterx()) **2)+ ((block2.getcentery() - block1.getcentery())**2)) < block_size: print("true") return true else: return false ------------------------------------------------------- not part of functiom if collide(thesnake[0],food) == true: food.undraw() foodx = random.randint(block_size, win_width-block_size) foody = random.randint(block_size, win_height-block_size) food.draw() thesnake.append(block) else: foodx = foodx foody = foody
i'd suggest modify collide function provide more information. eg.
def collide(block1,block2): dx = block2.getcenterx() - block1.getcenterx() dy = block2.getcentery() - block1.getcentery() dist = math.sqrt(dx ** 2 + dy ** 2) # equivalent math.hypot(dx, dy) if dist < block_size: print("true") return true else: print("false", dx, dy, dist) return false
Comments
Post a Comment