Skip to content

Commit

Permalink
Luchao's auto-commitment using shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
luchaoqi committed May 11, 2021
1 parent 23eb0a5 commit d8838e1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 250 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Python-based spaceship dodging game in terminal using curses (windows-curses), no pygame required.







```bash
# unit test for windows
conda env create -f environment_win.yml -n spaceship # env name
conda activate spaceship
python test.py
```

20 changes: 20 additions & 0 deletions environment_win.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: spaceship
channels:
- defaults
dependencies:
- ca-certificates=2021.4.13=haa95532_1
- certifi=2020.12.5=py39haa95532_0
- openssl=1.1.1k=h2bbff1b_0
- pip=21.0.1=py39haa95532_0
- python=3.9.4=h6244533_0
- setuptools=52.0.0=py39haa95532_0
- sqlite=3.35.4=h2bbff1b_0
- tzdata=2020f=h52ac0ba_0
- vc=14.2=h21ff451_1
- vs2015_runtime=14.27.29016=h5e58377_2
- wheel=0.36.2=pyhd3eb1b0_0
- wincertstore=0.2=py39h2bbff1b_0
- pip:
- pygame==2.0.1
- windows-curses==2.2.0
prefix: C:\Anaconda3\envs\spaceship
86 changes: 0 additions & 86 deletions main.py

This file was deleted.

121 changes: 0 additions & 121 deletions main2.py

This file was deleted.

82 changes: 39 additions & 43 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,76 @@

curses.initscr()
nlines,ncols,begin_y,begin_x = 10,30,0,0
win = curses.newwin(nlines,ncols,begin_y,begin_x)
win = curses.newwin(nlines,ncols + 40,begin_y,begin_x) # + 40: space for printing the score
curses.noecho()
curses.curs_set(False)
win.keypad(True)
# win.scroll(1)
# win.scrollok(1)
# win.border(0)
# win.nodelay(1)


win.nodelay(1)
# win.timeout(100)

# spaceship and obstacle init
ship = [nlines-1,ncols//2] # last line, mid of cols
ship = [begin_y+nlines-1,ncols//2] # last line, mid of cols
obstacles = collections.deque()



# game logic

# game
score = 0
c = 1
while True:
win.clear()
win.addch(ship[0],ship[1], '*')
win.clear() # imp

# create random obstacles
if len(obstacles) == nlines:
obstacles.pop()
score += 1

ncol = random.sample(range(begin_x,begin_x + ncols),k = random.randint(0,(ncols-begin_x)//4)) # col index of obstacle
obstacles.appendleft(ncol)

col_idx = random.sample(range(begin_x,begin_x + ncols),k = random.randint(0,ncols//5)) # col index of obstacle
obstacles.appendleft(col_idx)

# draw obstacles
for nline in range(len(obstacles)):
for ncol in obstacles[nline]:
win.insstr(nline,ncol,'-')
win.refresh()
for line_idx in range(len(obstacles)):
for col_idx in obstacles[line_idx]:
win.addstr(begin_y+line_idx,col_idx,'-')

# draw ship
key = win.getch()
if key == 27:
break
ship[1] = ship[1] + (key==curses.KEY_RIGHT) - (key==curses.KEY_LEFT)
# margin: case when ship out of display
if ship[1] > begin_x + ncols - 1:
ship[1] = begin_x + ncols - 1
if ship[0] < begin_x:
ship[0] = begin_x
win.addstr(ship[0],ship[1], '*')


# game logic

# ship will hit the obstacle in the next frame (second last line)
if len(obstacles) >= nlines-1 and ship[1] in obstacles[-2]:
# change the obstacle
win.addstr(begin_y + nlines - 2, ship[1], 'x')
win.addstr(ship[0],begin_y + ncols + 5, 'Score: '+ str(score))
win.refresh()
time.sleep(1)
break

# event = win.getch()
# key = event
# if key == curses.KEY_LEFT:

# if key == curses.KEY_RIGHT:


# # margin
# if x > ncols:
# x = ncols



# ship =

# if ship in obstacles[-1]:
win.addstr(ship[0],begin_y + ncols + 5, 'Score: '+ str(score))
win.refresh()
time.sleep(0.3)
# c += 1
# if c == 20:
# break

# if key in [curses.KEY_LEFT, curses.KEY_RIGHT]:






time.sleep(0.5)
c += 1
if c == 20:
break





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

0 comments on commit d8838e1

Please sign in to comment.