From 104ff78c74a28c62ace583ea18c3bf4875829e00 Mon Sep 17 00:00:00 2001 From: Adam Doyle Date: Fri, 15 Nov 2024 06:30:40 -0500 Subject: [PATCH] division returns a float i guess. also cleanup" " --- .gitignore | 131 ++--------------------------------------------- requirements.txt | 1 + snek.py | 65 ++++++++++++++--------- 3 files changed, 44 insertions(+), 153 deletions(-) create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index b6e4761..31e3ea5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,129 +1,4 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ +bin/ +include/ lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -pip-wheel-metadata/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -.python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ +pyvenv.cfg diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0cb7ff1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pygame diff --git a/snek.py b/snek.py index 9df3379..823faf1 100644 --- a/snek.py +++ b/snek.py @@ -1,6 +1,22 @@ -import pygame, sys, random +import random +import sys -class Snek(): +import pygame + +SCREEN_WIDTH = 768 +SCREEN_HEIGHT = 768 + +GRIDSIZE = 32 +GRID_WIDTH = int(SCREEN_HEIGHT / GRIDSIZE) +GRID_HEIGHT = int(SCREEN_WIDTH / GRIDSIZE) + +UP = (0, -1) +DOWN = (0, 1) +LEFT = (-1, 0) +RIGHT = (1, 0) + + +class Snek: def __init__(self): self.length = 1 self.positions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))] @@ -22,7 +38,10 @@ class Snek(): def move(self): cur = self.get_head_position() x, y = self.direction - new = (((cur[0] + (x*GRIDSIZE)) % SCREEN_WIDTH), (cur[1] + (y*GRIDSIZE)) % SCREEN_HEIGHT) + new = ( + ((cur[0] + (x * GRIDSIZE)) % SCREEN_WIDTH), + (cur[1] + (y * GRIDSIZE)) % SCREEN_HEIGHT, + ) if len(self.positions) > 2 and new in self.positions[2:]: self.reset() else: @@ -30,7 +49,7 @@ class Snek(): if len(self.positions) > self.length: self.positions.pop() - def reset(self): # snek die + def reset(self): # snek die self.length = 1 self.positions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))] self.direction = random.choice([UP, DOWN, LEFT, RIGHT]) @@ -51,15 +70,16 @@ class Snek(): sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP or event.key == pygame.K_w: - self.turn(UP) # all da way + self.turn(UP) # all da way elif event.key == pygame.K_DOWN or event.key == pygame.K_s: - self.turn(DOWN) # for what? + self.turn(DOWN) # for what? elif event.key == pygame.K_LEFT or event.key == pygame.K_a: self.turn(LEFT) elif event.key == pygame.K_RIGHT or event.key == pygame.K_d: self.turn(RIGHT) -class Food(): + +class Food: def __init__(self): self.position = (0, 0) self.bodyColor = (230, 38, 250) @@ -67,43 +87,36 @@ class Food(): self.randomize_position() def randomize_position(self): - self.position = (random.randint(0, GRID_WIDTH-1) * GRIDSIZE, random.randint(0, GRID_HEIGHT-1) * GRIDSIZE) + self.position = ( + random.randint(0, GRID_WIDTH - 1) * GRIDSIZE, + random.randint(0, GRID_HEIGHT - 1) * GRIDSIZE, + ) def draw(self, surface): r = pygame.Rect((self.position[0], self.position[1]), (GRIDSIZE, GRIDSIZE)) pygame.draw.rect(surface, self.bodyColor, r) pygame.draw.rect(surface, self.borderColor, r, 1) + def drawGrid(surface): for y in range(0, int(GRID_HEIGHT)): for x in range(0, int(GRID_WIDTH)): if (x + y) % 2 == 0: - r = pygame.Rect((x*GRIDSIZE, y*GRIDSIZE), (GRIDSIZE, GRIDSIZE)) + r = pygame.Rect((x * GRIDSIZE, y * GRIDSIZE), (GRIDSIZE, GRIDSIZE)) pygame.draw.rect(surface, (10, 10, 10), r) pygame.draw.rect(surface, (15, 15, 15), r, 1) else: - rr = pygame.Rect((x*GRIDSIZE, y*GRIDSIZE), (GRIDSIZE, GRIDSIZE)) + rr = pygame.Rect((x * GRIDSIZE, y * GRIDSIZE), (GRIDSIZE, GRIDSIZE)) pygame.draw.rect(surface, (10, 10, 10), rr) pygame.draw.rect(surface, (15, 15, 15), rr, 1) -SCREEN_WIDTH = 768 -SCREEN_HEIGHT = 768 - -GRIDSIZE = 32 -GRID_WIDTH = SCREEN_HEIGHT / GRIDSIZE -GRID_HEIGHT = SCREEN_WIDTH / GRIDSIZE - -UP = (0, -1) -DOWN = (0, 1) -LEFT = (-1, 0) -RIGHT = (1, 0) def main(): pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32) - pygame.display.set_caption('snek') + pygame.display.set_caption("snek") surface = pygame.Surface(screen.get_size()) surface = surface.convert() @@ -119,7 +132,7 @@ def main(): highScoreMarginTop = 10 scoreMarginTop = fontSize + (highScoreMarginTop * 2) - while (True): + while True: clock.tick(10) snek.handle_keys() drawGrid(surface) @@ -134,11 +147,13 @@ def main(): # scoreboard score = scoreFont.render("Score {0}".format(snek.score), 1, scoreColor) - highScore = scoreFont.render("High Score {0}".format(snek.highScore), 1, scoreColor) + highScore = scoreFont.render( + "High Score {0}".format(snek.highScore), 1, scoreColor + ) screen.blit(highScore, (scoreMarginLeft, highScoreMarginTop)) screen.blit(score, (scoreMarginLeft, scoreMarginTop)) - pygame.display.update() + main()