Skip to content

Instantly share code, notes, and snippets.

@yokmama
Last active December 18, 2017 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yokmama/8598c37767d6cb2bc44493fd6f4e525b to your computer and use it in GitHub Desktop.
Save yokmama/8598c37767d6cb2bc44493fd6f4e525b to your computer and use it in GitHub Desktop.
もぐらたたき
let width = 10
let height = 10
let sx = 0
let sz = 2
let maxMogura = 10
let moguralist = [];
let startTime
let beforTime
let kabocha = 0
let gameOver = false
let fireworks = {LifeTime:20,FireworksItem:{id:"fireworks",Count:1,tag:{Fireworks:{Explosions:[{Type:0,Flicker:1,Trail:1,Colors:[16732948],FadeColors:[16750169]},{Type:3,Flicker:1,Trail:0,Colors:[4063102],FadeColors:[12779462]},{Type:1,Flicker:0,Trail:1,Colors:[3613183],FadeColors:[10131199]},{Type:4,Flicker:0,Trail:0,Colors:[16724202],FadeColors:[16762099]}]}}}}
let challenger = world.getPlayer(new BlockPos(0, 0, -1))
if(challenger == null){
challenger = crab.getOwner()
}
let onUpdate = function(){
if(gameOver){
world.title("You failed!")
makeStage()
crab.stop()
}else{
for(let i=0; i<moguralist.length; i++){
moguralist[i].update()
}
let t = Math.floor((Date.now()-startTime)/1000)
if(t<15){
if(t!=beforTime){
world.titleActionBar("Last "+(15-t)+"sec")
}
beforTime = t
setTimeout(onUpdate, 500)
}else{
world.titleSubtitle("You got "+kabocha+" pumpkins")
world.title("Finish!")
setRanking(kabocha)
setSign()
finishGame()
}
}
}
function setSign(){
world.setBlock("minecraft:wall_sign", 0, 1, 0, 0, new BlockOption("back"))
let ranking = localStorage.getItem("ranking")
console.log(ranking)
if(ranking.length>0){
let json = JSON.parse(ranking)
console.log(json)
if(json.length>0){
let sign = {
Text1 : JSON.stringify({ text: "High Score"}),
Text2 : "",
Text3 : JSON.stringify({ text: json[0].player}),
Text4 : JSON.stringify({ text: json[0].score})}
world.blockdata(new BlockPos(1,0,0), sign)
}
}
}
function insertRanking(json, score){
for(let i=0; i<json.length; i++){
if(json[i].score<score){
json.splice(i, 0, {"score":score, "player":challenger.name})
return
}
}
json.push({"score":score, "player":challenger.name})
}
function setRanking(score){
let ranking = localStorage.getItem("ranking")
if(ranking.length>0){
let json = JSON.parse(ranking)
insertRanking(json, score)
if(json.length>3){
json.length = 3
}
ranking = JSON.stringify(json)
}else{
ranking = JSON.stringify([{"score":score, "player":challenger.name}])
}
console.log(ranking)
localStorage.setItem("ranking", ranking)
}
function finishGame(){
let ticks = 0
for(let i=0; i<kabocha; i++){
let x = Math.floor(Math.random()*width)
let y = Math.floor(Math.random()*height)
crab.runAfter(ticks+=10,
function() {
world.summon("fireworks_rocket", new BlockPos(x, 0, y+1), fireworks)
})
}
crab.runAfter(ticks, function(){crab.stop()})
}
function Mogura() {
this._stepCount = -Math.floor( Math.random() * 10 )
};
Mogura.prototype = {
setX: function(x) {
this._x = x;
},
getX: function() {
return this._x;
},
setY: function(y) {
this._y = y;
},
getY: function() {
return this._y;
},
update: function() {
if(this._stepCount == 0){
this.setX(Math.floor( Math.random() * width ))
this.setY(Math.floor( Math.random() * height ))
}else if(this._stepCount == 3){
setBlock1(this._x+sx, this._y+sz)
}else if(this._stepCount == 6){
setBlock2(this._x+sx, this._y+sz)
}else if(this._stepCount == 9){
setBlock(this._x+sx, this._y+sz)
this._stepCount = -Math.floor( Math.random() * 10 )
}
this._stepCount++
}
};
function setBlock(x, y){
world.setBlock(3, 1, x, -1, y)
world.setBlock(Items.Air, x, 0, y)
}
function setBlock1(x, y){
world.setBlock(86, 0, x, -1, y)
world.setBlock(Items.Air, x, 0, y)
}
function setBlock2(x, y){
world.setBlock(3, 1, x, -1, y)
let bom = Math.floor( Math.random() * 10 )
if(bom == 7){
world.setBlock(46, 0, x, 0, y)
}else{
world.setBlock(86, 0, x, 0, y)
}
}
function makeStage(){
for(let y=0; y<height; y++){
for(let x=0; x<width; x++){
setBlock(x+sx, y+sz)
}
}
}
function makeMogura(){
for(let i=0; i<maxMogura; i++){
moguralist.push(new Mogura())
}
}
function makeFence(){
for(let i=0; i<=height; i++){
world.setBlock(85, 0, sx-1, 0, i+sz)
}
for(let i=0; i<=width; i++){
world.setBlock(85, 0, i+sx, 0, height+2)
}
for(let i=0; i<height; i++){
world.setBlock(85, 0, width+sx, 0, height-i+1)
}
for(let i=0; i<=width+1; i++){
if(i != width/2){
world.setBlock(85, 0, width-i, 0, sz-1)
}
}
world.createPermissionArea(
new BlockPos(sx-1, 0, sz-1),
new BlockPos(sx-1, 0, height+sz)).allowBreakBlock(false)
world.createPermissionArea(
new BlockPos(sx+width, 0, sz-1),
new BlockPos(sx+width, 0, height+sz)).allowBreakBlock(false)
world.createPermissionArea(
new BlockPos(sx-1, 0, sz-1),
new BlockPos(sx+width, 0, sz-1)).allowBreakBlock(false)
world.createPermissionArea(
new BlockPos(sx-1, 0, height+sz),
new BlockPos(sx+width, 0, height+sz)).allowBreakBlock(false)
}
makeStage()
makeMogura()
let pm =world.createPermissionArea(
new BlockPos(sx-1,-1,sz-1),
new BlockPos(width+sx+1, -1, height+sz+1))
pm.allowBreakBlock(false)
makeFence()
let d1 = world.on(WorldEvent.BREAKBLOCK, new BlockPos(sx, 0, sz), new BlockPos(width+sx, 1, height+sz), function(e){
if(e.blockId == 46){
//bom
gameOver = true
}else if(e.blockId == 86){
kabocha++
}
})
startTime = Date.now()
world.titleSubtitle(challenger.name)
world.title("Game Start!")
world.summon("fireworks_rocket", new BlockPos(0, 0, 0), fireworks)
setTimeout(onUpdate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment