Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Luchao Qi committed May 17, 2021
1 parent 6e3fdf8 commit acceb61
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 27 deletions.
83 changes: 61 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -23,47 +30,52 @@ 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)

curses.endwin()
print('Final score: ' + str(score))


if __name__ == '__main__':
"""[Play the spaceship dodging game in terminal]
Args:
Expand All @@ -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)
main(nlines=args.canvas_height,
ncols=args.canvas_width,
difficulty=args.diff_level)
17 changes: 12 additions & 5 deletions start.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
return score, ship, obstacles

0 comments on commit acceb61

Please sign in to comment.