当前位置: 首页>>代码示例>>Python>>正文


Python World.restart方法代码示例

本文整理汇总了Python中World.World.restart方法的典型用法代码示例。如果您正苦于以下问题:Python World.restart方法的具体用法?Python World.restart怎么用?Python World.restart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在World.World的用法示例。


在下文中一共展示了World.restart方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: gui

# 需要导入模块: from World import World [as 别名]
# 或者: from World.World import restart [as 别名]
class gui():
    def __init__(self):
        self.world = World(200, 200)

        self.animal_info_window = None

        builder = Gtk.Builder()
        builder.add_from_file("MainWindow.glade")
        builder.connect_signals(self)

        self.window = builder.get_object("applicationwindow1")
        self.drawing_area = builder.get_object("drawingarea1")
        self.debug_lbl = builder.get_object("debug_lbl")

        self.window.realize()
        self.window.set_reallocate_redraws(True)
        self.window.set_title("Animals")
        self.window.connect('delete_event', Gtk.main_quit)
        self.window.connect('destroy', lambda quit: Gtk.main_quit())

        self.scrollbar1 = builder.get_object("scrollbar1")

        self.window.show_all()

        self.timer_interval = builder.get_object("timer_adjustment").get_value()
        self.timer_id = GObject.timeout_add(self.timer_interval, self.on_timer_event)

        self.selected_animal = None
        self.is_food_smell = False
        self.world_version = 0
        self.restart_if_all_dead = True

        self.dbg_text = ""

    def on_configure(self, widget, event):
        self.world.width = widget.get_allocated_width()
        self.world.height = widget.get_allocated_height()
        return True

    def on_draw(self, widget, cr):
        #Animals
        for animal in self.world.animals:
            if animal == self.selected_animal:
                cr.set_source_rgb(1, 0.84, 0)
            else:
                cr.set_source_rgb(0.6, 0.7, 0.5)
            cr.arc(animal.x, animal.y, animal.size, 0, TWO_PI)
            cr.fill()

            cr.set_source_rgb(0,0,0)
            for x, y in animal.sensors_positions:
                cr.arc(x, y, 1, 0, TWO_PI)
                cr.fill()
            cr.move_to(animal.x, animal.y)
            cr.line_to(animal.x+math.cos(animal.angle)*animal.size,
                       animal.y+math.sin(animal.angle)*animal.size)
            cr.stroke()


        for food in self.world.food:
            cr.set_source_rgb(0.3, 0.3, 0.3)
            cr.arc(food.x, food.y, food.size, 0, TWO_PI)
            cr.fill()
            cr.arc(food.x, food.y, World.MAX_EATING_DISTANCE + food.size, 0, TWO_PI)
            cr.set_line_width(0.1)
            cr.stroke()
            if self.is_food_smell:
                r1 = cairo.RadialGradient(food.x, food.y, food.size, food.x, food.y, food.smell_size)
                r1.add_color_stop_rgba(0, 0.3, 0.6, 0.3, 0.2)
                r1.add_color_stop_rgba(1, 0.3, 0.6, 0.3, 0.01)
                cr.set_source(r1)
                cr.arc(food.x, food.y, food.smell_size, 0, TWO_PI)
                cr.fill()
        return False


    def on_timer_event(self):
        self.world.update()

        if self.animal_info_window:
            self.animal_info_window.update_graphics()
            self.animal_info_window.drawing_area1.queue_draw()
            self.animal_info_window.drawing_area2.queue_draw()

        self.drawing_area.queue_draw()

        self.add_to_dbg("world version={}".format(self.world_version))
        self.add_to_dbg("animal count=" + str(len(self.world.animals)))
        self.add_to_dbg("world time={}".format(self.world.time))
        self.debug_lbl.set_text(self.dbg_text)
        self.dbg_text = ""

        if self.restart_if_all_dead and len(self.world.animals) == 0:
            self.world.restart()
            self.world_version += 1

        return True

    def add_to_dbg(self, text):
        self.dbg_text += text + "\n"
#.........这里部分代码省略.........
开发者ID:zshimanchik,项目名称:unconditioned-reflexes,代码行数:103,代码来源:MainWindow.py


注:本文中的World.World.restart方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。