From 0d0acc1dc354c3b656cc6edc3b2c834dd1290e6e Mon Sep 17 00:00:00 2001 From: geekiot Date: Wed, 29 Oct 2025 22:44:59 +0500 Subject: [PATCH] Refactor code --- pyproject.toml | 28 +++++++++++++++++++ the_snake.py | 75 ++++++++++++++++++++++++++++++-------------------- uv.lock | 28 +++++++++++++++++++ 3 files changed, 101 insertions(+), 30 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6cde314..2abda96 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,4 +12,32 @@ dependencies = [ "pygame>=2.6.1", "pytest==7.1.3", "pytest-timeout==2.1.0", + "ruff>=0.14.2", ] + +[tool.ruff] +target-version = "py313" +line-length = 70 +indent-width = 4 +exclude = [ + ".git/", + "venv/", + ".venv/", + "vscode/", + "__pycache__/", + ".ruff_cache/", +] + + +[tool.ruff.lint] +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" +select = ["E", "F", "N", "I", "C90", "T", "S"] +ignore = [] +fixable = ["ALL"] +unfixable = [] + +[tool.ruff.format] +quote-style = "double" +indent-style = "space" +skip-magic-trailing-comma = false +line-ending = "auto" diff --git a/the_snake.py b/the_snake.py index c6322ce..228c635 100644 --- a/the_snake.py +++ b/the_snake.py @@ -1,4 +1,4 @@ -from random import choice, randint +from random import randint import pygame @@ -43,7 +43,7 @@ clock = pygame.time.Clock() # Тут опишите все классы игры. -class GameObject(): +class GameObject: def __init__(self): self.position_ = [] @@ -70,9 +70,7 @@ class Food(GameObject): self.position_ = self.generate_random_position() def draw(self): - rect = pygame.Rect( - self.position_, (GRID_SIZE, GRID_SIZE) - ) + rect = pygame.Rect(self.position_, (GRID_SIZE, GRID_SIZE)) pygame.draw.rect(screen, self.COLOR, rect) pygame.draw.rect(screen, BORDER_COLOR, rect, 1) @@ -82,6 +80,7 @@ class Apple(Food): COLOR = APPLE_COLOR CHANGE_CELLS = 2 + class Onion(Food): IS_EATABLE = False COLOR = ONION_COLOR @@ -109,7 +108,12 @@ class Snake(GameObject): head_x += move_x * GRID_SIZE head_y += move_y * GRID_SIZE - if head_x >= SCREEN_WIDTH or head_x < 0 or head_y < 0 or head_y >= SCREEN_HEIGHT: + if ( + head_x >= SCREEN_WIDTH + or head_x < 0 + or head_y < 0 + or head_y >= SCREEN_HEIGHT + ): self.__init__() return @@ -119,10 +123,10 @@ class Snake(GameObject): if self.change_tail < 0: for _ in range(abs(self.change_tail)): self.position_.pop() - + if len(self.position_) == 0: - self.__init__() - return + self.__init__() + return head = [head_x, head_y] if head in self.position_: @@ -134,7 +138,6 @@ class Snake(GameObject): self.direction = self.next_direction self.change_tail = 0 - def try_eat(self, food: Food): if self.position_[0] == food.position_: if food.IS_EATABLE: @@ -145,10 +148,9 @@ class Snake(GameObject): return True return False - def draw(self): for position in self.position_: - rect = (pygame.Rect(position, (GRID_SIZE, GRID_SIZE))) + rect = pygame.Rect(position, (GRID_SIZE, GRID_SIZE)) pygame.draw.rect(screen, SNAKE_COLOR, rect) pygame.draw.rect(screen, BORDER_COLOR, rect, 1) @@ -160,17 +162,25 @@ def handle_keys(game_object: GameObject): pygame.quit() raise SystemExit - elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: + elif ( + event.type == pygame.KEYDOWN + and event.key == pygame.K_ESCAPE + ): pygame.quit() raise SystemExit elif event.type == pygame.KEYDOWN: - KEYS = (pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT) + keys = ( + pygame.K_UP, + pygame.K_DOWN, + pygame.K_LEFT, + pygame.K_RIGHT, + ) key = event.key # Отдельная обработка для змейки в 1 клетку # Так как она способна двигаться во всех направлениях. - if key in KEYS and len(game_object.position_) == 1: + if key in keys and len(game_object.position_) == 1: if key == pygame.K_UP: game_object.next_direction = UP elif key == pygame.K_DOWN: @@ -180,14 +190,20 @@ def handle_keys(game_object: GameObject): elif key == pygame.K_RIGHT: game_object.next_direction = RIGHT return - + if key == pygame.K_UP and game_object.direction != DOWN: game_object.next_direction = UP elif key == pygame.K_DOWN and game_object.direction != UP: game_object.next_direction = DOWN - elif key == pygame.K_LEFT and game_object.direction != RIGHT: + elif ( + key == pygame.K_LEFT + and game_object.direction != RIGHT + ): game_object.next_direction = LEFT - elif key == pygame.K_RIGHT and game_object.direction != LEFT: + elif ( + key == pygame.K_RIGHT + and game_object.direction != LEFT + ): game_object.next_direction = RIGHT @@ -196,21 +212,21 @@ def main(): snake = Snake() - CNT_OF_APPLES = 2 - CNT_OF_ONIONS = 1 + cnt_of_apples = 2 + cnt_of_onions = 1 # Макс. кол-во еды, которую можно скушать, # после происходит перестановка еды на карте. - MAX_EATABLE_CNT = None or 3 + max_eatable_cnt = None or 3 current_eatable_cnt = 0 foods = list() - for _ in range(CNT_OF_APPLES): + for _ in range(cnt_of_apples): foods.append(Apple()) - - for _ in range(CNT_OF_ONIONS): + + for _ in range(cnt_of_onions): foods.append(Onion()) while True: @@ -221,18 +237,17 @@ def main(): for food in foods: if snake.try_eat(food): food.update_position() - if MAX_EATABLE_CNT: + if max_eatable_cnt: current_eatable_cnt += 1 if food.IS_EATABLE else 0 - - if current_eatable_cnt >= MAX_EATABLE_CNT: + + if current_eatable_cnt >= max_eatable_cnt: for food in foods: food.update_position() - + current_eatable_cnt = 0 - snake.draw() - + for food in foods: food.draw() diff --git a/uv.lock b/uv.lock index 0b8175a..c2b6d7a 100644 --- a/uv.lock +++ b/uv.lock @@ -179,6 +179,32 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/23/02/c85ca1e18e0c00d5ee45f4012f241098c8995294e1178367cb638a26b6c8/pytest_timeout-2.1.0-py3-none-any.whl", hash = "sha256:f6f50101443ce70ad325ceb4473c4255e9d74e3c7cd0ef827309dfa4c0d975c6", size = 12701, upload-time = "2022-01-18T21:35:22.087Z" }, ] +[[package]] +name = "ruff" +version = "0.14.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/ee/34/8218a19b2055b80601e8fd201ec723c74c7fe1ca06d525a43ed07b6d8e85/ruff-0.14.2.tar.gz", hash = "sha256:98da787668f239313d9c902ca7c523fe11b8ec3f39345553a51b25abc4629c96", size = 5539663, upload-time = "2025-10-23T19:37:00.956Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/16/dd/23eb2db5ad9acae7c845700493b72d3ae214dce0b226f27df89216110f2b/ruff-0.14.2-py3-none-linux_armv6l.whl", hash = "sha256:7cbe4e593505bdec5884c2d0a4d791a90301bc23e49a6b1eb642dd85ef9c64f1", size = 12533390, upload-time = "2025-10-23T19:36:18.044Z" }, + { url = "https://files.pythonhosted.org/packages/5a/8c/5f9acff43ddcf3f85130d0146d0477e28ccecc495f9f684f8f7119b74c0d/ruff-0.14.2-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:8d54b561729cee92f8d89c316ad7a3f9705533f5903b042399b6ae0ddfc62e11", size = 12887187, upload-time = "2025-10-23T19:36:22.664Z" }, + { url = "https://files.pythonhosted.org/packages/99/fa/047646491479074029665022e9f3dc6f0515797f40a4b6014ea8474c539d/ruff-0.14.2-py3-none-macosx_11_0_arm64.whl", hash = "sha256:5c8753dfa44ebb2cde10ce5b4d2ef55a41fb9d9b16732a2c5df64620dbda44a3", size = 11925177, upload-time = "2025-10-23T19:36:24.778Z" }, + { url = "https://files.pythonhosted.org/packages/15/8b/c44cf7fe6e59ab24a9d939493a11030b503bdc2a16622cede8b7b1df0114/ruff-0.14.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3d0bbeffb8d9f4fccf7b5198d566d0bad99a9cb622f1fc3467af96cb8773c9e3", size = 12358285, upload-time = "2025-10-23T19:36:26.979Z" }, + { url = "https://files.pythonhosted.org/packages/45/01/47701b26254267ef40369aea3acb62a7b23e921c27372d127e0f3af48092/ruff-0.14.2-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:7047f0c5a713a401e43a88d36843d9c83a19c584e63d664474675620aaa634a8", size = 12303832, upload-time = "2025-10-23T19:36:29.192Z" }, + { url = "https://files.pythonhosted.org/packages/2d/5c/ae7244ca4fbdf2bee9d6405dcd5bc6ae51ee1df66eb7a9884b77b8af856d/ruff-0.14.2-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:3bf8d2f9aa1602599217d82e8e0af7fd33e5878c4d98f37906b7c93f46f9a839", size = 13036995, upload-time = "2025-10-23T19:36:31.861Z" }, + { url = "https://files.pythonhosted.org/packages/27/4c/0860a79ce6fd4c709ac01173f76f929d53f59748d0dcdd662519835dae43/ruff-0.14.2-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:1c505b389e19c57a317cf4b42db824e2fca96ffb3d86766c1c9f8b96d32048a7", size = 14512649, upload-time = "2025-10-23T19:36:33.915Z" }, + { url = "https://files.pythonhosted.org/packages/7f/7f/d365de998069720a3abfc250ddd876fc4b81a403a766c74ff9bde15b5378/ruff-0.14.2-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:a307fc45ebd887b3f26b36d9326bb70bf69b01561950cdcc6c0bdf7bb8e0f7cc", size = 14088182, upload-time = "2025-10-23T19:36:36.983Z" }, + { url = "https://files.pythonhosted.org/packages/6c/ea/d8e3e6b209162000a7be1faa41b0a0c16a133010311edc3329753cc6596a/ruff-0.14.2-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:61ae91a32c853172f832c2f40bd05fd69f491db7289fb85a9b941ebdd549781a", size = 13599516, upload-time = "2025-10-23T19:36:39.208Z" }, + { url = "https://files.pythonhosted.org/packages/fa/ea/c7810322086db68989fb20a8d5221dd3b79e49e396b01badca07b433ab45/ruff-0.14.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bc1967e40286f63ee23c615e8e7e98098dedc7301568bd88991f6e544d8ae096", size = 13272690, upload-time = "2025-10-23T19:36:41.453Z" }, + { url = "https://files.pythonhosted.org/packages/a9/39/10b05acf8c45786ef501d454e00937e1b97964f846bf28883d1f9619928a/ruff-0.14.2-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:2877f02119cdebf52a632d743a2e302dea422bfae152ebe2f193d3285a3a65df", size = 13496497, upload-time = "2025-10-23T19:36:43.61Z" }, + { url = "https://files.pythonhosted.org/packages/59/a1/1f25f8301e13751c30895092485fada29076e5e14264bdacc37202e85d24/ruff-0.14.2-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:e681c5bc777de5af898decdcb6ba3321d0d466f4cb43c3e7cc2c3b4e7b843a05", size = 12266116, upload-time = "2025-10-23T19:36:45.625Z" }, + { url = "https://files.pythonhosted.org/packages/5c/fa/0029bfc9ce16ae78164e6923ef392e5f173b793b26cc39aa1d8b366cf9dc/ruff-0.14.2-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:e21be42d72e224736f0c992cdb9959a2fa53c7e943b97ef5d081e13170e3ffc5", size = 12281345, upload-time = "2025-10-23T19:36:47.618Z" }, + { url = "https://files.pythonhosted.org/packages/a5/ab/ece7baa3c0f29b7683be868c024f0838770c16607bea6852e46b202f1ff6/ruff-0.14.2-py3-none-musllinux_1_2_i686.whl", hash = "sha256:b8264016f6f209fac16262882dbebf3f8be1629777cf0f37e7aff071b3e9b92e", size = 12629296, upload-time = "2025-10-23T19:36:49.789Z" }, + { url = "https://files.pythonhosted.org/packages/a4/7f/638f54b43f3d4e48c6a68062794e5b367ddac778051806b9e235dfb7aa81/ruff-0.14.2-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:5ca36b4cb4db3067a3b24444463ceea5565ea78b95fe9a07ca7cb7fd16948770", size = 13371610, upload-time = "2025-10-23T19:36:51.882Z" }, + { url = "https://files.pythonhosted.org/packages/8d/35/3654a973ebe5b32e1fd4a08ed2d46755af7267da7ac710d97420d7b8657d/ruff-0.14.2-py3-none-win32.whl", hash = "sha256:41775927d287685e08f48d8eb3f765625ab0b7042cc9377e20e64f4eb0056ee9", size = 12415318, upload-time = "2025-10-23T19:36:53.961Z" }, + { url = "https://files.pythonhosted.org/packages/71/30/3758bcf9e0b6a4193a6f51abf84254aba00887dfa8c20aba18aa366c5f57/ruff-0.14.2-py3-none-win_amd64.whl", hash = "sha256:0df3424aa5c3c08b34ed8ce099df1021e3adaca6e90229273496b839e5a7e1af", size = 13565279, upload-time = "2025-10-23T19:36:56.578Z" }, + { url = "https://files.pythonhosted.org/packages/2e/5d/aa883766f8ef9ffbe6aa24f7192fb71632f31a30e77eb39aa2b0dc4290ac/ruff-0.14.2-py3-none-win_arm64.whl", hash = "sha256:ea9d635e83ba21569fbacda7e78afbfeb94911c9434aff06192d9bc23fd5495a", size = 12554956, upload-time = "2025-10-23T19:36:58.714Z" }, +] + [[package]] name = "snowballstemmer" version = "3.0.1" @@ -200,6 +226,7 @@ dependencies = [ { name = "pygame" }, { name = "pytest" }, { name = "pytest-timeout" }, + { name = "ruff" }, ] [package.metadata] @@ -211,6 +238,7 @@ requires-dist = [ { name = "pygame", specifier = ">=2.6.1" }, { name = "pytest", specifier = "==7.1.3" }, { name = "pytest-timeout", specifier = "==2.1.0" }, + { name = "ruff", specifier = ">=0.14.2" }, ] [[package]]