Exemplo n.º 1
0
    def on_init(self):
        # Based on "Python GUI in Linux frame buffer"
        # http://www.karoltomala.com/blog/?p=679
        disp_no = os.getenv("DISPLAY")
        if disp_no:
            print "I'm running under X display = {0}".format(disp_no)
            found = True
        else:
            # Check which frame buffer drivers are available
            # Start with fbcon since directfb hangs with composite output
            drivers = ["fbcon", "directfb", "svgalib"]
            found = False
            for driver in drivers:
                # Make sure that SDL_VIDEODRIVER is set
                if not os.getenv("SDL_VIDEODRIVER"):
                    os.putenv("SDL_VIDEODRIVER", driver)
                try:
                    pygame.display.init()
                except pygame.error:
                    print "Driver: {0} failed.".format(driver)
                    continue
                found = True
                break
        if not found:
            raise Exception("No suitable video driver found!")

        pygame.init()
        infoObject = pygame.display.Info()
        self.width_screen = infoObject.current_w
        self.height_screen = infoObject.current_h

        self.clock = pygame.time.Clock()
        if disp_no:
            self.size = self.width, self.height
            self.screen = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
            print "X size: %d x %d" % (self.size[0], self.size[1])
        else:
            self.size = self.width_screen, self.height_screen
            self.screen = pygame.display.set_mode(self.size, pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF)
            print "Framebuffer size: %d x %d" % (self.size[0], self.size[1])
        self.sensor_camera = Sensor_Camera(self.width, self.height)
        self.drawtext = DrawText(self.width, self.height)
        self.background = pygame.Surface(self.screen.get_size()).convert()
        self._running = True
        self.sensor_camera.on_init()
        self.sensor_ultrasound.on_init()
        self.drawtext.on_init()
        self.servo_util.on_init()
        self.sensor_encoder.on_init()
        self.sensor_led.on_init()
        self.sensor_motor.on_init()
        self.sensor_dof.on_init()
        self.sensor_rocket.on_init()
Exemplo n.º 2
0
    def draw(self, surface, num=0):
        w_num = 20
        x_title = w_num + 5
        surface.fill(Color.darkBlue)
        # num
        num_col = Color.bg_selected_track if self.is_selected(
        ) else Color.bg_track
        pygame.draw.rect(surface, num_col, (0, 0, w_num, HEIGHT), 0)
        DrawText.draw(surface, str(num), 5, HEIGHT / 2)
        # visualization
        if self._img:
            surface.blit(self._img, (w_num, 0, WIDTH, 147))
        # title
        # pygame.draw.rect(surface, Color.darkBlue, (20,0,WIDTH-20,HEIGHT), 0)
        DrawText.draw(surface, self.get_name(), x_title, 5)

        # length
        length = int(self._sound.get_length())
        DrawText.draw(surface, str(length), WIDTH - 60, 5)

        # status
        if self.is_playing():
            w_r = 2
            pygame.draw.rect(surface, Color.yellow,
                             (w_num, 0, WIDTH - w_num - 1, HEIGHT - 1), w_r)

            if self._img:
                time = pygame.time.get_ticks()
                x_pos = ((time - self._time) / length *
                         (self._img.get_width())) / 1000.0 + w_num
                pygame.draw.line(surface, Color.orange, (x_pos, 0),
                                 (x_pos, HEIGHT), 2)
    def simulation_setup(self):
        """
        sets up the simulation by creating the grid and the GUI
        """
        simulation_grid = Grid(self.blocksize, self.width, self.height,
                               DISPLAYSURF)

        # do i need to return the grid here to add sheep and wolves
        self.grid = simulation_grid.generate_grid(DISPLAYSURF)
        self.saved_grid = simulation_grid.generate_grid(DISPLAYSURF)

        # adding all the buttons to the GUI
        start_button = Button.button(self.BUTTONCOLOR, self.width + 20, 20,
                                     150, 60, DISPLAYSURF, 'START')
        stop_button = Button.button(self.BUTTONCOLOR, self.width + 190, 20,
                                    150, 60, DISPLAYSURF, 'STOP')
        save_button = Button.button(self.BUTTONCOLOR, self.width + 20, 100,
                                    150, 60, DISPLAYSURF, 'SAVE')
        load_button = Button.button(self.BUTTONCOLOR, self.width + 190, 100,
                                    150, 60, DISPLAYSURF, 'LOAD')

        wolf_in_txt = DrawText.DrawText(BASICFONT, DISPLAYSURF,
                                        'Initial Wolves: ', self.width + 100,
                                        (self.height / 2) - 80)
        sheep_in_txt = DrawText.DrawText(BASICFONT, DISPLAYSURF,
                                         'Initial Sheep: ', self.width + 100,
                                         (self.height / 2) - 120)
        wolf_starve = DrawText.DrawText(BASICFONT, DISPLAYSURF,
                                        'Wolves starved: ', self.width + 100,
                                        self.height / 2)
        sheep_starve = DrawText.DrawText(BASICFONT, DISPLAYSURF,
                                         'Sheep starved: ', self.width + 100,
                                         (self.height / 2) - 40)
        max_sheep = DrawText.DrawText(BASICFONT, DISPLAYSURF, 'Max sheep: ',
                                      self.width + 100, (self.height / 2) + 40)
        max_wolves = DrawText.DrawText(BASICFONT, DISPLAYSURF, 'Max wolves: ',
                                       self.width + 100,
                                       (self.height / 2) + 80)
        sheep_killed = DrawText.DrawText(BASICFONT, DISPLAYSURF,
                                         'Sheep killed: ', self.width + 100,
                                         (self.height / 2) + 120)
        wolf_starve_num = DrawText.DrawText(BASICFONT, DISPLAYSURF,
                                            self.wolves_starved,
                                            self.width + 210,
                                            (self.height / 2) - 40)
        sheep_starve_num = DrawText.DrawText(BASICFONT, DISPLAYSURF,
                                             self.sheep_starved,
                                             self.width + 210, self.height / 2)
        max_sheep_num = DrawText.DrawText(BASICFONT, DISPLAYSURF,
                                          self.max_sheep, self.width + 210,
                                          (self.height / 2) + 40)
        max_wolves_num = DrawText.DrawText(BASICFONT, DISPLAYSURF,
                                           self.max_wolves, self.width + 210,
                                           (self.height / 2) + 80)
        sheep_killed_num = DrawText.DrawText(BASICFONT, DISPLAYSURF,
                                             self.sheep_killed,
                                             self.width + 210,
                                             (self.height / 2) + 120)

        wolf_cnt = DrawText.DrawText(BASICFONT, DISPLAYSURF, 'Wolf Count: ',
                                     200, self.height + 5)
        sheep_cnt = DrawText.DrawText(BASICFONT, DISPLAYSURF, 'Sheep Count: ',
                                      400, self.height + 5)
        sheep_num = DrawText.DrawText(BASICFONT, DISPLAYSURF, self.sheep, 485,
                                      self.height + 5)
        wolf_num = DrawText.DrawText(BASICFONT, DISPLAYSURF, self.wolves, 285,
                                     self.height + 5)
        author = DrawText.DrawText(BASICFONT, DISPLAYSURF, 'By: Cameron Cagle',
                                   self.width + 170, (self.height / 2) + 290)

        title2 = DrawText.DrawText(pygame.font.Font('freesansbold.ttf', 35),
                                   DISPLAYSURF, 'SIMULATION', self.width + 170,
                                   (self.height / 2) + 230)
        title = DrawText.DrawText(pygame.font.Font('freesansbold.ttf', 35),
                                  DISPLAYSURF, 'SHEEP & WOLF',
                                  self.width + 170, (self.height / 2) + 200)
        background2 = DrawText.DrawText(
            pygame.font.Font('freesansbold.ttf', 38), DISPLAYSURF,
            'SIMULATION', self.width + 170, (self.height / 2) + 230)
        background = DrawText.DrawText(
            pygame.font.Font('freesansbold.ttf', 38), DISPLAYSURF,
            'SHEEP & WOLF', self.width + 170, (self.height / 2) + 200)
        sheep_input = InputBox.InputBox(self.width + 180,
                                        (self.height / 2) - 125, 60, 30,
                                        BASICFONT, 'sheep', self.INPUTSURF)
        wolf_input = InputBox.InputBox(self.width + 180,
                                       (self.height / 2) - 85, 60, 30,
                                       BASICFONT, 'wolf', self.INPUTSURF)

        press_button = [start_button, stop_button, save_button, load_button]
        txt_boxes = [
            wolf_in_txt, sheep_in_txt, sheep_cnt, wolf_cnt, sheep_starve,
            wolf_starve, max_sheep, max_wolves, title, title2, author,
            sheep_killed
        ]
        color_boxes = [background, background2]
        input_boxes = [sheep_input, wolf_input]
        if self.simulation_intro(simulation_grid, txt_boxes, color_boxes,
                                 input_boxes, press_button, sheep_num,
                                 wolf_num, wolf_input, sheep_input,
                                 wolf_starve_num, sheep_starve_num,
                                 max_sheep_num, max_wolves_num,
                                 sheep_killed_num):
            simulation_ready = True
        setup = True
        while setup:
            for event in pygame.event.get():
                mouse_pos = pygame.mouse.get_pos()
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if start_button.isOver(mouse_pos) and simulation_ready:
                        self.simulation_loop(simulation_grid, press_button,
                                             txt_boxes, color_boxes,
                                             input_boxes, sheep_num, wolf_num,
                                             stop_button, start_button,
                                             wolf_starve_num, sheep_starve_num,
                                             max_sheep_num, max_wolves_num,
                                             sheep_killed_num)
                    if load_button.isOver(mouse_pos):
                        print("load")
                    if save_button.isOver(mouse_pos):
                        print("save")

            DISPLAYSURF.fill((0, 0, 0))
            self.redraw_screen(txt_boxes, color_boxes, input_boxes,
                               press_button, sheep_num, wolf_num,
                               wolf_starve_num, sheep_starve_num,
                               max_sheep_num, max_wolves_num, sheep_killed_num)
            simulation_grid.draw_grid(self.grid, DISPLAYSURF, self.blocksize)
            pygame.display.update()
