pip install PySFML python import sfml as sf def check_collision(rect1, rect2): if rect1.global_bounds.intersects(rect2.global_bounds): return True return False def main(): width, height = 800, 600 window = sf.RenderWindow(sf.VideoMode(width, height), "Collision Detection") window.framerate_limit = 60 rect1 = sf.RectangleShape((50, 50)) rect1.position = (200, 200) rect1.fill_color = sf.Color.RED rect2 = sf.RectangleShape((100, 100)) rect2.position = (400, 400) rect2.fill_color = sf.Color.GREEN while window.is_open: for event in window.events: if type(event) is sf.CloseEvent: window.close() if check_collision(rect1, rect2): print("Collision detected!") window.clear() window.draw(rect1) window.draw(rect2) window.display() if __name__ == '__main__': main()


上一篇:
下一篇:
切换中文