minor fixes and cleanups

master
obneq 2010-04-13 17:27:39 +02:00
parent d6b17bf708
commit b189251481
1 changed files with 42 additions and 62 deletions

104
tetris.js
View File

@ -37,7 +37,6 @@ var shapes = [ [ [[0,0],[1,0],[0,1],[1,1]] ], //box
[[1,1],[2,0],[2,1],[2,2]] ], ]; //t
function board(x, y){
//constructs a new empty board
this.x=x;
this.y=y;
this.val = function(x,y){
@ -48,16 +47,12 @@ function board(x, y){
}(x, y);
this.add = function(s){
//shapes are added to the board after
//they cant be moved anymore
var d=s.Shape[s.Rotation]
for(var i=0; i<4; i++)
this.val[s.Xpos+d[i][0]][s.Ypos+d[i][1]]=s.Color;
this.val[s.x+d[i][0]][s.y+d[i][1]]=s.Color;
}
this.check_lines=function(){
//this function checks the board
//for full lines and collapses them
for(var i=1; i<this.y; i++)
if (this.check_line(i))
this.scroll(i);
@ -80,35 +75,30 @@ function board(x, y){
}
}
function shape (){
//constructs a new random shape
this.Xpos = 4;
this.Ypos = 0;
function piece(){
this.x = 4;
this.y = 0;
var id = Math.floor(Math.random()*7);
this.Color=id+1;
this.Shape=shapes[id];
this.Rotation = 0;
this.move=function(x,y){
//move if new position is valid
var newX = this.Xpos + x;
var newY = this.Ypos + y;
//check for obstacles on board
if(!this.check_position(newX, newY, this.Rotation))
this.move=function(dx,dy){
var newx = this.x + dx;
var newy = this.y + dy;
if(!this.check_position(newx, newy, this.Rotation))
return;
this.Xpos = newX;
this.Ypos = newY;
this.x = newx;
this.y = newy;
view.update();
}
this.rotate = function(r){
//rotate if new position is valid
this.rotate = function(dr){
//make cw and ccw rotation work
var newrot = (this.Rotation+r)%this.Shape.length;
var newrot = (this.Rotation+dr)%this.Shape.length;
if (newrot<0)
newrot=this.Shape.length+newrot;
//check for obstacles on board
if (!this.check_position(this.Xpos, this.Ypos, newrot))
if (!this.check_position(this.x, this.y, newrot))
return;
this.Rotation = newrot;
view.update();
@ -116,31 +106,28 @@ function shape (){
this.drop=function(){
while(!this.stuck())
this.Ypos+=1;
this.y+=1;
view.update();
}
}
shape.prototype.check_position=function(newX, newY, newR){
var d = this.Shape[newR];
for(var i=0; i<4; i++){
if((newX+d[i][0]<0 || newX+d[i][0]>=board.x) ||
(newY+d[i][1]<0 || newY+d[i][1]>=board.y))
return false;
if(board.val[newX+d[i][0]][newY+d[i][1]])
return false;
this.check_position=function(newx, newy, newrot){
var d = this.Shape[newrot];
for(var i=0; i<4; i++)
if(newx+d[i][0]<0 || newx+d[i][0]>=board.x ||
newy+d[i][1]<0 || newy+d[i][1]>=board.y ||
board.val[newx+d[i][0]][newy+d[i][1]])
return false;
return true;
}
return true;
}
shape.prototype.stuck=function (){
if(this.check_position(this.Xpos, this.Ypos+1, this.Rotation))
return false;
return true;
this.stuck=function (){
if(this.check_position(this.x, this.y+1, this.Rotation))
return false;
return true;
}
}
function view(size){
//constructs the view
this.canvas = document.getElementById("canvas");
this.ctx = this.canvas.getContext("2d");
this.ctx.strokeStyle = colors[0];
@ -148,26 +135,23 @@ function view(size){
this.ysize=board.y*size;
this.update=function() {
//draw the board and the current shape
this.ctx.clearRect(0, 0, this.xsize, this.ysize);
this.draw_board();
var c=current.Shape[current.Rotation];
this.ctx.fillStyle = colors[current.Color];
for(var i=0; i<4; i++){
this.ctx.fillRect((c[i][0]+current.Xpos)*size,
(c[i][1]+current.Ypos)*size,
this.ctx.fillRect((c[i][0]+current.x)*size,
(c[i][1]+current.y)*size,
size, size);
this.ctx.strokeRect((c[i][0]+current.Xpos)*size,
(c[i][1]+current.Ypos)*size,
this.ctx.strokeRect((c[i][0]+current.x)*size,
(c[i][1]+current.y)*size,
size, size);
}
}
this.draw_board=function(){
//draw the board
for (var i=0; i<board.y; i++)
for(var j=0; j<board.x; j++){
var b=board.val[j][i];
@ -181,7 +165,6 @@ function view(size){
}
function preview(size){
//constructs the preview
this.canvas = document.getElementById("preview");
this.ctx = this.canvas.getContext("2d");
this.ctx.strokeStyle = colors[0];
@ -216,13 +199,11 @@ function status(){
}
function key(e){
//next 4 lines stolen from the internet...
//next 4 lines stolen from the internet
var evt=(e)?e:(window.event)?window.event:null;
if(evt){
var key=(evt.charCode)?evt.charCode:
((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));
//vi-style keys. you are allowed
//to move a shape up for now.
if(key=="74")
current.move(0, 1);
else if(key=="75")
@ -232,36 +213,35 @@ function key(e){
else if(key=="76")
current.move(1, 0);
else if(key=="65")
current.rotate(-1);
else if(key=="70")
current.rotate(1);
else if(key=="70")
current.rotate(-1);
else if(key=="32")
current.drop();
}
}
function step(){
//the main loop
if(current.stuck()){
if(current.Ypos==0)
if(!current.check_position(current.x, current.y+1, current.Rotation)){
if(current.y==0)
clearInterval(run);
board.add(current);
board.check_lines();
if(!next.check_position(next.Xpos, next.Ypos, next.Rotation))
if(!next.check_position(next.x, next.y, next.Rotation))
return;
current = next;
next = new shape();
next = new piece();
preview.update();
status.update();
} else
current.Ypos += 1;
current.y += 1;
view.update();
}
function new_game(){
board = new board(10,20);
next = new shape();
current = new shape();
next = new piece();
current = new piece();
view = new view(20);
preview = new preview(20);
status = new status();
@ -271,4 +251,4 @@ function new_game(){
view.update();
//go!
run=setInterval(step, levels[0]);
}
}