Exemplo n.º 4
0
def run(playlist_files):
    # Init pygame
    pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag
    print "Init pygame, version = ", pygame.version.ver
    
    pygame.init()
    if not pygame.display.get_init():
        print "Error during pygame init."
        print "Quit."
        sys.exit(1)
    
    
    # Init Screen
    fullscreen = False
    if fullscreen:
        size = width, height = 1366,768
    else:
        size = width, height = 1366/2,760
    
    screen = pygame.display.set_mode(size, (pygame.FULLSCREEN | pygame.HWSURFACE if fullscreen else pygame.RESIZABLE))
    pygame.display.set_caption("Ardent'Scene studio")
    #pygame.mouse.set_visible(False)

    # Init modules
    DrawText.init()
    
    # Init entities
    ball = pygame.image.load(ressources_dir + "ball.gif")
    ballrect = ball.get_rect()
    speed = [20, 18]


    # ***
    cmanager = CManager.CManager()

    # ---
    tmanagers = Loader.load_playlists(playlist_files)
    tman_i = 0
    
    cur_tmanager = tmanagers[0]

    # ---
    keys = [pygame.K_a, pygame.K_z, pygame.K_e, 
            pygame.K_r, pygame.K_t, pygame.K_y, 
            pygame.K_u, pygame.K_i, pygame.K_o,
            pygame.K_p ]
    selection = Selection.Selection(keys, pygame.K_ASTERISK)
    
    # ---
    commands = [
            Commands.Play(cmanager, pygame.K_v),
            Commands.Stop(cmanager, pygame.K_n),
            Commands.Fadein(cmanager, pygame.K_g),
            Commands.Fadeout(cmanager, pygame.K_j),
            Commands.UpTracks(cmanager, pygame.K_DOWN),
            Commands.DownTracks(cmanager, pygame.K_UP)
            ]

    # to know when exiting the soft
    quitting = 0

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                quit()
            # ---
            elif event.type == pygame.KEYDOWN:
                quitting = quitting-1
                if event.key == pygame.K_ESCAPE:
                    quit()
                elif event.key == pygame.K_BACKSPACE:
                    if quitting < 0:
                        quitting = 0
                    quitting = quitting + 2
                    if quitting >= 5:
                        quit()
                elif event.key == pygame.K_SPACE:
                    pass
                # select a track
                elif selection.is_selection(event.key):
                    selection.treat_key(event.key, cur_tmanager)
                # change current tmanager 
                elif event.key == pygame.K_LEFT:
                    if tman_i > 0:
                        print 'Previous playlist'
                        tman_i -= 1
                        cur_tmanager = tmanagers[tman_i]
                elif event.key == pygame.K_RIGHT:
                    if tman_i < len(tmanagers)-1:
                        print 'Next playlist'
                        tman_i += 1
                        cur_tmanager = tmanagers[tman_i]
                # execute a command
                else:
                    for cmd in commands:
                        if cmd.is_this_command(event.key):
                            cmd.execute(cur_tmanager)
            # ---
            elif event.type == pygame.locals.USEREVENT:
                print 'Event : Sound has finished'
                # Channel event
                cmanager.has_finished()
    
        ballrect = ballrect.move(speed)
        if ballrect.left < 0 or ballrect.right > width:
            speed[0] = -speed[0]
        if ballrect.top < 0 or ballrect.bottom > height:
            speed[1] = -speed[1]
    
        screen.fill(Color.black)
        screen.blit(ball, ballrect)
        cur_tmanager.draw(screen)
        draw_playlist_index(screen, tman_i)
        pygame.display.flip()
        pygame.time.delay(50)
