def obstacleCB(self, obstacleMsg): """ @brief Callback to the data_fusion obstacle topic. @param obstacleMsg The obstacle info message This function receives the obstacle info message fro data fusion, checks if the type of the obstacle is valid and if it's pose has a valid quaternion. Then creates an obstacle object using the obstacleMsg. After creating the new obstacle we check if an obstacle with the same id is already inside the obstacle list. In the case of duplicate obstacle we replace the old obstacle with the new one. """ # Check type of obstacle and quaternion if ((obstacleMsg.type != params.softObstacleType) and # noqa (obstacleMsg.type != params.hardObstacleType)): # noqa rospy.logerr( "[MapPatcher]You send me either a barrel or an invalid type. Type: %d", obstacleMsg.type) return quat = obstacleMsg.obstaclePose.pose.orientation if not utils.isQuaternionValid(quat): rospy.logerr( "[MapPatcher]An invalid quaternion was passed, containing NaNs or Infs" ) return if utils.quaternionNotInstantiated(quat): rospy.logerr( "[MapPatcher]An invalid quaternion was passed, containing all zeros" ) return # TODO (dimkirts) Check if length and width are very big # Create an obstacle from the message of the callback obs = Obstacle() obs.createObstacle(obstacleMsg) # Search inside the list if we have an obstacle with the same id # If we do, we delete it and then append the new version of this obstacle # If we can't find a duplicate obstacle we append it duplicate = False for i in xrange(0, len(self._obstacle_list)): if self._obstacle_list[i].id_ == obs.id_: rospy.logwarn( "[MapPatcher]Received new version of obstacle with id: %d", self._obstacle_list[i].id_) rospy.loginfo("[MapPatcher]Replacing it in the obstacle list") self._obstacle_list[i] = obs duplicate = True if not duplicate: self._obstacle_list.append(obs)
def main(stdscr, args): curses.curs_set(0) stdscr.nodelay(1) mh, mw = stdscr.getmaxyx() begin_x = 0 begin_y = 0 if args.canvas_height > mh - begin_y or args.canvas_width > mw - begin_x: raise RuntimeError( "The game can't initialize, please adjust your screen size or change canvas size." ) sh = args.canvas_height #leave space for score sw = args.canvas_width - 10 difference = [[50000, 6], [30000, 5], [20000, 4]] #the speed of generating obsttacles, according to different level speed, div = difference[args.diff_level - 1] obstacle = Obstacle(sw, sh, div) ship = Ship(sh, sw, begin_x, begin_y) stdscr.addstr(ship.location[0], ship.location[1], "*") moveList = [curses.KEY_LEFT, curses.KEY_RIGHT] cob = 0 score = 0 while 1: cob += 1 if cob == speed: score += 1 cob = 0 #add new obstacles stdscr.clear() obstacle.createObstacle() for row in range(len(obstacle.obstacles)): for col in obstacle.obstacles[row]: stdscr.addstr(row, col, "-") #move the ship key = stdscr.getch() if key in moveList: stdscr.addstr(ship.location[0], ship.location[1], " ") ship.move(key) elif key == 27: return #check if collision happens #beacuse the ship and obstacle could meet at the bottom, so just check if collision when the obstacle arrived the bottom if len(obstacle.obstacles) == sh: if not ship.safe(obstacle.obstacles[len(obstacle.obstacles) - 1]): #draw the result stdscr.addstr(ship.location[0], ship.location[1], "x") stdscr.addstr(sh - 1, sw + 3, "score: " + str(score - sh)) stdscr.addstr(sh // 2, sw // 2 - 13, "press any key to continue") stdscr.refresh() stdscr.nodelay(0) stdscr.getch() break stdscr.addstr(ship.location[0], ship.location[1], "*") stdscr.refresh()