├── README.md ├── LICENSE ├── .gitignore └── cat_chase_ball.py /README.md: -------------------------------------------------------------------------------- 1 | # Cat Chase Ball 2 | 3 | Basic Python mini game where a Cat trying to catch a Ball. 4 | 5 | --- 6 | 7 | ## 🕹️ Controls: 8 | 9 | 10 | - Move your mouse inside the window: the cat will chase it 11 | - Left-click to "teleport" the mouse-target to a random place 12 | - Esc or window close to quit 13 | 14 | --- 15 | 16 | ## ⚙️ Installation 17 | 18 | ### 1️ Requirements 19 | - Python **3.8+** 20 | - Works on **Windows** 21 | - Install dependency: 22 | ```bash 23 | pip install pygame 24 | 25 | ### 2️ Download & Run 26 | 27 | --- 28 | 29 | ### Third-Party Attributions 30 | This project uses: 31 | - **Pygame**, a cross-platform set of Python modules for writing video games. 32 | Licensed under the LGPL 2.1 License. 33 | https://www.pygame.org 34 | 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Menny Levinski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[codz] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py.cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # UV 98 | # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | #uv.lock 102 | 103 | # poetry 104 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 105 | # This is especially recommended for binary packages to ensure reproducibility, and is more 106 | # commonly ignored for libraries. 107 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 108 | #poetry.lock 109 | #poetry.toml 110 | 111 | # pdm 112 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 113 | # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python. 114 | # https://pdm-project.org/en/latest/usage/project/#working-with-version-control 115 | #pdm.lock 116 | #pdm.toml 117 | .pdm-python 118 | .pdm-build/ 119 | 120 | # pixi 121 | # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control. 122 | #pixi.lock 123 | # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one 124 | # in the .venv directory. It is recommended not to include this directory in version control. 125 | .pixi 126 | 127 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 128 | __pypackages__/ 129 | 130 | # Celery stuff 131 | celerybeat-schedule 132 | celerybeat.pid 133 | 134 | # SageMath parsed files 135 | *.sage.py 136 | 137 | # Environments 138 | .env 139 | .envrc 140 | .venv 141 | env/ 142 | venv/ 143 | ENV/ 144 | env.bak/ 145 | venv.bak/ 146 | 147 | # Spyder project settings 148 | .spyderproject 149 | .spyproject 150 | 151 | # Rope project settings 152 | .ropeproject 153 | 154 | # mkdocs documentation 155 | /site 156 | 157 | # mypy 158 | .mypy_cache/ 159 | .dmypy.json 160 | dmypy.json 161 | 162 | # Pyre type checker 163 | .pyre/ 164 | 165 | # pytype static type analyzer 166 | .pytype/ 167 | 168 | # Cython debug symbols 169 | cython_debug/ 170 | 171 | # PyCharm 172 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 173 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 174 | # and can be added to the global gitignore or merged into this file. For a more nuclear 175 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 176 | #.idea/ 177 | 178 | # Abstra 179 | # Abstra is an AI-powered process automation framework. 180 | # Ignore directories containing user credentials, local state, and settings. 181 | # Learn more at https://abstra.io/docs 182 | .abstra/ 183 | 184 | # Visual Studio Code 185 | # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore 186 | # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore 187 | # and can be added to the global gitignore or merged into this file. However, if you prefer, 188 | # you could uncomment the following to ignore the entire vscode folder 189 | # .vscode/ 190 | 191 | # Ruff stuff: 192 | .ruff_cache/ 193 | 194 | # PyPI configuration file 195 | .pypirc 196 | 197 | # Cursor 198 | # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to 199 | # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data 200 | # refer to https://docs.cursor.com/context/ignore-files 201 | .cursorignore 202 | .cursorindexingignore 203 | 204 | # Marimo 205 | marimo/_static/ 206 | marimo/_lsp/ 207 | __marimo__/ 208 | -------------------------------------------------------------------------------- /cat_chase_ball.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | Author: Menny Levinski 5 | 6 | Cat chases the computer mouse (cursor) inside the game window. 7 | 8 | Controls: 9 | - Move your mouse inside the window: the cat will chase it. 10 | - Left-click to "teleport" the mouse-target to a random place. 11 | - Esc or window close to quit. 12 | """ 13 | 14 | import warnings 15 | warnings.filterwarnings("ignore", category=UserWarning) 16 | 17 | import pygame 18 | import random 19 | import math 20 | from pygame.math import Vector2 21 | 22 | # --- Config --- 23 | WIDTH, HEIGHT = 800, 600 24 | FPS = 60 25 | 26 | BG_COLOR = (230, 240, 255) 27 | CAT_COLOR = (255, 165, 0) 28 | EYE_COLOR = (255, 255, 255) 29 | PUPIL_COLOR = (0, 0, 0) 30 | MOUSE_COLOR = (30, 144, 255) 31 | 32 | MAX_SPEED = 200.0 33 | ACCELERATION = 600.0 34 | FRICTION = 0.90 35 | CATCH_DISTANCE = 30 36 | 37 | # --- Pygame setup --- 38 | pygame.init() 39 | screen = pygame.display.set_mode((WIDTH, HEIGHT)) 40 | pygame.display.set_caption("Cat Chasing Mouse") 41 | clock = pygame.time.Clock() 42 | font = pygame.font.SysFont(None, 24) 43 | 44 | # --- Cat setup --- 45 | cat_pos = Vector2(WIDTH/2, HEIGHT/2) 46 | cat_vel = Vector2(0,0) 47 | 48 | # --- Mouse setup --- 49 | mouse_target = Vector2(pygame.mouse.get_pos()) 50 | caught_timer = 0.0 51 | 52 | # --- Draw cat function (simplified version, keep as-is) --- 53 | def draw_cat(surface, pos, angle, size=48): 54 | x, y = int(pos.x), int(pos.y) 55 | 56 | # body (ellipse) 57 | body_rect = pygame.Rect(0, 0, int(size*1.2), size) 58 | body_rect.center = (x, y) 59 | pygame.draw.ellipse(surface, CAT_COLOR, body_rect) 60 | 61 | # head (circle) 62 | head_offset = Vector2(math.cos(angle), math.sin(angle)) * (size*0.6) 63 | head_pos = Vector2(x, y) + head_offset 64 | pygame.draw.circle(surface, CAT_COLOR, (int(head_pos.x), int(head_pos.y)), int(size*0.45)) 65 | 66 | ear1 = [ 67 | (head_pos.x - size*0.25, head_pos.y - size*0.30), 68 | (head_pos.x - size*0.05, head_pos.y - size*0.60), 69 | (head_pos.x + size*0.10, head_pos.y - size*0.20), 70 | ] 71 | ear2 = [ 72 | (head_pos.x + size*0.25, head_pos.y - size*0.30), 73 | (head_pos.x + size*0.05, head_pos.y - size*0.60), 74 | (head_pos.x - size*0.10, head_pos.y - size*0.20), 75 | ] 76 | 77 | pygame.draw.polygon(surface, CAT_COLOR, ear1) 78 | pygame.draw.polygon(surface, CAT_COLOR, ear2) 79 | 80 | eye_offset = Vector2(math.cos(angle+0.15), math.sin(angle+0.15)) * (size*0.15) 81 | sep = size * 0.10 82 | left_eye = head_pos + eye_offset.rotate(20) + Vector2(-sep, 0) 83 | right_eye = head_pos + eye_offset.rotate(-20) + Vector2(sep, 0) 84 | pygame.draw.circle(surface, EYE_COLOR, (int(left_eye.x), int(left_eye.y)), int(size*0.08)) 85 | pygame.draw.circle(surface, EYE_COLOR, (int(right_eye.x), int(right_eye.y)), int(size*0.08)) 86 | 87 | # pupils 88 | look = Vector2(math.cos(angle), math.sin(angle)) * (size*0.03) 89 | pygame.draw.circle(surface, PUPIL_COLOR, (int(left_eye.x + look.x), int(left_eye.y + look.y)), int(size*0.04)) 90 | pygame.draw.circle(surface, PUPIL_COLOR, (int(right_eye.x + look.x), int(right_eye.y + look.y)), int(size*0.04)) 91 | 92 | # tail 93 | tail_start = Vector2(x, y) + Vector2(-size*0.5, size*0.05) 94 | tail_end = tail_start + Vector2(math.cos(angle-1.4), math.sin(angle-1.4)) * (size*0.9) 95 | pygame.draw.lines(surface, CAT_COLOR, False, [(tail_start.x, tail_start.y), (tail_end.x, tail_end.y)], max(2, int(size*0.09))) 96 | 97 | # --- Main loop --- 98 | running = True 99 | while running: 100 | dt = clock.tick(FPS) / 1000.0 # seconds 101 | 102 | for event in pygame.event.get(): 103 | if event.type == pygame.QUIT: 104 | running = False 105 | elif event.type == pygame.KEYDOWN: 106 | if event.key == pygame.K_ESCAPE: 107 | running = False 108 | elif event.type == pygame.MOUSEBUTTONDOWN: 109 | if event.button == 1: # left click: teleport target randomly 110 | mouse_target = Vector2(random.uniform(50, WIDTH-50), random.uniform(50, HEIGHT-50)) 111 | 112 | # Always update mouse_target to current cursor inside window 113 | mx, my = pygame.mouse.get_pos() 114 | mx = max(0, min(WIDTH, mx)) 115 | my = max(0, min(HEIGHT, my)) 116 | mouse_target = Vector2(mx, my) 117 | 118 | # compute steering toward mouse_target 119 | dir_vec = (mouse_target - cat_pos) 120 | dist = dir_vec.length() 121 | if dist > 0.001: 122 | desired = dir_vec.normalize() * MAX_SPEED 123 | else: 124 | desired = Vector2(0,0) 125 | # accelerate toward desired velocity 126 | steer = (desired - cat_vel) 127 | if steer.length() > 0: 128 | steer_limit = ACCELERATION * dt 129 | if steer.length() > steer_limit: 130 | steer.scale_to_length(steer_limit) 131 | cat_vel += steer 132 | 133 | # apply friction / damping 134 | cat_vel *= FRICTION 135 | 136 | # clamp speed 137 | if cat_vel.length() > MAX_SPEED: 138 | cat_vel.scale_to_length(MAX_SPEED) 139 | 140 | cat_pos += cat_vel * dt 141 | 142 | # catch detection 143 | if (cat_pos - mouse_target).length() <= CATCH_DISTANCE: 144 | if caught_timer <= 0.0: 145 | caught_timer = 0.9 146 | mouse_target += Vector2(random.choice([-1,1]), random.choice([-1,1])) * 40 147 | 148 | # draw 149 | screen.fill(BG_COLOR) 150 | 151 | # draw mouse as a perfectly round ball 152 | MOUSE_RADIUS = 12 153 | pygame.draw.circle(screen, MOUSE_COLOR, (int(mouse_target.x), int(mouse_target.y)), MOUSE_RADIUS) 154 | 155 | # draw cat 156 | angle = math.atan2(mouse_target.y - cat_pos.y, mouse_target.x - cat_pos.x) 157 | draw_cat(screen, cat_pos, angle, size=64) 158 | 159 | # HUD 160 | txt = font.render("Move your mouse inside the window", True, (20,20,20)) 161 | screen.blit(txt, (14, 10)) 162 | speed_txt = font.render(f"Cat speed: {int(cat_vel.length())} px/s Distance: {int((cat_pos-mouse_target).length())} px", True, (30,30,30)) 163 | screen.blit(speed_txt, (14, 36)) 164 | 165 | if caught_timer > 0.0: 166 | caught_surf = font.render("Caught", True, (200,30,30)) 167 | screen.blit(caught_surf, (WIDTH//2 - 20, 20)) 168 | caught_timer -= dt 169 | 170 | pygame.display.flip() 171 | 172 | pygame.quit() 173 | 174 | --------------------------------------------------------------------------------