Javascript 船舶不显示帆布游戏
我正试图开发一个简单的画布游戏,但我目前正致力于让敌人展示自己 以下是我使用的代码:Javascript 船舶不显示帆布游戏,javascript,html,canvas,Javascript,Html,Canvas,我正试图开发一个简单的画布游戏,但我目前正致力于让敌人展示自己 以下是我使用的代码: <html lang="en-us"> <head> <meta http-equiv="content-type" content="text/html" charset="utf-8"> <title>Cyber Space War</title> </head> <body style="margin:10px">
<html lang="en-us">
<head>
<meta http-equiv="content-type" content="text/html" charset="utf-8">
<title>Cyber Space War</title>
</head>
<body style="margin:10px">
<canvas id="canvas" style="background:#111;"></canvas>
<script type="text/javascript">
// SETUP INICIAL
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
var innerWidth = 360,
innerHeight = 620;
canvas.width = innerWidth;
canvas.height = innerHeight;
// VARIAVEIS
var score = 0,
lastTime = 0;
// TECLAS DE MOVIMENTAÇÃO
window.onkeydown = pressionaTecla;
function pressionaTecla(tecla){
if(tecla.keyCode == 38) {
player.y = player.y - 10;
}
if(tecla.keyCode == 40) {
player.y = player.y + 10;
}
if(tecla.keyCode == 39) {
player.x = player.x + 10;
}
if(tecla.keyCode == 37) {
player.x = player.x - 10;
}
}
// PERSONALIZAÇÃO DO PLAYER
var player = { },
player_width = 100,
player_height = 105,
player_img = new Image();
player_img.src = 'images/spaceship.png';
// OBJETO DO PLAYER
player = {
width : player_width,
height: player_height,
x : innerWidth/2 - player_width/2, // centralizar
y: innerHeight - (player_height+10), //deixar em baixo
power : 10,
draw: function(){
// FUNÇÃO QUE BLOQUEIA O OBJETO PLAYER SAIR DO CANVAS
if(this.x <= 0 ){
this.x = 0;
}else if (this.x >= (innerWidth - this.width)) {
this.x = (innerWidth - this.width);
}
if(this.y <= 0 ){
this.y = 0;
}else if (this.y >= (innerHeight - this.height)) {
this.y = (innerHeight - this.height);
}
ctx.drawImage(player_img, this.x, this.y, this.width, this.height);
}
};
// FUNDO DE GALAXIA *codigo fonte do fundo retirado do site codepen.io https://codepen.io/LeonGr/pen/fdCsI
var stars = [], // Array that contains the stars
FPS = 60, // Frames per second
x = canvas.width; // Number of stars
for (var i = 0; i < x; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
radius: Math.random(),
vx: Math.floor(Math.random() * 10) - 5,
vy: Math.floor(Math.random() * 10) - 5
});
}
function updatefundo() {
for (var i = 0, x = stars.length; i < x; i++) {
var s = stars[i];
s.x += s.vx / FPS;
s.y += s.vy / FPS;
if (s.x < 0 || s.x > canvas.width) s.x = -s.x;
if (s.y < 0 || s.y > canvas.height) s.y = -s.y;
}
}
function drawfundo() {
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.globalCompositeOperation = "lighter";
for (var i = 0, x = stars.length; i < x; i++) {
var s = stars[i];
ctx.fillStyle = "#fff";
ctx.beginPath();
ctx.arc(s.x, s.y, s.radius, 0, 2 * Math.PI);
ctx.fill();
}
}
// PERSONALIZAÇÃO DO INIMIGO
var enemyArray = [],
enemyIndex = 0,
enemy_width = 35,
enemy_height = 43,
enemy_timer = 1000,
enemy_img = new Image ();
enemy_img.src = 'images/spaceship.png';
// OBJETO DO INIMIGO
function enemy (x, y, dx, dy, enemy_img, enemy_width, enemy_height, rotation){
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.img = enemy_img;
this.width = enemy_width;
this.height = enemy_height;
this.rotation = rotation;
enemyIndex++;
enemyArray[enemyIndex] = this;
this.id = enemyIndex;
ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
this.update = function(){
this.y+= this.dy;
this.x+= this.dx;
this.draw();
}
this.delete = function(){
delete enemyArray[this.id];
}
this.draw = function(){
ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
}
}
// FUNÇÃO DE CRIAR INIMIGOS
function create_enemy(){
var x = Math.random() * (innerWidth - enemy_width);
var y = -enemy_height;
var dx = 3;
var dy = 3;
var rotation = Math.random();
new enemy (x, y, dx, dy, enemy_img, enemy_width, enemy_height, rotation);
}
// LOOPING DA ANIMAÇAO (MAINFRAME DO GAME)
function gameLoop(currentTime){
requestAnimationFrame(gameLoop);
ctx.clearRect (0,0, canvas.width, canvas.height);
drawfundo();
updatefundo();
// SCORE
ctx.font = '17px arial';
ctx.fillStyle = '#fff';
ctx.fillText('PONTOS: '+score , 15, 30);
// ENERGIA
ctx.font = '17px arial';
ctx.fillStyle = '#fff';
ctx.fillText('ENERGIA '+player.power , innerWidth-110, 30);
// JOGADOR
player.draw();
if(currentTime >= lastTime + enemy_timer){
lastTime = currentTime;
create_enemy();
}
create_enemy();
}
gameLoop();
</script>
网络空间战
//特殊设置
var canvas=document.getElementById('canvas'),
ctx=canvas.getContext('2d');
var innerWidth=360,
内高=620;
canvas.width=内部宽度;
canvas.height=内部高度;
//变化无常
var得分=0,
lastTime=0;
//圣卢西亚电影节
window.onkeydown=按IONATECLA;
功能压力tecla(tecla){
if(tecla.keyCode==38){
player.y=player.y-10;
}
如果(tecla.keyCode==40){
player.y=player.y+10;
}
if(tecla.keyCode==39){
player.x=player.x+10;
}
if(tecla.keyCode==37){
player.x=player.x-10;
}
}
//个性化切奥多玩家
var player={},
玩家_宽度=100,
球员身高=105,
player_img=新图像();
player_img.src='images/spaceship.png';
//目标球员
玩家={
宽度:播放器的宽度,
身高:球员的身高,
x:innerWidth/2-player\u width/2,//集中器
y:innerHeight-(玩家身高+10),//deixar em baixo
功率:10,
绘图:函数(){
//乐趣ÃO QUE BLOQUEIA O OBJETO玩家SAIR DO CANVAS
if(this.x=(innerWidth-this.width)){
this.x=(innerWidth-this.width);
}
if(this.y=(innerHeight-this.height)){
this.y=(innerHeight-this.height);
}
ctx.drawImage(player_img,this.x,this.y,this.width,this.height);
}
};
//加拉夏基金会*codigo fonte do FUNDO retirado do site codepen.iohttps://codepen.io/LeonGr/pen/fdCsI
var stars=[],//包含星号的数组
FPS=60,//帧/秒
x=canvas.width;//星星数
对于(变量i=0;icanvas.width)s.x=-s.x;
如果(s.y<0 | | s.y>画布高度)s.y=-s.y;
}
}
函数drawfundo(){
clearRect(0,0,canvas.width,canvas.height);
ctx.globalCompositeOperation=“打火机”;
对于(变量i=0,x=stars.length;i=最后时间+敌方计时器){
lastTime=当前时间;
制造敌人();
}
制造敌人();
}
gameLoop();
除了敌人不露面外,一切都很顺利
已经检查了图像文件夹,它的所有设置就像我在代码中输入的一样。
敌人的线条是“//PersonalizationÃO DO INIMIGO”“//OBJETO DO INIMIGO”你在画布外画敌人。只需将
var y=-敌方高度
更改为var y=敌方高度
:
const canvas=document.getElementById('canvas');
const ctx=canvas.getContext('2d');
const innerWidth=canvas.width=window.innerWidth;
const innerHeight=canvas.height=window.innerHeight;
//变数:
分数=0;
设lastTime=0;
//圣约翰运动会:
window.onkeydown=(e)=>{
e、 预防默认值();
如果(e.keyCode==38)player.y=player.y-10;
如果(e.keyCode==40)player.y=player.y+10;
如果(e.keyCode==39)player.x=player.x+10;
如果(e.keyCode==37)player.x=player.x-10;
};
//PersonaliaÃO DO播放器:
var player={},
玩家_宽度=100,
球员身高=105,
player_img=新图像();
播放器\u img.src=https://i.stack.imgur.com/L5XWd.png';
//目标玩家:
玩家={
宽度:播放器的宽度,