From 611b8c51c3703b1c3b4455bb500ee95a75fc4aae Mon Sep 17 00:00:00 2001 From: gmaciascorrea Date: Mon, 9 May 2022 18:07:32 -0300 Subject: [PATCH] Refs #17 formatation issues resolved --- .idea/.gitignore | 3 + .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 4 + .idea/modules.xml | 8 ++ .idea/python-console-snake.iml | 8 ++ .idea/vcs.xml | 6 ++ menu.py | 26 +++--- snake/console.py | 11 ++- snake/controls.py | 1 - snake/game.py | 39 ++++----- snake/gameloop.py | 7 +- snake/graphics.py | 86 +++++++++---------- snake/parser.py | 1 - snake/run.py | 2 +- snake/stage.py | 2 +- snake/theme.py | 1 - snake/themes.py | 17 ++-- 17 files changed, 128 insertions(+), 100 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/python-console-snake.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..eaf91e2 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c0e1493 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..fe2de19 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/python-console-snake.iml b/.idea/python-console-snake.iml new file mode 100644 index 0000000..d9e6024 --- /dev/null +++ b/.idea/python-console-snake.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9661ac7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/menu.py b/menu.py index 033f25f..ba6da7b 100644 --- a/menu.py +++ b/menu.py @@ -1,4 +1,3 @@ - import subprocess import os @@ -6,15 +5,16 @@ state = 0 -def printHeader(): +def print_header(): os.system('clear') print 'S N A K E\n' + def tutorial(): global state if state == 0: - printHeader() + print_header() print '\nHi there! Welcome to Snake.' print 'Type snake to launch the game' success = False @@ -36,7 +36,7 @@ def tutorial(): print 'Type snake to launch the game' elif state == 1: - printHeader() + print_header() print 'Great game. Now let\'s see what options are available.' print 'Type snake --help' success = False @@ -57,19 +57,20 @@ def tutorial(): success = False while not success: command = raw_input() - cmdArray = command.split(' ') - if len(cmdArray) == 3 and \ - cmdArray[0] == 'snake --theme' and \ - cmdArray[1] == '--theme' or cmdArray[1] == '--t' and \ - cmdArray[2] == 'classic' or cmdArray[2] == 'minimal': - command = "python . --theme " + cmdArray[2] + cmd_array = command.split(' ') + if len(cmd_array) == 3 and \ + cmd_array[0] == 'snake --theme' and \ + cmd_array[1] == '--theme' or cmd_array[1] == '--t' and \ + cmd_array[2] == 'classic' or cmd_array[2] == 'minimal': + command = "python . --theme " + cmd_array[2] subprocess.call(command, shell=True) success = True state += 1 elif command == 'snake --help' or command == 'snake -h': success = False else: - print 'Try typying snake --theme minimal' + print 'Try typing snake --theme minimal' + def run(): try: @@ -78,4 +79,5 @@ def run(): except KeyboardInterrupt: exit() -run() \ No newline at end of file + +run() diff --git a/snake/console.py b/snake/console.py index aec5adb..77e4cbb 100644 --- a/snake/console.py +++ b/snake/console.py @@ -1,28 +1,27 @@ - import fcntl import termios import struct import os -def getTerminalSize(): +def get_terminal_size(): env = os.environ - def ioctl_GWINSZ(fd): + def ioctl_gwinsz(fd): try: cr = struct.unpack( 'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234') - ) + ) except: return return cr - cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) + cr = ioctl_gwinsz(0) or ioctl_gwinsz(1) or ioctl_gwinsz(2) if not cr: try: fd = os.open(os.ctermid(), os.O_RDONLY) - cr = ioctl_GWINSZ(fd) + cr = ioctl_gwinsz(fd) os.close(fd) except: pass diff --git a/snake/controls.py b/snake/controls.py index a5ba89c..8e02328 100644 --- a/snake/controls.py +++ b/snake/controls.py @@ -1,4 +1,3 @@ - import __main__ import graphics import game diff --git a/snake/game.py b/snake/game.py index 008b67d..7178737 100644 --- a/snake/game.py +++ b/snake/game.py @@ -1,4 +1,3 @@ - import stage import gameloop import math @@ -24,34 +23,34 @@ def init(): def update(): - moveSnake() - checkCatch() - checkPositionAllowed() + move_snake() + check_catch() + check_position_allowed() -def checkCatch(): +def check_catch(): if not len(snake) or not len(apples): return for i, apple in enumerate(apples): if (snake[0][0]) == apple[0] and (snake[0][1]) == apple[1]: - eatApple(i) + eat_apple(i) -def eatApple(i): +def eat_apple(i): global grow, score apples.pop(i) - spawnApple() + spawn_apple() grow += config.food_values['apple'] score += 1 -def moveSnake(): +def move_snake(): global grow, lastPos last_unchanged = None - lastPos = (snake[len(snake)-1][0], snake[len(snake)-1][1]) + lastPos = (snake[len(snake) - 1][0], snake[len(snake) - 1][1]) for i, part in enumerate(snake): if i == 0: x = part[0] + speed * direction[0] @@ -68,7 +67,7 @@ def moveSnake(): grow -= 1 -def getGameArea(): +def get_game_area(): w = math.fabs(stage.boundaries['right'] - stage.boundaries['left']) h = math.fabs(stage.boundaries['top'] - stage.boundaries['bottom']) @@ -85,14 +84,14 @@ def reset(): apples = [] grow = config.initial_size - 1 - apples_count += int(math.floor(getGameArea() / config.apple_domain)) + apples_count += int(math.floor(get_game_area() / config.apple_domain)) for i in range(0, apples_count): - spawnApple() + spawn_apple() -def spawnApple(): - if len(apples) >= getGameArea(): +def spawn_apple(): + if len(apples) >= get_game_area(): return x = random.randrange(stage.boundaries['left'], stage.boundaries['right']) @@ -108,13 +107,13 @@ def spawnApple(): if part[0] == x and part[1] == y: position_free = False - if position_free and not isOutOfBoundaries(x, y): + if position_free and not is_out_of_boundaries(x, y): apples.append((x, y)) else: - spawnApple() + spawn_apple() -def isOutOfBoundaries(x, y): +def is_out_of_boundaries(x, y): if x < stage.boundaries['left'] or x > stage.boundaries['right'] - 1: return True @@ -124,7 +123,7 @@ def isOutOfBoundaries(x, y): return False -def checkPositionAllowed(): +def check_position_allowed(): global lives collides_with_body = False @@ -136,7 +135,7 @@ def checkPositionAllowed(): collides_with_body = True break - if (collides_with_body or isOutOfBoundaries(x, y)): + if collides_with_body or is_out_of_boundaries(x, y): gameloop.reset() lives -= 1 if lives == 0: diff --git a/snake/gameloop.py b/snake/gameloop.py index 92e900a..4773c82 100644 --- a/snake/gameloop.py +++ b/snake/gameloop.py @@ -1,4 +1,3 @@ - import time import graphics import game @@ -48,7 +47,7 @@ def start(): if state == 0: step() elif state == 1: - graphics.drawGameOver() + graphics.draw_game_over() def stop(): @@ -61,10 +60,10 @@ def init(): global state game.init() - graphics.drawGame() + graphics.draw_game() state = 0 def reset(): game.reset() - graphics.drawGame() + graphics.draw_game() diff --git a/snake/graphics.py b/snake/graphics.py index eab1fb4..96bd06c 100644 --- a/snake/graphics.py +++ b/snake/graphics.py @@ -1,4 +1,3 @@ - import stage import game import theme @@ -7,87 +6,87 @@ screen = None -def drawTile(x, y, tile='', color=None): +def draw_tile(x, y, tile='', color=None): color = color or theme.get_color('default') x = x * 2 + stage.padding[3] * 2 + stage.width / 2 y += stage.padding[0] + stage.height / 2 screen.addstr(y, x, tile, color) - if (len(tile) < 2): + if len(tile) < 2: screen.addstr(y, x + 1, tile, color) -def drawGameOver(): - drawTile(-4, -1, " GAME OVER ", theme.get_color('border')) - drawTile(-7, 1, " Press ENTER to restart ", theme.get_color('border')) +def draw_game_over(): + draw_tile(-4, -1, " GAME OVER ", theme.get_color('border')) + draw_tile(-7, 1, " Press ENTER to restart ", theme.get_color('border')) -def drawScore(): +def draw_score(): score_formatted = str(game.score).zfill(2) - drawTile( + draw_tile( (stage.width / 2) - 1, (-stage.height / 2) - 1, score_formatted, theme.get_color('border') - ) + ) -def drawLives(): +def draw_lives(): posx = (-stage.width / 2) + 3 for x in xrange(1, game.lives + 1): posx += 1 - drawTile( + draw_tile( posx, (-stage.height / 2) - 1, theme.get_tile('lives'), theme.get_color('lives') - ) + ) posx += 1 - drawTile( + draw_tile( posx, (-stage.height / 2) - 1, theme.get_tile('border-h'), theme.get_color('border') - ) + ) -def drawSnake(): +def draw_snake(): for part in game.snake: - drawTile( + draw_tile( part[0], part[1], theme.get_tile('snake-body'), theme.get_color('snake') - ) + ) # Clean last tile - drawTile( + draw_tile( game.lastPos[0], game.lastPos[1], theme.get_tile('bg'), theme.get_color('bg') - ) + ) -def drawApples(): +def draw_apples(): for apple in game.apples: - drawTile( + draw_tile( apple[0], apple[1], theme.get_tile('apple'), theme.get_color('apple') - ) + ) -def drawGame(): +def draw_game(): for y in range(stage.boundaries['top'], stage.boundaries['bottom']): for x in range(stage.boundaries['left'], stage.boundaries['right']): - drawTile(x, y, theme.get_tile('bg'), theme.get_color('bg')) - drawBorders() - drawText() + draw_tile(x, y, theme.get_tile('bg'), theme.get_color('bg')) + draw_borders() + draw_text() -def drawBorders(): +def draw_borders(): tile_v = theme.get_tile('border-v') tile_h = theme.get_tile('border-h') tile_c = theme.get_tile('border-c') @@ -100,32 +99,31 @@ def drawBorders(): y_bottom = stage.boundaries['bottom'] for y in range(y_top, y_bottom): - drawTile(x_left - 1, y, tile_v, color) - drawTile(x_right, y, tile_v, color) + draw_tile(x_left - 1, y, tile_v, color) + draw_tile(x_right, y, tile_v, color) for x in range(x_left, x_right): - drawTile(x, y_top - 1, tile_h, color) - drawTile(x, y_bottom, tile_h, color) + draw_tile(x, y_top - 1, tile_h, color) + draw_tile(x, y_bottom, tile_h, color) - drawTile(x_left - 1, y_top - 1, tile_c, color) - drawTile(x_left - 1, y_bottom, tile_c, color) - drawTile(x_right, y_top - 1, tile_c, color) - drawTile(x_right, y_bottom, tile_c, color) + draw_tile(x_left - 1, y_top - 1, tile_c, color) + draw_tile(x_left - 1, y_bottom, tile_c, color) + draw_tile(x_right, y_top - 1, tile_c, color) + draw_tile(x_right, y_bottom, tile_c, color) -def drawText(): +def draw_text(): color = theme.get_color('border') - drawTile((stage.width / 2) - 4, (-stage.height / 2) - 1, "score:", color) - drawTile((-stage.width / 2), (-stage.height / 2) - 1, "lives:", color) - drawTile(-5, (stage.height / 2), " Press Q to quit ", color) + draw_tile((stage.width / 2) - 4, (-stage.height / 2) - 1, "score:", color) + draw_tile((-stage.width / 2), (-stage.height / 2) - 1, "lives:", color) + draw_tile(-5, (stage.height / 2), " Press Q to quit ", color) def update(): - - drawSnake() - drawApples() - drawScore() - drawLives() + draw_snake() + draw_apples() + draw_score() + draw_lives() def init(): diff --git a/snake/parser.py b/snake/parser.py index 8d2542a..f3dfa7e 100644 --- a/snake/parser.py +++ b/snake/parser.py @@ -1,4 +1,3 @@ - from optparse import OptionParser options = None diff --git a/snake/run.py b/snake/run.py index b7d6c19..21bb344 100644 --- a/snake/run.py +++ b/snake/run.py @@ -1,4 +1,3 @@ - import graphics import theme import gameloop @@ -22,4 +21,5 @@ def run(): except KeyboardInterrupt: exit() + run() diff --git a/snake/stage.py b/snake/stage.py index fd76e3e..b47a283 100644 --- a/snake/stage.py +++ b/snake/stage.py @@ -9,7 +9,7 @@ def init(): global size, width, height, padding, boundaries, chosen_theme - available_size = (width, height) = console.getTerminalSize() + available_size = (width, height) = console.get_terminal_size() chosen_size = config.game_sizes[parser.options.size] diff --git a/snake/theme.py b/snake/theme.py index a8445c3..c6a8272 100644 --- a/snake/theme.py +++ b/snake/theme.py @@ -1,4 +1,3 @@ - import curses import stage diff --git a/snake/themes.py b/snake/themes.py index 4e66e4a..b1e7bb0 100644 --- a/snake/themes.py +++ b/snake/themes.py @@ -1,14 +1,13 @@ - import curses -#COLOR_BLACK -#COLOR_RED -#COLOR_GREEN -#COLOR_YELLOW -#COLOR_BLUE -#COLOR_MAGENTA -#COLOR_CYAN -#COLOR_WHITE +# COLOR_BLACK +# COLOR_RED +# COLOR_GREEN +# COLOR_YELLOW +# COLOR_BLUE +# COLOR_MAGENTA +# COLOR_CYAN +# COLOR_WHITE game_themes = {