A 2D simulation package for cat and line interaction with collision detection
Project description
rcline
A 2D simulation package for cat and line interaction with collision detection.
Overview
rcline is a Python library designed to simulate interactions between "cat" entities and line entities in a 2D environment, with built-in collision detection. It provides an easy-to-use interface for creating and visualizing simulations where cats can move (either controlled by user input or programmatically) and interact with various types of lines.
Features
- 2D simulation environment with customizable grid
- Controllable cat entities with physics-based movement
- Line entities (both solid/collidable and non-solid)
- Real-time collision detection between entities
- Visual feedback for collisions and movement
- Customizable colors and visual properties
From Source
git clone https://github.com/waliwuao/rcline.git
cd rcline
pip install .
For development (with optional dev dependencies):
pip install ".[dev]"
Quick Start
Here's a simple example to create a simulation with a controllable cat and some lines:
from rcline import Map, Cat, Line
# Create a 10x10 simulation map
sim_map = Map(width=10, height=10, title="Simple Cat Simulation")
# Add some lines (solid lines are collidable)
sim_map.add_entity(Line(2, 2, 8, 2, solid=True))
sim_map.add_entity(Line(2, 8, 8, 8, solid=True))
sim_map.add_entity(Line(5, 2, 5, 5)) # Non-solid line
# Add a controllable cat
sim_map.add_entity(Cat(5, 5, radius=0.5, controllable=True))
# Start the simulation
sim_map.start_simulation()
Use arrow keys to move the cat. The cat will collide with solid lines but can pass through non-solid lines.
Core Components
Map
The Map class is the main simulation container that manages all entities and visualization.
Map(width=10, height=10, title="Simulation Map", grid_step=1)
Key methods:
add_entity(entity): Add an entity to the simulationremove_entity(entity): Remove an entity from the simulationstart_simulation(interval=50): Start the animation with specified interval (ms)stop_simulation(): Stop the animation
Cat
The Cat class represents a circular entity that can move and collide with other entities.
Cat(x, y, color=None, radius=1, vx=0, vy=0, ax=0, ay=0,
move_acceleration=2, friction=0.9, controllable=False)
x,y: Initial positionradius: Radius of the catvx,vy: Initial velocity componentscontrollable: If True, can be controlled with arrow keysmove_acceleration: Acceleration when movingfriction: Friction coefficient (0-1) that reduces velocity over time
Line
The Line class represents line segments that can be either solid (collidable) or non-solid.
Line(start_x, start_y, end_x, end_y, color=None, solid=False)
start_x,start_y: Starting coordinatesend_x,end_y: Ending coordinatessolid: If True, cats will collide with this linecolor: Custom color (uses default fromENTITY_COLORSif not specified)
Collision Detection
The Collide class provides static methods for detecting collisions between entities:
check_collision(entity1, entity2): General collision check between any two entities- Supports circle-circle (cat-cat) and circle-line (cat-line) collisions
Customization
Colors
You can customize colors using the constants from rcline.colors:
from rcline import colors
# Example: Change primary color
colors.PRIMARY_COLOR = '#FF5733'
# Entity-specific colors
colors.ENTITY_COLORS['cat'] = '#33FF57'
colors.ENTITY_COLORS['solid_line'] = '#3357FF'
Available color constants:
PRIMARY_COLOR,SECONDARY_COLOR,ACCENT_COLORLIGHT_GRAY,MEDIUM_GRAY,DARK_GRAYBACKGROUND_COLOR,GRID_MAJOR_COLOR,GRID_MINOR_COLORENTITY_COLORS: Dictionary with colors for 'cat', 'line', and 'solid_line'
Advanced Usage
Creating Custom Entities
You can create custom entities by subclassing the Entity base class:
from rcline import Entity
import matplotlib.pyplot as plt
class Square(Entity):
def __init__(self, x, y, size, color=None):
super().__init__(color or '#888888')
self.x = x
self.y = y
self.size = size
self.type = 'square'
def draw(self, ax):
square = plt.Rectangle(
(self.x - self.size/2, self.y - self.size/2),
self.size, self.size,
color=self.color
)
ax.add_patch(square)
def update(self, dt, entity_list=None):
# Add custom update logic here
pass
Adding Collision Logic
To add collision detection for custom entities, extend the Collide class with new collision methods:
from rcline import Collide
@staticmethod
def _square_line_collision(square, line):
# Implement square-line collision logic
return False
Collide._square_line_collision = _square_line_collision
# Update the general collision check
def check_collision(entity1, entity2):
# Existing checks...
if type1 == 'square' and type2 == 'line':
return Collide._square_line_collision(entity1, entity2)
# More checks...
Collide.check_collision = check_collision
Dependencies
- matplotlib >= 3.5.0
- numpy >= 1.21.0
- keyboard >= 0.13.5
License
Apache License
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Issues
Please report any issues or feature requests on the GitHub Issue Tracker.
Author
WallyHao (1276672206@qq.com)
Acknowledgments
- matplotlib for visualization capabilities
- numpy for numerical operations
- keyboard library for input handling
rcline
一个用于猫与线条交互的2D模拟库,包含碰撞检测功能。
概述
rcline是一个Python库,旨在模拟2D环境中"猫"实体与线条实体之间的交互,并内置碰撞检测功能。它提供了易于使用的接口,用于创建和可视化模拟场景,其中猫可以移动(通过用户输入或编程控制)并与各种类型的线条进行交互。
特性
- 可自定义网格的2D模拟环境
- 具有基于物理运动的可控猫实体
- 线条实体(包括实心/可碰撞和非实心两种类型)
- 实体间的实时碰撞检测
- 碰撞和运动的视觉反馈
- 可自定义的颜色和视觉属性
从源码安装
git clone https://github.com/waliwuao/rcline.git
cd rcline
pip install .
开发环境安装(包含可选的开发依赖):
pip install ".[dev]"
快速入门
下面是一个简单的示例,创建一个包含可控猫和一些线条的模拟场景:
from rcline import Map, Cat, Line
# 创建一个10x10的模拟地图
sim_map = Map(width=10, height=10, title="简单的猫模拟")
# 添加一些线条(实心线条是可碰撞的)
sim_map.add_entity(Line(2, 2, 8, 2, solid=True))
sim_map.add_entity(Line(2, 8, 8, 8, solid=True))
sim_map.add_entity(Line(5, 2, 5, 5)) # 非实心线条
# 添加一个可控的猫
sim_map.add_entity(Cat(5, 5, radius=0.5, controllable=True))
# 启动模拟
sim_map.start_simulation()
使用方向键移动猫。猫会与实心线条发生碰撞,但可以穿过非实心线条。
核心组件
Map(地图)
Map类是主要的模拟容器,用于管理所有实体和可视化。
Map(width=10, height=10, title="模拟地图", grid_step=1)
主要方法:
add_entity(entity):向模拟中添加实体remove_entity(entity):从模拟中移除实体start_simulation(interval=50):以指定的时间间隔(毫秒)启动动画stop_simulation():停止动画
Cat(猫)
Cat类表示一个圆形实体,可以移动并与其他实体发生碰撞。
Cat(x, y, color=None, radius=1, vx=0, vy=0, ax=0, ay=0,
move_acceleration=2, friction=0.9, controllable=False)
x,y:初始位置radius:猫的半径vx,vy:初始速度分量controllable:如果为True,可以用方向键控制move_acceleration:移动时的加速度friction:摩擦系数(0-1),用于随时间减小速度
Line(线条)
Line类表示线段,可以是实心(可碰撞)或非实心。
Line(start_x, start_y, end_x, end_y, color=None, solid=False)
start_x,start_y:起始坐标end_x,end_y:结束坐标solid:如果为True,猫会与这条线发生碰撞color:自定义颜色(如果未指定,则使用ENTITY_COLORS中的默认值)
碰撞检测
Collide类提供静态方法用于检测实体之间的碰撞:
check_collision(entity1, entity2):检查任意两个实体之间的碰撞- 支持圆-圆(猫-猫)和圆-线(猫-线)碰撞
自定义
颜色
您可以使用rcline.colors中的常量来自定义颜色:
from rcline import colors
# 示例:更改主色调
colors.PRIMARY_COLOR = '#FF5733'
# 实体特定颜色
colors.ENTITY_COLORS['cat'] = '#33FF57'
colors.ENTITY_COLORS['solid_line'] = '#3357FF'
可用的颜色常量:
PRIMARY_COLOR(主色调)、SECONDARY_COLOR(辅助色)、ACCENT_COLOR(强调色)LIGHT_GRAY(浅灰)、MEDIUM_GRAY(中灰)、DARK_GRAY(深灰)BACKGROUND_COLOR(背景色)、GRID_MAJOR_COLOR(主网格色)、GRID_MINOR_COLOR(次网格色)ENTITY_COLORS:包含'cat'、'line'和'solid_line'的颜色字典
高级用法
创建自定义实体
您可以通过继承Entity基类来创建自定义实体:
from rcline import Entity
import matplotlib.pyplot as plt
class Square(Entity):
def __init__(self, x, y, size, color=None):
super().__init__(color or '#888888')
self.x = x
self.y = y
self.size = size
self.type = 'square'
def draw(self, ax):
square = plt.Rectangle(
(self.x - self.size/2, self.y - self.size/2),
self.size, self.size,
color=self.color
)
ax.add_patch(square)
def update(self, dt, entity_list=None):
# 在这里添加自定义更新逻辑
pass
添加碰撞逻辑
要为自定义实体添加碰撞检测,可以扩展Collide类,添加新的碰撞方法:
from rcline import Collide
@staticmethod
def _square_line_collision(square, line):
# 实现正方形-线条碰撞逻辑
return False
Collide._square_line_collision = _square_line_collision
# 更新通用碰撞检查
def check_collision(entity1, entity2):
# 现有检查...
if type1 == 'square' and type2 == 'line':
return Collide._square_line_collision(entity1, entity2)
# 更多检查...
Collide.check_collision = check_collision
依赖项
- matplotlib >= 3.5.0
- numpy >= 1.21.0
- keyboard >= 0.13.5
许可证
Apache许可证
贡献
- Fork仓库
- 创建您的特性分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add some amazing feature') - 推送到分支(
git push origin feature/amazing-feature) - 打开Pull Request
问题反馈
请在GitHub Issue Tracker上报告任何问题或功能请求。
作者
WallyHao(1276672206@qq.com)
致谢
- matplotlib提供的可视化功能
- numpy提供的数值运算支持
- keyboard库提供的输入处理功能
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rcline-0.1.0.tar.gz.
File metadata
- Download URL: rcline-0.1.0.tar.gz
- Upload date:
- Size: 18.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d592e89702778268d4716075399948b90ce9994d0e97b774d76bb6bbebb59247
|
|
| MD5 |
a6dc1720312b1fc193b480faf9e467b3
|
|
| BLAKE2b-256 |
ece4530e347ea43330f2d01fd6f5eaccf917d75df76176fadb9821e725722b18
|
File details
Details for the file rcline-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rcline-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb73afa4785d1e7f4ca24a2bb93ca295c7fe07ff08744ab8e7d376ca005d5f79
|
|
| MD5 |
c5eb952103abd5808aaabf4764fc6776
|
|
| BLAKE2b-256 |
862054fdad5edc92189a9d8bdfc0083891624d38b7781e6f8bdb8617056b8efe
|