所以我正在制作一个游戏,其中一个飞船移动到屏幕与PC控制器。 现在,我剩下的部分是使一个火球图像以精确的速度和数量随机下降屏幕(因为图像只有一个,我们必须乘以它)。 有人能做到这一点吗?
下面是代码:
火球图像:
<img src="Photo/fireball.png" id="fireball">
宇宙飞船图像:
<img src="Photo/Spaceship1.png" id="icon-p">
带控制器移动的飞船+防止它走出屏幕:
let display = document.getElementById("body");
let rect = icon;
let pos = { top: 1000, left: 570 };
const keys = {};
window.addEventListener("keydown", function(e) {
keys[e.keyCode] = true
});
window.addEventListener("keyup", function(e) {
keys[e.keyCode] = false
});
const loop = function() {
if (keys[37] || keys[81]) { pos.left -= 10; }
if (keys[39] || keys[68]) { pos.left += 10; }
if (keys[38] || keys[90]) { pos.top -= 10; }
if (keys[40] || keys[83]) { pos.top += 10; }
var owidth = display.offsetWidth;
var oheight = display.offsetHeight;
var iwidth = rect.offsetWidth;
var iheight = rect.offsetHeight;
if (pos.left < 0) { pos.left = -10; }
if (pos.top < 0) { pos.top = -10; }
if (pos.left + iwidth >= owidth) { pos.left = owidth - iwidth; }
if (pos.top + iheight >= oheight) { pos.top = oheight - iheight; }
rect.setAttribute("data", owidth + ":" + oheight);
rect.style.left = pos.left + "px";
rect.style.top = pos.top + "px";
};
let sens = setInterval(loop, 1000 / 60);
谢啦!
此解决方案包括三个可配置变量:spawnrate
,advancerate
和falldistance
。 它用它们来确定新的火球产生的频率,它们在屏幕上移动的频率,以及它们在每个“滴答声”上移动的距离。
脚本的“main”部分由两个setInterval
调用组成,一个用于处理产生新的火球,另一个用于处理将它们向屏幕下推进。
(请参阅代码内注释以获得更多解释。)
null
const
display = document.getElementById("display"), // Container element
fireballs = [], // Array to hold all fireball objects
fallDistance = 6; // Measured in `vh` units (but could be whatever)
spawnRate = 2000,
advanceRate = 500;
// Adds the first fireball immediately
spawnFireball(fireballs);
// Moves all fireballs down every 500 milliseconds
const advancerTimer = setInterval(
function(){ advanceAll(fireballs, fallDistance, display); },
advanceRate
);
// Spawns a new fireball every 2000 milliseconds
const spawnerTimer = setInterval(
function(){ spawnFireball(fireballs); },
spawnRate
);
// Defines a function to add a fireball to the array
function spawnFireball(fireballs){
const
img = document.createElement("img"), // Element to add to screen
x = Math.floor(Math.random() * 96) + 2, // Random `x` position
y = 3; // `y` position starts near top of screen
img.src = "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.UyMqod0eO6Qcmco1Zrmj0QAAAA%26pid%3DApi&f=1",
img.classList.add("fireball"); // To style fireballs
img.style.left = x + "vw"; // `x` position will never change
newFb = { x, y, img }; // `fb` object includes coords + img element
fireballs.push(newFb); // Adds the new fireball to the array
}
// Defines a function to advance a fireball's position
function advance(fb, distance){
fb.y += distance;
}
// Defines a function to draw a fireball in the container
function draw(fb, container){
if(fb.y > 100){ return; } // Ignores below-screen fireballs
fb.img.style.top = fb.y + "vh"; // Updates the location on screen
container.appendChild(fb.img); // The `img` property holds our DOM element
}
// Defines a function to advance and draw all fireballs
function advanceAll(fireballs, distance, container){
for(let fb of fireballs){
advance(fb, distance);
draw(fb, container)
}
}
#display{ height: 99vh; width: 99vw; position: relative; }
.fireball{ height: 2em; width: 2em; position: absolute; }
<div id="display"></div>
我想这会对你有所帮助:
null
js prettyprint-override">// Random X coordiante
function rndScreenX(offset) {
return Math.floor(Math.random() * (window.innerWidth - offset));
}
// Set fireball coordinates (X is random)
let fireballElement = document.querySelector('#fireball');
let fireball = {
x: rndScreenX(fireballElement.offsetWidth),
y: 0
}
const loop = function() {
// Change fireball Y
fireball.y += 10;
fireballElement.style.top = fireball.y + 'px';
if (fireball.y > window.innerHeight) {
// Fireball is out of window
// Reset Y and get new random X
fireball.x = rndScreenX(fireballElement.offsetWidth);
fireballElement.style.left = fireball.x + 'px';
fireball.y = 0;
}
};
fireballElement.style.left = fireball.x + 'px';
let sens = setInterval(loop, 1000 / 60);
#fireball {
position: absolute;
/* Ignore this rule if you're using an image */
width: 50px;
height: 50px;
background: red;
border-radius: 40% 40% 50% 50%;
}
<img src="Photo/fireball.png" id="fireball">
但是我想做的不是只有一个图像(火球)下屏,我想有一堆图像下屏。 以下是火球的代码: 和图像: 谢了!
抱歉我的问题,我被卡住了。我是lib gdx开发游戏的新手,不要严格评判我。我有我的游戏活动: } 我有两个屏幕: 还有我有暂停按钮的游戏屏幕: 还有我的主要问题--
您好,我正在中制作一个游戏,我想知道如何以及最好的方式是在屏幕上添加游戏。以下是玩家健康状况小于或等于0的代码: 我不确定该怎么做,因为我试图使用另一个py呼叫游戏,但玩家死亡的时间被重置为0并返回,所以玩家死亡的地方可能发生任何事情吗?
我目前正在编写一个游戏,你必须避免小行星。我不得不处理一些混乱的坐标,我不得不猜测精灵的坐标。我现在有了描述我的世界的任意单位。不幸的是,我的游戏屏幕不能完全工作。当我想渲染我的小行星时,游戏屏幕会显示一个黑屏。 上面显示的代码工作正常,并向我展示了以下内容: 当我在GameScreen类中添加小行星的渲染方法时,GameScreen只是黑色的: 小行星等级: 小行星等级:
我如何才能使它的应用程序是优化的所有屏幕?
问题内容: 现在,我正在Xcode 6中使用spritekit(swift)制作游戏。 它必须可在iPhone 4s及更高版本上播放。但据我所知,所有iPhone都来自4s和更高的Retina,所以我可以添加(750 x 1334像素)和@ 2x.png的所有图像。 因此,我不需要添加没有@ 2x的图像。 我对么? 问题答案: 示例:如果您具有512 x 512(1x)图像,并且希望它支持(2x和
我在 Libgdx 游戏上的触摸接收器有问题。 在我的玩家主页上,我有这个: 当我运行我的游戏时,除了触摸监听器有时工作正常,但是在触摸几秒钟后。 Gdx.app.log(“已点击”、“已触摸”);通过触摸发射。 但是Gdx.app.log("点击了","很棒");或者Gdx.app.log("Clicked "," nooooooooooo ");我必须执行长按,直到触摸被触发。 谢谢 我添加了
我想显示 3x3 大小的网格视图。我想根据设备大小设置高度和宽度。我从这个链接中获取参考。 主要活动- 活动_主要- 编辑- 就像首先得到屏幕高度和宽度,然后每个项目的高度和宽度是我得到的屏幕高度和宽度值的1/3。