- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道是否有人能够帮助我完成我在 P5.JS 上创建的游戏?我试图在这个游戏中创造生命作为一个功能,但是每当我在峡谷中死去时,它会带走所有的生命,而不是仅仅带走一个生命并按照它应该的方式重置游戏。任何建议都会非常有帮助。
var gameChar_x;
var gameChar_y;
var floorPos_y;
var scrollPos;
var gameChar_world_x;
var isLeft;
var isRight;
var isFalling;
var isPlummeting;
var trees_x;
var clouds;
var mountains;
var collectables;
var canyon;
var game_score;
var flagpole;
var lives;
function setup()
{
createCanvas(1024, 576);
floorPos_y = height * 3/4;
lives = 3;
startGame();
}
function draw()
{
background(100, 155, 255); // fill the sky blue
noStroke();
fill(0,155,0);
rect(0, floorPos_y, width, height/4); // draw some green ground
push();
translate(scrollPos, 0);
drawClouds();
drawMountains();
drawTrees();
for(var i = 0; i < canyon.length; i++)
{
drawCanyon(canyon[i]);
checkCanyon(canyon[i]);
}
// Draw collectable items.
strokeWeight(1)
for(var i = 0; i < collectables.length; i++)
{
if(!collectables[i].isFound)
{
drawCollectable(collectables[i]);
checkCollectable(collectables[i]);
}
}
renderFlagpole();
checkPlayerDie();
pop();
// Draw game character.
drawGameChar();
fill(255, 0, 0);
textSize(24);
text("Lives: " + lives, width/8, 20);
fill(0);
noStroke();
textSize(24);
text("score: " + game_score, width/2, 20)
textSize(12);
// Logic to make the game character move or the background scroll.
if(isLeft)
{
if(gameChar_x > width * 0.2)
{
gameChar_x -= 5;
}
else
{
scrollPos += 5;
}
}
if(isRight)
{
if(gameChar_x < width * 0.8)
{
gameChar_x += 5;
}
else
{
scrollPos -= 5; // negative for moving against the background
}
}
// Logic to make the game character rise and fall.
if(gameChar_y !== floorPos_y)
{
gameChar_y += 2
isFalling = true;
}
else if(isFalling)
{
isFalling = false;
}
if(flagpole.isReached == false)
{
checkFlagpole();
}
// Update real position of gameChar for collision detection.
gameChar_world_x = gameChar_x - scrollPos;
}
// ---------------------
// Key control functions
// ---------------------
function keyPressed()
{
if(keyCode == 37)
{
console.log("left arrow");
isLeft = true;
}
else if(keyCode == 39)
{
console.log("right arrow");
isRight = true;
}
if(keyCode == 32 && gameChar_y == floorPos_y)
{
console.log("jump")
gameChar_y -=100;
}
}
function keyReleased()
{
if(keyCode == 37)
{
console.log("left arrow");
isLeft = false;
}
else if(keyCode == 39)
{
console.log("right arrow");
isRight = false;
}
}
// ------------------------------
// Game character render function
// ------------------------------
// Function to draw the game character.
function drawGameChar()
{
if(isLeft && isFalling)
{
// add your jumping-left code
fill(255, 201, 169); //head
ellipse(gameChar_x, gameChar_y - 55, 30, 40);
fill(0); //back leg
ellipse(gameChar_x + 10, gameChar_y - 20, 20, 10);
fill(255, 201, 169); //back arm
ellipse(gameChar_x + 12, gameChar_y - 35, 10, 10);
fill(135, 206, 235); //body
rect(gameChar_x-10, gameChar_y - 38, 20, 15);
fill(0) //front leg
ellipse(gameChar_x - 10, gameChar_y - 20, 20, 10);
fill(0) //eye
ellipse(gameChar_x - 11, gameChar_y - 58, 2, 10);
fill(255, 201, 169); //front arm
ellipse(gameChar_x + 8, gameChar_y - 30, 10, 10);
}
else if(isRight && isFalling)
{
// add your jumping-right code
fill(255, 201, 169); //head
ellipse(gameChar_x, gameChar_y - 55, 30, 40);
fill(0); //back leg
ellipse(gameChar_x - 10, gameChar_y - 20, 20, 10);
fill(255, 201, 169); //back arm
ellipse(gameChar_x - 12, gameChar_y - 35, 10, 10);
fill(135, 206, 235); //body
rect(gameChar_x - 10, gameChar_y - 38, 20, 15);
fill(0) //front leg
ellipse(gameChar_x + 10, gameChar_y - 20, 20, 10);
fill(0) //eye
ellipse(gameChar_x + 11, gameChar_y - 58, 2, 10);
fill(255, 201, 169); //front arm
ellipse(gameChar_x - 8, gameChar_y - 30, 10, 10);
}
else if(isLeft)
{
// add your walking left code
fill(255, 201, 169); //head
ellipse(gameChar_x, gameChar_y - 45, 30, 40);
fill(0); //back leg
ellipse(gameChar_x + 10, gameChar_y - 7, 20, 10);
fill(255, 201, 169); //back arm
ellipse(gameChar_x + 12, gameChar_y - 25, 10, 10);
fill(135, 206, 235); //body
rect(gameChar_x-10, gameChar_y - 28, 20, 20);
fill(0) //front leg
ellipse(gameChar_x - 10, gameChar_y - 4, 20, 10);
fill(0) //eye
ellipse(gameChar_x - 11, gameChar_y - 48, 2, 10);
fill(255, 201, 169); //front arm
ellipse(gameChar_x - 13, gameChar_y - 16, 10, 10);
}
else if(isRight)
{
// add your walking right code
fill(255, 201, 169); //head
ellipse(gameChar_x, gameChar_y - 45, 30, 40);
fill(0); //back leg
ellipse(gameChar_x - 10, gameChar_y - 7, 20, 10);
fill(255, 201, 169); //back arm
ellipse(gameChar_x - 12, gameChar_y - 25, 10, 10);
fill(135, 206, 235); //body
rect(gameChar_x - 10, gameChar_y - 28, 20, 20);
fill(0) //front leg
ellipse(gameChar_x + 10, gameChar_y - 4, 20, 10);
fill(0) //eye
ellipse(gameChar_x + 11, gameChar_y - 48, 2, 10);
fill(255, 201, 169); //front arm
ellipse(gameChar_x + 13, gameChar_y - 16, 10, 10);
}
else if(isFalling || isPlummeting)
{
// add your jumping facing forwards code
fill(255, 201, 169);//head
ellipse(gameChar_x, gameChar_y - 55, 40, 40);
fill(135, 206, 235); //body
rect(gameChar_x-13, gameChar_y - 38, 26, 20);
fill(255, 201, 169); //arms
ellipse(gameChar_x - 13, gameChar_y - 33, 10, 10);
ellipse(gameChar_x + 18, gameChar_y - 70, 10, 10);
fill(0) //legs
ellipse(gameChar_x - 10, gameChar_y - 10, 20, 10);
ellipse(gameChar_x + 10, gameChar_y - 17, 20, 10);
fill(0) //eyes
ellipse(gameChar_x - 8, gameChar_y - 58, 5, 10);
ellipse(gameChar_x + 8, gameChar_y - 58, 5, 10);
}
else
{
// add your standing front facing code
fill(255, 201, 169);
ellipse(gameChar_x, gameChar_y - 45, 40, 40);
fill(135, 206, 235);
rect(gameChar_x-13, gameChar_y - 28, 26, 20);
fill(255, 201, 169);
ellipse(gameChar_x - 13, gameChar_y - 18, 10, 10);
ellipse(gameChar_x + 13, gameChar_y - 18, 10, 10);
fill(0)
ellipse(gameChar_x - 10, gameChar_y - 5, 20, 10);
ellipse(gameChar_x + 10, gameChar_y - 5, 20, 10);
fill(0)
ellipse(gameChar_x - 8, gameChar_y - 48, 5, 10);
ellipse(gameChar_x + 8, gameChar_y - 48, 5, 10);
strokeWeight(0)
}
}
// ---------------------------
// Background render functions
// ---------------------------
// Function to draw cloud objects.
function drawClouds()
{
for (var i = 0; i < clouds.length; i++)
{
fill(255,255,255);
ellipse(clouds[i].pos_x,
clouds[i].pos_y, 70, 80);
ellipse(clouds[i].pos_x + 30,
clouds[i].pos_y, 80, 100);
ellipse(clouds[i].pos_x + 60,
clouds[i].pos_y, 80, 100);
ellipse(clouds[i].pos_x + 90,
clouds[i].pos_y, 70, 80);
}
}
// Function to draw mountains objects.
function drawMountains()
{
for (var i = 0; i < mountains.length; i++)
{
fill(120, 120, 120);
triangle(mountains[i].pos_x,
mountains[i].pos_y,
mountains[i].pos_x + 270,
mountains[i].pos_y - 326,
mountains[i].pos_x + 410,
mountains[i].pos_y);
fill(255, 255, 255);
triangle(mountains[i].pos_x + 310,
mountains[i].pos_y - 247,
mountains[i].pos_x + 267,
mountains[i].pos_y - 326,
mountains[i].pos_x + 270,
mountains[i].pos_y - 266);
triangle(mountains[i].pos_x + 280,
mountains[i].pos_y - 266,
mountains[i].pos_x + 267,
mountains[i].pos_y - 326,
mountains[i].pos_x + 210,
mountains[i].pos_y - 255);
triangle(mountains[i].pos_x + 225,
mountains[i].pos_y - 267,
mountains[i].pos_x + 260,
mountains[i].pos_y - 247,
mountains[i].pos_x + 300,
mountains[i].pos_y - 267);
}
}
// Function to draw trees objects.
function drawTrees()
{
for (var i = 0; i < trees_x.length; i++)
{
fill(122, 31, 31); //tree
rect(trees_x[i], treePos_y + 105, 35, 40);
fill(34,139,34);
triangle(trees_x[i] + 80,
treePos_y + 115,
trees_x[i] + 20,
treePos_y + 45,
trees_x[i] - 45,
treePos_y + 115);
triangle(trees_x[i] + 80,
treePos_y + 75,
trees_x[i] + 20,
treePos_y - 5,
trees_x[i] - 45,
treePos_y + 75);
}
}
// ---------------------------------
// Canyon render and check functions
// ---------------------------------
// Function to draw canyon objects.
function drawCanyon(t_canyon)
{
fill(100, 155, 255);
rect(t_canyon.pos_x, floorPos_y, t_canyon.width, 144);
}
// Function to check character is over a canyon.
function checkCanyon(t_canyon)
{
if(gameChar_world_x > t_canyon.pos_x && gameChar_world_x < t_canyon.pos_x + 100)
{
gameChar_y += 2
isFalling = true;
}
}
// ----------------------------------
// Collectable items render and check functions
// ----------------------------------
// Function to draw collectable objects.
function drawCollectable(t_collectable)
{
stroke(1);
fill(255,215,0);
ellipse(t_collectable.x_pos,
t_collectable.y_pos - 20,
t_collectable.size,
t_collectable.size);
ellipse(t_collectable.x_pos,
t_collectable.y_pos - 20,
t_collectable.size - 10,
t_collectable.size - 10);
fill(0, 0, 0);
text("$", t_collectable.x_pos - 3,
t_collectable.y_pos - 16);
}
// Function to check character has collected an item.
function checkCollectable(t_collectable)
{
if(dist(gameChar_world_x, gameChar_y,
t_collectable.x_pos, t_collectable.y_pos - 20) < t_collectable.size)
{
t_collectable.isFound = true;
game_score += 1;
}
}
function checkPlayerDie()
{
console.log(lives);
if(gameChar_y > height)
{
lives = true;
lives = lives -= 1;
}
if(lives == true)
{
startGame;
}
}
function startGame()
{
gameChar_x = width/2;
gameChar_y = floorPos_y;
treePos_y = height/2;
// Variable to control the background scrolling.
scrollPos = 0;
// Variable to store the real position of the gameChar in the game
// world. Needed for collision detection.
gameChar_world_x = gameChar_x - scrollPos;
// Boolean variables to control the movement of the game character.
isLeft = false;
isRight = false;
isFalling = false;
isPlummeting = false;
// Initialise arrays of scenery objects.
trees_x = [25,
75,
125,
360,
410,
460,
800,
850,
900,
1240,
1290,
1340,
1680,
1730,
1780
];
clouds = [
{pos_x: 50, pos_y: 50},
{pos_x: 300, pos_y: 70},
{pos_x: 550, pos_y: 110},
{pos_x: 800, pos_y: 70},
{pos_x: 1000, pos_y: 50},
{pos_x: 1250, pos_y: 70},
{pos_x: 1500, pos_y: 110},
{pos_x: 1750, pos_y: 50}
];
mountains = [
{pos_x: 75, pos_y: 432},
{pos_x: 175, pos_y: 432},
{pos_x: 775, pos_y: 432},
{pos_x: 1675, pos_y: 432},
{pos_x: 1975, pos_y: 432},
];
collectables = [
{x_pos: 566, y_pos: floorPos_y, size: 30, isFound: false},
{x_pos: 700, y_pos: floorPos_y, size: 30, isFound: false},
{x_pos: 1100, y_pos: floorPos_y, size: 30, isFound: false},
{x_pos: 1900, y_pos: floorPos_y, size: 30, isFound: false}
];
canyon = [
{pos_x: 590, width: 100},
{pos_x: 1450, width: 100}
];
game_score = 0;
flagpole = {isReached: false, x_pos: 2000}
}
function renderFlagpole()
{
push();
strokeWeight(5);
stroke(255);
line(flagpole.x_pos, floorPos_y, flagpole.x_pos, floorPos_y - 350);
fill(255, 215, 0);
noStroke();
if(flagpole.isReached)
{
rect(flagpole.x_pos, floorPos_y - 50, 75, 50);
}
else
{
rect(flagpole.x_pos, floorPos_y - 350, 75, 50);
}
pop();
}
function checkFlagpole()
{
var d = abs(gameChar_world_x - flagpole.x_pos);
if(d < 15)
{
flagpole.isReached = true;
}
}
最佳答案
我相信你的错误在这里:
function checkPlayerDie()
{
console.log(lives);
if(gameChar_y > height)
{
lives = true;
lives = lives -= 1;
}
if(lives == true)
{
startGame;
}
}
lives = true;
lives = lives -= 1;
lives
是一个整数值,计算 Angular 色剩余生命的数量。但是,您将值更改为 bool 值
true
值,基本上丢弃任何存在的数量。因此,因为
true - 1
结果
0
你立即失去了所有现有的生命。
if
以下条件到
if (lives > 0)
,如果我正确理解你的逻辑。
lives = lives -= 1
.在这里你是多余的,因为你结合了
Assignment Operator与
Substraction Assignment operator .您应该将其更改为仅
lives -= 1;
或
lives = lives - 1;
.
if
第一个
if
中的声明陈述。这是我所做的编辑,导致它正常工作:
function checkPlayerDie()
{
console.log(lives);
if(gameChar_y > height)
{
lives -= 1;
if(lives > 0)
{
startGame();
}
}
}
关于javascript - 如何使用 P5.JS 在我的游戏中添加 "lives"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60711084/
我创建了一个用户可以添加测试的字段。这一切运行顺利我只希望当用户点击(添加另一个测试)然后上一个(添加另一个测试)删除并且这个显示在新字段中。 所有运行良好的唯一问题是点击(添加另一个字段)之前添加另
String[] option = {"Adlawan", "Angeles", "Arreza", "Benenoso", "Bermas", "Brebant
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在努力将 jQuery 滚动功能添加到 nav-tab (Bootstrap 3)。我希望用户能够选择他们想要的选项卡,并在选项卡内容中有一个可以平滑滚动到 anchor 的链接。这是我的代码,可
我正在尝试在用户登录后再添加 2 个 ui 选项卡。首先,我尝试做一个之后。 $('#slideshow').tabs('remove', '4'); $("#slideshow ul li:last
我有一个包含选择元素的表单,我想通过选择添加和删除其中一些元素。这是html代码(这里也有jsfiddle http://jsfiddle.net/txhajy2w/):
正在写这个: view.backgroundColor = UIColor.white.withAlphaComponent(0.9) 等同于: view.backgroundColor = UICo
好的,如果其中有任何信息,我想将这些列添加到一起。所以说我有 账户 1 2 3 . 有 4 个帐户空间,但只有 3 个帐户。我如何创建 java 脚本来添加它。 最佳答案 Live Example H
我想知道是否有一种有效的预制算法来确定一组数字的和/差是否可以等于不同的数字。示例: 5、8、10、2,使用 + 或 - 等于 9。5 - 8 = -3 + 10 = 7 + 2 = 9 如果有一个预
我似乎有一个卡住的 git repo。它卡在所有基本的添加、提交命令上,git push 返回所有内容为最新的。 从其他帖子我已经完成了 git gc 和 git fsck/ 我认为基本的调试步骤是
我的 Oracle SQL 查询如下- Q1- select hca.account_number, hca.attribute3, SUM(rcl.extended_amou
我正在阅读 http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingG
我正在尝试添加一个“加载更多”按钮并限制下面的结果,这样投资组合页面中就不会同时加载 1000 个内容,如下所示:http://typesetdesign.com/portfolio/ 我对 PHP
我遇到这个问题,我添加了 8 个文本框,它工作正常,但是当我添加更多文本框(如 16 个文本框)时,它不会添加最后一个文本框。有人遇到过这个问题吗?提前致谢。 Live Link: JAVASCRIP
add/remove clone first row default not delete 添加/删除克隆第一行默认不删除&并获取正确的SrNo(例如:添加3行并在看到问题后删除SrNo.2)
我编码this ,但删除按钮不起作用。我在控制台中没有任何错误.. var counter = 0; var dataList = document.getElementById('materi
我有一个类似数组的对象: [1:数组[10]、2:数组[2]、3:数组[2]、4:数组[2]、5:数组[3]、6:数组[1]] 我正在尝试删除前两个元素,执行一些操作,然后将它们再次插入到同一位置。
使用的 Delphi 版本:2007 你好, 我有一个 Tecord 数组 TInfo = Record Name : String; Price : Integer; end; var Info
我使用了基本的 gridster 代码,然后我声明了通过按钮添加和删除小部件的函数它工作正常但是当我将调整大小功能添加到上面的代码中时,它都不起作用(我的意思是调整大小,添加和删除小部件) 我的js代
title 323 323 323 title 323 323 323 title 323 323 323 JS $(document).keydown(function(e){
我是一名优秀的程序员,十分优秀!