»áÔ±£º ÃÜÂ룺 ¡¡Ãâ·Ñ×¢²á | Íü¼ÇÃÜÂë | »áÔ±µÇ¼ ÍøÒ³¹¦ÄÜ£º ¼ÓÈëÊÕ²Ø ÉèΪÊ×Ò³ ÍøÕ¾ËÑË÷  
¼¼ÊõÎĵµ > Delphi
ÓÃDelphi+DirectX¿ª·¢¼òµ¥RPGÓÎÏ·
·¢±íÈÕÆÚ£º2004-01-30 00:00:00×÷Õߣº¹ ³ö´¦£º  

¡¡¡¡Ìáµ½ RPG £¨½ÇÉ«°çÑÝÓÎÏ·£¬Role Play Game£©£¬ÔÚ×ù¸÷λ¿ÖÅÂûÓв»ÊìϤµÄ¡£´Ó¹ÅÀϾ­µäµÄ DOS °æ¡¶Ïɽ£ÆæÏÀ´«¡·µ½Ð³±»¨Éڵġ¶ÐùÔ¯½£¡·ÏµÁУ¬ÎÞ²»ÒÔÇúÕÛÓÅÃÀµÄ¹ÊÊÂÇé½Ú£¬ÃÀÀö¿ÉÈ˵ÄÖ÷½Ç£¬Ôöú¶¯ÇéµÄ±³¾°ÒôÀÖ£¬Õðº³ÁËÿһ¸öÍæ¼ÒµÄÐÄÁé¡£¶ø˵µ½ RPG£¬¾Í²»Äܲ»Ìá DirectX£¬ÒòΪ PC ÉÏ´ó²¿·ÖµÄ RPG ¶¼ÊÇÓÃÕâ¸ö¶¬¶¬¿ª·¢µÄ¡£ÔçÔÚ¡¶ÐùÔ¯½£ÈþÍâ´«¡ª¡ªÌìÖ®ºÛ¡·ÍƳöµÄʱºò£¬ÎÒ¾ÍÔøÏë¹ýÓà DirectX дһ¸ö×Ô¼ºµÄ RPG£¬×Ô¼ºÀ´°²ÅŹÊÊÂÇé½ÚµÄ·¢Õ¹£¬È´×ÜÊÇÒòΪÕâÑù»òÄÇÑùµÄÊÂÇ飬һֱûÓÐÄܹ»ÊµÏÖÕâ¸öÐÄÔ¸¡£ÔںķÑÁ˱¦¹óµÄ¼¸ÄêÇà´º£¬¸ã¶¨ÁËÖîÈ翼ÊÔ¡¢Áµ°®¡¢³ö¹úµÈËöËéÔÓÊÂÖ®ºó£¬ÎÒÖÕÓÚ¿ÉÒÔÔÚÕâ¸ö SARS ËÁÅ°µÄʱ´ú£¬×øÔÚÅã°éÁËÎÒÕû¸ö´óѧÉúÑĵĵçÄÔÇ°£¬Ìý×ÅÍǷ϶øÓÖÉùË»Á¦½ßµÄ²»ÖªÃû¸èÇú£¬Ð´Ò»Ð©Ò»Ö±ÏëдȴûÓÐдµÄ¶«Î÷¡£ DirectX ¼ò½é ¡¡¡¡DirectX ¶ÔÓÚ´ó¶àÊýÓÎÏ·°®ºÃÕßÀ´Ëµ¶¼²»Ä°Éú£¨µ±È»£¬ÄÇЩֻÔÚDOSϼè¿à×÷Õ½µÄÅóÓÑÀýÍ⣩£¬ÔÚ°²×°Ò»¸öÓÎÏ·Ç°£¬ÏµÍ³×ÜÊÇ»áÌáʾÄãÊÇ·ñÐèҪͬʱÉý¼¶ DirectX¡£¼òµ¥µØ˵£¬DirectX ¾ÍÊÇһϵÁÐµÄ DLL £¨¶¯Ì¬Á¬½Ó¿â£©£¬Í¨¹ýÕâЩ DLL£¬¿ª·¢Õß¿ÉÒÔÔÚÎÞÊÓÓÚÉ豸²îÒìµÄÇé¿öÏ·ÃÎʵײãµÄÓ²¼þ¡£DirectX ·â×°ÁËһЩ COM£¨Component Object Model£©¶ÔÏó£¬ÕâЩ COM ¶ÔÏóΪ·ÃÎÊϵͳӲ¼þÌṩÁËÒ»¸öÖ÷ÒªµÄ½Ó¿Ú¡£Ê×ÏÈ£¬ÎÒÃÇÏÈÀ´¿´Ò»Ï DirectX µÄ½á¹¹£º

ͼ1£ºDirectX »ù±¾½á¹¹ ¡¡¡¡DirectX Ä¿Ç°Ö÷ÒªÓÉÒÔÏÂÆ߸öÖ÷Òª²¿·Ö×é³É£º DirectDraw ¨C Ϊ³ÌÐòÖ±½Ó·ÃÎÊÏÔ´æÌṩ½Ó¿Ú£¬Í¬Ê±ºÍÆäËüµÄWindowsÓ¦ÓóÌÐò±£³Ö¼æÈÝ¡£ Direct3D ¨C Ϊ·ÃÎÊ3D¼ÓËÙÉ豸Ìṩ½Ó¿Ú¡£ DirectInput ¨C Ϊ¸÷ÖÖÊäÈëÉ豸Ìṩ½Ó¿Ú£¬±ÈÈçÊó±ê£¬¼üÅÌ£¬Á¦·´À¡ÓÎÏ·ÊÖ±úºÍ²Ù×ݸ˵ȡ£ DirectPlay ¨C ΪÓÎÏ·ÌṩÍøÂ繦Äܽӿڣ¬±ÈÈçÖ§³Öͨ¹ý TCP/I¡¢IPX µÈЭÒé½øÐÐÓÎÏ·ÖеÄÊý¾Ý´«Êä¡£ DirectSound ¨C Ϊ·ÃÎÊÉù¿¨Ìṩ½Ó¿Ú£¬Ö§³ÖWAV¡¢MIDI µÈÎļþµÄÖ±½Ó²¥·Å¡£ DirectSound3D ¨Cͨ¹ý´Ë½Ó¿Ú£¬¿ÉÒÔÄ£Äâ³öijһ¸öÉùÒôÔÚÈýά¿Õ¼äÖÐÈκÎÒ»¸öλÖõIJ¥·ÅËù²úÉúµÄЧ¹û£¬´Ó¶ø´ïµ½±ÆÕæµÄ»·ÈÆÁ¢ÌåÉù¡£ DirectMusic ¨C ´Ë½Ó¿ÚÖ÷ÒªÊÇÉú³ÉһϵÁеÄԭʼÉùÒô²ÉÑù·´À¡¸øÏàÓ¦µÄÓû§Ê¼þ¡£ ¿ª·¢¹¤¾ß£¨Delphi & DelphiX£© ¡¡¡¡ÏÂÒ»²½£¬ÎÒÃÇÀ´½éÉÜ¿ª·¢¹¤¾ß¡£ÎÒÃÇͨ³£Ëù°²×°µÄÆäʵֻÓÐ DirectX µÄÔËÐп⣨һϵÁзâ×°ºÃµÄDLLÎļþ£©£¬ÆäÄÚ²¿º¯Êý½á¹¹·Ç³£¸´ÔÓ£¬ËùÒÔÎÒÃÇ»¹ÐèÒª DirectX µÄ¿ª·¢¹¤¾ß¡£Ëùν¹¤ÓûÉÆÆäÊ£¬±ØÏÈÀûÆäÆ÷£¬ËäȻ΢Èí¹«²¼ÁË DirectX SDK£¬µ«ÊÇÓÉÓÚËùÓеÄÍ·Îļþ¶¼ÊÇÓà C/C++ д³ÉµÄ£¬×÷Ϊ Delphi µÄÈÈÇéÓµ»¤Õߣ¬ÎÒÃÇ»¹ÊÇÎÞ´ÓÏÂÊÖ¡£°Ñ C/C++ д³ÉµÄ´úÂëת»»³É Pascal ¿É²»ÊÇÒ»¼þÈÝÒ×µÄÊ£¬µ«ÊDz»±Øµ£ÐÄ£¬ÕâÏ×÷ÒѾ­ÓÐÈË×öºÃÁË¡£ÈÕ±¾ÈË Hiroyuki Hori Ϊ Delphi дÁËÒ»¸öÃâ·ÑµÄ×é¼þ°ü£¬³Æ×÷ DelphiX¡£ÕâЩ×é¼þ¿ÉÒÔʹµÃ¿ª·¢Õß¿ÉÒÔÇáËɵطÃÎÊ DirectX µÄ DirectDraw¡¢Direct3D¡¢DirectSound¡¢DirectInput£¨Ö§³ÖÁ¦·´À¡ÊÖ±ú£©ºÍ DirectPlay ¶ÔÏó¡£Ä¿Ç°µÄ DelphiX °üÖ§³Ö Borland Delphi 3/4/5/6/7 ºÍ DirectX 7.0 ÒÔÉÏ°æ±¾£¨¼ûͼ2£©¡£°²×°ÁË DelphiX Ö®ºó£¬ÎÒÃǽ«²»ÐèÒªÔÙ°²×°Î¢ÈíµÄ DirectX SDK¡£ÔÚÕâƪÎÄÕÂÀïÎÒÃǽ«Ê¹ÓõľÍÊÇ DelphiX¡£

TDXDraw DirectDraw ºÍ Direct3D ×é¼þ TDXDIB ÈÝÄÉDIB£¨É豸ÎÞ¹Øλͼ£¬Device Independent Bitmap£©µÄ×é¼þ TDXImageList ͼƬÁбí×é¼þ TDX3D Direct3D ×é¼þ £¨ºÍTDXDrawÒ»ÆðʹÓã© TDXSound DirectSound ×é¼þ TDXWave ÈÝÄÉ Wave£¨²¨ÐÎÒôƵÎļþ£©µÄ×é¼þ TDXWaveList WaveÎļþÁбí×é¼þ TDXInput ÊäÈë×é¼þ£¬°üÀ¨¼üÅ̺ÍÊÖ±úÊäÈë TDXPlay ͨѶ×é¼þ£¬ÓÃÓÚÍøÂçÓÎÏ·¿ª·¢ TDXSpriteEngine ¾«ÁéÒýÇ棬ÓÃÓÚ¹ÜÀíÓÎÏ·ÖвúÉúµÄ¾«Á飨Sprite£©Ïê¼ûÓÎÏ·¿ª·¢¹ý³Ì TDXTimer ¸ßËÙ¶¨Ê±Æ÷£¬±È TTimer Òª¸ü¿ì£¬¸ü׼ȷ TDXPaintBox TpaintBox µÄ DIB °æ±¾ TDXForm ÓÅ»¯¹ýµÄ TForm£¬×¨ÃÅÓÃÓÚÓÎÏ·¿ª·¢ ¶¯»­Ô­Àí ¡¡¡¡ÔÚ½éÉܶ¯»­µÄÔ­Àí֮ǰ£¬ÎÒÃÇÏÈÀ´½éÉܼ¸¸öDirectXµÄÊõÓï¡£ ±íÃ棨Surfaces£© ÕâÀïÎÒÃÇÔÝÇÒ³Æ×÷±íÃæ°É¡£ÔÚ DirectX ÖУ¬ÏÔ´æÖб»ÓÃÀ´´æ´¢¸÷ÖÖλͼºÍÊÓƵ»º³åµÄ²¿·Ö¾ù±»³Æ×÷±íÃæ¡£¶øµ±Ç°±»ÏÔʾÔÚÆÁÄ»ÉϵıíÃæ±»³Æ×÷Ö÷±íÃ棨primary surface£©¡£ÔÚʵÑ鶯»­µÄ¹ý³ÌÖУ¬ÎÒÃÇͨ³£ÒªÔ¤ÏÈÉú³ÉÖÁÉÙÒ»¸ö²»ÏÔʾµÄ±íÃ棬³Æ×÷ºǫ́±íÃ棨offscreen surface£©¡£ÔÚÐèÒª²úÉú¶¯»­µÄʱºò£¬ÎÒÃÇ¿ÉÒÔÖ±½Ó½«ºǫ́±íÃæÖеÄÄÚÈÝÏÔʾµ½ÆÁÄ»ÉÏ£¬¶ø½«Ô­À´Ö÷±íÃ淭תµ½ºǫ́¶ø²»ÔÙÏÔʾ¡£Õâô˵¿ÉÄÜ´ó¼Ò¸üÈÝÒ×Ã÷°×£¬ÎÒÃÇ´ó¼ÒÔÚСµÄʱºò¶¼Íæ¹ýÔÚÊéÉÏ»­Ð¡È˵ÄÓÎÏ·°É¡£ÔÚÿҳÊéµÄÏ·½¿Õ°×´¦»­ÉÏÒ»¸ö¸öСÈË£¬Ã¿Ò³ÉϵÄÄÚÈÝÖð½¥·¢Éú±ä»¯£¬È»ºó¿ìËÙ·­¶¯Õû±¾Ê飬¾Í¿ÉÒÔ¿´µ½ÓÐСÈËÔÚÌø¶¯¡£ÎÒÃÇÏÖÔÚµÄÖ÷±íÃæ¾ÍÊÇÄãÏÖÔÚÕý¿´µ½ÄÇÒ³Ê飬ºóÃæµÄÄÇЩ»¹Ã»Óз­µ½µÄÒ³Ãæ¾ÍÊǺǫ́±íÃ棬ͨ¹ý·­¶¯ÊµÏÖÁ˶¯»­Ð§¹û¡£ÔÚÔËÐÐһЩ´óÐÍÓÎÏ·µÄʱºò£¬ÔÚÏÔ´æÒѾ­ÓÃÍêµÄÇé¿öÏ£¬ÓÎÏ·½«×Ô¶¯µ÷ÓÃϵͳÄڴ档Ŀǰ¶øÑÔ£¬ÏëÒªÁ÷³©µØÔËÐÐÒ»¸ö DirectX ÓÎÏ·£¬·Ç 3D ÀàµÄ×îºÃÓÐ 4M ÒÔÉϵÄÏԴ棬3D ÀàµÄÔòÏÔ´æÔ½´óÔ½ºÃ¡£ ÿÃëÖ¡Êý£¨Frames per Second£© ͨ³£¼ò³Æ FPS£¬ÓÃÎÒÃǸղžٵÄÀý×ÓÀ´ÃèÊö£¬Ò²¾ÍÊÇÖ¸ÎÒÃÇÿÃëËù·­¹ýµÄÊéµÄÒ³Êý¡£µ±Ö¡Êý´ïµ½24֡ÿÃëʱ£¬ÈËÑÛÒѾ­ÎÞ·¨·Ö±æ£¬´Ó¶øÈÏΪ»­ÃæÊÇÁ¬ÐøµÄ¡£µçÓ°²ÉÓõÄ24֡ÿÃ룬¶øµçÊÓ²ÉÓõÄÊÇ25֡ÿÃ롣ʵ¼ÊÉÏ£¬ÔÚÈÕ±¾µÄ¶¯»­ÖÆ×÷ÖÐÓÐÒ»ÅĶþ£¨12֡ÿÃ룩ÉõÖÁÒ»ÅÄÈý£¨8֡ÿÃ룩µÄ×ö·¨£¬Í¬Ñù¿É×ö³öÆÛÆ­ÈâÑÛµÄЧ¹û£¬Õâ¸öÎÒÃÇÔÝÇÒ²»×÷ÌÖÂÛ¡£ÔÚÓÎÏ·ÖУ¬ÎÒÃǽ«²ÉÓÃ24Ö¡»ò25֡ÿÃëµÄËÙ¶ÈÒÔ´ïµ½ÆÛÆ­ÈâÑÛµÄЧ¹û¡£ λͼºÍ¾«Á飨Bitmaps and Sprites£© ¾«ÁéÊÇÖ¸ÔÚÓÎÏ·ÖÐÓÃÀ´ÊµÏÖ¶¯»­Ð§¹ûµÄÒ»×éͼƬ£¬ÕâЩͼƬͨ³£²ÉÓÃλͼ¸ñʽ£¬²¢ÇÒÓÐÓÃÌض¨ÑÕÉ«Ìî³äµÄ͸Ã÷ÇøÓò¡£ÈçÏÂͼ£¬¾ÍÊÇÒ»¸ö¾«Áé¡£

