def main():
    driver_type = 5
    full_screen = False
    stencil_buffer = False
    vsync = False
    run_app = True

    from video_choice_dialog import has_pywingui
    if has_pywingui:
        from video_choice_dialog import ChoiceDialog, IDOK, IDCANCEL
        dialog = ChoiceDialog()
        dialog.driver_type = driver_type
        dialog.full_screen = full_screen
        dialog.stencil_buffer = stencil_buffer
        dialog.vsync = vsync
        dialogResult = dialog.DoModal()
        if dialogResult == IDOK:
            driver_type = dialog.driver_type
            full_screen = dialog.full_screen
            stencil_buffer = dialog.stencil_buffer
            vsync = dialog.vsync
        elif dialogResult == IDCANCEL:
            run_app = False

    if run_app:

        import pymunk as pm
        import os, math, random
        X, Y, Z = 0, 1, 2  # Easy indexing

        info_text = '\nJoints. Just wait and the L will tip over'

        ### Physics stuff
        space = pm.Space()
        space.gravity = 0.0, -900.0

        ## Balls
        balls = []

        ### static stuff
        rot_center_body = pm.Body()
        rot_center_body.position = (300, 300)

        ### To hold back the L
        rot_limit_body = pm.Body()
        rot_limit_body.position = (200, 300)

        ### The moving L shape
        l1 = [(-150, 0), (255.0, 0.0)]
        l2 = [(-150.0, 0), (-150.0, 50.0)]

        body = pm.Body(10, 10000)
        body.position = (300, 300)

        lines = [
            pm.Segment(body, l1[0], l1[1], 5.0),
            pm.Segment(body, l2[0], l2[1], 5.0)
        ]

        space.add(body)
        space.add(lines)

        ### The L rotates around this
        rot_center = pm.PinJoint(body, rot_center_body, (0, 0), (0, 0))
        ### And is constrained by this
        joint_limit = 25
        rot_limit = pm.SlideJoint(body, rot_limit_body, (-100, 0), (0, 0), 0,
                                  joint_limit)
        space.add(rot_center, rot_limit)

        ticks_to_next_ball = 10

        screen_x, screen_y = 640, 480

        def reverse_coords(p):
            """Small hack to convert pymunk to pygame coordinates"""
            return p.x, -p.y + screen_y

        ### pyIrrlicht block
        import pyirrlicht as irr
        if not driver_type:
            driver_type = irr.EDT_SOFTWARE

        class UserIEventReceiver(irr.IEventReceiver):
            KeyIsDown = {}
            for key in range(irr.KEY_KEY_CODES_COUNT):
                KeyIsDown[key] = False

            def OnEvent(self, evt):
                event = irr.SEvent(evt)
                if event.EventType is irr.EET_KEY_INPUT_EVENT:
                    self.KeyIsDown[
                        event.KeyInput.Key] = event.KeyInput.PressedDown
                return False

            def IsKeyDown(self, keyCode):
                return self.KeyIsDown[keyCode]

        window_size = irr.dimension2du(screen_x, screen_y)
        device = irr.createDevice(driver_type, window_size, 16, full_screen,
                                  stencil_buffer, vsync)

        if device:
            window_caption = __doc__
            device.setWindowCaption(window_caption)
            device.setResizable(True)
            video_driver = device.getVideoDriver()
            scene_manager = device.getSceneManager()
            gui_environment = device.getGUIEnvironment()
            font = irr.CGUITTFont(
                gui_environment, os.environ['SYSTEMROOT'] + '/Fonts/arial.ttf',
                16)
            if not font:
                font = gui_environment.getBuiltInFont()
            gui_environment.getSkin().setFont(font)
            static_text = gui_environment.addStaticText(
                window_caption + info_text, irr.recti(10, 10, screen_x - 20,
                                                      50), True)
            color_red = irr.SColor(255, 255, 0, 0)
            color_green = irr.SColor(255, 0, 255, 0)
            color_blue = irr.SColor(255, 0, 0, 255)
            color_black = irr.SColor(255, 0, 0, 0)
            color_lightgray = irr.SColor(255, 200, 200, 200)
            color_text = irr.SColor(255, 0, 0, 0)
            color_screen = irr.SColor(255, 100, 100, 100)
            lastFPS = -1
            i_event_receiver = UserIEventReceiver()
            device.setEventReceiver(i_event_receiver)
            update_physics = False
            while device.run():
                if device.isWindowActive():
                    if i_event_receiver.IsKeyDown(irr.KEY_ESCAPE):
                        break

                    if device.getTimer().getTime() > 30:
                        ticks_to_next_ball -= 1
                        if ticks_to_next_ball <= 0:
                            ticks_to_next_ball = 25
                            mass = 1
                            radius = 14
                            inertia = pm.moment_for_circle(
                                mass, 0, radius, (0, 0))
                            body = pm.Body(mass, inertia)
                            x = random.randint(120, 380)
                            body.position = x, 550
                            shape = pm.Circle(body, radius, (0, 0))
                            space.add(body, shape)
                            balls.append(shape)
                        device.getTimer().setTime(0)
                        update_physics = True

                    ### Draw stuff
                    if video_driver.beginScene(True, True, color_screen):
                        balls_to_remove = []
                        for ball in balls:
                            if ball.body.position.y < 150:
                                balls_to_remove.append(ball)

                            x1, y1 = reverse_coords(ball.body.position)
                            video_driver.draw2DPolygon_f(
                                x1, y1, ball.radius, color_blue)

                        for ball in balls_to_remove:
                            space.remove(ball, ball.body)
                            balls.remove(ball)

                        for line in lines:
                            body = line.body
                            pv1 = body.position + line.a.rotated(body.angle)
                            pv2 = body.position + line.b.rotated(body.angle)
                            x1, y1 = reverse_coords(pv1)
                            x2, y2 = reverse_coords(pv2)
                            video_driver.draw2DLine_f(x1, y1, x2, y2,
                                                      color_lightgray)

                        ### The rotation center of the L shape
                        x, y = reverse_coords(rot_center_body.position)
                        video_driver.draw2DPolygon_f(x, y, 5, color_red)
                        ### The limits where it can move.
                        x, y = reverse_coords(rot_limit_body.position)
                        video_driver.draw2DPolygon_f(x, y, joint_limit,
                                                     color_green, 100)

                        gui_environment.drawAll()
                        video_driver.endScene()

                    ### Update physics
                    if update_physics:
                        dt = 1.0 / 50.0 / 10.0
                        for x in range(10):
                            space.step(dt)
                        update_physics = False

                    device.sleep(1)

                    fps = video_driver.getFPS()
                    if lastFPS != fps:
                        caption = '%s [%s] FPS:%d' % (
                            window_caption, video_driver.getName(), fps)
                        device.setWindowCaption(caption)
                        static_text.setText(caption + info_text)
                        lastFPS = fps
                else:
                    device._yield()

            device.closeDevice()
        else:
            print('ERROR createDevice')
