Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var xsnake={};
- var xpsnake;
- var xptail=[];
- var xtail=[];
- var xtotal=0;
- function xsnake_dir(x,y){
- xsnake.xdir=x;
- xsnake.ydir=y;
- }
- function xeat(){
- var d= euclideanDistance(xsnake.x,xsnake.y,food.x,food.y);
- if(d < 1){
- xtotal++;
- console.log(xtotal);
- document.getElementById("cscore").innerHTML = "Computer Score = " + xtotal;
- return true;
- }
- else{
- return false;
- }
- }
- function xinit_snake(){
- xsnake.x=100;
- xsnake.y=100;
- xsnake.xdir=1;
- xsnake.ydir=0;
- var geometry = new THREE.BoxGeometry(20,20,20);
- var cubeMaterials = [
- new THREE.MeshBasicMaterial({color:0x2c156b, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0x2c156b, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0x2c156b, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0x2c156b, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0x4c11ef, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0x2c156b, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- ];
- var material = new THREE.MeshFaceMaterial(cubeMaterials);
- xpsnake = new THREE.Mesh(geometry, material);
- xpsnake.position.set(xsnake.x,xsnake.y,6);
- scene.add( xpsnake);
- }
- function xupdate_snake(){
- for(var i=0;i<xtail.length-1;i++){
- xtail[i]=xtail[i+1];
- }
- if(xtotal>=1){
- temp ={}
- temp.x=xsnake.x;
- temp.y=xsnake.y;
- temp.xdir=xsnake.xdir;
- temp.ydir=xsnake.ydir;
- xtail[xtotal-1]=temp;
- }
- xsnake.x=xsnake.x+xsnake.xdir*20;
- xsnake.y=xsnake.y+xsnake.ydir*20;
- for(var i=0;i<xtail.length;i++){
- xptail[i].position.set(xtail[i].x,xtail[i].y,6)
- }
- xpsnake.position.set(xsnake.x,xsnake.y,6);
- }
- function xdeath(){
- for(var i=0;i<xtail.length;i++){
- var d = euclideanDistance(xsnake.x,xsnake.y,xtail[i].x,xtail[i].y);
- if(d<1){
- console.log("I died in xdeath")
- xtotal=0;
- document.getElementById("cscore").innerHTML = "Computer Score = 0";
- while(xtail.length){
- xtail.pop();
- }
- while(xptail.length){
- scene.remove(xptail.pop());
- }
- }
- }
- }
- function detect_xsnake_collision_to_snake(){
- for(var i=0;i<python_tail.length;i++){
- var d=euclideanDistance(xsnake.x,xsnake.y,python_tail[i].x,python_tail[i].y);
- if(d<1){
- console.log("I died after collision with other snake")
- xtotal=0;
- while(xtail.length){
- xtail.pop();
- }
- while(xptail.length){
- scene.remove(xptail.pop());
- }
- xsnake.x=random(-200,200);
- xsnake.y=random(-200,200);;
- xsnake.xdir=1;
- xsnake.ydir=0;
- xpsnake.position.set(xsnake.x,xsnake.y,6)
- }
- }
- }
- function random_xsnake(){
- var ii=random(0,100);
- if(ii<70)return;
- var i=random(0,100);
- if(i>=0&&i<=25) {
- xsnake_dir(0,1);
- }
- else if(i>=26&&i<=50) {
- xsnake_dir(-1,0);
- }
- if(i>=51&&i<=75) {
- xsnake_dir(0,-1);
- }
- if(i>=76&&i<=102) {
- xsnake_dir(1,0);
- }
- // var xd_ = 0;
- // var yd_ = 0;
- // if(food.x - xsnake.x < 0){
- // xd_ = -1;
- // }else if(food.x - xsnake.x > 0){
- // xd_ = 1;
- // }
- // if(food.y - xsnake.y < 0){
- // yd_ = -1;
- // }else if(food.y - xsnake.y > 0){
- // yd_ = 1;
- // }
- // if(xsnake.xdir + xd_ == 0){
- // console.log("x prob")
- // xsnake_dir(0, 1);
- // return ;
- // }
- // else if(xsnake.ydir + yd_ == 0){
- // console.log("yprob")
- // xsnake_dir(1,0);
- // return ;
- // }
- // if(xd_ == 0 && yd_ == 0){
- // // var ii=random(0,100);
- // // if(ii<70)return;
- // var i=random(0,100);
- // if(i>=0&&i<=25) {
- // xsnake_dir(0,1);
- // }
- // else if(i>=26&&i<=50) {
- // xsnake_dir(-1,0);
- // }
- // if(i>=51&&i<=75) {
- // xsnake_dir(0,-1);
- // }
- // if(i>=76&&i<=102) {
- // xsnake_dir(1,0);
- // }
- // return;
- // }
- // var i = random(0,100);
- // if(i < 50){
- // xsnake_dir(xd_, 0);
- // }else{
- // xsnake_dir(0, yd_);
- // }
- }
- function xhit_wall(){
- if(xsnake.x>200||xsnake.x<-200||xsnake.y>200||xsnake.y<-200){
- console.log("I collided with wall")
- xtotal=0;
- document.getElementById("cscore").innerHTML = "Computer Score = 0";
- while(xtail.length){
- xtail.pop();
- }
- while(xptail.length){
- scene.remove(xptail.pop());
- }
- xsnake.x=0;
- xsnake.y=0;
- xsnake.xdir=1;
- xsnake.ydir=0;
- xpsnake.position.set(0,0,6);
- }
- }
- var food={};
- var pfood;
- var time_interval_food=0;
- function pickLocation(){
- var cols =random(-10,10);
- var rows =random(-10,10);
- cols=Math.floor(cols);
- rows=Math.floor(rows);
- food.x=cols*20;
- food.y=rows*20;
- }
- function init_food(){
- pickLocation();
- var geometry = new THREE.BoxGeometry(20,20,10);
- var cubeMaterials = [
- new THREE.MeshBasicMaterial({color:0xa12834, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0xa12834, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0xa12834, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0xa12834, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0xfc071f, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0xa12834, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- ];
- var material = new THREE.MeshFaceMaterial(cubeMaterials);
- pfood = new THREE.Mesh( geometry, material );
- pfood.position.set(food.x,food.y,6);
- scene.add( pfood );
- }
- function update_food(){
- time_interval_food=0;
- pickLocation();
- pfood.position.set(food.x,food.y,6);
- }
- function xadd_tail_element(){
- console.log("why am i not here?")
- temp= {}
- temp.x=xsnake.x;
- temp.y=xsnake.y;
- temp.xdir=xsnake.xdir;
- temp.ydir=xsnake.ydir;
- xtail.push(temp);
- var geometry = new THREE.BoxGeometry(20,20,20);
- var cubeMaterials = [
- new THREE.MeshBasicMaterial({color:0x174d17, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0x174d17, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0x174d17, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0x174d17, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0x00ff00, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- new THREE.MeshBasicMaterial({color:0x174d17, transparent:true, opacity:0.5, side: THREE.DoubleSide}),
- ];
- var material = new THREE.MeshFaceMaterial(cubeMaterials);
- xptail.push(new THREE.Mesh( geometry, material ));
- xptail[xptail.length-1].position.set(xtail[xtail.length-1].x,xtail[xtail.length-1].y,6);
- scene.add(xptail[xptail.length-1]);
- }
- var python = {};
- var python_tail = [];
- var python_tail_count = 0;
- var player_score = 0;
- function keypressed(){
- document.addEventListener( 'keydown' , function( event ) {
- switch( event.keyCode ){
- case 38:
- if(python.ydir != 0)
- break;
- python.xdir = 0;
- python.ydir = 1;
- break;
- case 40:
- if(python.ydir != 0)
- break;
- python.xdir = 0;
- python.ydir = -1;
- break;
- case 37:
- if(python.xdir != 0)
- break;
- python.xdir = -1;
- python.ydir = 0;
- break;
- case 39:
- if(python.xdir != 0)
- break;
- python.xdir = 1;
- python.ydir = 0;
- break;
- }
- });
- }
- function eat(){
- if( euclideanDistance( python.x , python.y , food.x , food.y ) < 1){
- player_score = player_score + 1;
- document.getElementById("pscore").innerHTML = "Player Score : " + player_score;
- return 1;
- }
- return 0;
- }
- function starting_position(){
- python.x = 0;
- python.y = 0;
- python.xdir = Math.floor((Math.random() * 3) - 1);
- if(python.xdir == 0){
- python.ydir = Math.floor(Math.random() * 2);
- if(python.ydir == 0)
- python.ydir = -1;
- else python.ydir = 1;
- }else{
- python.ydir = 0;
- }
- // console.log(python.xdir + " : " + python.ydir);
- }
- function player_snake_setup(){
- starting_position();
- // python.x = 0;
- // python.y = 0;
- // python.xdir = Math.floor((Math.random() * 3) - 1);
- // if(python.xdir == 0){
- // python.ydir = Math.floor(Math.random() * 2);
- // if(python.ydir == 0)
- // python.ydir = -1;
- // }else{
- // python.ydir = 0;
- // }
- // console.log("Y this kolaveri di?")
- var geometry = new THREE.BoxGeometry( 20 , 20 , 20 );
- var cubeMaterials = [
- new THREE.MeshBasicMaterial( {color:0xa8794c, isMeshBasicMaterial: true} ),
- new THREE.MeshBasicMaterial( {color:0xa8794c, isMeshBasicMaterial: true} ),
- new THREE.MeshBasicMaterial( {color:0xa8794c, isMeshBasicMaterial: true} ),
- new THREE.MeshBasicMaterial( {color:0xa8794c, isMeshBasicMaterial: true} ),
- new THREE.MeshBasicMaterial( {color:0x4e032b, isMeshBasicMaterial: true} ),
- new THREE.MeshBasicMaterial( {color:0xa8794c, isMeshBasicMaterial: true} ),
- ];
- var material = new THREE.MeshFaceMaterial( cubeMaterials );
- python.head = new THREE.Mesh( geometry , material );
- python.head.position.set( python.x , python.y , 6 );
- scene.add( python.head );
- }
- function update_snake(){
- for(var i=0;i<python_tail.length-1;i++){
- python_tail[i].x=python_tail[i+1].x;
- python_tail[i].y=python_tail[i+1].y;
- python_tail[i].xdir=python_tail[i+1].xdir;
- python_tail[i].ydir=python_tail[i+1].ydir;
- }
- if(python_tail_count>=1){
- temp ={}
- python_tail[python_tail_count-1].x=python.x;
- python_tail[python_tail_count-1].y=python.y;
- python_tail[python_tail_count-1].xdir=python.xdir;
- python_tail[python_tail_count-1].ydir=python.ydir;
- }
- python.x=python.x+python.xdir*20;
- python.y=python.y+python.ydir*20;
- for(var i=0;i<python_tail.length;i++){
- python_tail[i].cube.position.set(python_tail[i].x,python_tail[i].y,6);
- }
- python.head.position.set(python.x,python.y,6);
- }
- function death(){
- for(var i=0;i<python_tail.length;i++){
- var d = euclideanDistance(python.x,python.y,python_tail[i].x,python_tail[i].y);
- if(d<1){
- player_score = 0;
- document.getElementById("pscore").innerHTML = "Player Score : " + player_score;
- python_tail_count=0;
- for(var i=0;i<python_tail.length;i++){
- scene.remove(python_tail[i].cube);
- }
- while(python_tail.length){
- python_tail.pop();
- }
- }
- }
- }
- function detect_snake_collision_to_xsnake(){
- for(var i=0;i<xtail.length;i++){
- var d=euclideanDistance(python.x,python.y,xtail[i].x,xtail[i].y);
- if(d<1){
- player_score = 0;
- document.getElementById("pscore").innerHTML = "Player Score : " + player_score;
- python_tail_count=0;
- for(var i=0;i<python_tail.length;i++){
- scene.remove(python_tail[i].cube);
- }
- while(python_tail.length){
- python_tail.pop();
- }
- // python.x=random(-200,200);
- // python.y=random(-200,200);;
- // python.xdir=1;
- // python.ydir=0;
- starting_position();
- python.head.position.set(python.x,python.y,6)
- }
- }
- }
- function hit_wall(){
- if(python.x>200||python.x<-200||python.y>200||python.y<-200){
- player_score = 0;
- document.getElementById("pscore").innerHTML = "Player Score : " + player_score;
- python_tail_count=0;
- for(var i=0;i<python_tail.length;i++){
- scene.remove(python_tail[i].cube);
- }
- while(python_tail.length){
- python_tail.pop();
- }
- // python.x=0;
- // python.y=0;
- // python.xdir=1;
- // python.ydir=0;
- starting_position();
- python.head.position.set(0,0,6);
- }
- }
- function add_tail_element(){
- temp= {}
- temp.x=python.x;
- temp.y=python.y;
- temp.xdir=python.xdir;
- temp.ydir=python.ydir;
- var geometry = new THREE.BoxGeometry(20,20,20);
- var cubeMaterials = [
- new THREE.MeshBasicMaterial({color:0x4e032b, isMeshBasicMaterial: true}),
- new THREE.MeshBasicMaterial({color:0x4e032b, isMeshBasicMaterial: true}),
- new THREE.MeshBasicMaterial({color:0x4e032b, isMeshBasicMaterial: true}),
- new THREE.MeshBasicMaterial({color:0x4e032b, isMeshBasicMaterial: true}),
- new THREE.MeshBasicMaterial({color:0xb3aaa2, isMeshBasicMaterial: true}),
- new THREE.MeshBasicMaterial({color:0x4e032b, isMeshBasicMaterial: true}),
- ];
- var material = new THREE.MeshFaceMaterial(cubeMaterials);
- temp.cube=new THREE.Mesh( geometry, material );
- python_tail.push(temp);
- python_tail[python_tail.length-1].cube.position.set(python_tail[python_tail.length-1].x,python_tail[python_tail.length-1].y,6);
- scene.add(python_tail[python_tail.length-1].cube);
- }
- var scene;
- var camera;
- var renderer;
- function random ( a , b ) {
- return Math.random() * ( b - a ) + a;
- }
- function euclideanDistance( p , q , a , b ){
- var x = p - a;
- var y = q - b;
- return Math.sqrt( ( x * x ) + ( y * y ) );
- }
- function detect_head_head_collision(){
- var d=euclideanDistance(xsnake.x,xsnake.y,python.x,python.y);
- if(d<1){
- python_tail_count=0;
- for(var i=0;i<python_tail.length;i++){
- scene.remove(python_tail[i].cube);
- }
- while(python_tail.length){
- python_tail.pop();
- }
- xtotal=0;
- while(xtail.length){
- xtail.pop();
- }
- while(xptail.length){
- scene.remove(xptail.pop());
- }
- python.x=random(-200,200);
- python.y=random(-200,200);
- python.xdir=1;
- python.ydir=0;
- python.head.position.set(python.x,python.y,6);
- xsnake.x=random(-200,200);
- xsnake.y=random(-200,200);
- xsnake.xdir=1;
- xsnake.ydir=0;
- xpsnake.position.set(xsnake.x,xsnake.y,6)
- }
- }
- function fence(){
- var geometry = new THREE.BoxGeometry( 20 , 420 , 30 );
- var left_bar = [
- new THREE.MeshBasicMaterial( {color:0x03174e} ),
- new THREE.MeshBasicMaterial( {color:0x03174e} ),
- new THREE.MeshBasicMaterial( {color:0x03174e} ),
- new THREE.MeshBasicMaterial( {color:0x03174e} ),
- new THREE.MeshBasicMaterial( {color:0x0144fd} ),
- new THREE.MeshBasicMaterial( {color:0x03174e} ),
- ];
- var material = new THREE.MeshFaceMaterial( left_bar );
- var left = new THREE.Mesh( geometry , material );
- left.position.set( -220 , 0 , 6 );
- scene.add( left );
- var geometry = new THREE.BoxGeometry( 460 , 20 , 30 );
- var bottom_bar = [
- new THREE.MeshBasicMaterial( {color:0x11255d} ),
- new THREE.MeshBasicMaterial( {color:0x11255d} ),
- new THREE.MeshBasicMaterial( {color:0x11255d} ),
- new THREE.MeshBasicMaterial( {color:0x11255d} ),
- new THREE.MeshBasicMaterial( {color:0x0144fd} ),
- new THREE.MeshBasicMaterial( {color:0x11255d} ),
- ];
- var material = new THREE.MeshFaceMaterial( bottom_bar );
- var bottom = new THREE.Mesh( geometry , material );
- bottom.position.set( 0 , -220 , 6 );
- scene.add( bottom );
- var geometry = new THREE.BoxGeometry( 20 , 420 , 30 );
- var right_bar = [
- new THREE.MeshBasicMaterial( {color:0x03174e} ),
- new THREE.MeshBasicMaterial( {color:0x03174e} ),
- new THREE.MeshBasicMaterial( {color:0x03174e} ),
- new THREE.MeshBasicMaterial( {color:0x03174e} ),
- new THREE.MeshBasicMaterial( {color:0x0144fd} ),
- new THREE.MeshBasicMaterial( {color:0x03174e} ),
- ];
- var material = new THREE.MeshFaceMaterial( right_bar );
- var right = new THREE.Mesh( geometry , material );
- right.position.set( 220 , 0 , 6 );
- scene.add( right );
- var geometry = new THREE.BoxGeometry( 460 , 20 , 30 );
- var top_bar = [
- new THREE.MeshBasicMaterial( {color:0x11255d} ),
- new THREE.MeshBasicMaterial( {color:0x11255d} ),
- new THREE.MeshBasicMaterial( {color:0x11255d} ),
- new THREE.MeshBasicMaterial( {color:0x11255d} ),
- new THREE.MeshBasicMaterial( {color:0x0144fd} ),
- new THREE.MeshBasicMaterial( {color:0x11255d} ),
- ];
- var material = new THREE.MeshFaceMaterial( top_bar );
- var top = new THREE.Mesh( geometry , material );
- top.position.set(0 , 220 , 6);
- scene.add( top );
- }
- function setup(){
- scene = new THREE.Scene();
- var aspect_ratio = window.innerWidth / window.innerHeight;
- camera = new THREE.PerspectiveCamera( 40 , aspect_ratio , 1 , 1000 );
- camera.position.set( 0 , 0 , 700 );
- camera.lookAt( 0 , 0 , 0 );
- renderer = new THREE.WebGLRenderer();
- renderer.setSize( window.innerWidth , window.innerHeight );
- document.body.appendChild( renderer.domElement );
- }
- function base(){
- var geometry = new THREE.PlaneGeometry( 410 , 410 );
- var material = new THREE.MeshBasicMaterial( {color: 0x1eacdc , side: THREE.DoubleSide , linewidth: 5 } ); ///change
- var plane = new THREE.Mesh( geometry , material );
- scene.add( plane );
- }
- function main() {
- setup();
- base();
- fence();
- player_snake_setup();
- keypressed();
- xinit_snake();
- init_food();
- renderer.render( scene, camera );
- var animate = function () {
- setTimeout( function() {
- requestAnimationFrame( animate );
- }, 4000 / 30 );
- renderer.render( scene, camera );
- if(eat()== 1){
- python_tail_count++;
- time_interval_food=0;
- add_tail_element();
- update_food();
- }
- if(xeat()== true){
- time_interval_food=0;
- xadd_tail_element();
- update_food();
- }
- if(time_interval_food>150){
- time_interval_food=0;
- update_food();
- }
- time_interval_food++;
- update_snake();
- xupdate_snake();
- death();
- xdeath();
- hit_wall();
- xhit_wall();
- random_xsnake();
- detect_head_head_collision();
- detect_xsnake_collision_to_snake();
- detect_snake_collision_to_xsnake();
- // keypressed();
- };
- animate();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement