Effect_Players

本文深入探讨了游戏开发领域的关键技术,包括cocos2dX、unity3d、Unreal Engine等游戏引擎的使用,以及OpenGL ES滤镜、OpenCV图像处理等图像处理AR特效的应用实例。
  1 package mgzj.impls.common.view.component
2 {
3 import com.greensock.easing.Back;
4
5 import flash.display.Bitmap;
6 import flash.display.BitmapData;
7 import flash.display.Loader;
8 import flash.display.Sprite;
9 import flash.events.Event;
10 import flash.events.IOErrorEvent;
11 import flash.events.TimerEvent;
12 import flash.geom.Rectangle;
13 import flash.net.URLLoader;
14 import flash.utils.ByteArray;
15 import flash.utils.Timer;
16
17 import ma.resource.ResManager;
18 import ma.resource.data.ResInfo;
19
20 public class Effect_Players extends Sprite
21 {
22 private var frameNum:int=-1;
23 private var currentframeNum:int=0;
24
25 private var bdList:Array=[];
26
27 private var _byteData:ByteArray;
28
29 private var _bd:Bitmap;
30
31 private var old_pos:int=0;
32
33 public function Effect_Players()
34 {
35 _bd=new Bitmap;
36 this.addChild(_bd);
37 }
38
39 /**
40 *
41 * @param _name
42 *
43 */
44 public function startEffect(_name:String):void
45 {
46
47 ResManager.instance.loadDynaResource("godbless", _name, callBack);
48 }
49
50 private function callBack(e:Event, resinfo:ResInfo, data:*):void
51 {
52 if (e.type != Event.COMPLETE || data == null)
53 {
54 return;
55 }
56
57 _byteData=data as ByteArray;
58 _byteData.position=0;
59
60 frameNum=_byteData.readInt();
61
62 for(var i:int=0;i<frameNum;i++)
63 {
64 var imgint:int=_byteData.readInt();
65
66 var imageByte:ByteArray=new ByteArray;
67
68 _byteData.readBytes(imageByte, 0, imgint);
69
70 var loader:Loader=new Loader;
71 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
72 loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
73 loader.loadBytes(imageByte);
74 }
75
76 var timer:Timer=new Timer(50);
77 timer.addEventListener(TimerEvent.TIMER, onTimer);
78 timer.start();
79 }
80
81
82 private function onComplete(e:Event):void
83 {
84 var bd:Bitmap=e.target.content as Bitmap;
85 bdList.push(bd.bitmapData);
86
87 currentframeNum++;
88 }
89
90 private function onTimer(e:TimerEvent):void
91 {
92 _bd.bitmapData=bdList[e.target.currentCount % bdList.length]
93 }
94
95 private function onError(e:IOErrorEvent):void
96 {
97
98 }
99
100 }
101 }

转载于:https://www.cnblogs.com/ndljava/archive/2011/12/28/2305396.html