Ejemplo n.º 2
0
			s += " " + str(cam.getTarget().Z)
			postext.setText(s)

			# update the tool dialog
			updateToolBox()

			#~ Device.sleep(10)
		else:
			Device._yield()
	Device.drop()

if __name__ == "__main__":
	run_app = True
	from video_choice_dialog import has_pywingui
	if has_pywingui:
		from video_choice_dialog import ChoiceDialog, IDOK, IDCANCEL
		dialog = ChoiceDialog()
		dialog.driver_type = driver_type
		dialog.full_screen = full_screen
		dialog.stencil_buffer = stencil_buffer
		dialog.vsync = vsync
		dialogResult = dialog.DoModal()
		if dialogResult == IDOK:
			driver_type = dialog.driver_type
			full_screen = dialog.full_screen
			stencil_buffer = dialog.stencil_buffer
			vsync = dialog.vsync
		elif dialogResult == IDCANCEL:
			run_app = False
	if run_app:
		main(driver_type, full_screen, stencil_buffer, vsync)
Ejemplo n.º 3
0
def main():
    driver_type = 0
    full_screen = False
    stencil_buffer = False
    vsync = False
    run_app = True

    from video_choice_dialog import has_pywingui
    if has_pywingui:
        from video_choice_dialog import ChoiceDialog, IDOK, IDCANCEL
        dialog = ChoiceDialog()
        dialog.driver_type = driver_type
        dialog.full_screen = full_screen
        dialog.stencil_buffer = stencil_buffer
        dialog.vsync = vsync
        dialogResult = dialog.DoModal()
        if dialogResult == IDOK:
            driver_type = dialog.driver_type
            full_screen = dialog.full_screen
            stencil_buffer = dialog.stencil_buffer
            vsync = dialog.vsync
        elif dialogResult == IDCANCEL:
            run_app = False

    if run_app:
        global pymunk, irr, Vec2d, color_darkgray

        import math, random
        import os

        import pymunk
        from pymunk import Vec2d

        import pyirrlicht as irr

        ### Physics stuff
        space = pymunk.Space()

        ### Game area
        # walls - the left-top-right walls
        static_lines = [
            pymunk.Segment(space.static_body, (50, 50), (50, 550), 5),
            pymunk.Segment(space.static_body, (50, 550), (550, 550), 5),
            pymunk.Segment(space.static_body, (550, 550), (550, 50), 5)
        ]
        for line in static_lines:
            line.color = irr.SColor(255, 200, 200, 200)
            line.elasticity = 1.0

        space.add(static_lines)

        # bottom - a sensor that removes anything touching it
        bottom = pymunk.Segment(space.static_body, (50, 50), (550, 50), 5)
        bottom.sensor = True
        bottom.collision_type = 1
        bottom.color = irr.SColor(255, 255, 0, 0)

        def remove_first(space, arbiter):
            first_shape = arbiter.shapes[0]
            space.add_post_step_callback(space.remove, first_shape,
                                         first_shape.body)
            return True

        space.add_collision_handler(0, 1, begin=remove_first)
        space.add(bottom)

        ### Player ship
        player_body = pymunk.Body(500, pymunk.inf)
        player_shape = pymunk.Circle(player_body, 35)
        player_shape.color = irr.SColor(255, 255, 0, 0)
        player_shape.elasticity = 1.0
        player_body.position = 300, 100
        # restrict movement of player to a straigt line
        move_joint = pymunk.GrooveJoint(space.static_body, player_body,
                                        (100, 100), (500, 100), (0, 0))
        space.add(player_body, player_shape, move_joint)

        # Start game
        setup_level(space, player_body)

        ### pyirrlicht init
        class UserIEventReceiver(irr.IEventReceiver):
            #~ mouse_button_down = False
            #~ mouse_position = Vec2d(0, 0)
            KeyIsDown = {}
            for key in range(irr.KEY_KEY_CODES_COUNT):
                KeyIsDown[key] = False

            def OnEvent(self, evt):
                event = irr.SEvent(evt)
                self.mouse_button_down = False
                self.mouse_position = Vec2d(0, 0)
                if event.EventType is irr.EET_KEY_INPUT_EVENT:
                    self.KeyIsDown[
                        event.KeyInput.Key] = event.KeyInput.PressedDown
                    if event.KeyInput.Key == irr.KEY_LEFT:
                        if event.KeyInput.PressedDown:
                            player_body.velocity = (-600, 0)
                        else:
                            player_body.velocity = 0, 0
                    elif event.KeyInput.Key == irr.KEY_RIGHT:
                        if event.KeyInput.PressedDown:
                            player_body.velocity = (600, 0)
                        else:
                            player_body.velocity = 0, 0
                    elif event.KeyInput.Key == irr.KEY_KEY_R:
                        setup_level(space, player_body)
                #~ elif event.EventType is irr.EET_MOUSE_INPUT_EVENT:
                #~ if event.MouseInput.EventType == irr.EMIE_LMOUSE_PRESSED_DOWN:
                #~ self.mouse_button_down = True
                #~ self.mouse_position.x = event.MouseInput.X
                #~ self.mouse_position.y = event.MouseInput.Y
                return False

            def IsKeyDown(self, keyCode):
                return self.KeyIsDown[keyCode]

        if not driver_type:
            driver_type = irr.EDT_SOFTWARE

        window_size = irr.dimension2du(width, height)
        device = irr.createDevice(driver_type, window_size, 16, full_screen,
                                  stencil_buffer, vsync)

        if device:
            device.setWindowCaption('pyMunk breakout game')
            device.setResizable(True)
            video_driver = device.getVideoDriver()
            gui_environment = device.getGUIEnvironment()
            font = irr.CGUITTFont(
                gui_environment, os.environ['SYSTEMROOT'] + '/Fonts/arial.ttf',
                16)
            if not font:
                font = gui_environment.getBuiltInFont()

            space_drawer = SpaceDrawer(space, video_driver)

            i_event_receiver = UserIEventReceiver()
            device.setEventReceiver(i_event_receiver)
            color_white = irr.SColor(255, 255, 255, 255)
            color_darkgray = irr.SColor(255, 100, 100, 100)
            color_screen = irr.SColor(255, 0, 0, 0)
            ticks = 60
            dt = 1. / ticks
            while device.run():
                if device.isWindowActive():
                    if i_event_receiver.IsKeyDown(irr.KEY_ESCAPE):
                        break
                    elif i_event_receiver.IsKeyDown(irr.KEY_SPACE):
                        spawn_ball(space, player_body.position + (0, 40),
                                   random.choice([(1, 1), (-1, 1)]))

                    ### Clear screen
                    if video_driver.beginScene(True, True, color_screen):

                        ### Draw stuff
                        space_drawer.draw()

                        ### Info
                        font.draw("fps: %d" % video_driver.getFPS(),
                                  irr.recti(0, 0, 100, 20), color_white)
                        font.draw(
                            "Move with left/right arrows, space to spawn a ball",
                            irr.recti(5, height - 35, 300, 20), color_darkgray)
                        font.draw("Press R to reset, ESC or Q to quit",
                                  irr.recti(5, height - 20, 300, 20),
                                  color_darkgray)

                        video_driver.endScene()

                    ### Update physics
                    if device.getTimer().getTime() > ticks:
                        space.step(dt)

                    device.sleep(1)
                else:
                    device._yield()

            device.closeDevice()