用java写的俄罗斯方块 建议看看Java图形设计卷IAWT 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我会的,谢谢YuLimin(阿敏当兵)的提议。不过我现在还是想先知道原因。 写错了一点,应是在按键事件中调用canvas.repaint()方法,不是直接调用update()方法。 能否将你的source code贴出来,我非常有兴趣 package russia_block;import java.awt.Canvas;import java.awt.Graphics;import java.awt.Color;import java.awt.Point;import java.util.Random;import java.lang.Math;public class DrawCanvas extends Canvas implements Runnable{ int Change = 1; //int Total = 15; boolean Clear = false; boolean GameStart = true; boolean Not_ToBottom = true; boolean No_player = true; boolean Not_Game_over = true; int x1 = 120 ,y1 =0 , x2,y2,x3,y3,x4,y4,width = 30 ,height = 30,Inc = 30 ; long speed = 1000; int[][] point = new int[10][15]; int n1,n2,n3; public Thread time_thread = new Thread(this); int level = 0; int total_record = 0; int once_record = 0; int standard_record = 1000; long pause_time = 1000; public DrawCanvas(){ try{ time_thread = new Thread(this); time_thread.start(); DrawCanvas_Init(); }catch(Exception e){ e.printStackTrace(); } } private void DrawCanvas_Init() throws Exception { for(int i = 0;i < 10; i++) for(int j = 0; j < 15; j++) point[i][j] = 0; } public void Gen_rand_number(){ double n1_rand = Math.abs(Math.random()*5); Double n1_obj = new Double(n1_rand); n1 = n1_obj.intValue();//5 big type double n2_rand = Math.abs(Math.random()*4); Double n2_obj = new Double(n2_rand); n2 = n2_obj.intValue();//4 sub type double n3_rand = Math.abs(Math.random()*2); Double n3_obj = new Double(n3_rand); n3 = n3_obj.intValue();//2 sub type } //Graphics g; public void run(){ //Graphics g; while(Not_Game_over){ Not_ToBottom = true; Clear = false; x1 = 120;y1 = 0; Gen_rand_number(); CalcuLate_other_point(); int temp_min_y1 = y1; int temp_min_y2 = (y2<y3)?y2:(y3<y4)?y3:y4; int temp_realmin = (temp_min_y1<temp_min_y2)?temp_min_y1:temp_min_y2; if(temp_realmin < 0){ y1+=Math.abs(temp_realmin);y2+=Math.abs(temp_realmin); y3+=Math.abs(temp_realmin);y4+=Math.abs(temp_realmin); } int tempy1 = y1/Inc+1;int tempx1 = x1/Inc; int tempy2 = y2/Inc+1;int tempx2 = x2/Inc; int tempy3 = y3/Inc+1;int tempx3 = x3/Inc; int tempy4 = y4/Inc+1;int tempx4 = x4/Inc; if(point[tempx1][tempy1]==1||point[tempx2][tempy2]==1||point[tempx3][tempy3]==1||point[tempx4][tempy4]==1){ repaint(); Not_Game_over = false; break; } repaint(); while(Not_ToBottom){ try{ time_thread.sleep(pause_time); }catch(InterruptedException e){ e.printStackTrace(); } MoveDown(); } } } public void paint( Graphics g){ if(Clear){ g.setColor(Color.blue); }else{ g.setColor(Color.red); } g.fillRect(x1,y1,width-1,height-1); g.fillRect(x2,y2,width-1,height-1); g.fillRect(x3,y3,width-1,height-1); g.fillRect(x4,y4,width-1,height-1); g.setColor(Color.red); for(int i = 0; i < 10; i++) for(int j = 1; j < 15; j++) if(point[i][j] == 1) g.fillRect(i*Inc,j*Inc,width-1,height-1); } //public void update(Graphics g){ //paint(g); //} public void MoveLeft(){ int tempy1 = y1/Inc;int tempx1 = x1/Inc-1; int tempy2 = y2/Inc;int tempx2 = x2/Inc-1; int tempy3 = y3/Inc;int tempx3 = x3/Inc-1; int tempy4 = y4/Inc;int tempx4 = x4/Inc-1; if(x1>=Inc&&x2>=Inc&&x3>=Inc&&x4>=Inc&&point[tempx1][tempy1]!=1&&point[tempx2][tempy2]!=1&&point[tempx3][tempy3]!=1&&point[tempx4][tempy4]!=1){ Clear = true; repaint(); x1-= Inc; x2-= Inc; x3-= Inc; x4-= Inc; Clear = false; repaint(); }else{ MoveDown(); } //Not_ToBottom = true; } public void MoveRight(){ int tempy1 = y1/Inc;int tempx1 = x1/Inc+1; int tempy2 = y2/Inc;int tempx2 = x2/Inc+1; int tempy3 = y3/Inc;int tempx3 = x3/Inc+1; int tempy4 = y4/Inc;int tempx4 = x4/Inc+1; if(x1<=8*Inc&&x2<=8*Inc&&x3<=8*Inc&&x4<=8*Inc&&point[tempx1][tempy1]!=1&&point[tempx2][tempy2]!=1&&point[tempx3][tempy3]!=1&&point[tempx4][tempy4]!=1){ Clear = true;repaint(); x1+=Inc;x2+=Inc;x3+=Inc;x4+=Inc;Clear = false;repaint(); }else{ MoveDown(); } } public void MoveDown(){ int tempy1 = y1/Inc+1;int tempx1 = x1/Inc; int tempy2 = y2/Inc+1;int tempx2 = x2/Inc; int tempy3 = y3/Inc+1;int tempx3 = x3/Inc; int tempy4 = y4/Inc+1;int tempx4 = x4/Inc; if(y1<14*Inc&&y2<14*Inc&&y3<14*Inc&&y4<14*Inc&&point[tempx1][tempy1]!=1&&point[tempx2][tempy2]!=1&&point[tempx3][tempy3]!=1&&point[tempx4][tempy4]!=1){ Clear = true; repaint(); y1+=Inc;y2+=Inc;y3+=Inc;y4+=Inc; Clear = false; repaint(); }else{ point[tempx1][tempy1-1] = 1; point[tempx2][tempy2-1] = 1; point[tempx3][tempy3-1] = 1; point[tempx4][tempy4-1] = 1; Not_ToBottom = false; int LineStat = 0;//to show one line state int FullLine_num = 0;//to record the number of full line for(int i = 0;i < 10;i++){ LineStat += point[i][tempy4-1]; } if(LineStat == 10){ LineStat = 0; FullLine_num++; for(int i = 0;i < 10;i++){ point[i][tempy4-1] = 0; } for(int j = tempy4-2;j>0;j--){ for(int i =0;i<10;i++){ if(point[i][j]==1){ point[i][j+1]=1;point[i][j]=0; } } if(j==tempy3-1){ tempy3++; }else{ if(j==tempy2-1){ tempy2++; }else{ if(j==tempy1-1){ tempy1++; } } } }repaint(); }else{ LineStat = 0; } for(int i = 0;i < 10;i++){ LineStat += point[i][tempy3-1]; } if(LineStat == 10){ LineStat = 0; FullLine_num++; for(int i = 0;i < 10;i++){ point[i][tempy3-1] = 0; } for(int j = tempy3-2;j>0;j--){ for(int i =0;i<10;i++){ if(point[i][j]==1){ point[i][j+1]=1;point[i][j]=0; } } if(j==tempy4-1){ tempy4++; }else{ if(j==tempy2-1){ tempy2++; }else{ if(j==tempy1-1){ tempy1++; } } } }repaint(); }else{ LineStat = 0; } for(int i = 0;i < 10;i++){ LineStat += point[i][tempy2-1]; } if(LineStat == 10){ LineStat = 0; FullLine_num++; for(int i = 0;i < 10;i++){ point[i][tempy2-1] = 0; } for(int j = tempy2-2;j>0;j--){ for(int i =0;i<10;i++){ if(point[i][j]==1){ point[i][j+1]=1;point[i][j]=0; } } if(j==tempy3-1){ tempy3++; }else{ if(j==tempy4-1){ tempy4++; }else{ if(j==tempy1-1){ tempy1++; } } } }repaint(); }else{ LineStat = 0; } for(int i = 0;i < 10;i++){ LineStat += point[i][tempy1-1]; } if(LineStat == 10){ LineStat = 0; FullLine_num++; for(int i = 0;i < 10;i++){ point[i][tempy1-1] = 0; } for(int j = tempy1-2;j>0;j--){ for(int i =0;i<10;i++){ if(point[i][j]==1){ point[i][j+1]=1;point[i][j]=0; } } if(j==tempy3-1){ tempy3++; }else{ if(j==tempy2-1){ tempy2++; }else{ if(j==tempy4-1){ tempy4++; } } } }repaint(); }else{ LineStat = 0; } //int once_record = 0; switch(FullLine_num){ case 0: once_record = 0;break; case 1: once_record = 100;break; case 2: once_record = 300;break; case 3: once_record = 800;break; case 4: once_record = 1500;break; } //Delete_total_lines += FullLine_num; //System.out.println("Deleted "+FullLine_num+" lines!"); //AutoMoveDown(); total_record += once_record; level = Math.round(total_record/standard_record); repaint(); if(pause_time > 0){ pause_time =(long)(1000-level*100); } } } So complex,I impress upon it.But ur program has no comment.It's not a good habit. java多线程一个问题。 怎样将以下代码改成用按钮选择 swing 页面跳转 下一步怎么显示新页面 ArrayList与LinkedList的效率比较 [请教]URLConnection,HttpURLConnection,HttpsURLConnection之间的区别是什么? jdbc找不到驱动怎么解决,谢谢 为啥不能画??? 如何将 菜单栏 放在窗体的下边(在java中)? 学了JAVA的基本东西以后,EJB,J2EE谁先学好一些, 关于Java Applet 日历(绝对送分) 高手帮帮忙! 急,请问怎样清掉先前留在socket.getInputStream()里面的东西?
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Point;
import java.util.Random;
import java.lang.Math;
public class DrawCanvas extends Canvas implements Runnable{ int Change = 1;
//int Total = 15;
boolean Clear = false;
boolean GameStart = true;
boolean Not_ToBottom = true;
boolean No_player = true;
boolean Not_Game_over = true;
int x1 = 120 ,y1 =0 , x2,y2,x3,y3,x4,y4,width = 30 ,height = 30,Inc = 30 ;
long speed = 1000;
int[][] point = new int[10][15];
int n1,n2,n3;
public Thread time_thread = new Thread(this);
int level = 0;
int total_record = 0;
int once_record = 0;
int standard_record = 1000;
long pause_time = 1000;
public DrawCanvas(){
try{
time_thread = new Thread(this);
time_thread.start();
DrawCanvas_Init();
}catch(Exception e){
e.printStackTrace();
}
} private void DrawCanvas_Init() throws Exception {
for(int i = 0;i < 10; i++)
for(int j = 0; j < 15; j++)
point[i][j] = 0; } public void Gen_rand_number(){
double n1_rand = Math.abs(Math.random()*5);
Double n1_obj = new Double(n1_rand);
n1 = n1_obj.intValue();//5 big type
double n2_rand = Math.abs(Math.random()*4);
Double n2_obj = new Double(n2_rand);
n2 = n2_obj.intValue();//4 sub type
double n3_rand = Math.abs(Math.random()*2);
Double n3_obj = new Double(n3_rand);
n3 = n3_obj.intValue();//2 sub type
} //Graphics g;
public void run(){
//Graphics g; while(Not_Game_over){
Not_ToBottom = true;
Clear = false;
x1 = 120;y1 = 0;
Gen_rand_number();
CalcuLate_other_point();
int temp_min_y1 = y1;
int temp_min_y2 = (y2<y3)?y2:(y3<y4)?y3:y4;
int temp_realmin = (temp_min_y1<temp_min_y2)?temp_min_y1:temp_min_y2;
if(temp_realmin < 0){
y1+=Math.abs(temp_realmin);y2+=Math.abs(temp_realmin);
y3+=Math.abs(temp_realmin);y4+=Math.abs(temp_realmin);
}
int tempy1 = y1/Inc+1;int tempx1 = x1/Inc;
int tempy2 = y2/Inc+1;int tempx2 = x2/Inc;
int tempy3 = y3/Inc+1;int tempx3 = x3/Inc;
int tempy4 = y4/Inc+1;int tempx4 = x4/Inc;
if(point[tempx1][tempy1]==1||point[tempx2][tempy2]==1||point[tempx3][tempy3]==1||point[tempx4][tempy4]==1){
repaint();
Not_Game_over = false;
break;
}
repaint();
while(Not_ToBottom){
try{
time_thread.sleep(pause_time);
}catch(InterruptedException e){
e.printStackTrace();
}
MoveDown();
}
}
}
g.setColor(Color.blue); }else{
g.setColor(Color.red);
}
g.fillRect(x1,y1,width-1,height-1);
g.fillRect(x2,y2,width-1,height-1);
g.fillRect(x3,y3,width-1,height-1);
g.fillRect(x4,y4,width-1,height-1); g.setColor(Color.red);
for(int i = 0; i < 10; i++)
for(int j = 1; j < 15; j++)
if(point[i][j] == 1)
g.fillRect(i*Inc,j*Inc,width-1,height-1);
}
//public void update(Graphics g){
//paint(g);
//} public void MoveLeft(){ int tempy1 = y1/Inc;int tempx1 = x1/Inc-1;
int tempy2 = y2/Inc;int tempx2 = x2/Inc-1;
int tempy3 = y3/Inc;int tempx3 = x3/Inc-1;
int tempy4 = y4/Inc;int tempx4 = x4/Inc-1;
if(x1>=Inc&&x2>=Inc&&x3>=Inc&&x4>=Inc&&point[tempx1][tempy1]!=1&&point[tempx2][tempy2]!=1&&point[tempx3][tempy3]!=1&&point[tempx4][tempy4]!=1){ Clear = true;
repaint(); x1-= Inc;
x2-= Inc;
x3-= Inc;
x4-= Inc;
Clear = false;
repaint();
}else{
MoveDown();
}
//Not_ToBottom = true;
}
public void MoveRight(){
int tempy1 = y1/Inc;int tempx1 = x1/Inc+1;
int tempy2 = y2/Inc;int tempx2 = x2/Inc+1;
int tempy3 = y3/Inc;int tempx3 = x3/Inc+1;
int tempy4 = y4/Inc;int tempx4 = x4/Inc+1;
if(x1<=8*Inc&&x2<=8*Inc&&x3<=8*Inc&&x4<=8*Inc&&point[tempx1][tempy1]!=1&&point[tempx2][tempy2]!=1&&point[tempx3][tempy3]!=1&&point[tempx4][tempy4]!=1){
Clear = true;repaint();
x1+=Inc;x2+=Inc;x3+=Inc;x4+=Inc;Clear = false;repaint();
}else{
MoveDown();
}
}
int tempy1 = y1/Inc+1;int tempx1 = x1/Inc;
int tempy2 = y2/Inc+1;int tempx2 = x2/Inc;
int tempy3 = y3/Inc+1;int tempx3 = x3/Inc;
int tempy4 = y4/Inc+1;int tempx4 = x4/Inc;
if(y1<14*Inc&&y2<14*Inc&&y3<14*Inc&&y4<14*Inc&&point[tempx1][tempy1]!=1&&point[tempx2][tempy2]!=1&&point[tempx3][tempy3]!=1&&point[tempx4][tempy4]!=1){
Clear = true;
repaint();
y1+=Inc;y2+=Inc;y3+=Inc;y4+=Inc;
Clear = false;
repaint();
}else{
point[tempx1][tempy1-1] = 1;
point[tempx2][tempy2-1] = 1;
point[tempx3][tempy3-1] = 1;
point[tempx4][tempy4-1] = 1;
Not_ToBottom = false; int LineStat = 0;//to show one line state
int FullLine_num = 0;//to record the number of full line
for(int i = 0;i < 10;i++){
LineStat += point[i][tempy4-1];
}
if(LineStat == 10){
LineStat = 0;
FullLine_num++;
for(int i = 0;i < 10;i++){
point[i][tempy4-1] = 0;
}
for(int j = tempy4-2;j>0;j--){
for(int i =0;i<10;i++){
if(point[i][j]==1){
point[i][j+1]=1;point[i][j]=0;
}
}
if(j==tempy3-1){
tempy3++;
}else{
if(j==tempy2-1){
tempy2++;
}else{
if(j==tempy1-1){
tempy1++;
}
}
}
}repaint();
}else{
LineStat = 0;
}
for(int i = 0;i < 10;i++){
LineStat += point[i][tempy3-1];
}
if(LineStat == 10){
LineStat = 0;
FullLine_num++;
for(int i = 0;i < 10;i++){
point[i][tempy3-1] = 0;
}
for(int j = tempy3-2;j>0;j--){
for(int i =0;i<10;i++){
if(point[i][j]==1){
point[i][j+1]=1;point[i][j]=0;
}
}
if(j==tempy4-1){
tempy4++;
}else{
if(j==tempy2-1){
tempy2++;
}else{
if(j==tempy1-1){
tempy1++;
}
}
}
}repaint();
}else{
LineStat = 0;
}
for(int i = 0;i < 10;i++){
LineStat += point[i][tempy2-1];
}
if(LineStat == 10){
LineStat = 0;
FullLine_num++;
for(int i = 0;i < 10;i++){
point[i][tempy2-1] = 0;
}
for(int j = tempy2-2;j>0;j--){
for(int i =0;i<10;i++){
if(point[i][j]==1){
point[i][j+1]=1;point[i][j]=0;
}
}
if(j==tempy3-1){
tempy3++;
}else{
if(j==tempy4-1){
tempy4++;
}else{
if(j==tempy1-1){
tempy1++;
}
}
}
}repaint();
}else{
LineStat = 0;
}
for(int i = 0;i < 10;i++){
LineStat += point[i][tempy1-1];
}
if(LineStat == 10){
LineStat = 0;
FullLine_num++;
for(int i = 0;i < 10;i++){
point[i][tempy1-1] = 0;
}
for(int j = tempy1-2;j>0;j--){
for(int i =0;i<10;i++){
if(point[i][j]==1){
point[i][j+1]=1;point[i][j]=0;
}
}
if(j==tempy3-1){
tempy3++;
}else{
if(j==tempy2-1){
tempy2++;
}else{
if(j==tempy4-1){
tempy4++;
}
}
}
}repaint();
}else{
LineStat = 0;
}
//int once_record = 0;
switch(FullLine_num){
case 0: once_record = 0;break;
case 1: once_record = 100;break;
case 2: once_record = 300;break;
case 3: once_record = 800;break;
case 4: once_record = 1500;break;
}
//Delete_total_lines += FullLine_num;
//System.out.println("Deleted "+FullLine_num+" lines!");
//AutoMoveDown();
total_record += once_record;
level = Math.round(total_record/standard_record);
repaint();
if(pause_time > 0){
pause_time =(long)(1000-level*100);
}
}
}