ͼÖеÄÓúÚÉ«Ìî³ä²¿·Ö¾ÍÊÇÔ¤ÏÈËù¶¨ÒåµÄ͸Ã÷ÇøÓò£¬ÔÚÓÎÏ·ÖУ¬ÎÒÃÇ°´ÕÕ°×É«ÏßÌõ¿ÉÒÔ½«Õû¸öͼƬ·Ö±ðÇпª³É9¿é£¬Ã¿¿éµÄ´óС¾ùΪ80¡Á50¡£ÎÒÃÇÏÈ´Ó×óÏòÓÒ£¬È»ºóÔÚ´ÓÉÏÍù϶ÔÆð½øÐбàºÅΪ1¡«9¡£Èç¹ûÎÒÃǽ«Õâ¾ÅÕÅͼƬÒÀ´ÎÑ­»·ÏÔʾ³öÀ´£¬²¢ÇÒÉèÖò¥·ÅµÄËÙ¶ÈΪÿÃë24ÕÅ£¬ÎÒÃǾͿÉÒԵõ½Ò»¸ö·É´¬ÔÚÐýתµÄ¶¯»­£¬Ò²¾ÍÊÇÒ»¸ö·É´¬¾«Áé¡£ ¿ª·¢¹ý³Ì ¡¡¡¡ËµÁËÐí¶à·Ï»°ÒÔºó£¬ÏÂÃæÎÒÃÇÍÑÀëÖ½ÉÏ̸±ø£¬¿ªÊ¼ÕýʽµÄ¿ª·¢¡£ÔÚ±¾ÀýÖУ¬ÎÒÃÇÖ÷ҪʵÏÖÓÃÊó±êÀ´¿ØÖƾ«ÁéÍù°Ë¸ö·½ÏòÐÐ×ß¡£ËùÓÐͼƬ¾ùÀ´×Ô´óÓ˾¡¶ÐùÔ¯½£Èþ¡ª¡ªÌìÖ®ºÛ¡·£¬ÆäÖо«Áé²ÉÓùÊÊÂÖг¾¸³ðµÄÐÎÏóÌØ´ËÖÂл¡£Í¬Ê±£¬Çë¸÷λ¶ÁÕßÎð°ÑÕâЩͼƬÓÃÓÚÉÌÒµÓÃ;£¬·ñÔòºó¹û×Ô¸º¡£

