jeremyjia / Games

Home Page Link:
https://jeremyjia.github.io/Games/
2 stars 7 forks source link

增加精灵对象 #1063

Open jeremyjia opened 2 weeks ago

jeremyjia commented 2 weeks ago
jeremyjia commented 2 weeks ago

//精灵对象之滑翔机插件 commentID:2169344009 var canvas; var ctx; var width = 300; var height = 200; var time = 0;

var t = 0; var gridColor = 'red'; var gridSize = 20; // 每个格子的大小 var gridWidth = 30; var gridHeight = 30; var grid = []; //initGrid(); function animateFrame2() { if(time==0){ initGrid(); } time++; canvas = document.getElementById('myCanvas'); ctx = canvas.getContext('2d'); //ctx.clearRect(0, 0, 2048, 1536);

nextGeneration(ctx);

ctx.fillStyle = "red";
ctx.font = "50px Arial";
ctx.fillText("滑翔机演示!", 300, 700);

}

function drawGrid(ctx) { for (var y = 0; y < gridHeight; y++) { for (var x = 0; x < gridWidth; x++) { if (grid[y][x]) { ctx.fillStyle = gridColor; ctx.fillRect(x gridSize, y gridSize, gridSize, gridSize); }else{ ctx.fillStyle = 'lightblue'; ctx.fillRect(x gridSize, y gridSize, gridSize, gridSize); } } } }

function initGrid() { for (var i = 0; i < gridHeight; i++) { grid[i] = []; for (var j = 0; j< gridWidth; j++) { grid[i][j] = false; } }

// 初始化滑翔机模式

var arrayNum = [-2, -1, 0, 1, 2, 3]; var arrayColor = ['red', 'green', 'blue', 'yellow', 'black', 'orange']; var k = arrayNum[getRandomInt(0, 5)]; grid[2+k][3+k] = true; grid[3+k][4+k] = true; grid[4+k][2+k] = true; grid[4+k][3+k] = true; grid[4+k][4+k] = true; gridColor = arrayColor[k]; }

function countNeighbors(x, y) { var count = 0; for (var i = -1; i <= 1; i++) { for (var j = -1; j <= 1; j++) { var nx = x + i; var ny = y + j; if ( nx >= 0 && nx < gridWidth && ny >= 0 && ny < gridHeight && !(i === 0 && j === 0) && grid[ny][nx] ) { count++; } } } return count; }

function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; }

function nextGeneration(ctx) { var nextGrid = []; for (var i = 0; i < gridHeight; i++) { nextGrid[i] = []; for (var j = 0; j < gridWidth; j++) { nextGrid[i][j] = grid[i][j]; } }

for (var y = 0; y < gridHeight; y++) {
    for (var x = 0; x < gridWidth; x++) {
        var neighbors = countNeighbors(x, y);
        if (grid[y][x]) {
            if (neighbors < 2 || neighbors > 3) {
                nextGrid[y][x] = false; // 死亡
            }
        } else {
            if (neighbors === 3) {
                nextGrid[y][x] = true; // 诞生
            }
        }
    }
}
//grid = nextGrid;
for (var i = 0; i < gridHeight; i++) {
    for (var j = 0; j < gridWidth; j++) {
        grid[i][j] = nextGrid[i][j];
    }
}

drawGrid(ctx);

}

jeremyjia commented 2 weeks ago

//精灵对象 commentID:2169377464 var canvas; var ctx; var width = 300; var height = 200; var time = 0; function animateFrame() { time++; canvas = document.getElementById('myCanvas'); ctx = canvas.getContext('2d'); //ctx.clearRect(0, 0, 2048, 1536); ctx.fillStyle = "red"; ctx.font = "40px Arial"; var x1 = (time+200)%width; ctx.fillText("动画演示!", x1, height/2); ctx.fillStyle = "yellow"; var y1 = (time+200)%height; ctx.fillRect(30, y1, 60, 60); var myDate = new Date(); var strTime = myDate.toLocaleString(); ctx.fillStyle = "Blue"; ctx.fillText(strTime, 10, height/3); }

