あもんノート TOP
text = {}
text.init = function(){
.e = document.getElementById( "text" )
.e.style.textAlign = "center"
.e.style.fontWeight = "bold"
}
text.put = function( s ){
.e.innerText = s
}
screen = {}
screen.init = function(){
.e = document.getElementById( "graphics" )
.e.style.borderRadius = .e.width / 100 + "px"
.ctx = .e.getContext( "2d" )
.bgColor = "black"
.clear()
}
screen.clear = function(){
.ctx.fillStyle = .bgColor
.ctx.fillRect( 0, 0, .e.width, .e.height )
}
screen.text = function( s, x, y, size, color ){
.ctx.font = size + "px sans-serif"
.ctx.fillStyle = color
.ctx.fillText( s, x, y )
}
screen.fillRect = function( x, y, width, height, color){
.ctx.fillStyle = color
.ctx.fillRect( x, y, width, height )
}
screen.fillTriangle = function( x1, y1, x2, y2, x3, y3, color ){
.ctx.fillStyle = color
.ctx.beginPath()
.ctx.moveTo( x1, y1 ); .ctx.lineTo( x2, y2 ); .ctx.lineTo( x3, y3 )
.ctx.fill()
}
screen.fillCircle = function( x, y, r, color ){
.ctx.fillStyle = color
.ctx.beginPath()
.ctx.arc( x, y, r, 0, 2 * Math.PI )
.ctx.fill()
}
screen.putPacman = function( x, y, size, direction, color, bite ){
var r = size / 2
var d = size / 2 - bite * size / 4
.fillCircle( x, y, r, color )
if( direction == "right" )
.fillTriangle( x + r, y - d, x, y, x + r, y + d, .bgColor )
if( direction == "left" )
.fillTriangle( x - r, y - d, x, y, x - r, y + d, .bgColor )
if( direction == "up" )
.fillTriangle( x - d, y - r, x, y, x + d, y - r, .bgColor )
if( direction == "down" )
.fillTriangle( x - d, y + r, x, y, x + d, y + r, .bgColor )
}
screen.putEnemy = function( x, y, size, direction, color ){
var r = size / 2, d = size / 5, dd = size / 11
var w = "white", b = "black"
.fillCircle( x, y, r, color )
.fillRect( x - r, y, 2 * r, r, color )
.fillCircle( x - d, y, r / 3, w )
.fillCircle( x + d, y, r / 3, w )
if( direction == "left" ){
.fillCircle( x - d - dd, y, r / 5, b)
.fillCircle( x + d - dd, y, r / 5, b )
}
if( direction == "right" ){
.fillCircle( x - d + dd, y, r / 5, b )
.fillCircle( x + d + dd, y, r / 5, b )
}
if( direction == "up" ){
.fillCircle( x - d, y - dd * 0.6, r / 5, b )
.fillCircle( x + d, y - dd * 0.6, r / 5, b )
}
if( direction == "down" ){
.fillCircle( x - d, y + dd * 0.8, r / 5, b )
.fillCircle( x + d, y + dd * 0.8, r / 5, b )
}
}
screen.putJunk = function( x, y, size, direction, color ){
var d = size / 5
var bloodColor = "red"
.fillCircle( x, y, d * 1.5, color )
.fillCircle( x - d, y - 1.2 * d, d / 1.2, color )
.fillCircle( x + d, y - d, d / 1.4, color )
.fillCircle( x - 1.6 * d, y + d, d / 1.3, color )
.fillCircle( x + d, y + d, d, color )
.fillCircle( x, y, d, bloodColor )
.fillCircle( x - d / 1.2, y - d, d / 1.8, bloodColor )
.fillCircle( x + d / 1.7, y - d / 1.3, d / 2.7, bloodColor )
.fillCircle( x - d, y + d / 1.7, d / 2.9, bloodColor )
.fillCircle( x + d / 1.1, y + d, d / 2.9, bloodColor )
}
tool = {}
tool.array2 = function( size1, size2 ){
var a = []
for( var i = 0; i < size1; i++ ){
a[ i ] = []
for( var j = 0; j < size2; j++ ){
a[ i ][ j ] = 0
}
}
return a
}
tool.oppositeDirection = function( d ){
if( d == "left" ) return "right"
if( d == "right" ) return "left"
if( d == "up" ) return "down"
if( d == "down" ) return "up"
}
tool.changeDirection = function( direction ){
while( true ){
var r = Math.random() * 4
var d = "left"
if( r < 3 ) d = "right"
if( r < 2 ) d = "up"
if( r < 1 ) d = "down"
if( d != .oppositeDirection( direction ) ) return d
}
}
tool.directionMove = function( position, direction, step ){
var p = position
if( direction == "left" ) p.x -= step
if( direction == "right" ) p.x += step
if( direction == "up" ) p.y -= step
if( direction == "down" ) p.y += step
p.x = Number( p.x.toFixed( 1 ) )
p.y = Number( p.y.toFixed( 1 ) )
}
field = {}
field.init = function(){
.marginX = 34
.marginY = 25
.width = 36
.padding = 2
.blockColor = "blue"
.foodColor = "goldenrod"
var d = [
"BBBBBBBBBBBB",
"BC C CB",
"B BB BBBBB B",
"B BB BBBBB B",
"BC C C CB",
"B BB BB BB B",
"B BB BB BB B",
"BC C C CB",
"B BBBBB BB B",
"B BBBBB BB B",
"BC C CB",
"BBBBBBBBBBBB",
]
.sizeX = d[ 0 ].length
.sizeY = d.length
.data = tool.array2( .sizeX, .sizeY )
.food = tool.array2( .sizeX, .sizeY )
.foodNum = 0
for( var x = 0; x < .sizeX; x++ )
for( var y = 0; y < .sizeY; y++ ){
if( d[ y ].substr( x, 1 ) == "B" ) .data[ x ][ y ] = "block"
if( d[ y ].substr( x, 1 ) == "C" ) .data[ x ][ y ] = "cross"
if( d[ y ].substr( x, 1 ) != "B" ){
.food[ x ][ y ] = true
.foodNum++
}
}
}
field.draw = function(){
var mx = .marginX, my = .marginY
var w = .width, p = .padding
for( var x = 0; x < .sizeX; x++ )
for( var y = 0; y < .sizeY; y++ ){
if( .data[ x ][ y ] == "block" )
screen.fillRect( mx + p + w * x, my + p + w * y, w - 2 * p, w - 2 * p, .blockColor )
if( .food[ x ][ y ] )
screen.fillCircle( mx + w * ( x + 0.5 ), my + w * ( y + 0.5 ), 4, .foodColor )
}
}
field.hitBlock = function( x, y ){
var x0 = Math.floor( x ), x1 = Math.ceil( x )
var y0 = Math.floor( y ), y1 = Math.ceil( y )
for( var i of [ x0, x1 ] )
for( var j of [ y0, y1 ] ){
if( .data[ i ][ j ] == "block" ) return true
}
return false
}
field.isCross = function( x, y ){
if( parseInt( x ) != x || parseInt( y ) != y ) return false
if( .data[ x ][ y ] == "cross" ) return true
return false
}
field.isFood = function( x, y ){
if( parseInt( x ) != x || parseInt( y ) != y ) return false
if( .food[ x ][ y ] ) return true
return false
}
field.removeFood = function( x, y ){
.food[ x ][ y ] = false
.foodNum--
}
enemy = {}
enemy.init = function( num ){
.no = [
{ x: 1, y: 1.1, direction: "down", color: "hotpink" },
{ x: 10, y: 1.2, direction: "down", color: "forestgreen" },
{ x: 1.3, y: 10, direction: "right", color: "orange" },
{ x: 4, y: 4.4, direction: "up", color: "lightblue" },
{ x: 7.5, y: 7, direction: "right", color: "blueviolet" },
]
if( num < .no.length ) .no.length = num
.speed = 0.1
}
enemy.draw = function( status ){
var mx = field.marginX, my = field.marginY
var w = field.width, p = field.padding
for( var i in .no ){
var x = .no[ i ].x, y = .no[ i ].y, d = .no[ i ].direction, c = .no[ i ].color
if( status == "dead" ) c = "blue"
screen.putEnemy( mx + w * ( x + 0.5 ), my + w * ( y + 0.5 ), w - 2 * p, d, c )
}
}
enemy.move = function(){
for( var i in .no ){
var d = .no[ i ].direction
while( true ){
if( field.isCross( .no[ i ].x, .no[ i ].y ) ) d = tool.changeDirection( .no[ i ].direction )
var p = { x: .no[ i ].x, y: .no[ i ].y }; tool.directionMove( p, d, .speed )
if( !field.hitBlock( p.x, p.y ) ){
.no[ i ].x = p.x
.no[ i ].y = p.y
.no[ i ].direction = d
break
}
}
}
}
enemy.hit = function( obj ){
for( var i = 0; i < .no.length; i++ ){
var dx = .no[ i ].x - obj.x
var dy = .no[ i ].y - obj.y
if( Math.sqrt( dx**2 + dy**2 ) < 0.3 ){
return true
}
}
return false
}
player = {}
player.init = function(){
.x = 10
.y = 10
.direction = "left"
.color = "yellow"
.speed = 0.1
.count = 0
.hpMax = .hp = 400
.hpMin = 20
}
player.draw = function( status ){
.count++
var s = screen
var mx = field.marginX, my = field.marginY
var w = field.width, p = field.padding
var size = ( w - 2 * p ) * ( 1 + .hp / .hpMax ) / 2
if( status == "dead" ){
s.putJunk( mx + w * ( .x + 0.5 ), my + w * ( .y + 0.5 ), size, .direction, .color )
} else if( status == "starved" ){
var size = ( w - 2 * p ) * 0.3
s.putPacman( mx + w * ( .x + 0.5 ), my + w * ( .y + 0.5 ), size, "", .color, 0 )
} else{
var bite = 0; if( .count % 6 < 3 ) bite = 1
s.putPacman( mx + w * ( .x + 0.5 ), my + w * ( .y + 0.5 ), size, .direction, .color, bite )
}
}
player.move = function(){
.hp--
.turnBack()
var d = .direction
for( var i = 0; true; i++ ){
if( field.isCross( .x, .y ) ){
if( i == 0 ){
if( key.in == "ArrowLeft" ) d = "left"
if( key.in == "ArrowRight" ) d = "right"
if( key.in == "ArrowUp" ) d = "up"
if( key.in == "ArrowDown" ) d = "down"
key.in = ""
} else{
d = tool.changeDirection( .direction )
}
}
var p = { x: .x, y: .y }; tool.directionMove( p, d, .speed )
if( !field.hitBlock( p.x, p.y ) ){
.x = p.x
.y = p.y
.direction = d
break
}
}
}
player.turnBack = function(){
var d = .direction
if( key.in == "ArrowLeft" && d == "right" ) d = "left"
if( key.in == "ArrowRight" && d == "left" ) d = "right"
if( key.in == "ArrowUp" && d == "down" ) d = "up"
if( key.in == "ArrowDown" && d == "up" ) d = "down"
if( d != .direction ) key.in = ""
.direction = d
}
player.eat = function(){
if( field.isFood( .x, .y ) ){
field.removeFood( .x, .y )
.hp += 80
if( .hp > .hpMax ) .hp = .hpMax
return true
}
return false
}
player.starved = function(){
if( .hp <= .hpMin ) return true
return false
}
key = {}
key.init = function(){
.in = ""
document.onkeydown = function( e ){
if( e.key.substr( 0, 5 ) == "Arrow" ) e.preventDefault()
key.in = e.key
}
}
game = {}
game.run = function(){
.highScore = 0
text.init()
screen.init()
key.init()
.init()
setInterval( "game.frame()", 40 )
}
game.init = function(){
.score = 0
.stage = 1
field.init()
enemy.init( .stage + 2 )
player.init()
.count = 0
.mode = "demo"
}
game.frame = function(){
.count++
screen.clear()
field.draw()
this[ .mode ]()
screen.text( "", 10, 20, 16, "white" )
screen.text( "SCORE " + .score, 70, 485, 22, "lightblue" )
screen.text( "HI-SCORE " + .highScore, 250, 485, 22, "violet" )
}
game.demo = function(){
enemy.draw( "alive" )
enemy.move()
if( Math.floor( .count ) % 10 < 6 ){
screen.text( "Press any key", 180, 240, 24, "white" )
screen.text( "to start game", 180, 280, 24, "white" )
}
if( key.in != "" ){
.init()
.mode = "wait"
key.in = ""
}
}
game.wait = function(){
player.draw( "alive" )
enemy.draw( "alive" )
screen.text( "STAGE " + .stage, 205, 240, 24, "white" )
if( .count > 30 ) .mode = "play"
}
game.play = function(){
player.draw( "alive" )
enemy.draw( "alive" )
player.move()
enemy.move()
if( player.eat() ) .score += 10 * .stage
if( field.foodNum == 0 ){
.mode = "clear"
.count = 0
}
if( enemy.hit( player ) || player.starved() ){
.mode = "dead"
.count = 0
}
}
game.clear = function(){
player.draw( "alive" )
enemy.draw( "dead" )
if( .count > 40 ){
field.init()
.stage++
enemy.init( .stage + 2 )
player.init()
.count = 0
.mode = "wait"
}
}
game.dead = function(){
if( player.starved() ){
player.draw( "starved" )
} else{
player.draw( "dead" )
}
enemy.draw( "alive" )
enemy.move()
if( Math.floor( .count ) % 10 < 6 ){
if( player.starved() ){
screen.text( "YOU LOST ENERGY", 145, 240, 24, "white")
} else{
screen.text( "YOU ARE DEAD", 160, 240, 24, "white")
}
}
if( .count > 60 ){
if( .score > .highScore ) .highScore = .score
key.in = ""
.mode = "demo"
}
}
game.run()