Текущая версия |
Ваш текст |
Строка 1: |
Строка 1: |
− | ==Описание==
| + | {{#widget:Iframe | url=http://tm.spbstu.ru/htmlets/js2020/Radeon/index.html | width=1000 | height= 1000 | border=0}} |
− | | |
− | Реализация компьютерной игры "3 в ряд" на языке программирования JavaScript
| |
− | | |
− | Исполнитель: [[Обловатный Родион|Обловатный Родион]] и [[Гусика Никита|Гусика Никита]]
| |
− | | |
− | Группа 3630103/90001, Кафедра Теоретической механики
| |
− | | |
− | | |
− | ==Визуализация==
| |
− | {{#widget:Iframe |url=http://tm.spbstu.ru/htmlets/js2020/Radeon/index.html | width=1000 | height= 1000 | border=0 }} | |
− | ==Код программы==
| |
− | <div class="mw-collapsible mw-collapsed">
| |
− | '''Код программы на языке JavaScript:''' <div class="mw-collapsible-content">
| |
− | <syntaxhighlight lang="javascript" line start="1" enclose="div">
| |
− | var mode=0;
| |
− | var maxstep=200;
| |
− | var maxtime=10;
| |
− | | |
− | var cell_x = 8;
| |
− | var cell_y = 8;
| |
− | | |
− | var colors=5;
| |
− | | |
− | var cell_bc1 = "rgba(230,230,230,0.5)";
| |
− | var cell_bc2 = "rgba(200,200,200,0.5)";
| |
− | var cell_size = 80; //Размер клетки
| |
− | | |
− | var bv = 0.01;
| |
− | | |
− | var bw = 20;
| |
− | var bc = 4;
| |
− | var bb = "rgba(200,255,200,0.7)";
| |
− | var bcc = "rgba(111,111,111,0.9)";
| |
− | | |
− | var speed=100;
| |
− | | |
− | var ctx = cnv.getContext('2d');
| |
− | var score=0;
| |
− | var w = 2*(bw+bc)+cell_x*cell_size;
| |
− | var h = 2*(bw+bc)+cell_y*cell_size;
| |
− | | |
− | var timer= new Date()/1000;
| |
− | var steps=0;
| |
− | | |
− | var img1=new Image();
| |
− | var img2=new Image();
| |
− | var img3=new Image();
| |
− | var img4=new Image();
| |
− | var img5=new Image();
| |
− | var imgb=new Image();
| |
− | var explodeimg =new Image();
| |
− | var arr = [];
| |
− | | |
− | var play=true;
| |
− | | |
− | var pressed=false;
| |
− | var g_x;
| |
− | var g_y;
| |
− | var l_x;
| |
− | var l_y;
| |
− | | |
− | window.onload = function (){
| |
− | img1.src='img/1.png';
| |
− | img2.src='img/2.png';
| |
− | img3.src='img/3.png';
| |
− | img4.src='img/4.png';
| |
− | img5.src='img/5.png';
| |
− | imgb.src='img/bonus.png';
| |
− | explodeimg.src='img/explode.png';
| |
− | cnv.width = w;
| |
− | cnv.height = h;
| |
− | draw_back();
| |
− | add_start_stones();
| |
− | score=0;
| |
− | if(mode==1){
| |
− | steper_t.innerHTML="Шагов осталось";
| |
− | }
| |
− | else{
| |
− | steper_t.innerHTML="Шаги";
| |
− | }
| |
− | if(mode==2){
| |
− | timer_t.innerHTML="Осталось секунд";
| |
− | }
| |
− | else{
| |
− | timer_t.innerHTML="Время";
| |
− | }
| |
− | checker=setInterval(play_checker,speed);
| |
− | }
| |
− | | |
− | function draw_current_stones(){
| |
− | for(var x=0;x<cell_x;x++){
| |
− | for(var y=0;y<cell_y;y++){
| |
− | if(arr[x][y].alive){
| |
− | ctx.drawImage(arr[x][y].img,bw+bc+(x)*cell_size,bw+bc+(y)*cell_size,cell_size,cell_size);
| |
− | if(arr[x][y].img==explodeimg){
| |
− | arr[x][y].alive=false;
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | | |
− | function stone_gen(){
| |
− | score+=10;
| |
− | if(Math.random()>bv){
| |
− | color=parseInt(1+Math.random()*(colors-0.01));
| |
− | }
| |
− | else{
| |
− | color=0;
| |
− | }
| |
− | return new stone(color);
| |
− | }
| |
− | | |
− | function add_start_stones(){
| |
− | var color;
| |
− | for(var x=0;x<cell_x;x++){
| |
− | arr.push([]);
| |
− | for(var y=0;y<cell_y;y++){
| |
− | arr[arr.length-1].push(stone_gen());
| |
− | }
| |
− | }
| |
− | while(check()){
| |
− | move_stones_auto_silent();
| |
− | }
| |
− | setTimeout(draw_current_stones, 100);
| |
− | }
| |
− | | |
− | function draw_back(){
| |
− | ctx.fillStyle = bb;
| |
− | ctx.strokeStyle = bb;
| |
− | ctx.fillRect(0,0,w,h);
| |
− | ctx.strokeRect(0,0,w,h);
| |
− |
| |
− | ctx.fillStyle = bcc;
| |
− | ctx.strokeStyle = bcc;
| |
− | ctx.fillRect(bw-bc,bw-bc,w-(bw-bc)*2,h-(bw-bc)*2);
| |
− | ctx.strokeRect(bw-bc,bw-bc,w-(bw-bc)*2,h-(bw-bc)*2);
| |
− | ctx.clearRect(bw+bc,bw+bc,w-(bw+bc)*2,h-(bw+bc)*2);
| |
− |
| |
− | for(var x=0;x<cell_x;x++){
| |
− | for(var y=0;y<cell_y;y++){
| |
− | if((x+y)%2==0){
| |
− | ctx.fillStyle = cell_bc1;
| |
− | ctx.strokeStyle = cell_bc1;
| |
− | }
| |
− | else{
| |
− | ctx.fillStyle = cell_bc2;
| |
− | ctx.strokeStyle = cell_bc2;
| |
− | }
| |
− | ctx.fillRect(bw+bc+(x)*cell_size,bw+bc+(y)*cell_size,cell_size,cell_size);
| |
− | ctx.strokeRect(bw+bc+(x)*cell_size,bw+bc+(y)*cell_size,cell_size,cell_size);
| |
− | }
| |
− | }
| |
− | }
| |
− | | |
− | function stone(type){
| |
− | this.type=type;
| |
− | switch(type){
| |
− | case 0:
| |
− | this.img=imgb;
| |
− | break;
| |
− | case 1:
| |
− | this.img=img1;
| |
− | break;
| |
− | case 2:
| |
− | this.img=img2;
| |
− | break;
| |
− | case 3:
| |
− | this.img=img3;
| |
− | break;
| |
− | case 4:
| |
− | this.img=img4;
| |
− | break;
| |
− | case 5:
| |
− | this.img=img5;
| |
− | break;
| |
− | }
| |
− | this.alive=true;
| |
− | }
| |
− | | |
− | function check(cucle=false){
| |
− | if(play){
| |
− | var color=0;
| |
− | var count=0;
| |
− | var changed=false;
| |
− | for(var x=0;x<cell_x;x++){
| |
− | count=0;
| |
− | for(var y=0;y<cell_y;y++){
| |
− | if(color==arr[x][y].type){
| |
− | count++;
| |
− | if(y+1>=cell_y){
| |
− | if(count>=3){
| |
− | changed=true;
| |
− | for(var z=y-count+1;z<=y;z++){
| |
− | arr[x][z].alive=false;
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | else{
| |
− | if(count>=3){
| |
− | changed=true;
| |
− | for(var z=y-count;z<y;z++){
| |
− | arr[x][z].alive=false;
| |
− | }
| |
− | }
| |
− | color=arr[x][y].type;
| |
− | count=1;
| |
− | }
| |
− | }
| |
− | }
| |
− | for(y=0;y<cell_y;y++){
| |
− | count=0;
| |
− | for(x=0;x<cell_x;x++){
| |
− | if(color==arr[x][y].type){
| |
− | count++;
| |
− | if(x+1>=cell_x){
| |
− | if(count>=3){
| |
− | changed=true;
| |
− | for(var z=x-count+1;z<=x;z++){
| |
− | arr[z][y].alive=false;
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | else{
| |
− | if(count>=3){
| |
− | changed=true;
| |
− | for(var z=x-count;z<x;z++){
| |
− | arr[z][y].alive=false;
| |
− | }
| |
− | }
| |
− | color=arr[x][y].type;
| |
− | count=1;
| |
− | }
| |
− | }
| |
− | }
| |
− | if(cucle){
| |
− | move_stones_auto();
| |
− | }
| |
− | return changed;
| |
− | }
| |
− | }
| |
− | | |
− | function move_stones_auto(){
| |
− | inter=setInterval(drawer,speed);
| |
− | }
| |
− | | |
− | function move_stones_auto_silent(){
| |
− | var gravity;
| |
− | for(var x=0;x<cell_x;x++){
| |
− | gravity=true;
| |
− | while(gravity){
| |
− | gravity=false;
| |
− | if(arr[x][0].alive==false){
| |
− | arr[x][0]=stone_gen();
| |
− | gravity=true;
| |
− | }
| |
− | for(var y=0;y<cell_y-1;y++){
| |
− | if(arr[x][y+1].alive==false&arr[x][y].alive==true){
| |
− | arr[x][y+1].type=arr[x][y].type;
| |
− | arr[x][y+1].img=arr[x][y].img;
| |
− | arr[x][y+1].alive=true;
| |
− | arr[x][y].alive=false;
| |
− | gravity=true;
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | | |
− | function drawer(){
| |
− | gravity=false;
| |
− | for(var x=0;x<cell_x;x++){
| |
− |
| |
− | if(arr[x][0].alive==false){
| |
− | arr[x][0]=stone_gen();
| |
− | //console.log('gen new x:'+x+" y:0");
| |
− | gravity=true;
| |
− | }
| |
− | for(var y=0;y<cell_y-1;y++){
| |
− | if(arr[x][y+1].alive==false&arr[x][y].alive==true){
| |
− | arr[x][y+1].type=arr[x][y].type;
| |
− | arr[x][y+1].img=arr[x][y].img;
| |
− | arr[x][y+1].alive=true;
| |
− | arr[x][y].alive=false;
| |
− | gravity=true;
| |
− | //console.log('move down x:'+x+" y:"+y);
| |
− | }
| |
− | }
| |
− |
| |
− | }
| |
− | full_draw();
| |
− | if(gravity==false){
| |
− | clearInterval(inter);
| |
− | check(true);
| |
− |
| |
− | }
| |
− | }
| |
− | | |
− | | |
− | function full_draw(){
| |
− | draw_back();
| |
− | draw_current_stones();
| |
− | }
| |
− | | |
− | function mousedown(x,y){
| |
− | var x=parseInt((-(bw+bc)+x)/cell_size);
| |
− | var y=parseInt((-(bw+bc)+y)/cell_size);
| |
− | pressed=true;
| |
− | g_x=x;
| |
− | g_y=y;
| |
− | //console.log("CLick!!! x:"+x+" y:"+y);
| |
− | }
| |
− | | |
− | function mousemove(x,y){
| |
− | l_x=parseInt((-(bw+bc)+x)/cell_size);
| |
− | l_y=parseInt((-(bw+bc)+y)/cell_size);
| |
− | //console.log("x:"+x+" y:"+y);
| |
− | }
| |
− | | |
− | function mouseup(){
| |
− | if(pressed){
| |
− | if(l_x!=g_x&l_y==g_y){
| |
− | if(g_x>l_x){
| |
− | var mx=-1;
| |
− | }
| |
− | if(g_x<l_x){
| |
− | var mx=1;
| |
− | }
| |
− | tmp=[];
| |
− | tmp.type=arr[g_x][g_y].type;
| |
− | tmp.alive=arr[g_x][g_y].alive;
| |
− | tmp.img=arr[g_x][g_y].img;
| |
− |
| |
− | arr[g_x][g_y].type=arr[g_x+mx][g_y].type;
| |
− | arr[g_x][g_y].alive=arr[g_x+mx][g_y].alive;
| |
− | arr[g_x][g_y].img=arr[g_x+mx][g_y].img;
| |
− |
| |
− | arr[g_x+mx][g_y].type=tmp.type;
| |
− | arr[g_x+mx][g_y].alive=tmp.alive;
| |
− | arr[g_x+mx][g_y].img=tmp.img;
| |
− | draw_back();
| |
− | draw_current_stones();
| |
− | if(arr[g_x+mx][g_y].type==0){
| |
− | explode(g_x+mx,g_y);
| |
− | }
| |
− | else if(arr[g_x][g_y].type==0){
| |
− | explode(g_x,g_y);
| |
− | }
| |
− | else{
| |
− | setTimeout(function(){
| |
− | if(check()){
| |
− | full_work();
| |
− | }
| |
− | else{
| |
− | tmp=[];
| |
− | tmp.type=arr[g_x][g_y].type;
| |
− | tmp.alive=arr[g_x][g_y].alive;
| |
− | tmp.img=arr[g_x][g_y].img;
| |
− |
| |
− | arr[g_x][g_y].type=arr[g_x+mx][g_y].type;
| |
− | arr[g_x][g_y].alive=arr[g_x+mx][g_y].alive;
| |
− | arr[g_x][g_y].img=arr[g_x+mx][g_y].img;
| |
− |
| |
− | arr[g_x+mx][g_y].type=tmp.type;
| |
− | arr[g_x+mx][g_y].alive=tmp.alive;
| |
− | arr[g_x+mx][g_y].img=tmp.img;
| |
− | full_draw();
| |
− | }
| |
− | }, speed*2);
| |
− | }
| |
− | }
| |
− | if(l_x==g_x&l_y!=g_y){
| |
− | if(g_y>l_y){
| |
− | var my=-1;
| |
− | }
| |
− | if(g_y<l_y){
| |
− | var my=1;
| |
− | }
| |
− | tmp=[];
| |
− | tmp.type=arr[g_x][g_y].type;
| |
− | tmp.alive=arr[g_x][g_y].alive;
| |
− | tmp.img=arr[g_x][g_y].img;
| |
− |
| |
− | arr[g_x][g_y].type=arr[g_x][g_y+my].type;
| |
− | arr[g_x][g_y].alive=arr[g_x][g_y+my].alive;
| |
− | arr[g_x][g_y].img=arr[g_x][g_y+my].img;
| |
− |
| |
− | arr[g_x][g_y+my].type=tmp.type;
| |
− | arr[g_x][g_y+my].alive=tmp.alive;
| |
− | arr[g_x][g_y+my].img=tmp.img;
| |
− | draw_back();
| |
− | draw_current_stones();
| |
− | if(arr[g_x][g_y+my].type==0){
| |
− | explode(g_x,g_y+my);
| |
− | }
| |
− | else if(arr[g_x][g_y].type==0){
| |
− | explode(g_x,g_y);
| |
− | }
| |
− | else {
| |
− | setTimeout(function(){
| |
− | if(check()){
| |
− | full_work();
| |
− | }
| |
− | else{
| |
− | tmp=[];
| |
− | tmp.type=arr[g_x][g_y].type;
| |
− | tmp.alive=arr[g_x][g_y].alive;
| |
− | tmp.img=arr[g_x][g_y].img;
| |
− |
| |
− | arr[g_x][g_y].type=arr[g_x][g_y+my].type;
| |
− | arr[g_x][g_y].alive=arr[g_x][g_y+my].alive;
| |
− | arr[g_x][g_y].img=arr[g_x][g_y+my].img;
| |
− |
| |
− | arr[g_x][g_y+my].type=tmp.type;
| |
− | arr[g_x][g_y+my].alive=tmp.alive;
| |
− | arr[g_x][g_y+my].img=tmp.img;
| |
− | full_draw();
| |
− | }
| |
− | }, speed*2);
| |
− | }
| |
− | }
| |
− |
| |
− | }
| |
− | }
| |
− | | |
− | function full_work(){
| |
− | steps++;
| |
− | check();
| |
− | draw_back();
| |
− | draw_current_stones();
| |
− | move_stones_auto();
| |
− | draw_back();
| |
− | draw_current_stones();
| |
− | }
| |
− | | |
− | | |
− | function explode(cx,cy){
| |
− | arr[cx][cy].img=explodeimg;
| |
− | for(var x=0;x<cell_x;x++){
| |
− | arr[x][cy].img=explodeimg;
| |
− | }
| |
− | for(var y=0;y<cell_x;y++){
| |
− | arr[cx][y].img=explodeimg;
| |
− | }
| |
− | draw_back();
| |
− | draw_current_stones();
| |
− | setTimeout(function(){
| |
− | full_work();
| |
− | },speed
| |
− | );
| |
− | }
| |
− | | |
− | function play_checker(){
| |
− |
| |
− |
| |
− | if(mode==1){
| |
− | if(steps>=maxstep){
| |
− | play=false;
| |
− | alert("Шаги кончились")
| |
− | clearInterval(checker);
| |
− | }
| |
− | steper_s.innerHTML=maxstep-steps;
| |
− | }
| |
− | else{
| |
− | steper_s.innerHTML=steps;
| |
− | }
| |
− | if(mode==2){
| |
− | timer_s.innerHTML=parseInt(100*(maxtime- ((new Date)/1000-timer)))/100;
| |
− | if((new Date)/1000-timer>=maxtime){
| |
− | timer_s.innerHTML=0;
| |
− | play=false;
| |
− | alert("Время вышло")
| |
− | clearInterval(checker);
| |
− | }
| |
− | }
| |
− | else{
| |
− | timer_s.innerHTML=parseInt(100*((new Date)/1000-timer))/100;
| |
− | }
| |
− | score_view.innerHTML=score;
| |
− | } | |