Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: newera31 (posted 2013-04-16 18:45:25, viewed 569 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399   400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439   440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479   480   481   482   483   484   485   486   487   488   489   490   491   492   493   494   495   496   497   498   499   500   501   502   503   504   505   506   507   508   509  
require("leola.lang.SystemLeolaLibrary", "sys")
import("java.lang.Math")

var zoom = 1.0          // zoom in/out
var volume = 1.0        // 0 - 1.0
var debug = false        // show hit boxes
var numberOfHits = 13    // number of times you can get hit

/*
=============================
Game components
=============================
*/


// Entity is anything in the game world, ships, enemies, explosions, missles
class Entity(pos, speed) {
   var vel = live:math:newVec2(0,0)
   var bounds = live:math:newRect(0,0, 64, 64)
   var isAlive = true

   var update = def(dt) {
      if isAlive {
         pos.x += vel.x * speed * dt.asFraction();
         pos.y += vel.y * speed * dt.asFraction();

         if pos.x > live:getScreenWidth() + bounds.width {
            pos.x = 0
         }
         else if pos.x < -bounds.width {
            pos.x = live:getScreenWidth()
         }

         bounds.x = pos.x
         bounds.y = pos.y

         vel.zeroOut()
      }
   }

   var onKill = null

   var kill = def() {
      isAlive = false

      if onKill {
         onKill(this)
      }
   }
}

/**
   AI system -- responssible for the enemy "AI"
*/   
class AI(game) {
   
   // available enemy formations -- a value of > 0
   // indicates an enemy, the value indicates their speed
   var formations = [   
      [
         0,0,0,0,0,0,0,0,0,0,
         0,0,1,1,0,0,1,1,0,0,
         0,0,0,0,1,1,0,0,0,0,
         0,0,1,0,1,1,0,1,0,0,
         0,0,0,0,0,0,0,0,0,0,
         0,0,0,0,0,0,0,0,0,0,
      ],
      
      [
         0,0,0,0,0,0,0,0,0,0,
         0,0,1,1,1,1,1,1,0,0,
         0,0,0,0,1,1,0,0,0,0,
         0,0,1,1,1,1,1,1,0,0,
         0,0,0,0,2,2,0,0,0,0,
         0,0,0,0,0,0,0,0,0,0,
      ],
      
      [
         3,0,0,0,2,2,0,0,0,3,
         0,0,1,1,1,1,1,1,0,0,
         0,0,0,0,1,1,0,0,0,0,
         2,0,1,0,1,1,0,1,0,2,
         0,0,0,0,1,1,0,0,0,0,
         0,0,0,0,0,0,0,0,0,0,
      ],
      
      [
         3,3,1,1,3,3,1,1,3,3,
         0,0,1,1,0,0,1,1,0,0,
         0,0,0,0,1,1,0,0,0,0,
         2,1,2,0,2,2,0,2,1,2,
         0,0,0,0,1,1,0,0,0,0,
         0,0,0,0,3,3,0,0,0,0,
      ]
   
   ]
   
   var _currentTime = 0
   var currentYDir = 1
   var currentXDir = 1
   var _skip = true
   
   var _numberOfEnemies = 0
   var _level = 0
   
   var think = def(dt) {
      _currentTime += dt.getDeltaTime()
      if _currentTime > 1000 {
         _currentTime = 0
         if _skip {
            currentXDir *= -1            
         }
         _skip = !_skip
         currentYDir *= -1
      }
   }
   var newFlood = def() {
      var WIDTH = 64
      var HEIGHT = 64

      var locations = formations[_level]

      var x = 0
      var y = 0

      _numberOfEnemies = 0
      
      foreach(locations, def(grade) {
         if grade > 0 {
            game.newEnemy(live:math:newVec2(x * WIDTH + WIDTH,y * HEIGHT + HEIGHT), grade, this )
            _numberOfEnemies += 1
         }

         x += 1
         if x >= 10 {
            y += 1
            x = 0
         }
      })
      
      _level = (_level + 1) % length(formations)
   }
   
   /**
      When an enemy dies (callback -- called
      when an enemy is killed)
   */
   var onDeath = def(entity) {
      _numberOfEnemies -= 1
      if _numberOfEnemies <= 0 {
         newFlood()
      }
   }
}



class Game() {
   var ai = new AI(this)
   var entities = []
   var spaceShip = new Entity(live:math:newVec2(300, 500), 450.0)
   var isGameOver = false
   var score = 0

   var initGame = def() {
      isGameOver = false
      score = 0
      
      array:clear(entities)

      spaceShip.isSpaceShip = true
      spaceShip.hits = numberOfHits
      spaceShip.isAlive = true
      
      spaceShip.onKill = def(entity) {
         spaceShip.hits -= 1
         if spaceShip.hits > 0 {
            spaceShip.isAlive = true
         }
         else {
            isGameOver = true
         }
      }

      entities += spaceShip

      newFlood()
   }

   /**
      Main game update method
   */
   var update = def(dt) {      
      ai.think(dt)


      /*
         to remove all the dead entities,
         we first build an array of 'alive' entities
      */
      var aliveEntities = []
      foreach(entities, def(e) if e.isAlive aliveEntities +=e )

      /*
         Now we clear out the entities list and
         add all of the alive entities
      */
      array:clear(entities)
      array:addAll(entities, aliveEntities)

      /*
         Now update each entitiy
      */
      foreach(entities, def(e) e.update(dt) )
   }

   var newFlood = def() {
      ai.newFlood()
   }

   var playerFire = def() {
      newBullet(spaceShip, -1)
   }

   var playerMoveLeft = def() {
      spaceShip.vel.x = -1
   }

   var playerMoveRight = def() {
      spaceShip.vel.x = 1
   }

   var newBullet = def(owner, direction) {
      var bullet = new Entity(live:math:newVec2(owner.pos.x, owner.pos.y + (direction * (owner.bounds.width+10)) ), 200.0)
      bullet.isBullet = true
      bullet.direction = direction
      bullet.bounds.width = 32
      bullet.bounds.height = 32

      bullet.anim = newMissileAnimation();
      
      /**
         override the update method
      */
      var update = bullet.update
      bullet.update = def(dt) {
         update(dt) // call the parents update method
         bullet.vel.y = direction
         
         bullet.anim.update(dt)

         // if the bullet exits the screen, kill it
         if bullet.pos.y < 0 || (bullet.pos.y > live:getScreenHeight() + 80){
            bullet.kill()
         }
         else {
                     
            // do a collision detection with each other entity
            foreach(entities, def(e) {
               
               if e != bullet {                  
                  if e != owner && e.bounds.intersects(bullet.bounds) {
                     if owner.isEnemy && e.isEnemy {
                        // disable friendly fire
                     }
                     else {
                        e.kill()
                        bullet.kill()
                        newExplosion(e.pos)
                     }                                          
                  }
               }
               
            })
         }
      }

      // add the bullet to the list of entities
      entities += bullet
   }

   var newEnemy = def(pos, grade, ai) {
      var enemy = new Entity(live:math:newVec2(pos.x, pos.y), grade * 80)
      enemy.isEnemy = true
      enemy.grade = grade

      var update = enemy.update
      enemy.update = def(dt) {
         enemy.vel.x = ai.currentXDir
         enemy.vel.y = ai.currentYDir

         update(dt)

         // random firing
         if ( sys:random(100/grade) == 10 ) {
            newBullet(enemy, 1)
         }
      }
      
      // when the enemy dies, let the AI system know
      // also add up the points!
      enemy.onKill = def(entity) {
         ai.onDeath(entity)
         score += 1
      }

      entities += enemy
   }
   
   var newExplosion = def(pos) {   
      var explosion = new Entity(live:math:newVec2(pos.x, pos.y), 0.0)
      explosion.isExplosion = true
      
      var anim = newExplosionAnim()
      explosion.anim = anim
      anim.loop(false)       
      
      explosionSnd() // play the explosion sound
      
      explosion.update = def(dt) {                  
         anim.update(dt)         
         if anim.isDone() {
            explosion.kill()
         }
      }

      entities += explosion
   }
}

var game = new Game()
game.initGame()

/*
=============================
Input
=============================
*/

/* key buffer -- this allows for holding keys down (ex. left and right) */
var keyBuf = newArray(256)  


/* key map */
var KEYS = {
    UP -> 38,
    DOWN -> 40,
    LEFT -> 37,
    RIGHT -> 39,

   SPACE -> 32,
}


var keyPressed = def(e) {
    var key = e.getKeyCode()
    keyBuf[key] = true
}

var keyReleased = def(e) {
   var key = e.getKeyCode()
    keyBuf[key] = false

   if key == KEYS.SPACE {
      game.playerFire()
   }
   if key == KEYS.UP {
      game.initGame()
   }
}



/**

Update method

**/
var update = def(dt)
{

   // allow the player to hold LEFT|RIGHT, to move left
   if keyBuf[KEYS.LEFT] {
      game.playerMoveLeft()
   }
   if keyBuf[KEYS.RIGHT] {
      game.playerMoveRight()
   }

   game.update(dt)         
}


/*
=============================
Graphics
=============================
*/
var background = live:gfx:loadImage("./spaceinvaders/stars.jpg") on Error e println(e)
var spaceship = live:gfx:loadImage("./spaceinvaders/spaceship.png") on Error e println(e)
var enemy1 = live:gfx:loadImage("./spaceinvaders/enemy_1.png") on Error e println(e)
var enemy2 = live:gfx:loadImage("./spaceinvaders/enemy_2.png") on Error e println(e)
var enemy3 = live:gfx:loadImage("./spaceinvaders/enemy_3.png") on Error e println(e)

var explosionSnd = def() {
   // TODO : there is a memory leak with the sound API
   /*var explosionSnd = live:sound:loadSound("./spaceinvaders/explosion.wav") on Error e println(e)
   if explosionSnd {
      explosionSnd.setVolume(volume)
      explosionSnd.play()
   }*/
}
var newMissileAnimation = def() {
   return live:anim:newAnimatedSplitImage([{ time -> 200 },  // frame 1 play for 200msec
                                   { time -> 100 },  // frame 2 play for 100msec
                                 ]
                                , "./spaceinvaders/missle.png", 1, 2) // load the image, split by 1 row, 2 columns
                                  on Error e println(e)
}
var newExplosionAnim = def() {
   return live:anim:newAnimatedSplitImage([{ time -> 200 },  
                                  { time -> 100 },  
                                  { time -> 200 }, 
                                  { time -> 100 }, 
                                 ]
                                , "./spaceinvaders/explosion.png", 2, 2) on Error e println(e)   
}
                                     

/**
Render method
**/
var render = def(canvas) {    
   // render background
   canvas.drawScaledImage(background, 0,0, canvas.getWidth() + 20, canvas.getHeight() + 50)

   // render all the entities
    canvas.pushZoom(zoom)
   foreach(game.entities, def(entity) {
      
      if debug {
         canvas.drawRect(entity.pos.x, entity.pos.y, entity.bounds.width, entity.bounds.height, 0xFF00FF)
      }
      
      
      switch {
         when entity.isSpaceShip -> {
            canvas.drawScaledImage(spaceship, entity.pos.x, entity.pos.y, 64, 64)
         }
         when entity.isEnemy -> {
            switch entity.grade {
               when 1 -> canvas.drawScaledImage(enemy1, entity.pos.x, entity.pos.y, 64, 64)
               when 2 -> canvas.drawScaledImage(enemy2, entity.pos.x, entity.pos.y, 64, 64)
               when 3 -> canvas.drawScaledImage(enemy3, entity.pos.x, entity.pos.y, 64, 64)
               else canvas.drawScaledImage(enemy1, entity.pos.x, entity.pos.y, 64, 64)
            }
         }
         when entity.isBullet -> {
            
            if entity.direction <= 0 {
               canvas.drawImage(entity.anim.getCurrentImage(), entity.pos.x, entity.pos.y)
            }
            else {
               canvas.translate(32,32)
               canvas.rotate(180, entity.pos.x, entity.pos.y)
               canvas.drawImage(entity.anim.getCurrentImage(), entity.pos.x, entity.pos.y)
               canvas.rotate(-180, entity.pos.x, entity.pos.y)
               canvas.translate(-32,-32)            
            }            
         }
         when entity.isExplosion -> {
            canvas.translate(32,32)            
            canvas.drawImage(entity.anim.getCurrentImage(), entity.pos.x, entity.pos.y)            
            canvas.translate(-32,-32)            
         }
         else {
            canvas.fillRect(entity.pos.x, entity.pos.y, entity.bounds.width, entity.bounds.height, 0xFF00FF)
         }
      }

   })
   canvas.popZoom()

   canvas.clearTransform()
   
   // render hit count
   canvas.resizeFont(64)
   canvas.boldFont()
   
   var live = " "
   repeat( game.spaceShip.hits, def() {live += "* " })
   canvas.drawString( live, 10, canvas.getHeight(), 0xffff )

   // render the score
   canvas.resizeFont(32)
   canvas.drawString( "Score: " + game.score, 10, 50, 0xffff) 
   
   canvas.drawString( "FPS: " + live:getFps(), canvas.getWidth() - 125, 50, 0xffff)
   
   // render game state
    if game.isGameOver 
   {      
      canvas.resizeFont(64)
      
      canvas.setColor(255, 0, 0, 150)
      canvas.fillRect(0,0,canvas.getWidth() + 50, canvas.getHeight()+50)
      canvas.drawString("G A M E  O V E R", 120, canvas.getHeight()/2, 0xa0a0a0)
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
ivj94 (584 views)
2018-03-24 14:47:39

ivj94 (49 views)
2018-03-24 14:46:31

ivj94 (382 views)
2018-03-24 14:43:53

Solater (63 views)
2018-03-17 05:04:08

nelsongames (109 views)
2018-03-05 17:56:34

Gornova (159 views)
2018-03-02 22:15:33

buddyBro (703 views)
2018-02-28 16:59:18

buddyBro (92 views)
2018-02-28 16:45:17

xxMrPHDxx (493 views)
2017-12-31 17:17:51

xxMrPHDxx (733 views)
2017-12-31 17:15:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!