本文整理汇总了Python中world.World.add_object方法的典型用法代码示例。如果您正苦于以下问题:Python World.add_object方法的具体用法?Python World.add_object怎么用?Python World.add_object使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类world.World
的用法示例。
在下文中一共展示了World.add_object方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import add_object [as 别名]
def main():
# Init graphics
pygame.init()
window = pygame.display.set_mode((1060, 760)) # This is the size of the field + contestant area. (5300 x 3800)
pygame.display.set_caption('Robotex 2011 Simulator')
screen = pygame.display.get_surface()
# Init world.
world = World(screen)
# Add 11 balls (coordinates are world-coords)
#random.seed(3)
for i in range(11):
world.add_object(Ball(Point(random.uniform(10,world.width-10),random.uniform(10,world.height-10))))
# Create two robots
robot1 = Robot(world, "Robot A", Point(12+45/2, 12+35/2), beacon_point = Point(world.width + 50, world.cy))
robot1.rotate(3.1415/2)
robot2 = Robot(world, "Robot B", Point(world.width-(12+45/2), world.height-(12+35/2)), beacon_point = Point(-50, world.cy))
robot2.rotate(-3.1415/2)
world.add_object(robot1)
world.add_object(robot2)
# Start robot command servers
RobotServer(robot1, 5000).serve()
RobotServer(robot2, 5001).serve()
# Do the simulation/drawing/event cycle
last_sim = -1000
last_draw = -1000
while True:
t = get_ticks()
if (t - last_sim) > 1:
# Simulate world (~1 iteration once every millisecond or so)
# NB: This is kinda hard-coded into the logic currently,
# i.e. World.simulate() and Ball.simulate() and anyone else is
# free to assume that a simulation step is 1ms. In particular,
# the ball computes it's friction coefficient like that.
world.simulate()
last_sim = t
if (t - last_draw) > 40:
# Draw a frame once every 40 milliseconds or so (~25 fps)
BACKGROUND_BLUE = (120,119,253)
screen.fill(BACKGROUND_BLUE)
world.draw(screen)
pygame.display.flip()
last_draw = t
# Process input
input(pygame.event.get())
示例2: TestWindow
# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import add_object [as 别名]
#.........这里部分代码省略.........
Wall(world_width, world_height, facing=DIRECTIONS['right'], gaps=range(60, 90)),
Monster(23, 25, color=COLOURS['grey']),
Monster(53, 83, color=COLOURS['white']),
Monster(10, 40, color=COLOURS['grey'])
],
[
Wall(world_width, world_height, facing=DIRECTIONS['up']),
Wall(world_width, world_height, facing=DIRECTIONS['down']),
Wall(world_width, world_height, facing=DIRECTIONS['left']),
Wall(world_width, world_height, facing=DIRECTIONS['right'])
]
]
levels[0].extend([Monster(randint(0, world_width - 9),
randint(0, world_height - 9)) for x in xrange(90)])
self.world = World(self.player, levels=levels, width=world_width, height=world_height)
self.widget = GLPlotWidget(100, 100, self.world)
self.color = COLOURS['white']
self.keys = set()
self.widget.setGeometry(0, 0, self.widget.width, self.widget.height)
self.setCentralWidget(self.widget)
self.show()
self.paint_timer = QtCore.QTimer()
QtCore.QObject.connect(self.paint_timer, QtCore.SIGNAL("timeout()"), self.widget.updateGL)
self.button_timer = QtCore.QTimer()
QtCore.QObject.connect(self.button_timer, QtCore.SIGNAL("timeout()"), self.check)
self.tick_timer = QtCore.QTimer()
QtCore.QObject.connect(self.tick_timer, QtCore.SIGNAL("timeout()"), self.world.tick)
QtCore.QMetaObject.connectSlotsByName(self)
self.paint_timer.start(30)
self.tick_timer.start(25)
self.button_timer.start(25)
self.resize(600, 400)
def keyPressEvent(self, event):
self.keys.add(event.key())
def keyReleaseEvent(self, event):
try:
self.keys.remove(event.key())
except:
pass
def check(self):
x = self.world.player.x
y = self.world.player.y
player_movement = DIRECTIONS['still']
face_movement = DIRECTIONS['still']
egg_direction = DIRECTIONS['still']
egg = None
for key in self.keys:
if key == QtCore.Qt.Key_A:
face_movement += DIRECTIONS['left']
elif key == QtCore.Qt.Key_D:
face_movement += DIRECTIONS['right']
elif key == QtCore.Qt.Key_W:
face_movement += DIRECTIONS['up']
elif key == QtCore.Qt.Key_S:
face_movement += DIRECTIONS['down']
elif key == QtCore.Qt.Key_Up:
player_movement += DIRECTIONS['up']
elif key == QtCore.Qt.Key_Down:
player_movement += DIRECTIONS['down']
elif key == QtCore.Qt.Key_Right:
player_movement += DIRECTIONS['right']
elif key == QtCore.Qt.Key_Left:
player_movement += DIRECTIONS['left']
elif key == QtCore.Qt.Key_Space:
self.world.add_object(Egg(x, y, COLOURS['white'], 'Up', 2))
elif key == QtCore.Qt.Key_1:
self.world.player.color = COLOURS['white']
elif key == QtCore.Qt.Key_2:
self.world.player.color = COLOURS['grey']
elif key == QtCore.Qt.Key_3:
self.world.player.color = COLOURS['other-grey']
elif key == QtCore.Qt.Key_4:
self.world.player.color = COLOURS['black']
if player_movement != DIRECTIONS['still']:
self.world.player.facing = player_movement
self.world.player.spawn_egg(self.world)
self.world.player.movement_facing = face_movement
示例3: main
# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import add_object [as 别名]
def main():
# Read two parameters identifying modules for the first and the second robots.
if (len(sys.argv) < 3):
print "Usage: python main.py <first_robot> <second_robot> [random seed]"
print ""
print "The <first_robot> and <second_robot> should identify modules containing classes Robot and RobotServer"
print "E.g if you invoke "
print " python main.py telliskivi ekrs"
print "The simulator will import telliskivi.Robot, telliskivi.RobotServer, ekrs.Robot, ekrs.RobotServer"
sys.exit(1)
# Try to import modules
r1module = __import__(sys.argv[1])
r2module = __import__(sys.argv[2])
(a,b,c,d) = (r1module.Robot, r1module.RobotServer, r2module.Robot, r2module.RobotServer) # Testing
random_seed = int(sys.argv[3]) if len(sys.argv) > 3 else None
# random seeds 1,2,3,4 are already interesting use cases
# Init graphics
pygame.init()
window = pygame.display.set_mode((1060, 760)) # This is the size of the field + contestant area. (5300 x 3800)
pygame.display.set_caption('Robotex 2011 Simulator')
screen = pygame.display.get_surface()
# Init world.
world = World(screen)
# Add 11 balls (coordinates are world-coords)
# Make sure the balls are added symmetrically. That means the first ball goes in the center
world.add_object(Ball(Point(world.width/2, world.height/2)))
for i in range(5):
while True:
xpos = random.uniform(10,world.width-10)
ypos = random.uniform(10,world.height-10)
# Make sure the positions do not get in the robot's starting corners ( 0..60px, i.e. 0..60px )
if not ((xpos < 60 and ypos < 60) or (xpos > world.width - 60 and ypos > world.height - 60)):
break
world.add_object(Ball(Point(xpos, ypos)))
world.add_object(Ball(Point(world.width-xpos, world.height-ypos)))
# Create two robots
robot1 = r1module.Robot(world, "Robot A", "TOPLEFT")
robot2 = r2module.Robot(world, "Robot B", "BOTTOMRIGHT")
world.add_object(robot1)
world.add_object(robot2)
# Start robot command servers
r1module.RobotServer(robot1, 5000).serve()
r2module.RobotServer(robot2, 5001).serve()
# Do the simulation/drawing/event cycle
last_sim = -1000
last_draw = -1000
while True:
t = get_ticks()
if (t - last_sim) > 1:
# Simulate world (~1 iteration once every millisecond or so)
# NB: This is kinda hard-coded into the logic currently,
# i.e. World.simulate() and Ball.simulate() and anyone else is
# free to assume that a simulation step is 1ms. In particular,
# the ball computes it's friction coefficient like that.
world.simulate()
last_sim = t
if (t - last_draw) > 40:
# Draw a frame once every 40 milliseconds or so (~25 fps)
BACKGROUND_BLUE = (120,119,253)
screen.fill(BACKGROUND_BLUE)
world.draw(screen)
pygame.display.flip()
last_draw = t
# Process input
input(pygame.event.get())