From 2dd546af5d734476dbe3007efe8be9da155f6763 Mon Sep 17 00:00:00 2001 From: MageJohn Date: Sat, 19 Jul 2014 14:22:29 +0100 Subject: [PATCH] Fixed linting errors Check everthing with flake8, and fix things. --- classes.py | 12 ++++++------ constants.py | 23 ++++++++++++----------- dialogue.py | 23 ++++++++++++----------- display_utils.py | 4 ++-- intro.py | 6 +++--- main.py | 43 +++++++++++++++++++++---------------------- misc_utils.py | 2 +- 7 files changed, 57 insertions(+), 56 deletions(-) mode change 100644 => 100755 main.py diff --git a/classes.py b/classes.py index f06ec5d..a7c049d 100644 --- a/classes.py +++ b/classes.py @@ -49,7 +49,7 @@ def new_snake_struct(self, head_pos, length): Used by __init__ ''' self.pos_list = [[head_pos[0], (head_pos[1] - (i + 1))] - for i in range(length - 1)] + for i in range(length - 1)] self.pos_list.insert(0, head_pos) def move(self, d): @@ -179,7 +179,7 @@ def collide_detect(self, window): # The reason this is here now is because an early version # of the game had walls with WALLCHR. - #if window.inch( + # if window.inch( # self.pos_list[0][0], # self.pos_list[0][1]) == ord(constants.WALLCHR): # return True @@ -199,7 +199,7 @@ class Apple: ''' def __init__(self, snake, bug, window): pos_exclude_list = snake.pos_list[:] - if bug != None: + if bug is not None: pos_exclude_list.append(bug.pos) self.pos = misc_utils.get_random_pos(window, pos_exclude_list) @@ -230,7 +230,7 @@ def write(self, window): def display_timer(self, stdscr): '''Shows the timer in the statusbar''' stdscr.addstr(constants.BUG_TIMER_POS[0], constants.BUG_TIMER_POS[1], - '%s %s ' % (self.bug_chr, self.timeout)) + '%s %s ' % (self.bug_chr, self.timeout)) def remove(self, window, stdscr, no_window_overwrite=False): '''Removes the bug and the counter. @@ -240,5 +240,5 @@ def remove(self, window, stdscr, no_window_overwrite=False): if not no_window_overwrite: window.addstr(self.pos[0], self.pos[1], ' ') - stdscr.addstr(constants.BUG_TIMER_POS[0], constants.BUG_TIMER_POS[1], - ' ') + stdscr.addstr(constants.BUG_TIMER_POS[0], + constants.BUG_TIMER_POS[1], ' ') diff --git a/constants.py b/constants.py index dc0c22a..cada6cc 100644 --- a/constants.py +++ b/constants.py @@ -46,15 +46,15 @@ DOWN = 'down' ADJUSTMAP = { - LEFT: [ 0, -1], - RIGHT: [ 0, 1], - UP: [-1, 0], - DOWN: [ 1, 0] + LEFT: [ 0, -1], # noqa + RIGHT: [ 0, 1], # noqa + UP: [-1, 0], # noqa + DOWN: [ 1, 0] # noqa } # NB: For configuration purposes, only things # below here should need to be changed -#============================================ +# ============================================ APPLECHR = 'O' @@ -64,7 +64,7 @@ WINWIDTH = 60 assert WINHEIGHT % 2 == 0 and WINWIDTH % 2 == 0, ( - 'Window height and window width need to be even numbers') + 'Window height and window width need to be even numbers') # This makes life easier, because then we can do things # like halve the window size and still end up with whole numbers @@ -80,15 +80,16 @@ INITPOS[1] < WINWIDTH and INITPOS[0] > 0 and INITPOS[0] < WINHEIGHT), ( - 'The snake needs to start in the window! ' - 'Remember, the snake is not in the window if ' - 'INITPOS - INITLENGTH goes outside the window') + 'The snake needs to start in the window! ' + 'Remember, the snake is not in the window if ' + 'INITPOS - INITLENGTH goes outside the window') # Intial speed, in milliseconds INITSPEED = 150 -WALL_KILL = False # Set to True if you want running into walls to mean - # Game Over. +# Set to True if you want running into walls to mean +# Game Over. +WALL_KILL = False CONTROL_KEYS = {LEFT: 'h', DOWN: 'j', UP: 'k', RIGHT: 'l'} # While the arrow keys will always be available, you can set up an diff --git a/dialogue.py b/dialogue.py index 4633e53..8c9f000 100644 --- a/dialogue.py +++ b/dialogue.py @@ -103,27 +103,28 @@ def dialogue(window, text, answer_map=None): body_text = text[1].split('\n') - #-------------------- + # -------------------- # Now for initial variable setup win_height, win_width = window.getmaxyx() win_start_y, win_start_x = window.getbegyx() width = int((win_width * 70) / 100) # 70% of the window - height = len(body_text) + len(answer_map) + 5 # 1 line of padding - # around the text. + + # 1 line of padding around the text. + height = len(body_text) + len(answer_map) + 5 start_y = int(((win_height - height) / 2) + win_start_y) start_x = int(((win_width - width) / 2) + win_start_x) - #-------------------- + # -------------------- # Now we set up the menu object menu = Menu() for item in answer_map[1:]: menu.add_item(item[0], item[1]) - #----------------- + # ----------------- # That done, now we do the setup the dialogue box. dialogue = curses.newwin(height, width, start_y, start_x) @@ -133,7 +134,7 @@ def dialogue(window, text, answer_map=None): dialogue_title = text[0].center(width - 2, '-') dialogue.addstr(0, 0, ''.join(['+', dialogue_title, '+']), - curses.A_REVERSE | curses.A_BOLD) + curses.A_REVERSE | curses.A_BOLD) for i, s in enumerate(body_text, 2): dialogue.addstr(i, 1, s.center(width - 2)) @@ -143,7 +144,7 @@ def dialogue(window, text, answer_map=None): dialogue.refresh() - #--------------------------- + # --------------------------- # Display of the dialog box dealt with, time for the input. window.nodelay(0) @@ -159,7 +160,7 @@ def dialogue(window, text, answer_map=None): selection = menu.get_current() break else: - menu.has_changed == False + menu.has_changed is False for key in answer_map[0].keys(): if c == key or c == ord(key): @@ -172,7 +173,7 @@ def dialogue(window, text, answer_map=None): dialogue.refresh() window.nodelay(1) - #------------------------ + # ------------------------ # Cleanup and return del dialogue @@ -188,9 +189,9 @@ def test(stdscr): if __name__ == '__main__': answer = curses.wrapper(test) - if answer == True: + if answer is True: print('You replied Yes') - elif answer == False: + elif answer is False: print('You replied No') elif answer == None: print('You exited') diff --git a/display_utils.py b/display_utils.py index 09a55e3..854f6e3 100644 --- a/display_utils.py +++ b/display_utils.py @@ -34,8 +34,8 @@ def set_up_scr(stdscreen, window): # Lastly, we add the 'titlebar' titlebar = 'Terminal Worm (\'?\' for help)%sScore = 0 ' titlebar_len = len(titlebar % ('')) - stdscreen.addstr(0, 0, - titlebar % (' ' * (constants.WINWIDTH - titlebar_len - 1))) + stdscreen.addstr(0, 0, titlebar % ( + ' ' * (constants.WINWIDTH - titlebar_len - 1))) def display_score(score, stdscreen): diff --git a/intro.py b/intro.py index 17b1c02..b61b7c1 100644 --- a/intro.py +++ b/intro.py @@ -61,13 +61,13 @@ def splash(window): window.addstr(int(start_y + i), start_x, line.center(line_length)) window.addstr(int(start_y + snake_num_lines) + 2, 1, - 'T E R M I N A L'.center(line_length)) + 'T E R M I N A L'.center(line_length)) window.addstr(int(start_y + snake_num_lines) + 3, 1, - 'W O R M'.center(line_length)) + 'W O R M'.center(line_length)) window.addstr(int(start_y + snake_num_lines + 5), 1, - '(press any key to start)'.center(line_length)) + '(press any key to start)'.center(line_length)) # The two lines below are chock full of a magic numbers. # Here's a little explanation: diff --git a/main.py b/main.py old mode 100644 new mode 100755 index 3cdd32d..d779afb --- a/main.py +++ b/main.py @@ -1,3 +1,5 @@ +#!/usr/bin/python3.4 + '''Terminal Worm: A remake of the classic Snake game Copyright (C) 2012, 2013 Yuri Pieters @@ -29,17 +31,6 @@ # NB: In curses, positions are 'y,x', instead of 'x,y' -def move_handling(char): - '''This function handles movement keys''' - if char == curses.KEY_LEFT or char == ord(CONTROL_KEYS[LEFT]): - return LEFT - elif char == curses.KEY_RIGHT or char == ord(CONTROL_KEYS[RIGHT]): - return RIGHT - elif char == curses.KEY_UP or char == ord(CONTROL_KEYS[UP]): - return UP - elif char == curses.KEY_DOWN or char == ord(CONTROL_KEYS[DOWN]): - return DOWN - def main(stdscreen): '''The main function. This is where things happen!''' @@ -47,8 +38,10 @@ def main(stdscreen): curses.curs_set(0) # Invisible cursor curses.use_default_colors() - window.keypad(1) # With this setting on, python will interpret special - # keys, suchar as arrow keys, or the numpad. + + # With this setting on, python will interpret special + # keys, such as arrow keys, or the numpad. + window.keypad(1) intro.splash(window) while True: @@ -72,15 +65,21 @@ def main(stdscreen): while True: # The game loop # Event handling char = window.getch() - direction = move_handling(char) - if direction is not None: - if char == ord('q'): - if flow_control.confirm_quit(window): - break - elif char == ord('p'): - flow_control.pause(window) - elif char == ord('?'): - misc_utils.help(window) + if char == curses.KEY_LEFT or char == ord(CONTROL_KEYS[LEFT]): + direction = LEFT + elif char == curses.KEY_RIGHT or char == ord(CONTROL_KEYS[RIGHT]): + direction = RIGHT + elif char == curses.KEY_UP or char == ord(CONTROL_KEYS[UP]): + direction = UP + elif char == curses.KEY_DOWN or char == ord(CONTROL_KEYS[DOWN]): + direction = DOWN + elif char == ord('q'): + if flow_control.confirm_quit(window): + break + elif char == ord('p'): + flow_control.pause(window) + elif char == ord('?'): + misc_utils.help(window) snake.move(direction) diff --git a/misc_utils.py b/misc_utils.py index 090c01a..ea6e2d3 100644 --- a/misc_utils.py +++ b/misc_utils.py @@ -98,7 +98,7 @@ def get_random_pos(win, exclude_list=(None,)): ''' while True: random_pos = [random.randrange(1, win.getmaxyx()[0] - 1), - random.randrange(1, win.getmaxyx()[1] - 1)] + random.randrange(1, win.getmaxyx()[1] - 1)] if random_pos in exclude_list: continue else: