主题:菜鸟写的俄罗斯方块出现问题
我自己试着写了一俄罗斯方块,刚开始就出现问题,一直玩法解决,问题出在如果连续按向左键或是向右键,方块移动的很不自然,要是一直按着向左键或是向右键,那方块就一直往左或右移,也不下落了,请各位高手帮帮忙忙,小弟感激不尽!!!
代码如下:
MainView
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainView extends JFrame implements KeyListener {
private static final long serialVersionUID = 1L;
private Game left = new Game();
private JPanel right = new JPanel();
private JPanel up = new JPanel();
private JPanel down = new JPanel();
public MainView() {
this.setTitle("俄罗斯方块");
this.setSize(300, 435);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.setResizable(false);
this.setLayout(new BorderLayout());
this.add(left,BorderLayout.WEST);
this.add(right,BorderLayout.CENTER);
left.setPreferredSize(new Dimension(200,400));
left.setBorder(BorderFactory.createLoweredBevelBorder());
left.setBackground(Color.BLACK);
right.setLayout(new BorderLayout());
right.add(up,BorderLayout.NORTH);
right.add(down,BorderLayout.CENTER);
up.setPreferredSize(new Dimension(100,200));
up.setBorder(BorderFactory.createLoweredBevelBorder());
down.setPreferredSize(new Dimension(100,200));
down.setBorder(BorderFactory.createLoweredBevelBorder());
addKeyListener(this);
}
public static void main(String[] args) {
new MainView().setVisible(true);
}
public void keyPressed(KeyEvent e) {
left.key(e);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
Game
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game extends JPanel {
private int[][] map = new int[24][14];
private static int[][] box = new int[5][5];
private int num;
private Block block = new Block();
private Control con = new Control();
private gameThread g = new gameThread();
public Game() {
iniMap();
g.start();
}
public void iniMap(){
for(int i=0;i<14;i++) {
map[0][i] = 1;
map[1][i] = 1;
map[22][i] = 1;
map[23][i] = 1;
}
for(int i=0;i<24;i++){
map[i][0] = 1;
map[i][1] = 1;
map[i][12] = 1;
map[i][13] = 1;
}
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.green);
for(int i=2;i<22;i++) {
for(int j=2;j<12;j++) {
if(map[i][j]==1) {
g.fill3DRect(j*20-40, i*20-40, 20, 20, true);
}
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(box[i][j]==1){
g.fill3DRect((block.getJ()+j)*20-40, (block.getI()+i)*20-40, 20, 20, true);
}
}
}
}
public void key(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_LEFT) {
block.setDirection(1);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
block.setDirection(2);
}
if(e.getKeyCode()==KeyEvent.VK_UP) {
}
}
class gameThread extends Thread {
public void loadBox(){
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(map[block.getI()+i][block.getJ()+j]==0 && box[i][j]==1){
map[block.getI()+i][block.getJ()+j] = 1;
}
}
}
block.setDown(false);
}
public void run() {
while(true) {
try {
if(!block.isDown()) {
num = (int)(Math.random()*7);
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
box[i][j] = StaticBox.staticBox[num][i][j];
}
}
block.ini();
block.setDown(true);
repaint();
sleep(400);
}
switch(block.getDirection()){
case 1:con.set(map, box, block.getI(), block.getJ()-1);break;
case 2:con.set(map, box, block.getI(), block.getJ()+1);break;
case 3:con.set(map, box, block.getI()+1, block.getJ());break;
}
if(!con.isMove()){
if(block.getDirection()==3) loadBox();
} else {
switch(block.getDirection()){
case 1:block.setJ(block.getJ()-1);break;
case 2:block.setJ(block.getJ()+1);break;
case 3:block.setI(block.getI()+1);break;
}
}
block.setDirection(3);
repaint();
sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Control
public class Control {
private int[][] map;
private int[][] box;
private int x,y;
private int t;
public void set(int[][] map,int[][] box,int x,int y) {
this.map = map;
this.box = box;
this.x = x;
this.y = y;
}
public int getT() {
return t;
}
public boolean isGameOver() {
boolean t = false;
return t;
}
public boolean isMove(){
if(x+4>=24 || y+4>=14 || y<0) return false;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(map[x+i][y+j]==1 && box[i][j]==1) return false;
}
}
return true;
}
}
Block
public class Block {
private boolean isDown; //方块是否在下落
private int direction; //方块的方向,1代表向左,2代表向右,3代表向下
private int i; //方块位于地图数组中的i位置
private int j; //方块位于地图数组中的j位置
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public void ini() {
this.direction = 3;
this.i = 1;
this.j = 4;
}
public boolean isDown() {
return isDown;
}
public void setDown(boolean isDown) {
this.isDown = isDown;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
}
StaticBox
public class StaticBox {
public static int[][][] staticBox = {
{
{0,0,0,0,0},
{0,0,0,0,0},
{1,1,1,1,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,1,0,0},
{0,1,1,1,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,1,1,0,0},
{0,0,1,1,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,1,1,0},
{0,1,1,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,1,1,0,0},
{0,0,1,0,0},
{0,0,1,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,1,1,0},
{0,0,1,0,0},
{0,0,1,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,1,1,0},
{0,0,1,1,0},
{0,0,0,0,0},
{0,0,0,0,0}
}
};
}
代码如下:
MainView
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainView extends JFrame implements KeyListener {
private static final long serialVersionUID = 1L;
private Game left = new Game();
private JPanel right = new JPanel();
private JPanel up = new JPanel();
private JPanel down = new JPanel();
public MainView() {
this.setTitle("俄罗斯方块");
this.setSize(300, 435);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.setResizable(false);
this.setLayout(new BorderLayout());
this.add(left,BorderLayout.WEST);
this.add(right,BorderLayout.CENTER);
left.setPreferredSize(new Dimension(200,400));
left.setBorder(BorderFactory.createLoweredBevelBorder());
left.setBackground(Color.BLACK);
right.setLayout(new BorderLayout());
right.add(up,BorderLayout.NORTH);
right.add(down,BorderLayout.CENTER);
up.setPreferredSize(new Dimension(100,200));
up.setBorder(BorderFactory.createLoweredBevelBorder());
down.setPreferredSize(new Dimension(100,200));
down.setBorder(BorderFactory.createLoweredBevelBorder());
addKeyListener(this);
}
public static void main(String[] args) {
new MainView().setVisible(true);
}
public void keyPressed(KeyEvent e) {
left.key(e);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
Game
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game extends JPanel {
private int[][] map = new int[24][14];
private static int[][] box = new int[5][5];
private int num;
private Block block = new Block();
private Control con = new Control();
private gameThread g = new gameThread();
public Game() {
iniMap();
g.start();
}
public void iniMap(){
for(int i=0;i<14;i++) {
map[0][i] = 1;
map[1][i] = 1;
map[22][i] = 1;
map[23][i] = 1;
}
for(int i=0;i<24;i++){
map[i][0] = 1;
map[i][1] = 1;
map[i][12] = 1;
map[i][13] = 1;
}
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.green);
for(int i=2;i<22;i++) {
for(int j=2;j<12;j++) {
if(map[i][j]==1) {
g.fill3DRect(j*20-40, i*20-40, 20, 20, true);
}
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(box[i][j]==1){
g.fill3DRect((block.getJ()+j)*20-40, (block.getI()+i)*20-40, 20, 20, true);
}
}
}
}
public void key(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_LEFT) {
block.setDirection(1);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
block.setDirection(2);
}
if(e.getKeyCode()==KeyEvent.VK_UP) {
}
}
class gameThread extends Thread {
public void loadBox(){
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(map[block.getI()+i][block.getJ()+j]==0 && box[i][j]==1){
map[block.getI()+i][block.getJ()+j] = 1;
}
}
}
block.setDown(false);
}
public void run() {
while(true) {
try {
if(!block.isDown()) {
num = (int)(Math.random()*7);
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
box[i][j] = StaticBox.staticBox[num][i][j];
}
}
block.ini();
block.setDown(true);
repaint();
sleep(400);
}
switch(block.getDirection()){
case 1:con.set(map, box, block.getI(), block.getJ()-1);break;
case 2:con.set(map, box, block.getI(), block.getJ()+1);break;
case 3:con.set(map, box, block.getI()+1, block.getJ());break;
}
if(!con.isMove()){
if(block.getDirection()==3) loadBox();
} else {
switch(block.getDirection()){
case 1:block.setJ(block.getJ()-1);break;
case 2:block.setJ(block.getJ()+1);break;
case 3:block.setI(block.getI()+1);break;
}
}
block.setDirection(3);
repaint();
sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Control
public class Control {
private int[][] map;
private int[][] box;
private int x,y;
private int t;
public void set(int[][] map,int[][] box,int x,int y) {
this.map = map;
this.box = box;
this.x = x;
this.y = y;
}
public int getT() {
return t;
}
public boolean isGameOver() {
boolean t = false;
return t;
}
public boolean isMove(){
if(x+4>=24 || y+4>=14 || y<0) return false;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(map[x+i][y+j]==1 && box[i][j]==1) return false;
}
}
return true;
}
}
Block
public class Block {
private boolean isDown; //方块是否在下落
private int direction; //方块的方向,1代表向左,2代表向右,3代表向下
private int i; //方块位于地图数组中的i位置
private int j; //方块位于地图数组中的j位置
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public void ini() {
this.direction = 3;
this.i = 1;
this.j = 4;
}
public boolean isDown() {
return isDown;
}
public void setDown(boolean isDown) {
this.isDown = isDown;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
}
StaticBox
public class StaticBox {
public static int[][][] staticBox = {
{
{0,0,0,0,0},
{0,0,0,0,0},
{1,1,1,1,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,1,0,0},
{0,1,1,1,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,1,1,0,0},
{0,0,1,1,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,1,1,0},
{0,1,1,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,1,1,0,0},
{0,0,1,0,0},
{0,0,1,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,1,1,0},
{0,0,1,0,0},
{0,0,1,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,1,1,0},
{0,0,1,1,0},
{0,0,0,0,0},
{0,0,0,0,0}
}
};
}