Exemplo n.º 5
0
def draw_playlist_index(surface, index):
    width = surface.get_width()
    pygame.draw.rect(surface, Color.gray, (0,0, width, 40), 0)
    pygame.draw.rect(surface, Color.yellow, (0,0, width, 40), 1)
    DrawText.draw(surface, str(index), width/2, 15)
Exemplo n.º 6
0
class App:
    def __init__(self, width=640, height=400, fps=30):

        self._running = True

        self._display_surf = None

        self.width = width
        self.height = height

        self.height_screen = 0
        self.width_screen = 0
        self.fps = fps
        self.playtime = 0.0
        self.servo_util = Servo_Util()
        # self.sensor_camera = Sensor_Camera(width/2,height/2)
        self.sensor_ultrasound = Sensor_UltraSound()
        self.sensor_encoder = Sensor_Encoder()
        self.sensor_led = Sensor_Led()
        self.sensor_motor = Sensor_Motor()
        self.sensor_motor.SetLedSensor(self.sensor_led)
        self.sensor_dof = Sensor_Dof()
        self.sensor_rocket = Sensor_Rocket()

        print "Main :key esc\n"

    def on_init(self):
        # Based on "Python GUI in Linux frame buffer"
        # http://www.karoltomala.com/blog/?p=679
        disp_no = os.getenv("DISPLAY")
        if disp_no:
            print "I'm running under X display = {0}".format(disp_no)
            found = True
        else:
            # Check which frame buffer drivers are available
            # Start with fbcon since directfb hangs with composite output
            drivers = ["fbcon", "directfb", "svgalib"]
            found = False
            for driver in drivers:
                # Make sure that SDL_VIDEODRIVER is set
                if not os.getenv("SDL_VIDEODRIVER"):
                    os.putenv("SDL_VIDEODRIVER", driver)
                try:
                    pygame.display.init()
                except pygame.error:
                    print "Driver: {0} failed.".format(driver)
                    continue
                found = True
                break
        if not found:
            raise Exception("No suitable video driver found!")

        pygame.init()
        infoObject = pygame.display.Info()
        self.width_screen = infoObject.current_w
        self.height_screen = infoObject.current_h

        self.clock = pygame.time.Clock()
        if disp_no:
            self.size = self.width, self.height
            self.screen = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
            print "X size: %d x %d" % (self.size[0], self.size[1])
        else:
            self.size = self.width_screen, self.height_screen
            self.screen = pygame.display.set_mode(self.size, pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF)
            print "Framebuffer size: %d x %d" % (self.size[0], self.size[1])
        self.sensor_camera = Sensor_Camera(self.width, self.height)
        self.drawtext = DrawText(self.width, self.height)
        self.background = pygame.Surface(self.screen.get_size()).convert()
        self._running = True
        self.sensor_camera.on_init()
        self.sensor_ultrasound.on_init()
        self.drawtext.on_init()
        self.servo_util.on_init()
        self.sensor_encoder.on_init()
        self.sensor_led.on_init()
        self.sensor_motor.on_init()
        self.sensor_dof.on_init()
        self.sensor_rocket.on_init()

    def on_event_key(self, event):
        if event.key == pygame.K_ESCAPE:
            self._running = False

    def on_event_mouse(self, down, event):
        pass

    def on_event(self, event):
        self.sensor_camera.on_event(event, self.screen)
        self.sensor_ultrasound.on_event(event)
        self.servo_util.on_event(event)
        self.sensor_encoder.on_event(event)
        self.sensor_led.on_event(event)
        self.sensor_motor.on_event(event)
        self.sensor_dof.on_event(event)
        self.sensor_rocket.on_event(event)

        if event.type == pygame.QUIT:
            self._running = False
        elif event.type == pygame.KEYDOWN:
            self.on_event_key(event)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            self.on_event_mouse(True, event)
        elif event.type == pygame.MOUSEBUTTONUP:
            self.on_event_mouse(False, event)

    def on_loop(self):
        self.miliseconds = self.clock.tick(self.fps)
        self.playtime += self.miliseconds / 1000.0
        self.sensor_camera.on_loop()
        self.servo_util.on_loop()
        self.sensor_ultrasound.on_loop()
        self.sensor_encoder.on_loop()
        self.sensor_led.on_loop()
        self.sensor_motor.on_loop()
        self.sensor_dof.on_loop()
        self.sensor_rocket.on_loop()

    def on_render(self):
        self.screen.blit(self.background, (0, 0))
        self.sensor_camera.on_render(self.screen)
        self.sensor_ultrasound.on_render(self.screen, self.drawtext)
        self.drawtext.on_render(
            self.screen,
            "FPS : {:6.3} {} PLAYTIME: {:6.3} Seconds".format(self.clock.get_fps(), " " * 5, self.playtime),
            "bottom",
        )
        self.servo_util.on_render(self.screen, self.drawtext)
        self.sensor_encoder.on_render(self.screen, self.drawtext)
        self.sensor_led.on_render(self.screen, self.drawtext)
        self.sensor_motor.on_render(self.screen, self.drawtext)
        self.sensor_dof.on_render(self.screen, self.drawtext)
        self.sensor_rocket.on_render(self.screen, self.drawtext)

        pygame.display.flip()

    def on_cleanup(self):
        self.sensor_camera.on_cleanup()
        self.sensor_ultrasound.on_cleanup()
        self.servo_util.on_cleanup()
        self.drawtext.on_cleanup()
        self.sensor_encoder.on_cleanup()
        self.sensor_led.on_cleanup()
        self.sensor_motor.on_cleanup()
        self.sensor_rocket.on_cleanup()
        self.sensor_dof.on_cleanup()
        pygame.quit()

    def on_execute(self):
        if self.on_init() == False:
            self._running = False

        while self._running:
            for event in pygame.event.get():
                self.on_event(event)
            self.on_loop()
            self.on_render()
        self.on_cleanup()