Skip to content

Commit

Permalink
Fixed linting errors
Browse files Browse the repository at this point in the history
Check everthing with flake8, and
fix things.
  • Loading branch information
MageJohn committed Jul 19, 2014
1 parent 98fb7fd commit 2dd546a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 56 deletions.
12 changes: 6 additions & 6 deletions classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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.
Expand All @@ -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], ' ')
23 changes: 12 additions & 11 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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

Expand All @@ -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
Expand Down
23 changes: 12 additions & 11 deletions dialogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -172,7 +173,7 @@ def dialogue(window, text, answer_map=None):
dialogue.refresh()
window.nodelay(1)

#------------------------
# ------------------------
# Cleanup and return

del dialogue
Expand All @@ -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')
4 changes: 2 additions & 2 deletions display_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions intro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
43 changes: 21 additions & 22 deletions main.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/python3.4

'''Terminal Worm: A remake of the classic Snake game
Copyright (C) 2012, 2013 Yuri Pieters
Expand Down Expand Up @@ -29,26 +31,17 @@

# 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!'''
window = curses.newwin(WINHEIGHT, WINWIDTH, 1, 0)

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:
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2dd546a

Please sign in to comment.