From 396133d12efce53def3b1521d3a6d38f28944a10 Mon Sep 17 00:00:00 2001 From: kaito Date: Fri, 9 Nov 2018 21:23:55 +0300 Subject: [PATCH 1/2] add flappy bird --- hackathons/games/flappy_bird.py | 98 +++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 hackathons/games/flappy_bird.py diff --git a/hackathons/games/flappy_bird.py b/hackathons/games/flappy_bird.py new file mode 100644 index 0000000..2ed1658 --- /dev/null +++ b/hackathons/games/flappy_bird.py @@ -0,0 +1,98 @@ +import curses +from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN +from random import randint +import sys +sys.path.insert(0, "..") + +import BaseGame + + +class Game(BaseGame): + + @property + def score(self): + #if self.iteration > 44: + return self.iteration // 4 + + def new_bird(self): + new_bird = self.bird + # check key + if self.key == KEY_UP: + new_bird = self.bird[0]-5, self.bird[1] + elif self.key == -1: + new_bird = self.bird[0]+1, self.bird[1] + return new_bird + + def drop_barrier(self): + return self.win_borders[0]//2 + randint(-self.dist//2, self.dist//2) + + def print_barr(self, win, y1, y2, x1, width): + for i in range(1,self.win_borders[0]-1): + if i not in range(y1,y2): + win.addnstr(i, x1, '=', width) + win.addnstr(i, x1+width, ' ', width) + + def loop(self, win): + barr = self.drop_barrier() + self.barriers.insert(0,[barr, barr+self.dist]) + z = 3 + while True: + self.iteration += 1 + win.border(0) + win.addstr(0, 2, 'Score : ' + str(self.score) + ' ') + win.addstr(0, 20, ' FLAPPY BIRD ') + win.timeout(200) + + event = win.getch() + if event == 27: # Escape + break + if event in self.keys: + self.key = event + + bird = self.new_bird() + win.addch(bird[0], bird[1], self.bird_ch) + win.addch(self.bird[0], self.bird[1], ' ') + self.bird = bird + for idx, b in enumerate(self.barriers): + self.print_barr(win, b[0], b[1], self.win_borders[1]-idx*4-5, 4) + if self.iteration % 8: + continue + barr = self.drop_barrier() + self.barriers.insert(0, [barr, barr+self.dist]) + + + def run(self): + self.win_borders = (30, 60, 0, 0) + self.bird = [9,15] + self.barriers = [] + self.key = -1 + self.keys = [KEY_UP, -1] + self.bird_ch = '*' + score = 0 + self.dist = 7 + self.jump = 5 + self.iteration = -1 + curses.initscr() + win = curses.newwin(*self.win_borders) + win.keypad(1) + curses.noecho() + curses.curs_set(0) + win.border(0) + win.nodelay(1) + + try: + self.loop(win) + except KeyboardInterrupt: + pass + except Exception as e: + print(e) + + win.nodelay(0) + win.keypad(0) + curses.echo() + curses.endwin() + print("\nScore - " + str(self.score)) + + +if __name__ == '__main__': + Game().run() \ No newline at end of file From fdce5fb8d53508363ffbb27611b7af6a0df17b99 Mon Sep 17 00:00:00 2001 From: kaito Date: Sat, 10 Nov 2018 09:45:44 +0300 Subject: [PATCH 2/2] small fix printing, api use added --- .../games/{flappy_bird.py => FlappyBird.py} | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) rename hackathons/games/{flappy_bird.py => FlappyBird.py} (71%) diff --git a/hackathons/games/flappy_bird.py b/hackathons/games/FlappyBird.py similarity index 71% rename from hackathons/games/flappy_bird.py rename to hackathons/games/FlappyBird.py index 2ed1658..6310c24 100644 --- a/hackathons/games/flappy_bird.py +++ b/hackathons/games/FlappyBird.py @@ -6,9 +6,9 @@ import BaseGame +NAME = "FlappyBird" -class Game(BaseGame): - +class FlappyBird(BaseGame.BaseGame): @property def score(self): #if self.iteration > 44: @@ -24,17 +24,21 @@ def new_bird(self): return new_bird def drop_barrier(self): - return self.win_borders[0]//2 + randint(-self.dist//2, self.dist//2) + return self.win_borders[0]//2 + randint(0, self.dist) - self.dist def print_barr(self, win, y1, y2, x1, width): for i in range(1,self.win_borders[0]-1): if i not in range(y1,y2): - win.addnstr(i, x1, '=', width) - win.addnstr(i, x1+width, ' ', width) + win.addstr(i, x1, '='*10) + win.addstr(i, x1+width, ' '*10) + + def clear_left_border(self, win): + for i in range(1, self.win_borders[0]-1): + win.addch(i,1, ' ') def loop(self, win): barr = self.drop_barrier() - self.barriers.insert(0,[barr, barr+self.dist]) + self.barriers.append([barr, barr+self.dist]) z = 3 while True: self.iteration += 1 @@ -53,22 +57,27 @@ def loop(self, win): win.addch(bird[0], bird[1], self.bird_ch) win.addch(self.bird[0], self.bird[1], ' ') self.bird = bird + #self.add_scores(self.score) + count = len(self.barriers) for idx, b in enumerate(self.barriers): - self.print_barr(win, b[0], b[1], self.win_borders[1]-idx*4-5, 4) + if self.win_borders[1]-8*(count-idx)-1 < 1: + self.barriers.pop(0) + continue + self.print_barr(win, b[0], b[1], self.win_borders[1]-8*(count-idx)-1, 4) + self.clear_left_border(win) if self.iteration % 8: continue barr = self.drop_barrier() - self.barriers.insert(0, [barr, barr+self.dist]) + self.barriers.append([barr, barr+self.dist]) def run(self): - self.win_borders = (30, 60, 0, 0) - self.bird = [9,15] + self.win_borders = (30, 60, 0, 0) #no constructor = more pain + self.bird = [9,10] self.barriers = [] self.key = -1 self.keys = [KEY_UP, -1] self.bird_ch = '*' - score = 0 self.dist = 7 self.jump = 5 self.iteration = -1 @@ -91,8 +100,9 @@ def run(self): win.keypad(0) curses.echo() curses.endwin() + self.add_scores(self.score) print("\nScore - " + str(self.score)) if __name__ == '__main__': - Game().run() \ No newline at end of file + FlappyBird(NAME, FlappyBird.score).run() \ No newline at end of file