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')
else: print('Unknown EventType', event.EventType) return False windowSize = pyirrlicht.dimension2du(640, 480) i_event_receiver = UserIEventReceiver() device = pyirrlicht.createDevice(driverType, windowSize, 16, False, False, False, i_event_receiver) if device: device.setWindowCaption('Hello World! - Irrlicht Engine Demo') device.setResizable(True) driver = device.getVideoDriver() scene_manager = device.getSceneManager() guienv = device.getGUIEnvironment() guienv.addStaticText('Hello World! This is the Irrlicht Software renderer!', pyirrlicht.recti(10,10,260,22), True) i_animated_mesh = scene_manager.getMesh('media//sydney.md2') if i_animated_mesh: node = scene_manager.addAnimatedMeshSceneNode2(i_animated_mesh) if node: node.setMaterialFlag(pyirrlicht.EMF_LIGHTING, False) node.setMD2Animation(pyirrlicht.EMAT_STAND) node.setMaterialTexture(0, driver.getTexture('media//sydney.bmp')) position = pyirrlicht.vector3df(0.0, 30.0, -40.0) lookat = pyirrlicht.vector3df(0.0, 5.0, 0.0) scene_manager.addCameraSceneNode(node, position, lookat) scolor = pyirrlicht.SColor(255,100,101,140) while device.run(): if device.isWindowActive(): if driver.beginScene(True, True, scolor): scene_manager.drawAll()
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')
def main(): global text, line_point1, static_lines, run_physics driver_type = 1 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 os, math import pymunk as pm from pymunk import Vec2d ### Physics stuff space = pm.Space() space.gravity = Vec2d(0.0, -900.0) ## Balls balls = [] ### Mouse mouse_body = pm.Body() mouse_shape = pm.Circle(mouse_body, 3, Vec2d(0,0)) mouse_shape.collision_type = COLLTYPE_MOUSE space.add(mouse_shape) space.add_collision_handler(COLLTYPE_MOUSE, COLLTYPE_BALL, None, mouse_coll_func, None, None) ### pyIrrlicht block import pyirrlicht as irr if not driver_type: driver_type = irr.EDT_SOFTWARE class UserIEventReceiver(irr.IEventReceiver): shift = False control = False pressed_down = False video_driver = None screen_size = irr.dimension2du() mouse_position = Vec2d(0, flipy(0)) KeyIsDown = {} for key in range(irr.KEY_KEY_CODES_COUNT): KeyIsDown[key] = False def OnEvent(self, evt): global line_point1 event = irr.SEvent(evt) if event.EventType is irr.EET_KEY_INPUT_EVENT: self.shift = event.KeyInput.Shift self.control = event.KeyInput.Control self.pressed_down = event.KeyInput.PressedDown self.KeyIsDown[event.KeyInput.Key] = self.pressed_down elif event.EventType is irr.EET_MOUSE_INPUT_EVENT: if event.MouseInput.EventType == irr.EMIE_MOUSE_MOVED: self.mouse_position.x = event.MouseInput.X self.mouse_position.y = event.MouseInput.Y elif event.MouseInput.EventType == irr.EMIE_LMOUSE_PRESSED_DOWN: body = pm.Body(10, 100) body.position = (event.MouseInput.X, flipy(event.MouseInput.Y)) shape = pm.Circle(body, 10, (0,0)) shape.friction = 0.5 shape.collision_type = COLLTYPE_BALL space.add(body, shape) balls.append(shape) elif event.MouseInput.EventType == irr.EMIE_RMOUSE_PRESSED_DOWN: if line_point1 is None: line_point1 = Vec2d(event.MouseInput.X, flipy(event.MouseInput.Y)) elif event.MouseInput.EventType == irr.EMIE_RMOUSE_LEFT_UP: if line_point1 is not None: line_point2 = Vec2d(event.MouseInput.X, flipy(event.MouseInput.Y)) body = pm.Body() shape= pm.Segment(body, line_point1, line_point2, 0.0) shape.friction = 0.99 space.add(shape) static_lines.append(shape) line_point1 = None return False def IsKeyDown(self, keyCode): return self.KeyIsDown[keyCode] def mouse_pos(self): return self.mouse_position 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 + text, irr.recti(10, 10, screen_x-20, 110), True) color_red = irr.SColor(255, 255, 0, 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() i_event_receiver.video_driver = video_driver i_event_receiver.gui = gui_environment device.setEventReceiver(i_event_receiver) while device.run(): if device.isWindowActive(): if i_event_receiver.IsKeyDown(irr.KEY_ESCAPE): run_physics = not run_physics break p = i_event_receiver.mouse_pos() mouse_pos = Vec2d(p[X],flipy(p[Y])) mouse_body.position = mouse_pos if i_event_receiver.shift and i_event_receiver.pressed_down: body = pm.Body(10, 10) body.position = mouse_pos shape = pm.Circle(body, 10, (0,0)) shape.collision_type = COLLTYPE_BALL space.add(body, shape) balls.append(shape) ### Update physics if run_physics: dt = 1.0/60.0 for x in range(1): space.step(dt) if video_driver.beginScene(True, True, color_screen): ### Display some text #~ font.draw(text, irr.recti(5, 40, screen_x-10, 50), color_text) for ball in balls: r = ball.radius v = ball.body.position rot = ball.body.rotation_vector x1, y1 = v.x, flipy(v.y) #~ x2, y2 = (Vec2d(rot.x, -rot.y) * r * 0.9 + (x1, y1)).int_tuple end = (Vec2d(rot.x, -rot.y) * r * 0.9 + (x1, y1)) video_driver.draw2DPolygon_f(x1, y1, r, color_blue, 100) video_driver.draw2DLine_f(x1, y1, end.x, end.y, color_red) if line_point1 is not None: video_driver.draw2DLine_f(line_point1.x, flipy(line_point1.y), mouse_pos.x, flipy(mouse_pos.y), color_black) for line in static_lines: body = line.body pv1 = body.position + line.a.rotated(body.angle) pv2 = body.position + line.b.rotated(body.angle) video_driver.draw2DLine_f(pv1.x, flipy(pv1.y), pv2.x, flipy(pv2.y), color_lightgray) gui_environment.drawAll() video_driver.endScene() device.sleep(10) 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 + text) lastFPS = fps else: device._yield() device.closeDevice() else: print('ERROR createDevice')
windowSize = pyirrlicht.dimension2du(640, 480) i_event_receiver = UserIEventReceiver() device = pyirrlicht.createDevice(driverType, windowSize, 16, False, False, False, i_event_receiver) if device: device.setWindowCaption('Hello World! - Irrlicht Engine Demo') device.setResizable(True) driver = device.getVideoDriver() scene_manager = device.getSceneManager() guienv = device.getGUIEnvironment() guienv.addStaticText( 'Hello World! This is the Irrlicht Software renderer!', pyirrlicht.recti(10, 10, 260, 22), True) i_animated_mesh = scene_manager.getMesh('media//sydney.md2') if i_animated_mesh: node = scene_manager.addAnimatedMeshSceneNode2(i_animated_mesh) if node: node.setMaterialFlag(pyirrlicht.EMF_LIGHTING, False) node.setMD2Animation(pyirrlicht.EMAT_STAND) node.setMaterialTexture(0, driver.getTexture('media//sydney.bmp')) position = pyirrlicht.vector3df(0.0, 30.0, -40.0) lookat = pyirrlicht.vector3df(0.0, 5.0, 0.0) scene_manager.addCameraSceneNode(node, position, lookat) scolor = pyirrlicht.SColor(255, 100, 101, 140) while device.run(): if device.isWindowActive(): if driver.beginScene(True, True, scolor): scene_manager.drawAll()
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()
if self.GetEventType(event) is pyirrlicht.EET_KEY_INPUT_EVENT: if self.KeyInput_Key(self.GetKeyInput(event)) == pyirrlicht.KEY_ESCAPE: if self.main_loop: self.main_loop.stop() return False windowSize = pyirrlicht.dimension2du(640, 480) i_event_receiver = UserIEventReceiver() device = pyirrlicht.createDevice(driverType, windowSize, 16, False, False, False, i_event_receiver) if device: device.setResizable(True) driver = device.getVideoDriver() scene_manager = device.getSceneManager() guienv = device.getGUIEnvironment() guienv.addStaticText('Hello World! This is C++ example main loop realisation!', pyirrlicht.recti(10,10,260,22), True) i_animated_mesh = scene_manager.getMesh('media//sydney.md2') if i_animated_mesh: node = scene_manager.addAnimatedMeshSceneNode2(i_animated_mesh) if node: node.setMaterialFlag(pyirrlicht.EMF_LIGHTING, False) node.setMD2Animation(pyirrlicht.EMAT_STAND) node.setMaterialTexture(0, driver.getTexture('media//sydney.bmp')) position = pyirrlicht.vector3df(0.0, 30.0, -40.0) lookat = pyirrlicht.vector3df(0.0, 5.0, 0.0) scene_manager.addCameraSceneNode(node, position, lookat) main_loop = pyirrlicht.MainLoop(device, driver, scene_manager, guienv) i_event_receiver.main_loop = main_loop print('START MAIN LOOP') main_loop.start()
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()