“#include <graphics.h> #include <conio.h> #include <vector> #include <algorithm> #include <ctime> #include <string> #include <cmath> using namespace std; // 游戏常量 const int WIDTH = 1200; const int HEIGHT = 800; const int PLAYER_MIN = 6; const int PLAYER_MAX = 10; const int MAX_N = 9; const int MIN_N = 2; const int THINKING_TIME = 9; // 9秒思考时间 // 牌的类型 enum CardType { EMPTY, // 空牌 BAOWEI, // 保位牌 KILL, // 杀人牌 CHANGE_POS, // 换位牌 CHANGE_N, // 换数牌 IMMUNE // 不死牌 }; // 玩家类 class Player { public: int id; // 玩家ID int position; // 位置 (1-n) CardType card; // 持有的牌 bool usedCard; // 是否已使用牌 bool alive; // 是否存活 bool baowei; // 是否有保位状态 bool immune; // 是否使用不死牌 int target; // 目标玩家 (用于杀人牌和换位牌) int newN; // 新的n值 (用于换数牌) Player(int id, int pos) : id(id), position(pos), card(EMPTY), usedCard(false), alive(true), baowei(false), immune(false), target(-1), newN(-1) {} // 重置状态 (每轮开始) void reset() { card = EMPTY; usedCard = false; baowei = false; immune = false; target = -1; newN = -1; } }; // 游戏类 class Game { private: vector<Player> players; // 玩家列表 int currentN; // 当前报数n值 int currentPlayer; // 当前报数的玩家索引 int round; // 当前回合 bool gameOver; // 游戏是否结束 int winner; // 获胜者ID // 牌堆 vector<CardType> deck; public: Game() : currentN(0), currentPlayer(0), round(1), gameOver(false), winner(-1) {} // 初始化游戏 void initialize(int playerCount) { players.clear(); deck.clear(); gameOver = false; round = 1; winner = -1; // 创建玩家 for (int i = 0; i < playerCount; i++) { players.push_back(Player(i + 1, i + 1)); } // 随机n值 currentN = rand() % (MAX_N - MIN_N + 1) + MIN_N; currentPlayer = 0; // 从第一个玩家开始 // 初始化牌堆 resetDeck(); } // 重置牌堆 void resetDeck() { deck.clear(); // 添加技能牌 deck.push_back(BAOWEI); deck.push_back(KILL); deck.push_back(CHANGE_POS); deck.push_back(CHANGE_N); deck.push_back(IMMUNE); // 添加空牌 int emptyCount = players.size() - 5; if (emptyCount > 0) { for (int i = 0; i < emptyCount; i++) { deck.push_back(EMPTY); } } // 洗牌 random_shuffle(deck.begin(), deck.end()); } // 发牌 void dealCards() { resetDeck(); for (int i = 0; i < players.size(); i++) { if (players[i].alive) { players[i].reset(); players[i].card = deck.back(); deck.pop_back(); } } } // 使用牌 void useCard(int playerIndex) { Player& player = players[playerIndex]; if (!player.alive || player.usedCard || player.card == EMPTY) return; player.usedCard = true; switch (player.card) { case BAOWEI: player.baowei = true; break; case KILL: // 随机选择一个目标玩家 (不是自己) do { player.target = rand() % players.size(); } while (player.target == playerIndex || !players[player.target].alive); break; case CHANGE_POS: // 随机选择一个目标玩家交换位置 do { player.target = rand() % players.size(); } while (player.target == playerIndex || !players[player.target].alive); // 交换位置 swap(players[playerIndex].position, players[player.target].position); break; case CHANGE_N: // 随机一个新的n值 player.newN = rand() % (MAX_N - MIN_N + 1) + MIN_N; break; case IMMUNE: player.immune = true; break; default: break; } } // 处理牌效果 void processCardEffects() { // 按顺序处理牌: 保位牌->杀人牌->换位牌->换数牌->不死牌 // 1. 保位牌 for (Player& p : players) { if (p.alive && p.usedCard && p.card == BAOWEI) { p.baowei = true; } } // 2. 杀人牌 for (Player& p : players) { if (p.alive && p.usedCard && p.card == KILL) { Player& target = players[p.target]; if (target.alive && !target.baowei) { target.alive = false; } } } // 3. 换位牌 (已经在useCard阶段处理) // 4. 换数牌 for (Player& p : players) { if (p.alive && p.usedCard && p.card == CHANGE_N) { currentN = p.newN; } } // 5. 不死牌 (在报数阶段处理) } // 报数 bool count() { if (gameOver) return true; // 找到下一个存活的玩家 int count = 0; int startPlayer = currentPlayer; bool counted = false; while (!counted) { if (players[currentPlayer].alive) { count++; // 如果达到n if (count == currentN) { Player& p = players[currentPlayer]; // 如果使用了不死牌 if (p.immune) { p.immune = false; // 消耗不死牌 // 停止报数,下一轮从当前玩家开始 currentPlayer = (currentPlayer + 1) % players.size(); return false; } // 否则淘汰玩家 else { p.alive = false; // 检查游戏是否结束 if (checkGameOver()) { return true; } // 下一轮从淘汰玩家的下一位开始 currentPlayer = (currentPlayer + 1) % players.size(); return false; } } } // 移动到下一个玩家 currentPlayer = (currentPlayer + 1) % players.size(); // 防止无限循环 if (currentPlayer == startPlayer) { break; } } return false; } // 检查游戏是否结束 bool checkGameOver() { int aliveCount = 0; int lastAlive = -1; for (int i = 0; i < players.size(); i++) { if (players[i].alive) { aliveCount++; lastAlive = i; } } if (aliveCount == 1) { gameOver = true; winner = players[lastAlive].id; return true; } return false; } // 开始下一轮 void nextRound() { round++; dealCards(); processCardEffects(); } // 获取玩家列表 vector<Player>& getPlayers() { return players; } // 获取当前n值 int getCurrentN() const { return currentN; } // 获取当前回合 int getRound() const { return round; } // 获取当前玩家索引 int getCurrentPlayer() const { return currentPlayer; } // 游戏是否结束 bool isGameOver() const { return gameOver; } // 获取获胜者 int getWinner() const { return winner; } // 获取存活玩家数量 int getAliveCount() const { int count = 0; for (const Player& p : players) { if (p.alive) count++; } return count; } }; // 渲染玩家 void renderPlayers(const vector<Player>& players, int currentPlayer, int centerX, int centerY, int radius) { const int playerRadius = 40; const int count = players.size(); const double angleStep = 2 * 3.1415926535 / count; // 渲染玩家 for (int i = 0; i < count; i++) { const Player& p = players[i]; double angle = i * angleStep - 3.1415926535 / 2; // 从顶部开始 int x = centerX + static_cast<int>(radius * cos(angle)); int y = centerY + static_cast<int>(radius * sin(angle)); // 设置玩家颜色 COLORREF color; if (!p.alive) { color = RGB(100, 100, 100); // 灰色表示淘汰 } else if (i == currentPlayer) { color = RGB(255, 215, 0); // 金色表示当前玩家 } else { color = HSVtoRGB(static_cast<float>(i) / count * 360, 0.8f, 0.9f); } // 绘制玩家圆圈 setfillcolor(color); setlinecolor(BLACK); fillcircle(x, y, playerRadius); // 绘制玩家ID setbkmode(TRANSPARENT); settextcolor(BLACK); char idText[10]; sprintf(idText, "%d", p.id); RECT r = {x - 20, y - 10, x + 20, y + 10}; drawtext(idText, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE); // 绘制位置 sprintf(idText, "Pos:%d", p.position); r.top += 20; r.bottom += 20; drawtext(idText, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE); // 绘制状态图标 if (p.baowei) { setfillcolor(RGB(0, 255, 0)); solidcircle(x - 15, y - 35, 8); } if (p.immune) { setfillcolor(RGB(0, 0, 255)); solidcircle(x + 15, y - 35, 8); } } } // 渲染牌 void renderCard(CardType card, int x, int y, bool isFaceUp = true) { const int cardWidth = 80; const int cardHeight = 120; if (!isFaceUp) { // 牌背面 setfillcolor(RGB(200, 30, 30)); setlinecolor(RGB(150, 20, 20)); fillrectangle(x - cardWidth/2, y - cardHeight/2, x + cardWidth/2, y + cardHeight/2); return; } // 牌正面 setfillcolor(WHITE); setlinecolor(BLACK); fillrectangle(x - cardWidth/2, y - cardHeight/2, x + cardWidth/2, y + cardHeight/2); rectangle(x - cardWidth/2, y - cardHeight/2, x + cardWidth/2, y + cardHeight/2); // 牌类型文本 const char* cardText = ""; COLORREF textColor = BLACK; switch (card) { case BAOWEI: cardText = "保位"; textColor = RGB(0, 150, 0); break; case KILL: cardText = "杀人"; textColor = RGB(200, 0, 0); break; case CHANGE_POS: cardText = "换位"; textColor = RGB(150, 0, 150); break; case CHANGE_N: cardText = "换数"; textColor = RGB(0, 0, 200); break; case IMMUNE: cardText = "不死"; textColor = RGB(0, 100, 200); break; case EMPTY: cardText = "空牌"; textColor = RGB(100, 100, 100); break; } settextcolor(textColor); setbkmode(TRANSPARENT); RECT r = {x - cardWidth/2, y - cardHeight/2, x + cardWidth/2, y + cardHeight/2}; drawtext(cardText, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE); } // 渲染游戏状态 void renderGameInfo(const Game& game, int timeLeft) { // 渲染回合信息 char roundText[50]; sprintf(roundText, "回合: %d", game.getRound()); settextcolor(BLACK); settextstyle(24, 0, _T("宋体")); outtextxy(50, 50, roundText); // 渲染n值 char nText[50]; sprintf(nText, "当前 n: %d", game.getCurrentN()); outtextxy(50, 90, nText); // 渲染存活玩家数量 char aliveText[50]; sprintf(aliveText, "存活玩家: %d", game.getAliveCount()); outtextxy(50, 130, aliveText); // 渲染倒计时 if (timeLeft >= 0) { char timeText[50]; sprintf(timeText, "思考时间: %d秒", timeLeft); settextcolor(RGB(200, 0, 0)); outtextxy(WIDTH - 200, 50, timeText); // 绘制进度条 float progress = timeLeft / static_cast<float>(THINKING_TIME); setfillcolor(RGB(static_cast<int>(255 * (1 - progress)), static_cast<int>(255 * progress), 0)); fillrectangle(WIDTH - 220, 90, WIDTH - 220 + static_cast<int>(200 * progress), 120); setlinecolor(BLACK); rectangle(WIDTH - 220, 90, WIDTH - 20, 120); } // 渲染游戏结束 if (game.isGameOver()) { settextcolor(RGB(200, 0, 0)); settextstyle(48, 0, _T("宋体")); char winText[100]; sprintf(winText, "玩家 %d 获胜!", game.getWinner()); int textWidth = textwidth(winText); int x = (WIDTH - textWidth) / 2; int y = HEIGHT / 2 - 50; outtextxy(x, y, winText); } } // 渲染玩家手牌 void renderPlayerCards(const vector<Player>& players, int centerX, int centerY, int radius) { const int count = players.size(); const double angleStep = 2 * 3.1415926535 / count; for (int i = 0; i < count; i++) { const Player& p = players[i]; if (!p.alive) continue; double angle = i * angleStep - 3.1415926535 / 2; // 从顶部开始 angle += angleStep / 2; // 偏移到玩家之间的位置 int x = centerX + static_cast<int>((radius + 80) * cos(angle)); int y = centerY + static_cast<int>((radius + 80) * sin(angle)); // 渲染牌 renderCard(p.card, x, y, true); // 显示使用状态 if (p.usedCard) { settextcolor(RGB(0, 150, 0)); settextstyle(20, 0, _T("宋体")); outtextxy(x - 30, y - 70, "已使用"); } } } // 主函数 int main() { // 初始化图形窗口 initgraph(WIDTH, HEIGHT); setbkcolor(RGB(240, 240, 240)); cleardevice(); // 设置随机种子 srand(static_cast<unsigned int>(time(nullptr))); // 创建游戏 Game game; game.initialize(PLAYER_MIN + rand() % (PLAYER_MAX - PLAYER_MIN + 1)); // 游戏主循环 while (true) { // 处理退出事件 if (_kbhit()) { int key = _getch(); if (key == 27) { // ESC键退出 break; } else if (key == 'r' || key == 'R') { // R键重新开始 game.initialize(PLAYER_MIN + rand() % (PLAYER_MAX - PLAYER_MIN + 1)); } } // 清屏 cleardevice(); // 游戏逻辑 if (!game.isGameOver()) { // 思考阶段 (9秒) for (int i = THINKING_TIME; i >= 0; i--) { // 清屏 cleardevice(); // 渲染玩家 renderPlayers(game.getPlayers(), game.getCurrentPlayer(), WIDTH/2, HEIGHT/2, 250); // 渲染玩家手牌 renderPlayerCards(game.getPlayers(), WIDTH/2, HEIGHT/2, 250); // 渲染游戏信息 renderGameInfo(game, i); // 延迟1秒 Sleep(1000); // 随机使用牌 for (int j = 0; j < game.getPlayers().size(); j++) { if (game.getPlayers()[j].alive && !game.getPlayers()[j].usedCard) { if (rand() % 3 == 0) { // 1/3概率使用牌 game.useCard(j); } } } } // 处理牌效果 game.processCardEffects(); // 报数阶段 game.count(); // 如果游戏未结束,进入下一轮 if (!game.isGameOver()) { game.nextRound(); } } // 渲染最终状态 cleardevice(); renderPlayers(game.getPlayers(), game.getCurrentPlayer(), WIDTH/2, HEIGHT/2, 250); renderPlayerCards(game.getPlayers(), WIDTH/2, HEIGHT/2, 250); renderGameInfo(game, -1); // 延迟一段时间 Sleep(2000); } closegraph(); return 0; }”改善代码 游戏规则:6-10名玩家围成一圈为一组,每名玩家抽取顺序牌决定自己位置,随机抽报数牌决定n,2< =n< = 9,根据剩余玩家数量发牌,一人一张,五张技能牌,剩余牌为空,用牌前,可思考计算9秒并公开手牌(注:先用保位牌,再用杀人牌,再用换位牌,再用换数牌,最后用不死牌,可弃权)(保位牌:自身位置不可被交换,不被杀人牌杀死;杀人牌:指定淘汰一个玩家;换位牌:改变自身位置;换数牌:改变报数n;不死牌:本轮报数不被淘汰),报到n的玩家出局,再根据剩余玩家重新数量发牌,用牌,从淘汰的人的后一位开始报数,依次循环,止至只剩一个人(在使用技能牌阶段也可),此人便为胜利者,规则明确:如果报到n的玩家有不死牌,不会继续报下一个玩家
07-29
BallisticCalc = require("BallisticCalc") Basalt = require("basalt") CoordTransformer = require("CoordTransformer") Quaternion = require("Quaternion") Track = require("Track") Util = require("Util") Vector3d = require("Vector3d") -- 系统初始化 -- local targetpos = { x = 0, y = 0, z = 0 } local Cannon_Config = { Charge = 6, length = 18, G = -0.05, Drag = 0.01, Dep = -5, Ele = 90, Tol = 0.01, Mode = 3 } local selfpos = coord.getCoord() local q = peripheral.wrap("front").getRotation() local vel = ship.getVelocity() local i = 1 local rotation_matrix = { x = Vector3d:new(1, 0, 0), y = Vector3d:new(0, 1, 0), z = Vector3d:new(0, 0, 1) } local cannon = peripheral.wrap("back") local getPlayers = coord.getPlayersAll(50) local players = {} cannon.assemble() local hasTargrt = false local current_position = Vector3d:new( -1 , 0 , 0 ) local err_yaw, err_pitch local yaw_control = peripheral.wrap("Create_RotationSpeedController_0") local pitch_control = peripheral.wrap("Create_RotationSpeedController_1") local yaw_speed, pitch_speed = 0, 0 local mode = 1 local controller = "AkashiSCO" -- GUI初始化函数 -- local function createGUI() local MainFrame = Basalt.getMainFrame() -- 目标坐标显示组件 local targetXGui = MainFrame:addTextBox({ editable = false , background = colors.white , foreground = colors.red}):setPosition(4, 1):setSize(13, 1):setText(" TargetX"); local targetYGui = MainFrame:addTextBox({ editable = false , background = colors.white , foreground = colors.green}):setPosition(20, 1):setSize(13, 1):setText(" TargetY"); local targetZGui = MainFrame:addTextBox({ editable = false , background = colors.white , foreground = colors.blue}):setPosition(36, 1):setSize(13, 1):setText(" TargetZ"); -- 玩家选择控制组件 local TargetController = MainFrame:addTextBox({ editable = false , background = colors.white , foreground = colors.black}):setPosition(16, 2):setSize(20, 1):setText( " control_player"); local buttomleft = MainFrame:addButton({ text = "<<--" , background = colors.white , foreground = colors.black }):setPosition(11, 2):setSize(4, 1) local buttomright = MainFrame:addButton({ text = "-->>" , background = colors.white , foreground = colors.black }):setPosition(36, 2):setSize(4, 1) -- 火炮配置组件 local Charge_text = MainFrame:addTextBox():setPosition(2, 4):setSize(10, 1):setText("Charge:" .. Cannon_Config.Charge) local length_text = MainFrame:addTextBox():setPosition(15, 4):setSize(10, 1):setText("length:" ..Cannon_Config.length) local Dep_text = MainFrame:addTextBox():setPosition(27, 4):setSize(10, 1):setText("Dep:" .. Cannon_Config.Dep) local Ele_text = MainFrame:addTextBox():setPosition(40, 4):setSize(10, 1):setText("Ele:" .. Cannon_Config.Ele) local G_text = MainFrame:addTextBox():setPosition(27, 5):setSize(10, 1):setText("G:" .. Cannon_Config.G) local Drag_text = MainFrame:addTextBox():setPosition(15, 5):setSize(10, 1):setText("Drag:" .. Cannon_Config.Drag) local reload_buttom = MainFrame:addButton({ text = "Apply Setting" }):setPosition(18, 6):setSize(13, 1) return { targetXGui = targetXGui, targetYGui = targetYGui, targetZGui = targetZGui, TargetController = TargetController, buttomleft = buttomleft, buttomright = buttomright, reload_buttom = reload_buttom, Charge_text = Charge_text, length_text = length_text, Dep_text = Dep_text, Ele_text = Ele_text, G_text = G_text, Drag_text = Drag_text } end -- 初始化GUI并获取组件引用 -- local guiElements = createGUI() -- 玩家列表处理 -- local function getPlayerslist() i = 1 for _, v1 in pairs(getPlayers) do for k, v2 in pairs(v1) do if k == "name" then players[i] = v2 i = i + 1 end end end i = 1 end getPlayerslist() -- 工具函数 -- local function calcuspaces(max_length, text) local text_length = string.len(text) if text_length >= max_length then return "" end local total_spaces = max_length - text_length local left_spaces = math.floor(total_spaces / 2) return string.rep(" ", left_spaces) .. text end local function getChatBoxContent(chatMessage) if string.find(chatMessage, "xaero%-waypoint") then local chatLocation = {} for word in string.gmatch(chatMessage, "[%~%-%d]+") do table.insert(chatLocation, word) end if chatLocation[3] == nil then chatLocation[3] = 69 end if #chatLocation >= 4 then return { x = tonumber(chatLocation[2]), y = tonumber(chatLocation[3]), z = tonumber(chatLocation[4]) } end end return nil end local function normalize_angle_diff(angle) local normalized = angle % 360 if normalized > 180 then normalized = normalized - 360 elseif normalized < -180 then normalized = normalized + 360 end return normalized end local function extract_number(input_str) -- 匹配字符串中任意位置的第一个连续数字 local number_str = input_str:match("[-+]?%d*%.?%d+") -- 加强类型转换校验 return tonumber(number_str) or 0 end --计算目标相对于自身的速度 local function calculate_relative_velocity( self_world_velocity, -- 自身世界速度 (Vector3d) self_quaternion, -- 自身四元数 (Quaternion) target_world_velocity -- 目标世界速度 (Vector3d) ) -- 步骤1:计算相对世界速度 local relative_world_vel = target_world_velocity - self_world_velocity -- 步骤2:应用四元数旋转 local rotated_velocity = self_quaternion:conj():rotate(relative_world_vel) return rotated_velocity end -- 按钮事件处理 -- guiElements.buttomleft:onClick(function() i = i - 1 if i < 1 then i = #players end local len = #players[i] local SpaceRep = string.rep(" ", math.floor((19 - len) / 2)) guiElements.TargetController:setText(SpaceRep .. players[i]) controller = players[i] end) guiElements.buttomright:onClick(function() i = i + 1 if i > #players then i = 1 end local len = #players[i] local SpaceRep = string.rep(" ", math.floor((19 - len) / 2)) guiElements.TargetController:setText(SpaceRep .. players[i]) controller = players[i] end) guiElements.reload_buttom:onClick(function() Cannon_Config.Charge = extract_number(guiElements.Charge_text:getText()) Cannon_Config.length = extract_number(guiElements.length_text:getText()) Cannon_Config.Drag = extract_number(guiElements.Drag_text:getText()) Cannon_Config.Dep = extract_number(guiElements.Dep_text:getText()) Cannon_Config.Ele = extract_number(guiElements.Ele_text:getText()) Cannon_Config.G = extract_number(guiElements.G_text:getText()) end) -- 目标获取函数 -- local function getTarget() while true do local _, user, msg = os.pullEvent("chat") local target = getChatBoxContent(msg) if target ~= nil and user == controller then targetpos = Vector3d:new( target.x , target.y or 69 , target.z) hasTargrt = true local x_text = calcuspaces(13, "X=" .. targetpos.x) local y_text = calcuspaces(13, "Y=" .. targetpos.y) local z_text = calcuspaces(13, "Z=" .. targetpos.z) guiElements.targetXGui:setText(x_text) guiElements.targetYGui:setText(y_text) guiElements.targetZGui:setText(z_text) end end end -- 控制逻辑函数 -- local function calc_speed_t(target) local speed = target / 0.32222222222222222 return math.min(math.max(speed, -48), 48) end local function isdisassemble() while true do os.pullEvent("redstone") if redstone.getInput("top") then cannon.disassemble() else cannon.assemble() end end end -- 主控制逻辑 -- local function Main() repeat os.sleep(0.5) until hasTargrt == true while true do selfpos = coord.getCoord() q = peripheral.wrap("front").getRotation() q = Quaternion:new( q.x , q.y , q.z , q.w ) vel = ship.getVelocity() local speed = Vector3d:new( vel.x , vel.y , vel.z ) local targetspeed = Vector3d:new( 0 , 0 , 0 ) local target_relative = CoordTransformer.InverseCoord(selfpos, targetpos, q) local relative_Targrtspeed = calculate_relative_velocity( speed , q , targetspeed) local relative_targetpos = Vector3d:new( target_relative.x - current_position.x, target_relative.y - current_position.y, target_relative.z - current_position.z) --local Yaw, Dist_xz = BallisticCalc.calc_Yaw_and_Dist_xz(relative_targetpos, rotation_matrix, 90) --Yaw = Util:setInScope(Yaw) --local t_y = targetpos.y - selfpos.y - 1 --local Pitch , time = BallisticCalc.customcalc(Dist_xz, t_y, Cannon_Config.Charge, Cannon_Config.length, Cannon_Config.G,Cannon_Config.Drag, Cannon_Config.Dep, Cannon_Config.Ele, Cannon_Config.Tol, mode) local fire_solution = BallisticCalc.calc_leading_shot( relative_targetpos, relative_Targrtspeed, { n = Cannon_Config.Charge , k = Cannon_Config.length , g = Cannon_Config.G , drag = Cannon_Config.Drag}, { a_min = Cannon_Config.Dep , a_max = Cannon_Config.Ele }, 500, 0.001, 90 ) local Yaw = Util:setInScope(fire_solution.yaw) local Pitch = fire_solution.pitch if mode == 3 then Pitch = Pitch.high or Pitch.low or 0 else Pitch = Pitch or 0 end local now_yaw = cannon.getYaw() local now_pitch = cannon.getPitch() err_yaw = normalize_angle_diff(Yaw - now_yaw) err_pitch = normalize_angle_diff(Pitch - (now_pitch or 0)) yaw_speed = calc_speed_t(err_yaw) pitch_speed = -calc_speed_t(err_pitch) os.sleep(0.05) end end -- 执行函数 -- local function setYawspeed() repeat os.sleep(0.5) until hasTargrt == true while true do os.sleep(0.05) if math.abs(yaw_speed) > 1 or math.abs(err_yaw) > 1 then yaw_control.setTargetSpeed(yaw_speed) else yaw_control.setTargetSpeed(0) if Yaw then cannon.setYaw(Yaw) end end end end local function setpitchspeed() repeat os.sleep(0.5) until hasTargrt == true while true do os.sleep(0.05) if math.abs(pitch_speed) > 1 or math.abs(err_pitch) > 1 then pitch_control.setTargetSpeed(pitch_speed) else pitch_control.setTargetSpeed(0) if Pitch then cannon.setPitch(Pitch) end end end end -- 启动所有并行任务 -- parallel.waitForAll( Main, setYawspeed, setpitchspeed, getTarget, isdisassemble, Basalt.run ) 那么这个代码有没有用到呢
06-03
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值