| 
			
			
			
			
			
			 
				dfzetcc
			 
			
			
			
			 
			
				
				- 中级会员
 
				- 330
 - 0
 - 473
 - 2 
 - 5143.8 元
 - 2014-08-22
 	
			 
			
			
			
		 | 
		
			
				
					
					
					7#
					
					
				
				 
					
						
						
						t
						T
					 
					
					 
					
					
					发表于 2024-12-24 21:35
					
				
					|只看楼主  
					
				 
			 
            
              
            
			
			
			
				
				    
				    
					
                        我参考大佬的mame脚本为奥运会的打枪游戏写的代码..目前效果并不完美.p.s:需要启用Script>Settings>Allow access to I/O and OS functions - -- 射击目标
  
- local targetSpriteIds = {0x27}
  
- -- 瞄准框
  
- local shootingBoxSpriteIds = {0x73}
  
- -- 防止射击到射击目标的阴影
  
- local excludedSpriteIds = {0x00, 0x29, 0x2b, 0x1a, 0x1c, 0x0f}
  
- for i = 0x2d, 0x2f do table.insert(excludedSpriteIds, i) end
  
- for i = 0x3b, 0x3c do table.insert(excludedSpriteIds, i) end
  
- table.insert(excludedSpriteIds, 0x41)
  
- for i = 0x43, 0x48 do table.insert(excludedSpriteIds, i) end
  
  
- local screenCenterX = 256 / 2
  
- -- 迭代计数
  
- local lastInputTime = 0
  
- -- 延迟计数
  
- local inputDelay = 5
  
  
- -- 检查精灵的值是否有效
  
- local function isSpriteExcluded(spriteId)
  
-     for _, id in ipairs(excludedSpriteIds) do
  
-         if spriteId == id then
  
-             return true
  
-         end
  
-     end
  
-     return false
  
- end
  
  
- local function processSprites()
  
-     local nesSpriteRam = emu.memType.nesSpriteRam
  
-     local largeSprites = emu.getState()["ppu.control.largeSprites"]
  
-     local height = largeSprites and 16 or 8
  
-     local sprites = {}
  
  
-     for address = 0x00, 0xFF, 4 do
  
-         local y = emu.read(address, nesSpriteRam)
  
-         local tileIndex = emu.read(address + 1, nesSpriteRam)
  
-         local x = emu.read(address + 3, nesSpriteRam)
  
  
-         if y < 240 then 
  
-             table.insert(sprites, {x = x, y = y + 1, id = tileIndex})  
  
-         end
  
-     end
  
  
-     local targetSprite = nil
  
-     local leftBox = nil
  
-     local rightBox = nil
  
  
-     for _, sprite in ipairs(sprites) do
  
-         if sprite.x ~= 0 and sprite.x ~= 0xF8 and not isSpriteExcluded(sprite.id) then  -- 获取射击精灵的类型,避免射击阴影
  
-             if table.contains(targetSpriteIds, sprite.id) then
  
-                 targetSprite = sprite
  
-             elseif table.contains(shootingBoxSpriteIds, sprite.id) then
  
-                 if sprite.x < screenCenterX then
  
-                     leftBox = sprite
  
-                 else
  
-                     rightBox = sprite
  
-                 end
  
-             end
  
-         end
  
-     end
  
  
-     _G.targetSprite = targetSprite
  
-     _G.leftBox = leftBox
  
-     _G.rightBox = rightBox
  
- end
  
-  
  
- local function handleInput()
  
-     local targetSprite = _G.targetSprite
  
-     local leftBox = _G.leftBox
  
-     local rightBox = _G.rightBox
  
  
-     if targetSprite and leftBox and rightBox then
  
-         local now = os.clock()
  
-         if now - lastInputTime > inputDelay / 60 then
  
-             local input = {
  
-                 ["left"] = false,  
  
-                 ["a"] = false
  
-             }
  
  
-             local diffLeft = math.abs(targetSprite.x - leftBox.x)
  
-             local diffRight = math.abs(targetSprite.x - rightBox.x)
  
  
-             emu.log("Target X: " .. targetSprite.x .. ", Left Box X: " .. leftBox.x .. ", Right Box X: " .. rightBox.x)
  
-             emu.log("Diff Left: " .. diffLeft)
  
-             emu.log("Diff Right: " .. diffRight)
  
  
-             if diffLeft <= 3 then
  
-                 input["left"] = true
  
-                 emu.log("Simulating Left press.")
  
-             elseif diffRight <= 3 then
  
-                 input["a"] = true
  
-                 emu.log("Simulating A press.")
  
-             end
  
  
-             emu.setInput(input, 0)
  
-             lastInputTime = now
  
-         end
  
-     end
  
-         emu.log("inputPolled is running.")
  
- end
  
  
  
- local endFrameCallbackId = emu.addEventCallback(processSprites, emu.eventType.endFrame)
  
- local inputPolledCallbackId = emu.addEventCallback(handleInput, emu.eventType.inputPolled)
  
  
- if not table.contains then
  
-     function table.contains(table, element)
  
-         for _, value in pairs(table) do
  
-             if value == element then
  
-                 return true
  
-             end
  
-         end
  
-         return false
  
-     end
  
- end
  
  复制代码 
                    
				     
				
		 
		 |