如何解决如何在俄罗斯方块中分别在图案的每个矩形上设置图像
我正在玩俄罗斯方块游戏here。我想在下降模式的每个单独的框中添加图片。
我稍微调整了render.js
文件,以便预加载图像,如果它是橙色图像,则将其添加,因此,我可以看到模式中的每个rect
都包含橙色图像,而不是橙色框
但是当我将createpattern
设置为repeat
时,图像以某种完整的样式显示,如果将createpattern
设置为no-repeat
,则图像没有显示出来。
有人可以帮我吗?
调整后的是 render.js :
var canvas = document.getElementsByTagName( 'canvas' )[ 0 ];
var ctx = canvas.getContext( '2d' );
var W = 300,H = 600;
var BLOCK_W = W / COLS,BLOCK_H = H / ROWS;
var img,ptrn;
function setColors(){
img = new Image();
img.src = 'img/wood.png';
img.onload = function(){
console.log('Inside orangeload');
// create pattern
ptrn = ctx.createPattern(orangeImg,'repeat'); // Create a pattern with this image,and set it to "repeat".
}
}
setColors();
// draw a single square at (x,y)
function drawBlock( x,y ) {
ctx.fillRect( BLOCK_W * x,BLOCK_H * y,BLOCK_W - 1,BLOCK_H - 1 );
ctx.strokeRect( BLOCK_W * x,BLOCK_H - 1 );
}
// draws the board and the moving shape
function render() {
ctx.clearRect( 0,W,H );
ctx.strokeStyle = 'black';
for ( var x = 0; x < COLS; ++x ) {
for ( var y = 0; y < ROWS; ++y ) {
if ( board[ y ][ x ] ) {
ctx.fillStyle = colors[ board[ y ][ x ] - 1 ];
drawBlock( x,y );
}
}
}
ctx.fillStyle = 'red';
ctx.strokeStyle = 'black';
for ( var y = 0; y < 4; ++y ) {
for ( var x = 0; x < 4; ++x ) {
if ( current[ y ][ x ] ) {
if((colors[board[ y ][ x ]-1]) == 'orange'){
ctx.fillStyle = ptrn;
}else{
ctx.fillStyle = colors[ board[ y ][ x ] - 1 ];
}
drawBlock( currentX + x,currentY + y );
}
}
}
}
解决方法
如果我正确理解您所需要的俄罗斯方块的另一种质地, 为此,您可以使用canvas drawImage函数
[0,10]
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import beta,expon,gamma,laplace,norm
x = norm.rvs(5,1.5,1000)
graph = plt.plot(x,norm.pdf(x,5,1.5))
在此示例中,我使用一个承诺来等待所有图像的加载,请参见: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。