Forum'da ara:
Ara


Yazar Mesaj
Mesaj24.07.2009, 11:00 (UTC)    
Mesaj konusu: Yılan Oyunu Kodu - DemoStyle

Kod:
<script type="text/javascript"><br />
<br />
<br />
         function cbsnake(){<br />
         <br />
            //Pixels to move at once<br />
            this.jump = 8;<br />
            //Size of snake. Make this one less than jump. Doesn't have to be,but adds good effect<br />
            this.sos = 7;<br />
            //Size of board<br />
            //DANGER!!! this.sofb must be EVENLY dividable by this.jump DANGER!!!!<br />
            this.sofb = 400;<br />
            //Set things up<br />
            this.daway = this.sofb - this.jump;<br />
            this.correct = new Array();<br />
            this.correct[0] = 0;<br />
            while(this.correct[this.correct.length -1] != this.daway){<br />
               this.correct[this.correct.length] = this.correct[this.correct.length -1]+this.jump<br />
            }<br />
            this.zero = 0;<br />
            var gameboard = ' <div class="board" id="board"> <div id="i2">Basit bir yılan oyunu oyuna başlamak için bir seviye seçin.      </div> </div><div class="board" id="score"> <span id="cscore">0</span> <span id="buttons"> <button type="button" id="yavaş" onClick="snake.slow()">yavaş</button> <button type="button" id="medium"  onClick="snake.medium()">orta</button> <button type="button" id="fast"  onClick="snake.fast()">hızlı</button> </span></div>';<br />
            document.write(gameboard);<br />
         }<br />
         <br />
            cbsnake.prototype.setup = function(setspeed){<br />
               var thisObj = this;<br />
               //Score...<br />
               this.score = 0;<br />
               //Snake Direction<br />
               this.sdir = 'none';<br />
               this.sdirb = 'none'; <br />
               this.sdirp = 'none';<br />
               //Snake arrays<br />
               this.ctop = new Array();<br />
               this.cleft = new Array();<br />
               //Top of snake class<br />
               this.ctop[0] = 200;<br />
               this.ctop[1] = -8;<br />
               //Left of Snake class<br />
               this.cleft[0] = 200;<br />
               this.cleft[1] = -8;<br />
               //current top of apple<br />
               this.atop = 0;<br />
               //current left of apple<br />
               this.aleft = 0;<br />
               //Milliseconds between move<br />
               this.speed = setspeed;<br />
               document.getElementById('board').innerHTML = '<div id="apple"></div><div id="snake0" class="snake"></div><div id="snake1" class="snake"></div>';<br />
               this.moveapple();<br />
               this.stopgame = false;<br />
               setTimeout(function(){ thisObj.msnake() },this.speed);<br />
               document.onkeydown = function(e){ return thisObj.snakedir(e); };<br />
            }<br />
            cbsnake.prototype.slow = function(){<br />
               this.setup(100);<br />
               this.buttons('true');<br />
               document.getElementById('slow').blur();<br />
            }<br />
            cbsnake.prototype.medium = function(){<br />
               this.setup(70);<br />
               this.buttons('true');<br />
               document.getElementById('medium').blur();<br />
            }<br />
            cbsnake.prototype.fast = function(){<br />
               this.setup(30);<br />
               this.buttons('true');<br />
               document.getElementById('fast').blur();<br />
            }<br />
            cbsnake.prototype.rannum = function(num1,num2){<br />
               num1 = parseInt(num1);<br />
               num2 = parseInt(num2);<br />
               var generator = Math.random()*(Math.abs(num2-num1));<br />
               generator = Math.round(num1+generator);<br />
               return generator;<br />
            }<br />
            cbsnake.prototype.moveapple = function(){<br />
               var usethis = false;<br />
               while(!usethis){<br />
                  this.atop = this.correct[this.rannum(0,this.correct.length-1)];<br />
                  this.aleft = this.correct[this.rannum(0,this.correct.length-1)];<br />
                  if(this.numInArray(this.ctop,this.cleft,this.atop,this.aleft) == 0){<br />
                     usethis = true;<br />
                  }         <br />
               }<br />
               document.getElementById('apple').style.top = this.atop+"px";<br />
               document.getElementById('apple').style.left = this.aleft+"px";<br />
            }<br />
            cbsnake.prototype.snakedir = function(e){  <br />
                  if(!e){<br />
                     //IE...<br />
                     e = window.event;<br />
                  }<br />
                  switch(e.keyCode){<br />
                     case 38:<br />
                        if(this.sdir != 'down' && this.sdirp != 'down'){<br />
                           this.sdirb = 'up';<br />
                           this.sdirp = 'up';<br />
                        }<br />
                        break;<br />
                     case 40:<br />
                        if(this.sdir != 'up' && this.sdirp != 'up'){<br />
                        this.sdirb = 'down';<br />
                        this.sdirp = 'down';<br />
                        }<br />
                        break;<br />
                     case 37:<br />
                        if(this.sdir != 'right' && this.sdirp != 'right'){<br />
                        this.sdirb = 'left';<br />
                        this.sdirp = 'left';<br />
                        }<br />
                        break;<br />
                     case 39:<br />
                        if(this.sdir != 'left' && this.sdirp != 'left'){<br />
                        this.sdirb = 'right';<br />
                        this.sdirp = 'right';<br />
                        }<br />
                        break;<br />
                     case 32:<br />
                        if(this.sdir == 'none' && this.sdirp != 'none'){<br />
                           this.sdirb = this.sdirp;<br />
                           this.sdirp = 'none';<br />
                        }<br />
                        else{<br />
                        this.sdirp = this.sdir;<br />
                        this.sdirb = 'none';<br />
                        }<br />
                        break;<br />
                  }<br />
                  return this.stopgame;<br />
                  <br />
            }<br />
            cbsnake.prototype.msnake = function(){<br />
               if(this.stopgame === false){<br />
                  if(this.sdir != 'none'){<br />
                     this.moveall();<br />
                  }<br />
                  var thisObj = this;<br />
                  switch(this.sdir){<br />
                        case 'up':<br />
                           this.ctop[0] = this.ctop[0] - this.jump;<br />
                           document.getElementById('snake0').style.top = this.ctop[0]+"px";<br />
                           if((this.ctop[0] == this.zero && this.sdirb == 'up') || this.ctop[0] < this.zero){<br />
                              this.gover();<br />
                           }<br />
                           break;<br />
                        case 'down':<br />
                           this.ctop[0] = this.ctop[0] + this.jump;<br />
                           document.getElementById('snake0').style.top = this.ctop[0]+"px";<br />
                           if((this.ctop[0] == this.daway && this.sdirb == 'down') || this.ctop[0] > this.daway){<br />
                              this.gover();<br />
                           }<br />
                           break;<br />
                        case 'left':<br />
                           this.cleft[0] = this.cleft[0] - this.jump;<br />
                           document.getElementById('snake0').style.left = this.cleft[0]+"px";<br />
                           if((this.cleft[0] == this.zero && this.sdirb == 'left') || this.cleft[0] < this.zero){<br />
                              this.gover();<br />
                           }<br />
                           break;<br />
                        case 'right':<br />
                           this.cleft[0] = this.cleft[0] + this.jump;<br />
                           document.getElementById('snake0').style.left = this.cleft[0]+"px";<br />
                           if((this.cleft[0] == this.daway && this.sdirb == 'right') || this.cleft[0] > this.daway){<br />
                              this.gover();<br />
                           }<br />
                           break;<br />
                  }<br />
                  if(this.sdir != 'none'){<br />
                     this.hitself();<br />
                     this.happle();<br />
                  }<br />
               this.sdir = this.sdirb<br />
               setTimeout(function(){ thisObj.msnake() },this.speed);<br />
               }<br />
            }<br />
            cbsnake.prototype.gover = function(){<br />
               if(!this.stopgame){<br />
                  this.stopgame = true;<br />
                  var inner = document.getElementById('board').innerHTML;<br />
                  document.getElementById('board').innerHTML = inner+'<div id="notice">oyun bitti senin puanın '+this.score+'</div><div id="i2">Yılan oyununu <a href="http://htmlkod.tr.gg">sitene ekle</a></div>';<br />
                  document.getElementById('apple').style.backgroundColor = '#D7BEBE';<br />
                  for(i=0;i<this.cleft.length;i++){<br />
                     document.getElementById('snake'+i).style.backgroundColor = '#BEBEBE';<br />
                  }<br />
                  this.buttons('');<br />
               }<br />
            }<br />
            cbsnake.prototype.happle = function(){<br />
               if(this.atop == this.ctop[0] && this.aleft == this.cleft[0]){<br />
                  //HIT!!!<br />
                  this.score++;<br />
                  document.getElementById('cscore').innerHTML = this.score;<br />
                  this.moveapple();<br />
                  this.addsnake();<br />
               }<br />
            }<br />
            cbsnake.prototype.addsnake = function(){<br />
            var newsnake = document.createElement('div');<br />
            var newid = 'snake'+this.cleft.length;<br />
            newsnake.setAttribute('id',newid);<br />
            //this crap is for IE. I would rather add the class name.<br />
            newsnake.style.position = 'absolute';<br />
            newsnake.style.top = '-10px';<br />
            newsnake.style.left = '-10px';<br />
            newsnake.style.display = 'none';<br />
            newsnake.style.backgroundColor = 'black';<br />
            newsnake.style.height = '7px';<br />
            newsnake.style.width = '7px';<br />
            newsnake.style.overflow = 'hidden';<br />
            document.getElementById('board').appendChild(newsnake);<br />
            this.cleft[this.cleft.length] = -10;<br />
            this.ctop[this.ctop.length] = -10;<br />
            }<br />
            cbsnake.prototype.moveall = function(){<br />
               var i = this.ctop.length - 1;<br />
               while(i != 0){<br />
                  document.getElementById('snake'+i).style.top = document.getElementById('snake'+(i-1)).style.top;<br />
                  document.getElementById('snake'+i).style.left = document.getElementById('snake'+(i-1)).style.left;<br />
                  document.getElementById('snake'+i).style.display = 'block';<br />
                  this.ctop[i] = this.ctop[i-1];<br />
                  this.cleft[i] = this.cleft[i-1];<br />
                  i = i - 1;<br />
               }<br />
            }<br />
            cbsnake.prototype.numInArray = function(array,array2,value,value2){<br />
               var n = 0;<br />
               for (var i=0; i < array.length; i++) {<br />
                  if (array[i] === value && array2[i] === value2) {<br />
                     n++;<br />
                  }<br />
               }<br />
               return n;<br />
            }<br />
            cbsnake.prototype.hitself = function(){<br />
               if(this.numInArray(this.ctop,this.cleft,this.ctop[0],this.cleft[0]) > 1){<br />
                  this.gover();<br />
               }<br />
            }<br />
            cbsnake.prototype.buttons = function(setto){<br />
               document.getElementById('slow').disabled = setto;<br />
               document.getElementById('medium').disabled = setto;<br />
               document.getElementById('fast').disabled = setto;<br />
            }<br />
      </script>       <style type="text/css">          .board{          width: 399px;          background-color: lightgrey;          border: 1px solid gray;          position: relative;          margin-left: 0;          margin-top: 0;          }          #board{          height: 399px;          border-bottom: 0px;          }          #apple{          position: absolute;          background-color: red;          height: 7px;          width: 7px;          overflow: hidden;          }          .snake{          position: absolute;          top: 200px;          left: 200px;          background-color: black;          height: 7px;          width: 7px;          overflow: hidden;          }          .snake2{          position: absolute;          top: -10px;          left: -10px;          background-color: black;          height: 7px;          width: 7px;          overflow: hidden;          }          #score{          height: 50px;          margin-top: 0px;          }          #cscore{          color: black;          padding-left: 10px;          float: left;          width: 25%;          font-size: xx-large;          }          #buttons{          float: right;          width: 50%;          text-align: right;          padding-top: 10px;          }          #notice{          position: absolute;          top: 1em;          left: 1em;          right: 1em;          text-align: center;          font-size: 150%;          }          #i2{          position: absolute;          bottom: 1em;          left: 1em;          right: 1em;          text-align: center;          font-size: 95%;          }       </style>          <script type="text/javascript"><br />
var snake = new cbsnake();<br />
</script>        
Önceki mesajları göster:   


Powered by phpBB © 2001, 2005 phpBB Group
Türkçe Çeviri: phpBB Türkiye & Erdem Çorapçıoğlu