仿照那些代码自己写了个程序,只用来做怀念

mac2024-08-17  59

做了个俄罗斯方块,欢迎大神指正我的错误。

这个版本还是有bug的,比如动不了这些,欢迎大佬批评我!

这个版本高仿版本,看不懂可以移步到这个大佬的贴下 https://blog.csdn.net/qq_40521205/article/details/80467101 先建一个tetris类,类中有说明

package Tetris; /* Tetris类,先初始化游戏资源,主要是用BufferedImage和ImageIO流来完成,因为IO流有一个检查型的异常 */ import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import static java.lang.Thread.sleep; import static javax.imageio.ImageIO.*; public class Tetris extends JPanel { //加载图片 public static BufferedImage T; public static BufferedImage I; public static BufferedImage O; public static BufferedImage J; public static BufferedImage L; public static BufferedImage S; public static BufferedImage Z; public static BufferedImage backgroundOne; public static BufferedImage gameover; static { try { T = read(Tetris.class.getResource("T.jpg")); I = read(Tetris.class.getResource("I.jpg")); O = read(Tetris.class.getResource("O.jpg")); J = read(Tetris.class.getResource("J.jpg")); L = read(Tetris.class.getResource("L.jpg")); S = read(Tetris.class.getResource("S.jpg")); Z = read(Tetris.class.getResource("Z.jpg")); backgroundOne = read(Tetris.class.getResource("background.jpg")); } catch (IOException e) { e.printStackTrace(); } } //生成方块的方法 //正要下落的四方格 private Tetromino currentOne = Tetromino.redomOne(); //将要下落的四方格 private Tetromino nextOne = Tetromino.redomOne(); //墙的占用面积 private Cell[][] wall = new Cell[20][20]; //SIZE为一个方块的宽度 private static final int CELL_SIZE = 29; //重写paint public void paint(Graphics g){ g.drawImage(backgroundOne,0,0,null); g.translate(15,15); paintWall(g); paintCurrentOne(g); paintNextOne(g); } //重绘下一个方格 private void paintNextOne(Graphics g) { Cell[] cells = nextOne.cells; for(Cell c:cells){ int row = c.getRow(); int col = c.getCol(); int x = col*CELL_SIZE; int y = row*CELL_SIZE; g.drawImage(c.getImage(),x,y,null); } } //重绘正在下落的方格 private void paintCurrentOne(Graphics g) { Cell[] cells = currentOne.cells; for(Cell c:cells){ int x = c.getCol()*CELL_SIZE; int y = c.getCol()*CELL_SIZE; g.drawImage(c.getImage(),x,y,null); } } //重绘墙 private void paintWall(Graphics g) { for(int i=0;i<20;i++) { for (int j = 0; j < 10; j++) { int x = j * CELL_SIZE; int y = i * CELL_SIZE; Cell cell = wall[i][j]; if(cell==null){ g.drawRect(x,y,CELL_SIZE,CELL_SIZE); } else{ g.drawImage(cell.getImage(),x,y,null); } } } } /** * 封装了游戏的逻辑 */ private void start() throws InterruptedException { //键盘监听事件 KeyListener key1 = new KeyListener() { @Override public void keyTyped(KeyEvent e) { } /** * 这个方法是键盘按下去加载的方法 */ @Override public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); switch (code){ case KeyEvent.VK_D: softDropAction(); break; case KeyEvent.VK_W: moveLeftAction(); break; case KeyEvent.VK_A: moveRightAction(); break; case KeyEvent.VK_S: break; default: break; } repaint(); } public void keyReleased(KeyEvent e) { } }; this.addKeyListener(1); this.requestFocus(); while (true) { sleep(300); if(canDrop()){ currentOne.softDrop(); } else{ landToWall(); currentOne = nextOne; nextOne = Tetromino.redomOne(); } repaint(); elimination(); } } private void addKeyListener(int i) { } private void elimination() { } public void moveRightAction(){ currentOne.moveRight(); if(outOfBounds()||coincide()){ currentOne.moveLeft(); } } public void moveLeftAction(){ currentOne.moveLeft(); if(outOfBounds()||coincide()){ currentOne.moveRight(); } } private boolean coincide(){ Cell[] cells = currentOne.cells; for(Cell c:cells){ int col = c.getCol(); int row = c.getRow(); if(wall[row][col]!=null){ return true; } } return false; } public boolean outOfBounds(){ Cell[] cells = currentOne.cells; for(Cell c:cells){ int col = c.getCol(); if(col<0||col>9) return true; } return false; } //控制下落 public void softDropAction(){ if(canDrop()){ currentOne.softDrop(); } else{ landToWall(); currentOne = nextOne; nextOne = Tetromino.redomOne(); } } public boolean canDrop(){ Cell[] cells = currentOne.cells; for(Cell c:cells){ int row = c.getRow(); int col = c.getCol(); if(row==19){ return false; } else if(wall[row+1][col]!=null){ return false; } } return true; } public void landToWall(){ Cell[] cells = currentOne.cells; for(Cell c:cells){ int row = c.getRow(); int col=c.getCol(); wall[row][col] = c; } } public static void main(String args[]){ JFrame frame = new JFrame(); Tetris panel = new Tetris(); frame.add(panel); frame.setVisible(true); frame.setSize(535,595); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try { panel.start(); } catch (InterruptedException e) { e.printStackTrace(); } } }

在进行下一步 创建各种类

package Tetris; public class O extends Tetromino { public O(){ cells[0] = new Cell(0,4,Tetris.O); cells[1] = new Cell(1,5,Tetris.O); cells[2] = new Cell(1,4,Tetris.O); cells[3] = new Cell(1,5,Tetris.O); } }

在进行下一个

package Tetris; //此类作为7个经典形状的父类,并提供相应的成员 /* Cell数组,用于创建4个小方块() moveLeft(),moveRight(),softDrop(),分别用于四格方块的左移,右移和软下落,软下落也就是四格方块下落一。 */ import java.util.Arrays; public class Tetromino { protected Cell[] cells = new Cell[4]; //四格方块向左移动 public void moveLeft(){ for(Cell c:cells) c.lefe(); } //四格方块向右移动 public void moveRight(){ for(Cell c:cells) c.right(); } //四格方块向下运动 public void softDrop(){ for(Cell c:cells) c.drop(); } public String toString(){ return"["+ Arrays.toString(cells)+"]"; } //随机生成一个四格方块 public static Tetromino redomOne(){ Tetromino t = null; int num = (int) (Math.random()*7); switch (num){ case 0: t=new T();break; case 1: t=new I();break; case 2: t=new O();break; case 3: t=new Z();break; case 4: t=new S();break; case 5: t=new L();break; case 6: t=new J();break; } return t; } }

在进行下一步

package Tetris; //此类用来表示一个小方块 /* cell的主要成员 1.row,表示小方块的行号 2.col,表示小方块的序列号 3.image,表示之前的图片 4.left(),right(),drop(),分别表示一个小方块的左移一格,右移一格,下降一格。 */ import java.awt.image.BufferedImage; public class Cell { private int row; private int col; private BufferedImage image; public String toString(){ return"("+row+","+col+")"; } public int getRow(){ return row; } public void setRow(int row){ this.row=row; } public int getCol(){ return col; } public void setCol(int col){ this.col=col; } public BufferedImage getImage(){ return image; } public void setImage(BufferedImage image){ this.image=image; } public Cell(){} //创建全参合无参构造器,属性的get/set方法并重写toString方法。 public Cell(int row, int col,BufferedImage image ){ this.row = row; this.col = col; this.image = image; } //向左移动 public void lefe(){ col--; } //向右移动 public void right(){ col++; } //向下移动 public void drop(){ row++; } }

欢迎一起讨论,嘿嘿!

最新回复(0)