¡¡¡¡´ò¿ª Delphi ²¢Ð½¨Ò»¸öÓ¦ÓóÌÐò£¬ÒÀ´ÎÑ¡ÖÐ DelphiX ×é¼þÀ¸µÄ TDXDraw¡¢TDXImageList¡¢TDXInput¡¢TDXTimer¡¢TDXSpriteEngine ×é¼þ£¬Ìí¼Óµ½Óû§Çø£¬·Ö±ðÃüÃûΪ DXDraw¡¢DXImageList¡¢DXInput¡¢DXTimer¡¢DXSpriteEngine£¬°´ÕÕϱíÉèÖÃÆä¸÷ÏîÊôÐÔ¡£¶ÔÓÚ DXImageList£¬µã»÷ Object Inspector ÖÐµÄ Items£¬ÔÚÆäÖмÓÈëÁ½ÕÅλͼ£¨background.bmpºÍplayer.bmp£©£¬·Ö±ðÃüÃûΪ background ºÍ player£¬ÉèÖà player µÄ PatternHeight ºÍ PatternWidth ¾ùΪ120ÏóËØ£¬ÉèÖÃÆä transparentcolor Ϊ·ÛºìÉ«£¨clFuchsia£©¡£ ¿Ø¼þ ÊôÐÔ Öµ DXDraw Align alClient Display.Width 800 Display.Height 600 Display.BitCount 24 Options [doAllowReboot, doWaitVBlank, doCenter, doFlip] AutoInitialize True DXTimer Enabled True Interval 0 DXInput Mouse.BindInputState True Mouse.Enabled True DXSpriteEngine DXDraw DXDraw ¡¡¡¡ÏÂÃæ¾ÍÊÇÈ«²¿µÄÔ´³ÌÐò£¬ÇëÏÈÔÚ Delphi ÖвúÉúÏàӦʼþÈ»ºóÌîÈë´úÂ룬×îºó°´ÏÂF9ÔËÐоͿÉÒÔÔËÐгÌÐòÁË¡£ÓÃÊó±êµã»÷ÄãµÄÄ¿µÄµØ£¬³Â¾¸³ð¾Í»á×Ô¶¯Åܵ½Ö¸¶¨µØµã¡£³¢ÊÔһϿª·¢Ò»Ð©¼òµ¥µÄÓÎÏ·°É£¬Óà DelphiX Õâ°ÑÅ£µ¶£¡ËùÓгÌÐòÔÚ Delphi 4.0 + DirectX 8.0 »·¾³Ï²âÊÔͨ¹ý¡£±¾ÎÄËùÐè¿Ø¼þ¿ÉÒÔÔÚ ÕâÀï ÏÂÔØ¡£ Unit Main; Interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Menus, DXClass, DXSprite, DXInput, DXDraws; type TDirection = (DrUp, DrDown, DrLeft, DrRight, DrUpLeft, DrUpRight, DrDownLeft, DrDownRight); {×Ô¶¨ÒåÓÎÏ·ÖÐËùÓõ½µÄ·½Ïò} TPlayerSprite = class (TImageSprite) CanMove: Boolean; protected procedure DoMove(MoveCount: Integer); override; procedure MoveTo(MoveCount:Integer; Direction: TDirection); procedure DoCollision(Sprite: TSprite; var Done: Boolean); override; end; TMainForm = class(TDXForm) {´Ë´¦Ê¹ÓÃÓÅ»¯¹ýµÄTDXFormÀ´´úÌæTForm} DXTimer: TDXTimer; DXDraw: TDXDraw; DXSpriteEngine: TDXSpriteEngine; DXInput: TDXInput; ImageList: TDXImageList; procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure DXDrawFinalize(Sender: TObject); procedure DXDrawInitialize(Sender: TObject); procedure FormCreate(Sender: TObject); procedure DXTimerTimer(Sender: TObject; LagCount: Integer); procedure DXDrawMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure DXDrawMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure DXDrawMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormClose(Sender: TObject; var Action: TCloseAction); private AnchorX: Integer; AnchorY: Integer; {Êó±êµã»÷·¢ÉúµÄλÖÃ} MouseX: Integer; MouseY: Integer; {Êó±êµ±Ç°Î»ÖÃ} PlayerSprite: TPlayerSprite; {ÓÎÏ·ÖÐÎÒÃÇËùÓÃÊó±ê¿ØÖƵÄÈËÎï} BackSprite: TBackGroundSprite; {ÓÎÏ·µÄ±³¾°Í¼} end; const speed=5; {ÓÎÏ·ÈËÎïÏò¸÷¸ö·½ÏòÔ˶¯Ê±µÄ¶¯»­²¥·ÅËÙ¶È} var MainForm: TMainForm; Steps: Integer; {ÓÃÓÚ¿ØÖÆÇл»¾«Á鶯»­Í¼Æ¬µÄ²ÎÊý} implementation {$R *.DFM} procedure TPlayerSprite.DoCollision(Sprite: TSprite; var Done: Boolean); begin Done:=False; {ÒѾ­Õì²âµ½Åöײ£¬²»ÔÙÖظ´¼ì²âÅöײ} {¼ì²âÓÎÏ·ÈËÎïÊÇ·ñÓëÆäËü¾«Áé·¢ÉúÁËÅöײ£¬´Ë´¦¿ÉÒÔÀ©Õ¹Îª¶Ô»°µÈÇé½Ú} end; procedure TPlayerSprite.DoMove(MoveCount: Integer); var l,r,d,u: Boolean; absX,absY: Integer; {ÓÎÏ·ÈËÎïµÄµ±Ç°Î»ÖÃÓëÄ¿µÄµØµÄ¾ø¶Ô¾àÀë} begin inherited DoMove(MoveCount); MoveCount:=Trunc(MoveCount*1.5); l:=false; r:=false; u:=false; d:=false; if (Trunc(X)-MainForm.AnchorX>0) then l:=true else r:=true; if (Trunc(Y)-MainForm.AnchorY>0) then u:=true else d:=true; absX:=abs(Trunc(X)-MainForm.AnchorX); absY:=abs(Trunc(Y)-MainForm.AnchorY); if absX<4 then begin l:=false; r:=false; end; if absY<4 then begin u:=false; d:=false; end; {Èç¹û¾ø¶Ô¾àÀëÒѾ­Ð¡ÓÚËĸöÏóËØ£¬ÔòÈÏΪÒѾ­µ½´ïÄ¿µÄµØ} if u and l and not d and not r then MoveTo(MoveCount,DrUpLeft); if u and r and not l and not d then MoveTo(MoveCount,DrUpRight); if d and l and not r and not u then MoveTo(MoveCount,DrDownLeft); if d and r and not u and not l then MoveTo(MoveCount,DrDownRight); if d and not l and not r and not u then MoveTo(MoveCount,DrDown); if u and not l and not r and not d then MoveTo(MoveCount,DrUp); if l and not u and not r and not d then MoveTo(MoveCount,DrLeft); if r and not l and not u and not d then MoveTo(MoveCount,DrRight); {¸ù¾ÝÄ¿µÄµØÀ´ÅжÏÔ˶¯µÄ·½Ïò£¬´Ó¶ø²¥·ÅÏàÓ¦·½ÏòÔ˶¯µÄ¶¯»­} Collision; {¼ì²âÅöײ} Engine.X := -X+Engine.Width div 2 - Width div 2; Engine.Y := -Y+Engine.Height div 2 - Height div 2; {Òƶ¯ÒýÇ棬´Ó¶øÊÇÓÎÏ·ÈËÎï´¦ÓÚÎę̀µÄÕýÖÐÑë} end; procedure TMainForm.DXTimerTimer(Sender: TObject; LagCount: Integer); begin if not DXDraw.CanDraw then exit; {¼ì²âDXDrawÊÇ·ñ¿ÉÒÔ»­£¬·ñÔòÍ˳ö} DXInput.Update; {²¶×½¸÷ÀàÉ豸ÊäÈ룬ÕâÀïÎÒÃÇÓÃÀ´¼ì²âÊó±êµÄÊäÈë} LagCount := 1000 div 60; {ÓÃÀ´¿ØÖÆÕû¸öÓÎÏ·ÔËÐÐËٶȵIJÎÊý} DXSpriteEngine.Move(LagCount); DXSpriteEngine.Dead; DXDraw.Surface.Fill(0); {½«Õû¸öÆÁÄ»Ìî³äΪºÚÉ«} DXSpriteEngine.Draw; with DXDraw.Surface.Canvas do begin brush.style:=bsclear; pen.style:=psclear; pen.color:=clwhite; Font.Color:=clWhite; Font.Size:=10; textout(10,10,'Press ESC to Quit'); textout(100,100,'X: '+IntToStr(AnchorX)+'Y: '+IntToStr(AnchorY)); {Êó±êµã»÷µÄλÖþ­×ª»»ºóÔÚÓÎÏ·ÊÀ½çÖеÄ×ø±ê} textout(100,200,'Sprit x:'+IntToStr(Trunc(PlayerSprite.x))+'Y: ' +IntToStr(Trunc(PlayerSprite.y))); {¾«ÁéÔÚÓÎÏ·ÊÀ½çÖеÄ×ø±ê} textout(100,300,'Relative x:'+IntToStr(AnchorX-Trunc(PlayerSprite.x))+'Y: ' +IntToStr(AnchorY-Trunc (PlayerSprite.y))); {¾«Á鵱ǰλÖÃÓëÄ¿µÄµØÖ®¼äµÄ¾ø¶Ô¾àÀë} textout(200,100,'Mouse x:'+IntToStr(MainForm.MouseX)+'Y: ' +IntToStr(MainForm.MouseY)); {Êó±êµ±Ç°Î»Öã¬Ïà¶ÔÓÚ´°¿Ú×óÉϽǣ¬Î´×ª»»ÎªÓÎÏ·ÊÀ½ç×ø±ê} Release; end; {ÔÚ×ÖĸÉÏÊä³öÏàÓ¦²ÎÊý£¬ÓÃÓÚ³ÌÐòµ÷ÊÔ} DXDraw.Flip; {½«ÄÚ´æÖеĺǫ́±íÃ淭תµ½µ±Ç°²¢ÇÒÏÔʾ} end; procedure TMainForm.DXDrawFinalize(Sender: TObject); begin DXTimer.Enabled := False; {¹Ø±Õ¶¨Ê±Æ÷} end; procedure TMainForm.DXDrawInitialize(Sender: TObject); begin DXTimer.Enabled := True; {Æô¶¯¶¨Ê±Æ÷} end; procedure TMainForm.FormCreate(Sender: TObject); begin Steps:=0; AnchorX:=0; AnchorY:=0; MouseX:=320; MouseY:=240; {ĬÈÏʹÊó±ê´¦ÓÚÆÁÄ»µÄÖÐÑë} ImageList.Items.MakeColorTable; DXDraw.ColorTable := ImageList.Items.ColorTable; DXDraw.DefColorTable := ImageList.Items.ColorTable; DXDraw.UpdatePalette; {¸üÐÂϵͳµ÷É«°å} BackSprite:=TBackgroundSprite.Create(DXSpriteEngine.Engine); with TBackgroundSprite(BackSprite) do begin SetMapSize(1, 1);{É趨±³¾°ÏÔʾÑùʽΪ1¡Á1} Image := ImageList.Items.Find('background'); {ÔØÈë±³¾°Í¼Æ¬} Z := -2; {É趨±³¾°²ã´Î} Tile := True; {É趨±³¾°Ìî³äÑùʽΪƽÆÌ} end; PlayerSprite := TPlayerSprite.Create(DXSpriteEngine.Engine); with TPlayerSprite(PlayerSprite) do begin Image := ImageList.Items.Find('player'); Z := 2; Width := Image.Width; Height := Image.Height; end; {ÔØÈëÓÎÏ·ÈËÎï} end; procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin {Èç¹û°´ÁËEsc£¬ÔòÍ˳ö} if Key=VK_ESCAPE then Close; {È«ÆÁģʽºÍ´°¿ÚģʽµÄÇл»} if (ssAlt in Shift) and (Key=VK_RETURN) then begin DXDraw.Finalize; if doFullScreen in DXDraw.Options then begin RestoreWindow; DXDraw.Cursor := crNone; BorderStyle := bsSizeable; DXDraw.Options := DXDraw.Options - [doFullScreen]; end else begin StoreWindow; DXDraw.Cursor := crNone; BorderStyle := bsNone; DXDraw.Options := DXDraw.Options + [doFullScreen]; end; DXDraw.Initialize; end; end; procedure TMainForm.DXDrawMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin AnchorX := x + Trunc(PlayerSprite.x)-320; AnchorY := y + Trunc(PlayerSprite.y)-240; {½«Êó±êÔÚÆÁÄ»Éϵã»÷µÄλÖÃת»»µ½ÓÎÏ·ÊÀ½çÖÐ} PlayerSprite.CanMove:=True; {´Ë²ÎÊýÔÊÐíÊó±êÍ϶¯} end; procedure TMainForm.DXDrawMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if PlayerSprite.CanMove then begin AnchorX := x + Trunc(PlayerSprite.x)-320; AnchorY := y + Trunc(PlayerSprite.y)-240; {ÔÚÊó±êÍ϶¯¹ý³ÌÖн«Êó±êÔÚÆÁÄ»Éϵã»÷µÄλÖÃת»»µ½ÓÎÏ·ÊÀ½çÖÐ} end; MouseX:=X; MouseY:=Y; {Êó±êµ±Ç°Î»ÖÃ} end; procedure TMainForm.DXDrawMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin PlayerSprite.CanMove:=False; end; procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction); begin DXSpriteEngine.Free; end; procedure TPlayerSprite.MoveTo(MoveCount: Integer; Direction: TDirection); begin {¿ØÖƾ«ÁéÍù¸÷¸ö·½ÏòÒƶ¯} case Direction of DrUp: begin Y := Y-(150/1000)*MoveCount; Inc(steps); AnimPos:=steps div speed+20+1; {µ±Ç°¶¯»­Öв¥·ÅµÄͼƬÐòºÅ} if steps>4*speed-2 then steps:=0; end; DrDown: begin Y := Y+(150/1000)*MoveCount; Inc(steps); AnimPos:=steps div speed+1; if steps> 4*speed-2 then steps:=0; end; DrLeft: begin X := X-(150/1000)*MoveCount; Inc(steps); AnimPos:=steps div speed+10+1; if steps>4*speed-2 then steps:=0; end; DrRight: begin X := X+(150/1000)*MoveCount; Inc(steps); AnimPos:=steps div speed+30+1; if steps>4*speed-2 then steps:=0; end; DrUpLeft: begin X := X-(150/1000)*MoveCount; Y := Y-(150/1000)*MoveCount; Inc(steps); AnimPos:=steps div speed+15+1; if steps>4*speed-2 then steps:=0; end; DrUpRight: begin X := X+(150/1000)*MoveCount; Y := Y-(150/1000)*MoveCount; Inc(steps); AnimPos:=steps div speed+25+1; if steps>4*speed-2 then steps:=0; end; DrDownLeft: begin X := X-(150/1000)*MoveCount; Y := Y+(150/1000)*MoveCount; Inc(steps); AnimPos:=steps div speed+5+1; if steps>4*speed-2 then steps:=0; end; DrDownRight: begin X := X + (150/1000)*MoveCount; Y := Y + (150/1000)*MoveCount; Inc(steps); AnimPos:=steps div speed+35+1; if steps>4*speed-2 then steps:=0; end; end; end; end. ºó¼Ç ¡¡¡¡Í¨¹ýÒÔÉϵĽ²½âºÍÀý×Ó£¬ÏàÐÅ´ó¼ÒÒѾ­¶Ô Delphi Ï嵀 DirectX ÓÎÏ·¿ª·¢ÓÐÁ˳õ²½µÄ¸ÅÄî¡£¹úÄÚ½²½â¿ª·¢ DirectX ÓÎÏ·µÄȨÍþ×ÊÁϺÜÉÙ£¬Èç¹ûÕâƪ½²½â¿ÉÒÔÆðµ½Å×שÒýÓñµÄЧ¹ûµÄ»°£¬ÎÒ½«ÔÙ¸ßÐ˲»¹ýÁË¡£±¾ÎIJ¿·ÖÄÚÈݽè¼øÁ˹úÍâһЩÓÎÏ·¿ª·¢ÍøÕ¾µÄ¼¼ÊõÎĵµ£¬ÔÚ´ËÒ»²¢ÖÂл¡£Èç¹û¸÷λÓÐʲôÎÊÌâÐèÒªºÍÎÒ½»Á÷£¬Çë·¢ÐÅÖÁeagle_california@yahoo.com¡£ ¡¡¡¡Ô´³ÌÐòÏÂÔصØÖ·£ºÓÃDelphi+DirectX¿ª·¢¼òµ¥RPGÓÎÏ·Ô´´úÂë
¡¾·µ»Ø¶¥²¿¡¿ ¡¾´òÓ¡±¾Ò³¡¿ ¡¾¹Ø±Õ´°¿Ú¡¿

¹ØÓÚÎÒÃÇ / ¸øÎÒÁôÑÔ / °æȨ¾Ù±¨ / Òâ¼û½¨Òé / ÍøÕ¾±à³ÌQQȺ   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net ¼ÓÔØʱ¼ä 0.00367