- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我说的没有意义,我提前道歉,我对 Javascript 编码还是个新手。
我正在尝试使用取自“基础:用于游戏和环境的 HTML5 Canvas ”第 9 章中的 Javascript 代码来创建“小行星回避”游戏。我的问题是我无法让“玩家的船”移动。它似乎锁定在屏幕的左上角。我无法弄清楚如何让船移动,即使我有键代码的变量以及如何为“keyup”和“keydown”做些什么。
我没有收到任何语法错误,所以我猜我的代码放错地方了,我只是不确定放在哪里。如果我可能会问,我可以得到一双额外的眼睛,以便能够看到我哪里做错了吗?
这是 jsbin ( http://jsbin.com/dunaxakifu/ )
$(document).ready(function(){
var canvas = $("#gameCanvas");
var context = canvas.get(0).getContext("2d");
//Canvas Dimension
var canvasWidth = canvas.width();
var canvasHeight = canvas.height();
//Game Settings
var playGame;
var asteroids;
var numAsteroids;
var player;
//Scoring
var score;
var scoreTimeout;
//Key codes
var arrowUp = 38;
var arrowRight = 39;
var arrowDown = 40;
//Game UI
var ui = $("#gameUI");
var uiIntro = $("#gameIntro");
var uiStats = $("#gameStats");
var uiComplete = $("#gameComplete");
var uiPlay = $("#gamePlay");
var uiReset = $(".gameReset");
var uiScore = $(".gameScore");
var soundBackground = $("#gameSoundBackground").get(0);
var soundThrust = $("#gameSoundThrust").get(0);
var soundDeath = $("#gameSoundDeath").get(0);
var Asteroid = function(x, y, radius, vX){
this.x = x;
this.y = y;
this.radius = radius;
this.vX = vX;
};
var Player = function(x, y){
this.x = x;
this.y = y;
this.width = 24;
this.height = 24;
this.halfWidth = this.width/2;
this.halfHeight = this.height/2;
this.vX = 0;
this.vY = 0;
this.moveRight = false;
this.moveUp = false;
this.moveDown = false;
this.flameLength = 20;
};
//Reset and start the game
function startGame() {
//Reset game stats
uiScore.html("0");
uiStats.show();
asteroids = new Array();
numAsteroids = 10;
score = 0;
for (var i = 0; i < numAsteroids; i++){
var radius = 5+(Math.random()*10);
var x = canvasWidth+radius+Math.floor(Math.random()*canvasWidth);
var y = Math.floor(Math.random()*canvasHeight);
var vX = -5-(Math.random()*5);
asteroids.push(new Asteroid(x, y, radius, vX));
};
player = new Player(150, canvasHeight/2);
//Set up initial game settings
playGame = false;
//Keyboard events
$(window).keydown(function(e){
var keyCode = e.keyCode;
if (!playGame){
playGame = true;
soundBackground.currentTime = 0;
soundBackground.play();
animate();
timer();
};
if (keyCode == arrowRight){
player.moveRight = true;
} else if (keyCode == arrowUp){
player.moveUp = true;
} else if (keyCode == arrowDown){
player.moveDown = true;
};
if (soundThrust.paused){
soundThrust.currentTime = 0;
soundThrust.play();
};
});
$(window).keyup(function(e){
var keyCode = e.keyCode;
if (keyCode == arrowRight){
player.moveRight = false;
} else if (keyCode == arrowUp){
player.moveUp = false;
} else if (keyCode == arrowDown){
player.moveDown = false;
};
soundThrust.pause();
});
//Start the animation loop
animate();
};
//Initialize the game environment
function init() {
uiStats.hide();
uiComplete.hide();
uiPlay.click(function(e){
e.preventDefault();
uiIntro.hide();
startGame();
});
$(window).unbind("keyup");
$(window).unbind("keydown");
uiReset.click(function(e){
e.preventDefault();
uiComplete.hide();
startGame();
soundThrust.pause();
soundBackground.pause();
clearTimeout(scoreTimeout);
});
};
//Timer
function timer(){
if(playGame){
scoreTimeout = setTimeout(function(){
uiScore.html(++score);
if (score % 5 == 0) {
numAsteroids += 5;
};
timer();
}, 1000);
};
};
//Animation loop that does all the fun stuff
function animate(){
//Clear
context.clearRect(0, 0, canvasWidth, canvasHeight);
var asteroidsLength = asteroids.length;
for (var i = 0; i < asteroidsLength; i++){
var tmpAsteroid = asteroids[i];
var dX = player.x - tmpAsteroid.x;
var dY = player.y - tmpAsteroid.y;
var distance = Math.sqrt((dX*dY)+(dY*dY));
if (distance < player.halfWidth+tmpAsteroid.radius){
soundThrust.pause();
soundDeath.currentTime = 0;
soundDeath.play();
//Game over
playGame = false;
clearTimeout(scoreTimeout);
uiStats.hide();
uiComplete.show();
soundBackground.pause();
$(window).unbind("keyup");
$(window).unbind("keydown");
};
if(tmpAsteroid.x+tmpAsteroid.radius < 0){
tmpAsteroid.radius = 5+(Math.random()*10);
tmpAsteroid.x = canvasWidth+tmpAsteroid.radius;
tmpAsteroid.y = Math.floor(Math.random()*canvasHeight);
tmpAsteroid.vX = -5-(Math.random()*5);
};
tmpAsteroid.x += tmpAsteroid.vX;
context.fillStyle = "rgb(255, 255, 255)";
context.beginPath();
context.arc(tmpAsteroid.x, tmpAsteroid.y, tmpAsteroid.radius, 0, Math.PI*2, true);
context.closePath();
context.fill();
player.vX = 0;
player.vY = 0;
if (player.moveRight){
player.vX = 3;
} else {
player.vX = -3;
};
if (player.moveUp){
player.vy = 3;
};
if (player.moveDown){
player.vy = 3;
};
player.x = player.vX;
player.y = player.vY;
if (player.moveRight){
context.save();
context.translate(player.x-player.halfWidth, player.y);
if (player.flameLength == 20) {
player.flameLength = 15;
} else {
player.flameLength = 20;
};
};
if (player.x-player.halfWidth < 20){
player.x = 20+player.halfWidth;
} else if (player.x+player.halfWidth > canvasWidth-20) {
player.x = canvasWidth-20-player.halfWidth;
}
if (player.y-player.halfHeight < 20){
player.y = 20+player.halfHeight;
} else if (player.y+player.halfHeight > canvasHeight-20) {
player.y = canvasHeight-20-player.halfHeight;
};
context.fillStyle = "orange";
context.beginPath();
context.moveTo(0, -5);
context.lineTo(-player.flameLength, 0);
context.lineTo(0, 5);
context.closePath();
context.fill();
context.restore();
context.fillStyle = "rgb(255, 0, 0)";
context.beginPath();
context.moveTo(player.x+player.halfWidth, player.y);
context.lineTo(player.x-player.halfWidth, player.y-player.halfHeight);
context.lineTo(player.x-player.halfWidth, player.y+player.halfHeight);
context.closePath();
context.fill();
while (asteroids.length < numAsteroids){
var radius = 5+(Math.random()*10);
var x = Math.floor(Math.random()*canvasWidth)+canvasWidth+radius;
var y = Math.floor(Math.random()*canvasHeight);
var vX = -5-(Math.random()*5);
asteroids.push(new Asteroid(x, y, radius, vX));
};
};
if (playGame){
//run the animation loop again in 33 milliseconds
setTimeout(animate, 33);
};
};
init();
});
* {
margin:0;
padding:0;
}
html,body {
height:100%;
width:100%;
}
canvas {
display:block;
}
body{
background:#000;
color:#fff;
font-family:Verdana, Arial, sans-serif;
font-size:18px;
}
h1{
font-size:30px;
}
h6{
font-size:15px;
}
p{
margin:0 20px;
}
a{
color:#fff;
text-decoration:none;
}
a:hover{
text-decoration:underline;
}
a.button{
background:#185da8;
border-radius:5px;
display:block;
font-size:30px;
margin:40px 0 0 270px;
padding:10px;
width:200px;
text-align:center;
}
a.button:hover{
background:#2488f5;
color:#fff;
text-decoration:none;
}
#game{
height:600px;
left:50%;
margin:-300px 0 0 -400px;
position:relative;
top:50%;
width:980px;
}
#gameCanvas{
background:#001022;
border:5px solid green;
background-image:url(../images/space.jpg);
background-position:center top;
background-repeat:no-repeat;
background-size:cover;
}
#gameUI{
height:600px;
position:absolute;
width:980px;
}
#gameIntro, #gameComplete {
background:rgba(0, 0, 0, 0.5);
margin-top: 100px;
padding:40px 0;
text-align:center;
}
#gameStats{
font-size:14px;
margin:20px 0;
}
#gameStats .gameReset{
margin:20px 20px 0 0;
position:absolute;
right:0;
top:0;
}
<!Doctype HTML>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<title>Debris Fields of Spiral Galaxy</title>
<meta charset="utf-8">
<link href="css/game.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/jquery-2-1-4min.js"></script>
<script type="text/javascript" src="js/game.js"></script>
</head>
<body>
<div id="game">
<div id="gameUI">
<div id="gameIntro">
<h1>Debris Fields of Spiral Galaxy</h1>
<h6>A <i>Galaxy Smuggler's Run</i> Game</h6>
<p>Click "Play" and then press any key to start.</p>
<p><a id="gamePlay" class="button" href="">Play!</a></p>
</div>
<div id="gameStats">
<p>Time: <span class="gameScore"></span> seconds</p>
<p><a class="gameReset" href="">Reset</a></p>
</div>
<div id="gameComplete">
<h1>Game Over!</h1>
<p>You survived for <span class="gameScore"></span> seconds.</p>
<p><a class="gameReset button" href="">Play Again?</a></p>
</div>
</div>
<canvas id="gameCanvas" width="980" height="600">
</canvas>
<audio id="gameSoundBackground" loop>
<source src="sounds/background.ogg">
<source src="sounds/background.mp3">
</audio>
<audio id="gameSoundThrust" loop>
<source src="sounds/thrust.ogg">
<source src="sounds/thrust.mp3">
</audio>
<audio id="gameSoundDeath">
<source src="sounds/death.ogg">
<source src="sounds/death.mp3">
</audio>
</div>
</body>
</html>
最佳答案
那里有很多代码,尝试直接在 Player 对象上调用移动函数
Player.prototype.moveUp = function() {
this.y++;
}
Player.prototype.moveDown = function() {
this.y--;
}
$(window).keyDown(function(key) {
if(key == x) {
Player.moveUp();
}
else if (key == y) {
Player.moveDown();
}
});
不确定这是否会有所不同,这就是我做事的方式:B
关于javascript - "Ship"赢了 't move for "小行星回避”游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30448603/
我对这个错误很困惑: Cannot implicitly convert type 'System.Func [c:\Program Files (x86)\Reference Assemblies\
考虑这段代码: pub trait Hello { fn hello(&self); } impl Hello for Any { fn hello(&self) {
问题很简单。是否可以构造这样一个类型 T,对于它下面的两个变量声明会产生不同的结果? T t1 = {}; T t2{}; 我已经研究 cppreference 和标准一个多小时了,我了解以下内容:
Intellij idea 给我这个错误:“Compare (T, T) in Comparator cannot be applied to (T, T)” 对于以下代码: public class
任何人都可以告诉我 : n\t\t\t\t\n\t\t\t 在以下来自和 dwr 服务的响应中的含义和用途是什么. \r\n\t\t\t \r\n\t\t\t
让 T 成为一个 C++ 类。 下面三个指令在行为上有什么区别吗? T a; T a(); T a = T(); T 为不带参数的构造函数提供了显式定义这一事实是否对问题有任何改变? 后续问题:如果
Rust中的智能指针是什么 智能指针(smart pointers)是一类数据结构,是拥有数据所有权和额外功能的指针。是指针的进一步发展 指针(pointer)是一个包含内存地
比如我有一个 vector vector > v={{true,1},{true,2},{false,3},{false,4},{false,5},{true,6},{false,7},{true,8
我有一个来自 .xls 电子表格的数据框,我打印了 print(df.columns.values) 列,输出包含一个名为:Poll Responses\n\t\t\t\t\t。 我查看了 Excel
This question already has answers here: What are good reasons for choosing invariance in an API like
指针类型作为类型前缀与在类型前加斜杠作为后缀有什么区别。斜线到底是什么意思? 最佳答案 语法 T/~ 和 T/& 基本上已被弃用(我什至不确定编译器是否仍然接受它)。在向新向量方案过渡的初始阶段,[T
我正在尝试找到一种方法来获取模板参数的基类。 考虑以下类: template class Foo { public: Foo(){}; ~Foo(){};
这是一个让我感到困惑的小问题。我不知道如何描述它,所以只看下面的代码: struct B { B() {} B(B&) { std::cout ::value #include
为什么有 T::T(T&) 而 T::T(const T&) 更适合 copy ? (大概是用来实现move语义的???) 原始描述(被melpomene证明是错误的): 在C++11中,支持了一种新
在 Java 7 中使用 eclipse 4.2 并尝试实现 List 接口(interface)的以下方法时,我收到了警告。 public T[] toArray(T[] a) { ret
假设有三个函数: def foo[T](a:T, b:T): T = a def test1 = foo(1, "2") def test2 = foo(List(), ListBuffer()) 虽
我对柯里化(Currying)和非柯里化(Currying)泛型函数之间类型检查的差异有点困惑: scala> def x[T](a: T, b: T) = (a == b) x: [T](a: T,
考虑一个类A,我如何编写一个具有与相同行为的模板 A& pretty(A& x) { /* make x pretty */ return x; } A pretty(A&& x) {
Eclipse 表示由于泛型类型橡皮擦,类型参数不允许使用 instanceof 操作。 我同意在运行时不会保留任何类型信息。但是请考虑以下类的通用声明: class SomeClass{ T
在 C++14 中: 对于任何整数或枚举类型 T 以及对于任何表达式 expr: 有没有区别: struct S { T t { expr }; }; 和 struct S { T t = { exp
我是一名优秀的程序员,十分优秀!