From acceb61cc4aeafb7dee668c32a7e7ba557a1be7c Mon Sep 17 00:00:00 2001 From: Luchao Qi Date: Mon, 17 May 2021 12:52:48 -0400 Subject: [PATCH] reformat code --- main.py | 83 +++++++++++++++++++++++++++++++++++++++++--------------- start.py | 17 ++++++++---- 2 files changed, 73 insertions(+), 27 deletions(-) diff --git a/main.py b/main.py index 59cf2f1..3e5d0a7 100644 --- a/main.py +++ b/main.py @@ -4,15 +4,22 @@ import curses from start import game -def main(nlines=10,ncols=30,begin_y=0,begin_x=0,difficulty=2): # sourcery no-metrics - difficulty_levels = [[20,0.5],[15,0.3],[5,0.2]] # number of obstables per line, move speed of obstacles and spaceship - obstacle_n,speed = difficulty_levels[difficulty] - + +def main(nlines=10, + ncols=30, + begin_y=0, + begin_x=0, + difficulty=2): # sourcery no-metrics + difficulty_levels = [[20, 0.5], [15, 0.3], [ + 5, 0.2 + ]] # number of obstables per line, move speed of obstacles and spaceship + obstacle_n, speed = difficulty_levels[difficulty] + try: - tmp = game(nlines,ncols,begin_y,begin_x) + tmp = game(nlines, ncols, begin_y, begin_x) win = tmp.draw_window() - score,ship,obstacles = tmp.game_init() - win.addstr(ship[0],ship[1], '*') + score, ship, obstacles = tmp.game_init() + win.addstr(ship[0], ship[1], '*') except: return @@ -23,40 +30,44 @@ def main(nlines=10,ncols=30,begin_y=0,begin_x=0,difficulty=2): # sourcery no-me curses.flushinp() if key == 27: break - + # create random obstacles if len(obstacles) == nlines: obstacles.pop() score += 1 - col_idx = random.sample(range(begin_x,begin_x + ncols),k = random.randint(0,ncols//obstacle_n)) # col index of obstacle + col_idx = random.sample( + range(begin_x, begin_x + ncols), + k=random.randint(0, ncols // obstacle_n)) # col index of obstacle obstacles.appendleft(col_idx) # show obstacles for line_idx in range(len(obstacles)): for col_idx in obstacles[line_idx]: - win.addstr(begin_y+line_idx,col_idx,'-') + win.addstr(begin_y + line_idx, col_idx, '-') # move the ship - ship[1] = ship[1] + (key==curses.KEY_RIGHT) - (key==curses.KEY_LEFT) + ship[1] = ship[1] + (key == curses.KEY_RIGHT) - (key + == curses.KEY_LEFT) # margin case when ship is out of display if ship[1] > begin_x + ncols - 1: ship[1] = begin_x + ncols - 1 if ship[1] < begin_x: ship[1] = begin_x # show ship - win.addstr(ship[0],ship[1], '*') + win.addstr(ship[0], ship[1], '*') # ship hits the obstacle - if (len(obstacles) == nlines and ship[1] in obstacles[-2]) or (len(obstacles)==nlines-1 and ship[1] in obstacles[-1]): + if (len(obstacles) == nlines and ship[1] in obstacles[-2]) or ( + len(obstacles) == nlines - 1 and ship[1] in obstacles[-1]): # change the obstacle shape to 'x' win.addstr(begin_y + nlines - 2, ship[1], 'x') - win.addstr(ship[0],begin_y + ncols + 5, 'Score: '+ str(score)) + win.addstr(ship[0], begin_y + ncols + 5, 'Score: ' + str(score)) win.refresh() time.sleep(1) break # show current score - win.addstr(ship[0],begin_y + ncols + 5, 'Score: '+ str(score)) + win.addstr(ship[0], begin_y + ncols + 5, 'Score: ' + str(score)) win.refresh() win.clear() time.sleep(speed) @@ -64,6 +75,7 @@ def main(nlines=10,ncols=30,begin_y=0,begin_x=0,difficulty=2): # sourcery no-me curses.endwin() print('Final score: ' + str(score)) + if __name__ == '__main__': """[Play the spaceship dodging game in terminal] Args: @@ -74,12 +86,39 @@ def main(nlines=10,ncols=30,begin_y=0,begin_x=0,difficulty=2): # sourcery no-me difficulty (int, optional): [difficulty level: control the number of obstacles/move speed]. Choices = [0,1,2]. Defaults to 2 . """ - parser = argparse.ArgumentParser(description="Play the spaceship dodging game in terminal") - parser.add_argument("--canvas_height", nargs='?', default=10, type=int, help="height of canvas") - parser.add_argument("--canvas_width", nargs='?', default=30, type=int,help="width of canvas") - parser.add_argument("--begin_y", nargs='?', default=0, type=int,help="position of upper margin of canvas") - parser.add_argument("--begin_x", nargs='?', default=0, type=int,help="position of left margin of canvas") - parser.add_argument("--diff_level", nargs='?', default=1, type=int, choices=[0,1,2], help="difficulty level: control the number of obstacles/move speed. Choices = [0,1,2]. Defaults to 2") + parser = argparse.ArgumentParser( + description="Play the spaceship dodging game in terminal") + parser.add_argument("--canvas_height", + nargs='?', + default=10, + type=int, + help="height of canvas") + parser.add_argument("--canvas_width", + nargs='?', + default=30, + type=int, + help="width of canvas") + parser.add_argument("--begin_y", + nargs='?', + default=0, + type=int, + help="position of upper margin of canvas") + parser.add_argument("--begin_x", + nargs='?', + default=0, + type=int, + help="position of left margin of canvas") + parser.add_argument( + "--diff_level", + nargs='?', + default=1, + type=int, + choices=[0, 1, 2], + help= + "difficulty level: control the number of obstacles/move speed. Choices = [0,1,2]. Defaults to 2" + ) args = parser.parse_args() - main(nlines=args.canvas_height,ncols=args.canvas_width,difficulty=args.diff_level) \ No newline at end of file + main(nlines=args.canvas_height, + ncols=args.canvas_width, + difficulty=args.diff_level) diff --git a/start.py b/start.py index 97d2b5b..b8169c4 100644 --- a/start.py +++ b/start.py @@ -1,10 +1,11 @@ import curses import collections + class game: """[game initialization module for drawing canvas / basic game logistic information] """ - def __init__(self,nlines,ncols,begin_y,begin_x): + def __init__(self, nlines, ncols, begin_y, begin_x): self.nlines = nlines self.ncols = ncols self.begin_y = begin_y @@ -18,20 +19,26 @@ def draw_window(self): curses.initscr() curses.noecho() curses.curs_set(False) - win = curses.newwin(self.nlines,self.ncols + 40,self.begin_y,self.begin_x) # + 40: space for printing the score + win = curses.newwin( + self.nlines, self.ncols + 40, self.begin_y, + self.begin_x) # + 40: space for printing the score win.keypad(True) win.nodelay(True) win.refresh() # win.timeout(1000) return win except: - print('Screen initialization failed, please adjust the size of canvas or the size of terminal and try again.') + print( + 'Screen initialization failed, please adjust the size of canvas or the size of terminal and try again.' + ) return # raise def game_init(self): # basic information initialization: score, position of ship, obstacles score = 0 - ship = [self.begin_y+self.nlines-1,self.begin_x+self.ncols//2] # last line, mid of cols + ship = [ + self.begin_y + self.nlines - 1, self.begin_x + self.ncols // 2 + ] # last line, mid of cols obstacles = collections.deque() - return score,ship,obstacles \ No newline at end of file + return score, ship, obstacles