From d8838e151194c053051c0e742934ef3c2267c9b5 Mon Sep 17 00:00:00 2001 From: Luchao Qi Date: Tue, 11 May 2021 13:43:48 -0400 Subject: [PATCH] Luchao's auto-commitment using shell script --- README.md | 15 ++++++ environment_win.yml | 20 ++++++++ main.py | 86 ------------------------------- main2.py | 121 -------------------------------------------- test.py | 82 ++++++++++++++---------------- 5 files changed, 74 insertions(+), 250 deletions(-) create mode 100644 README.md create mode 100644 environment_win.yml delete mode 100644 main.py delete mode 100644 main2.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..7b6ceeb --- /dev/null +++ b/README.md @@ -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 +``` + diff --git a/environment_win.yml b/environment_win.yml new file mode 100644 index 0000000..cd2a219 --- /dev/null +++ b/environment_win.yml @@ -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 diff --git a/main.py b/main.py deleted file mode 100644 index 26c0ae0..0000000 --- a/main.py +++ /dev/null @@ -1,86 +0,0 @@ -import os -# Left arrow is 68, right arrow is 67, space is 32 -import getch - -# What the ship looks like. -ship_string = '|-/\-|' - - -### FUNCTIONS ### - -def draw_screen(x_ship, bullets, x_bullets, y_bullets): - """Given all current parameters, draw the screen.""" - os.system('clear') - print("Terminal Invaders - press 'q' to quit") - draw_ship(x_ship) - draw_bullets(bullets, x_bullets, y_bullets) - -def draw_ship(x_pos): - y_spacing = '\n\n\n' - x_spacing = '' - for x in range(0, x_pos): - x_spacing += ' ' - print(y_spacing + x_spacing + ship_string) - - # Move cursor down, out of the way - print("\n\n\n\n\n") - -def draw_bullet(bullet, bullets, x_bullets, y_bullets): - index = bullets.index(bullet) - x_spacing = '' - for x in range(0, x_bullets[index]): - x_spacing += ' ' - print(x_spacing + '|') - #print(x_bullets, x_bullets[index]) - -def draw_bullets(bullets, x_bullets, y_bullets): - """Loops through all bullets. Draws bullets, and removes any - that have disappeared from the screen.""" - for bullet in bullets: - draw_bullet(bullet, bullets, x_bullets, y_bullets) - - -### MAIN GAME ### - -# These should be dynamic, from current size of window. -game_width = 80 -game_height = 40 - -x_ship = 0 -bullet_number = 0 -# Store bullets in a list -bullets = [] -x_bullets = [] -y_bullets = [] - -# Start with input set to any key value other than 'q'. -input = 'y' - -while input != 'q': - # Test input to see if ship has moved. - if ord(input) == 67: - # Right arrow, increase ship position. - x_ship += 1 - elif ord(input) == 68: - # Left arrow, decrease ship position. - x_ship -= 1 - elif ord(input) == 32: - # Fire a bullet. - bullets.append('bullet_' + str(bullet_number)) - # This bullet's x position is the ship's x at time of firing. - x_bullets.append(x_ship) - y_bullets.append(0) - bullet_number += 1 - - # Keep the ship in bounds. - if x_ship < 0: - x_ship = 0 - elif x_ship > game_width: - x_ship = game_width - - # Draw the screen, which needs the ship's current position, - # the list of bullets, - draw_screen(x_ship, bullets, x_bullets, y_bullets) - - # Poll for input each time we pass through the loop. - input = getch.getch() diff --git a/main2.py b/main2.py deleted file mode 100644 index ae0d47b..0000000 --- a/main2.py +++ /dev/null @@ -1,121 +0,0 @@ -import pygame - - -class Game: - screen = None - aliens = [] - rockets = [] - lost = False - - def __init__(self, width, height): - pygame.init() - self.width = width - self.height = height - self.screen = pygame.display.set_mode((width, height)) - self.clock = pygame.time.Clock() - done = False - - hero = Hero(self, width / 2, height - 20) - generator = Generator(self) - rocket = None - - while not done: - if len(self.aliens) == 0: - self.displayText("VICTORY ACHIEVED") - - pressed = pygame.key.get_pressed() - if pressed[pygame.K_LEFT]: # sipka doleva - hero.x -= 2 if hero.x > 20 else 0 # leva hranice plochy - elif pressed[pygame.K_RIGHT]: # sipka doprava - hero.x += 2 if hero.x < width - 20 else 0 # prava hranice - - for event in pygame.event.get(): - if event.type == pygame.QUIT: - done = True - if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and not self.lost: - self.rockets.append(Rocket(self, hero.x, hero.y)) - - pygame.display.flip() - self.clock.tick(60) - self.screen.fill((0, 0, 0)) - - for alien in self.aliens: - alien.draw() - alien.checkCollision(self) - if (alien.y > height): - self.lost = True - self.displayText("YOU DIED") - - for rocket in self.rockets: - rocket.draw() - - if not self.lost: hero.draw() - - def displayText(self, text): - pygame.font.init() - font = pygame.font.SysFont('Arial', 50) - textsurface = font.render(text, False, (44, 0, 62)) - self.screen.blit(textsurface, (110, 160)) - - -class Alien: - def __init__(self, game, x, y): - self.x = x - self.game = game - self.y = y - self.size = 30 - - def draw(self): - pygame.draw.rect(self.game.screen, # renderovací plocha - (81, 43, 88), # barva objektu - pygame.Rect(self.x, self.y, self.size, self.size)) - self.y += 0.05 - - def checkCollision(self, game): - for rocket in game.rockets: - if (rocket.x < self.x + self.size and - rocket.x > self.x - self.size and - rocket.y < self.y + self.size and - rocket.y > self.y - self.size): - game.rockets.remove(rocket) - game.aliens.remove(self) - - -class Hero: - def __init__(self, game, x, y): - self.x = x - self.game = game - self.y = y - - def draw(self): - pygame.draw.rect(self.game.screen, - (210, 250, 251), - pygame.Rect(self.x, self.y, 8, 5)) - - -class Generator: - def __init__(self, game): - margin = 30 # mezera od okraju obrazovky - width = 50 # mezera mezi alieny - for x in range(margin, game.width - margin, width): - for y in range(margin, int(game.height / 2), width): - game.aliens.append(Alien(game, x, y)) - - # game.aliens.append(Alien(game, 280, 50)) - - -class Rocket: - def __init__(self, game, x, y): - self.x = x - self.y = y - self.game = game - - def draw(self): - pygame.draw.rect(self.game.screen, # renderovací plocha - (254, 52, 110), # barva objektu - pygame.Rect(self.x, self.y, 2, 4)) - self.y -= 2 # poletí po herní ploše nahoru 2px/snímek - - -if __name__ == '__main__': - game = Game(600, 400) diff --git a/test.py b/test.py index 56774b4..0fb7aa7 100644 --- a/test.py +++ b/test.py @@ -7,46 +7,61 @@ 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 @@ -54,33 +69,14 @@ # 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)) \ No newline at end of file