def __init__(self): self.food = None self._pause = False self._stop = False self.screen = Screen() if self.RIGHT < 50 or self.BOTTOM < 10: print('屏幕空间不足,无法启动游戏') self.exit() raise ValueError() self.screen.open() self.snake = Snake((10, 10), 0, 1) self.cmd_watcher = KeyWatcher(self)
def __init__(self): # Begining: # Exit condition initialization self.running = True # Tempo initialization (Refresh rate) self.t = Tempo() self.fps = 30 self.delay = 1.0 / self.fps # Tempo initialization (Level words) self.t_w = Tempo() # Screen initialization self.scr = Screen() #self.scr.addWord(Word('Score: ', 1, -1, 0)) #self.scr.addWord(Word('Life: ', 15, -1, 0)) #self.scr.addWord(Word(name='video', speed=-0.02)) #self.scr.addWord(Word(name='shot', speed=-0.03, pos_x=15, pos_y=-3)) #self.scr.addWord(Word(name=':)', speed=-0.05, pos_x=22, pos_y=-3)) # Dynamics initialization #self.dyn = Dynamics() # Load levels self.lev = Level() #self.curr_lev = self.lev.loadLev('levels/debug.wrd') self.curr_lev = self.lev.loadLev('levels/rand.lex') # Main loop... self.mainLoop()
def setup_for_drawing(self) -> None: """ Creates a Screen instance that can be supplied with program's variables to draw the current elevator status """ self.screen = Screen()
class Game: def __init__(self): self.food = None self._pause = False self._stop = False self.screen = Screen() if self.RIGHT < 50 or self.BOTTOM < 10: print('屏幕空间不足,无法启动游戏') self.exit() raise ValueError() self.screen.open() self.snake = Snake((10, 10), 0, 1) self.cmd_watcher = KeyWatcher(self) def get_snake_initial_pos(self): x, y = self.get_random_pos() def restore_state(self): chars = [] for x, y in self.snake: chars.append((x, y, SNAKE_NODE_CHAR)) if self.food: chars.append((self.food[0], self.food[1], SNAKE_FOOD_CHAR)) self.screen.refresh() self.screen.draw_n(*chars) def exit(self): self.screen.close() def draw_snake(self, x, y): chars = [(x, y, EMPTY_CHAR)] for nx, ny in self.snake: chars.append((nx, ny, SNAKE_NODE_CHAR)) self.screen.draw_n(*chars) def set_pause(self): flag = not bool(self._pause) if flag: self.screen.show_pad('已暂停') else: self.restore_state() self._pause = flag def stop(self): self._stop = True def is_stop(self): return self._stop @property def paused(self): return self._pause def get_random_pos(self): left = self.LEFT + 1 if left % 2 != 0: left += 1 x = random.randrange(left, self.RIGHT, 2) y = random.randint(self.TOP + 1, self.BOTTOM) return x, y def ready_food(self): while not self.food: x, y = self.get_random_pos() if (x, y) not in self.snake: self.food = x, y self.screen.draw(x, y, SNAKE_FOOD_CHAR) break @property def stdscr(self): return self.screen.get_stdscr() @property def LEFT(self): return self.screen.LEFT @property def RIGHT(self): return self.screen.RIGHT - 2 @property def TOP(self): return self.screen.TOP @property def BOTTOM(self): return self.screen.BOTTOM - 1 def turn_around(self, direction): if not self.paused and not self.is_stop(): if not self.snake.turn_to(*direction): self.screen.warning() def run(self): self.cmd_watcher.watch() while not self._stop: if not self.paused: self.ready_food() last_pos = self.snake.slide( self.LEFT, self.RIGHT, self.TOP, self.BOTTOM) if self.snake.alive: if self.snake.head == self.food: self.snake.eat(*self.food) self.food = None self.draw_snake(*last_pos) else: break time.sleep(0.1) # self.screen.get_stdscr().getch() def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.exit() if exc_val: raise exc_val return not exc_val
class Game: def __init__(self): # Begining: # Exit condition initialization self.running = True # Tempo initialization (Refresh rate) self.t = Tempo() self.fps = 30 self.delay = 1.0 / self.fps # Tempo initialization (Level words) self.t_w = Tempo() # Screen initialization self.scr = Screen() #self.scr.addWord(Word('Score: ', 1, -1, 0)) #self.scr.addWord(Word('Life: ', 15, -1, 0)) #self.scr.addWord(Word(name='video', speed=-0.02)) #self.scr.addWord(Word(name='shot', speed=-0.03, pos_x=15, pos_y=-3)) #self.scr.addWord(Word(name=':)', speed=-0.05, pos_x=22, pos_y=-3)) # Dynamics initialization #self.dyn = Dynamics() # Load levels self.lev = Level() #self.curr_lev = self.lev.loadLev('levels/debug.wrd') self.curr_lev = self.lev.loadLev('levels/rand.lex') # Main loop... self.mainLoop() def main(self): # Print current score #self.scr.addstr(1, 8, str(self.scr.score)) # Check input keys self.c = self.scr.getch() #if self.c != -1: if self.c >= 0 and self.c < 128: #self.scr.addstr(3, 3, chr(self.c)) self.key_res = self.scr.addKey(chr(self.c)) self.runCommand(self.key_res) self.scr.flushinp() def runCommand(self, comm): if comm: if comm == 'exit': self.running = False else: self.scr.scoreWord(comm) def mainLoop(self): while not self.scr.game_over: self.t.tick() self.main() # Level words if len(self.curr_lev) > 0: print "Entrei!" print "ELAPSED:", self.t_w.elapsed() print "ETA BEFORE IF:", self.curr_lev[0].eta if self.t_w.elapsed() >= self.curr_lev[0].eta: print "name", self.curr_lev[0].name print "yln", self.curr_lev[0].pos_y print "eta", self.curr_lev[0].eta print "life", self.scr.life print "elapsed:", self.t_w.elapsed() self.scr.addWord(self.curr_lev[0]) #print "WORDS", self.scr.words[3].name del self.curr_lev[0] print "blowup" print "length:", len(self.curr_lev) self.t_w.reset() elif len(self.scr.words) == 0: self.scr.game_over = True self.scr.init_pair(1, self.scr.COLOR_GREEN, self.scr.COLOR_BLACK) self.scr.addstr(11, 15, "Game Over!", self.scr.color_pair(1)) self.scr.refresh() sleep(3) print "Before dynPump" # Refresh dynamics self.scr.dynPump() print "Before drawPump" # Drawing refresh self.scr.drawPump() print "Before waitUntil" # Keep FPS value stable... self.t.waitUntilMultiple(self.delay)