You need to think through what you want. What is obs2
before obs1
has gotten halfway through the screen? Is it nothing? An obstacle that's off the screen? An obstacle that works normally, but just doesn't get drawn?
Whatever you decide is pretty easy to code. I'll pick the most difficult and show how to make it work: we won't even create obs2
until obs1
gets far enough. This means we have to check if obs2 is not None
everywhere we use it, and then add a line somewhere that creates it at the appropriate time. So, something like this:
obs1 = obstacle()
obs2 = None
while q < 0:
q = screen.getch()
if obs1.pos < 0:
obs1 = obstacle()
if obs2 is not None and obs2.pos < 0:
obs2 = obstacle()
# Here's where we create obs2 on the fly
if obs2 is None and obs1.pos == dims[0]/2:
obs2 = obstacle()
screen.clear()
screen.addstr(obs1.pos, 0, obs1.s, curses.color_pair(1))
if obs2 is not None:
screen.addstr(obs2.pos, 0, obs2.s, curses.color_pair(1))
obs1.scroll()
if obs2 is not None:
obs2.scroll()
screen.refresh()
time.sleep(.04)
Let's say instead we want to start obs2
offset a bit from obs1
. In that case, we just want to construct it with a different pos
the first time, and everything else is unchanged from your original code:
class obstacle:
def __init__(self, offset=1.0):
self.pos = int((dims[0]-1) * offset)
self.s = ''
for x in range(0,dims[1]-1):
self.s += '='
def scroll(self):
self.pos -= 1
screen = curses.initscr()
curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
screen.nodelay(1)
dims = screen.getmaxyx()
q=-1
obs1 = obstacle()
obs2 = obstacle(.5)
while q < 0:
q = screen.getch()
if obs1.pos < 0:
obs1 = obstacle()
if obs2.pos < 0:
obs2 = obstacle()
screen.clear()
screen.addstr(obs1.pos, 0, obs1.s, curses.color_pair(1))
screen.addstr(obs2.pos, 0, obs2.s, curses.color_pair(1))
obs1.scroll()
obs2.scroll()
screen.refresh()
time.sleep(.04)
screen.getch()
curses.endwin()