gpt4 book ai didi

javascript - 在游戏中旋转 Canvas 中的单个图像

转载 作者:行者123 更新时间:2023-11-28 04:12:39 25 4
gpt4 key购买 nike

我正在尝试制作一个鸟类游戏,其中我仅旋转我通过游戏类绘制的单个鸟类图像(底部的最后一个函数)。有没有办法让我的小鸟在跳跃和下落时旋转三十度 Angular ?我不想只为我的鸟创建另一 block Canvas 。目前,除非我不在跳转功能中恢复,否则什么都不起作用。

class Bird extends Sprite {

constructor(options, game) {
super(options);
this.image = options.image;
this.sX = options.sX;
this.sY = options.sY;
this.sWidth = options.sWidth;
this.sHeight = options.sHeight;
this.dX = options.dX;
this.dY = options.dY;
this.dWidth = options.dWidth;
this.dHeight = options.dHeight;
this.frameIndex = 1;
this.tickCount = 0;
this.ticksPerFrame = 10;
this.numFrames = 8;
this.game = game;
this.birdRight = this.dX + this.dWidth;
}

jump(e, ctx) {

ctx.save();
ctx.translate(this.xPos + this.width / 2, this.yPos + this.height / 2);
ctx.rotate(this.dY * 15 * Math.PI / 360);
ctx.restore();
this.dY -= 40;
this.sX = 0;
}

fall(ctx) {
this.tickCount += 1;



if (this.tickCount > this.ticksPerFrame) {
this.sX = 27;
this.tickCount = 0;
}



if (this.dY < 450 ) {
this.dY += 1.5;
} else {
this.dY = this.dY;
}

// this.sX = (this.frameIndex * this.sWidth) / (this.numFrames);
}

draw(ctx) {
ctx.clearRect(0, 0, Game.DIM_X, Game.DIM_Y);
this.allObjects().forEach((object) => {
object.draw(ctx);
});
}

最佳答案

旋转图像

// img is an image, canvas, or video element
// x,y where on the canvas the image center will be
// angle is radians with positive in clockwise
function drawImageRotated(img,x,y,angle){
ctx.setTransform(1,0,0,1,x,y); // set position of image center
ctx.rotate(angle); // rotate
ctx.drawImage(img,-img.width/2,-img.height/2); // draw image offset so its center is at x,y
ctx.setTransform(1,0,0,1,0,0); // restore default transform
}

旋转、缩放和包含 Alpha 值

// Note the function does not restore. This improves the speed of this function
// if you call it many times in a row as SetTransform overwrite any existing transform
// alpha is a value from 0 - 1
// scaleX is scale in image X direction
// scaleY is scale in image Y Direction.
// image is rotated and scaled about its center
// center of image is located at canvas pixel coords x,y
function drawImage(img,x,y,scaleX,scaleY,angle,alpha){
if(!alpha) { return }
ctx.globalAlpha = alpha
ctx.setTransform(scaleX,0,0,scaleY,x,y); // set position of image center
ctx.rotate(angle); // rotate
ctx.drawImage(img,-img.width/2,-img.height/2); // draw image offset so its center is at x,y
}

// same as above with cx,cy as the point of rotation
// cx,cy are in image pixels
function drawImageCenterAt(img,x,y,cx,cy,scaleX,scaleY,angle,alpha){
if(!alpha) { return }
ctx.globalAlpha = alpha
ctx.setTransform(scaleX,0,0,scaleY,x,y); // set position of image center
ctx.rotate(angle); // rotate
ctx.drawImage(img,-cx,-cy); // draw image offset so its center is at cx,cy
}

所有这些功能可以在普通 PC/笔记本电脑上每 60 秒渲染大约 500-1000 个中等尺寸的图像

关于javascript - 在游戏中旋转 Canvas 中的单个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46134651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com