jeremyjia commented 2 weeks ago

{ "request": { "version": "0.0.26", "description":"sprite-test", "width": 1024, "height": 768, "music": "gydgs.mp3", "rate": "2", "frames": [ { "number": "1", "time": "110", "objects": [ { "text": "${VAR_TITLE}", "x": 200, "y": 300, "size": 70, "color": "105,55,25", "layer": 2 }, { "graphic": "sprite", "attribute": { "script": "commentID:2169344009", "function": "animateFrame2" } } ], "backgroundColor": "180,180,180" } ], "Macros": [ { "name": "VAR_TITLE", "value": "精灵对象测试1063" }
] } }

jeremyjia commented 2 weeks ago

{ "request": { "version": "0.0.27", "description":"sprite-two-dif-sprite", "width": 1024, "height": 768, "music": "gydgs.mp3", "rate": "2", "frames": [ { "number": "1", "time": "110", "objects": [ { "text": "${VAR_TITLE}", "x": 200, "y": 300, "size": 70, "color": "105,55,25", "layer": 2 }, { "graphic": "sprite", "attribute": { "script": "commentID:2169344009", "function": "animateFrame2" }, "layer": 2 }, { "graphic": "sprite", "attribute": { "script": "commentID:2169377464", "function": "animateFrame" }, "layer": 2 } ], "backgroundColor": "180,180,180" } ], "Macros": [ { "name": "VAR_TITLE", "value": "多个精灵对象测试1063" }
] } }

jeremyjia commented 2 weeks ago

{ "request": { "version": "0.0.27", "description":"sprite-fly", "width": 1024, "height": 768, "music": "gydgs.mp3", "rate": "2", "frames": [ { "number": "1", "time": "110", "objects": [ { "text": "${VAR_TITLE}", "x": 200, "y": 300, "size": 70, "color": "105,55,25", "layer": 2 }, { "graphic": "sprite", "attribute": { "script": "commentID:2183978998", "function": "animateFrame2183978998" }, "layer": 1 } ], "backgroundColor": "180,180,180" } ], "Macros": [ { "name": "VAR_TITLE", "value": "精灵对象测试1063" }
] } }

jeremyjia commented 1 week ago

//2183978998 var width = 600; var height = 400; var canvas; var ctx;

function animateFrame2183978998(){ canvas = document.getElementById('myCanvas'); ctx = canvas.getContext('2d'); drawCylinder(ctx, 500, 400, 0, 50, 200, 30); }

function drawCylinder(ctx, centerX, centerY, z, radius, height, numSlices) { // 绘制顶面 ctx.beginPath(); ctx.fillStyle = 'blue'; //顶面颜色 ctx.arc(centerX, centerY + height / 2, radius, 0, Math.PI * 2, false); ctx.fill(); ctx.closePath();

// 绘制底面
ctx.beginPath();
ctx.fillStyle = 'lightblue'; // 底面颜色,比顶面稍浅
ctx.arc(centerX, centerY - height / 2, radius, 0, Math.PI * 2, false);
ctx.fill();
ctx.closePath();

// 绘制侧面
ctx.beginPath();
ctx.strokeStyle = 'yellow'; 
ctx.moveTo(centerX + radius, centerY + height / 2); // 从顶面右侧开始

for (var i = 0; i < numSlices; i++) {
    var theta = (i / numSlices) * Math.PI * 2; // 计算角度
    var x = centerX + radius * Math.cos(theta); // 计算x坐标
    var y = centerY + height / 2 - (radius * Math.sin(theta) * height) / (2 * radius); // 计算y坐标,使用简单的透视近似
    ctx.lineTo(x, y); // 绘制到侧面
    ctx.lineTo(x, y - height); // 绘制到底面
}
ctx.closePath(); // 闭合路径(但这里不填充,因为我们使用线条)

// 绘制侧面线条
ctx.strokeStyle = 'black'; // 侧面线条颜色
ctx.lineWidth = 2; // 线条粗细
ctx.stroke(); // 绘制线条

}