Skip to content

Instantly share code, notes, and snippets.

@sleepntsheep
Created October 2, 2021 13:01
Show Gist options
  • Save sleepntsheep/5f24cccdf5e5f2376bc6c40f2965801d to your computer and use it in GitHub Desktop.
Save sleepntsheep/5f24cccdf5e5f2376bc6c40f2965801d to your computer and use it in GitHub Desktop.
bad flappy bird game made in python curses
import curses
import time
import random
class Player:
def __init__(self, y, x, jumping, jumped):
self.y = y #int(height/2)
self.x = x# 10 if width > 20 else int(width/2)
self.jumping = jumping
self.jumped = jumped
self.score = 0
self.distance = x
class Pipe:
def __init__(self, height, dbp, x, width = 5):
self.height = height
self.dbp = dbp
self.x = x
self.width = width
def cmain(stdscr):
curses.noecho()
stdscr.nodelay(1)
height, width = stdscr.getmaxyx()
player = Player(int(height/2), 20 if width > 40 else int(width/2), 0, 0)
key = None
f = 0
pipes = [
Pipe(random.randint(5, height-15), random.randint(6, 10), 100)
]
while key != ord('q'):
# LOGIC
while len(pipes) < 10:
if f == 0:
newpipe = Pipe(random.randint(5, height-15), random.randint(6, 10), pipes[0].x + random.randint(26, 30))
break
else:
newpipe = Pipe(random.randint(5, height-15), random.randint(6, 10), newpipe.x + random.randint(26, 30))
pipes.append(newpipe)
player.distance += 1
if f % 3 == 0:
if not player.jumping:
player.y += 1
if f % 5 == 0:
for pipe in pipes:
pipe.x -= 1
if player.jumping:
if player.jumped > 2:
player.jumping, player.jumped = 0, 0
else:
player.y -= 1
player.jumped += 1
if player.y >= height: player.y = height
if player.y < 0: player.y = 0
# COLLIDE
for pipe in pipes:
if player.x >= pipe.x and player.x <= pipe.x + pipe.width:
if player.y <= pipe.height or player.y >= pipe.height + pipe.dbp:
return gameover(stdscr, player.score, player.x)
# RENDER
stdscr.clear()
# cord
stdscr.insstr(1, 1, f"x: {player.distance}, y: {player.y}")
stdscr.insstr(0, 1, f"score: {player.score}")
# player
stdscr.insstr(player.y, player.x, '#')
#pipe
for pipe in pipes:
if pipe.x <= 1:
pipes.remove(pipe)
player.score += 1
else:
renderpipe(stdscr, pipe)
stdscr.refresh()
# KEY HANDLE
key = stdscr.getch()
if key == 32:
player.jumping = 1
time.sleep(1/60)
f += 1
def renderpipe(win: curses.window, pipe: Pipe):
y, x = win.getmaxyx()
if pipe.x + pipe.width >= x - 1:
return
win.insstr(pipe.height, pipe.x, '└' + '─' * (pipe.width -1) + '┘')
for i in range(0, pipe.height):
win.addch(i, pipe.x, curses.ACS_VLINE)
win.addch(i, pipe.x + pipe.width, curses.ACS_VLINE)
win.insstr(pipe.height + pipe.dbp, pipe.x, '┌' + '─' * (pipe.width -1) + '┐')
for i in range(pipe.height + pipe.dbp + 1, y):
win.addch(i, pipe.x, curses.ACS_VLINE)
win.addch(i, pipe.x + pipe.width, curses.ACS_VLINE)
def gameover(win: curses.window, score, x):
y, x = win.getmaxyx()
win.clear()
prompt = f'You lost! Distance: {x} Score: {score}, press q to quit'
win.insstr(int(y/2), int(x/2)-int(len(prompt)/2), prompt)
win.refresh()
key = win.getch()
while key != ord('q'):
key = win.getch()
def main():
return curses.wrapper(cmain)
if __name__ == '__main__':
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment