贪吃蛇和俄罗斯方块

贪吃蛇

一、创建新项目

创建一个新的项目,并命名。

创建一个名为images的文件夹用来存放游戏相关图片。

然后再在项目的src文件下创建一个com.xxx.view的包用来存放所有的图形界面类,

创建一个com.xxx.controller的包用来存放启动的入口类(控制类)

二、游戏界面 

package com.snake.view;import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;public class SnakeJPanel extends JPanel implements ActionListener{private boolean start;//当前游戏状态private int speed;//速度private boolean exist;//当前是否存在食物private int foodType;//食物种类private int x;//豆子的横坐标private int y;//豆子的纵坐标private ArrayList<int[]> localList;//蛇public String direction;//方向private String direction2;//引导方向public boolean flag;Random rand = new Random();private ImageIcon up;private ImageIcon down;private ImageIcon right;private ImageIcon left;private ImageIcon body;private ImageIcon food;private ImageIcon title;Timer time;private int score;//当前得分情况private int num;//吃到的食物个数//    private Image offScreenImage;  //图形缓存//图片绘制@Overridepublic void paint(Graphics g) {direction = direction2;g.setColor(Color.WHITE);g.fillRect(0, 0, 900, 700);//绘制游戏框//标题框
//		g.drawRect(25, 30, 800, 75);title.paintIcon(this, g, 25, 10);//内容框g.setColor(Color.black);g.fillRect(25, 75, 850, 600);//绘制食物的坐标位置if(!exist) {//如果当前不存在豆子,随机绘制一个豆子	if(num % 5 == 0) {foodType = 1;}else {foodType = 0;}boolean isProduce = true;while(isProduce) {isProduce = false;x = rand.nextInt(33) * 25 + 25;		y = rand.nextInt(23) * 25 + 75;			for (int[] arr:localList) {if(x == arr[0] && y == arr[1]) {	isProduce = true;break;	}}}			System.out.println(x + "---" + y);}if(eat()) {exist = false;}else {exist = true;}if(foodType == 0) {//绘制食物g.setColor(Color.blue);
//			g.fillRect(x, y, 25, 25);g.drawImage(food.getImage(),x, y, 25, 25,null);}else {//绘制食物g.setColor(Color.WHITE);g.fillRect(x, y, 25, 25);
//			g.drawImage(food.getImage(),x, y, 25, 25,null);}//绘制头g.setColor(Color.red);
//		g.fillRect(localList.get(0)[0], localList.get(0)[1], 25, 25);	ImageIcon head = null;//判断当前方向if(direction.equals("R")) {head = right;}else if(direction.equals("L")) {head = left;}else if(direction.equals("U")) {head = up;}else if(direction.equals("D")) {head = down;}		
//		g.drawImage(head.getImage(), localList.get(0)[0], localList.get(0)[1], 25, 25,null);head.paintIcon(this, g,localList.get(0)[0], localList.get(0)[1]);//绘制身体g.setColor(Color.white);for (int i = 1; i < localList.size(); i++) {
//			g.fillRect(localList.get(i)[0], localList.get(i)[1], 25, 25);
//			g.drawImage(body.getImage(), localList.get(i)[0], localList.get(i)[1], 25, 25,null);body.paintIcon(this, g, localList.get(i)[0], localList.get(i)[1]);}
//		g.fillRect(localList.get(1)[0], localList.get(1)[1], 25, 25);
//		g.fillRect(localList.get(2)[0], localList.get(2)[1], 25, 25);//绘制分数和长度//长度g.setColor(Color.GREEN);g.setFont(new Font("宋体", Font.BOLD, 18));g.drawString("长度:" + (localList.size() - 1), 25, 30);//分数g.drawString("分数:" + score, 25, 48);if(!start) {//如果游戏未启动,结束移动和重绘g.setColor(Color.white);g.setFont(new Font("宋体", Font.BOLD, 30));g.drawString("暂停/开始(请按任意键开始,空格键暂停)", 150, 300);time.stop();}else {time.start();}//		speed();//移动后进行下一次绘制		
//      move();//移动
//		repaint();//重新绘制		}//	//解决闪烁问题
//	//如果为JFrame 为重量级  程序不会调用update()方法
//	//如果为Frame 为轻量级  重写update()方法 做双缓冲
//	//如果为JPanel 不会闪烁
//	  @Override
//	    public void update(Graphics g)
//	    {
//	    	System.out.println("update");
//	           if(offScreenImage == null)
//	              offScreenImage = this.createImage(900, 700);  //新建一个图像缓存空间,这里图像大小为800*600
//	              Graphics gImage = offScreenImage.getGraphics();  //把它的画笔拿过来,给gImage保存着
//	              paint(gImage);                                   //将要画的东西画到图像缓存空间去
//	              g.drawImage(offScreenImage, 0, 0, null);         //然后一次性显示出来
//	    }@Overridepublic void actionPerformed(ActionEvent e) {	    //移动后进行下一次绘制		move();//移动repaint();//重新绘制		}/*** 绘制速度*/
//	private void speed() {
//		try {//按一定速度进行移动
//			Thread.sleep(speed);//控制移动速度
//		} catch (InterruptedException e) {
//			// TODO 自动生成的 catch 块
//			e.printStackTrace();
//		}
//	}/*** 初始化图片*/private void drawImage() {up = new ImageIcon("images/up.png");down = new ImageIcon("images/down.png");right = new ImageIcon("images/right.png");left = new ImageIcon("images/left.png");body = new ImageIcon("images/body.png");food = new ImageIcon("images/food.png");title = new ImageIcon("images/title.jpg");}private boolean eat() {if(localList.get(0)[0] == x && localList.get(0)[1] == y) {//如果当前蛇头吃到了豆子System.out.println("eat");num++;if(foodType == 0) {score += 10;}else {score += (rand.nextInt(5) * 10 + 10);}int last = localList.size() - 1;//蛇尾			//在蛇尾后面添加一节身体localList.add(new int[] {localList.get(last)[0],localList.get(last)[1]});return true;}return false;}//移动方法public void move() {//判断是否游戏结束if(isbody()) {System.out.println("game over");start = false;//结束游戏移动JOptionPane.showMessageDialog(null,"游戏已结束!");time.stop();init();		}if(flag && localList != null) {//如果长度不为空且游戏未结束				int last = localList.size() - 1;//记录蛇尾for (int i = last; i > 0; i--) {//从蛇尾开始,每节身体移动到前一节身体的位置上localList.set(i,new int[] {localList.get(i - 1)[0],localList.get(i - 1)[1]});}//记录头位置int[] local = localList.get(0);//判断当前方向,并进行模拟移动,判断是否与边界重合if(direction.equals("R")) {if(local[0] >= 850) {local[0] = 25;}else {local[0] += 25;}}else if(direction.equals("L")) {if(local[0] <= 25) {local[0] = 850;}else {local[0] -= 25;}}else if(direction.equals("U")) {if(local[1] <= 75) {local[1] = 650;}else {local[1] -= 25;}}else if(direction.equals("D")) {if(local[1] >= 650) {local[1] = 75;}else {local[1] += 25;}}			//更改头的位置localList.set(0, local);		}	}//判断下一步是否为蛇身private boolean isbody() {// TODO 自动生成的方法存根//记录头位置int x = localList.get(0)[0];int y = localList.get(0)[1];//判断当前方向,并进行模拟移动,判断是否与边界重合if(direction.equals("R")) {x += 25;}else if(direction.equals("L")) {x -= 25;}else if(direction.equals("U")) {y -= 25;}else if(direction.equals("D")) {y += 25;}			for (int i = 1; i < localList.size(); i++) {if(localList.get(i)[0] == x && localList.get(i)[1] == y) {return true;}}return false;}//	//判断下一步是否为边界
//	private boolean isborder() {
//		// TODO 自动生成的方法存根
//		//记录头位置
//		// TODO 自动生成的方法存根
//		//记录头位置
//		int x = localList.get(0)[0];
//		int y = localList.get(0)[1];
//
//		//判断当前方向,并进行模拟移动,判断是否与边界重合
//		if(direction.equals("R")) {
//			x += 25;
//		}else if(direction.equals("L")) {
//			x -= 25;
//		}else if(direction.equals("U")) {
//			y -= 25;
//		}else if(direction.equals("D")) {
//			y += 25;
//		}	
//				
//		if(x < 25 || x > (33 * 25 + 25)) {
//			return true;//当x坐标超出边界,则返回true
//		}
//		if(y < 105 || y > (23 * 25 + 105)) {
//			return true;//当y坐标超出边界,则返回true
//		}
//		return false;//蛇头移动后未超出边界,返回false
//		
//	}/*** Create the frame.*/public SnakeJPanel(int speed) {this.speed = speed; //初始化速度//初始化游戏面板的基本信息this.setSize(900, 700);this.setLocation(0, 30);this.setFocusable(true);init();//初始化界面drawImage();//绘制图片moveByKey();//给界面添加一个键盘监听}/** 键盘监听* 通过键盘输入上下左右来控制当前蛇头移动的方向* 先判断当前蛇头方向,再来改变引导方向* 当进行绘制时再修改蛇的方向* 保证不会因为在短时间内快速变换方向导致蛇头逆向转向*/private void moveByKey() {addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {int key = e.getKeyCode();//边界值判断switch(key) {case 65:case 37:{//向左走if(!direction.equals("R")) {direction2 = "L";}break;}				case 87:case 38:{//向上走if(!direction.equals("D")) {direction2 = "U";}				break;}				case 68:case 39:{//向右走if(!direction.equals("L")) {direction2 = "R";}break;}case 83:case 40:{//向下走if(!direction.equals("U")) {direction2 = "D";}					break;}case KeyEvent.VK_SPACE:{//如果当前键盘输入为空格start = !start;//调整游戏状态System.out.println("暂停/开始");repaint();//重绘}}//任意键开始if(!start && key != KeyEvent.VK_SPACE) {//如果当前状态为暂停状态,且键盘输入不是空格start = true;repaint();//重绘}				}});}/*** 初始化游戏基本信息*/private void init() {start = false;exist = true;direction2 = "U";flag = true;localList = new ArrayList<int[]>();localList.add(0,new int[] {75,125});//蛇头localList.add(1,new int[] {75,150});//蛇身1localList.add(2,new int[] {75,175});//蛇身2//创建第一个食物的位置//通过循环保证当前生成的食物不在身体所在的坐标上boolean isProduce = true;while(isProduce) {//循环生成食物坐标isProduce = false;//结束本次循环x = rand.nextInt(33) * 25 + 25;		y = rand.nextInt(23) * 25 + 75;			for (int[] arr:localList) {//循环遍历蛇头及蛇身的坐标if(x == arr[0] && y == arr[1]) {//如果食物坐标和蛇的某一节坐标重合isProduce = true;//跳转循环状态,继续下一次食物生成break;	}}//蛇身遍历完成,没有重合坐标,结束食物坐标生成}time = new Timer(speed, this);setLayout(null);score = 0;num = 0;foodType = 0;//		repaint();}}

三、构造启动类

package com.snake.controller;import javax.swing.JFrame;
import javax.swing.JOptionPane;import com.snake.view.SnakeJPanel;public class SnakeStart {public static void main(String[] args) {int speed = 0;String showInputDialog = null;//初始化时间//得到速度while(true) {showInputDialog = JOptionPane.showInputDialog("蛇移动速度(1 - 5)","3");if(showInputDialog == null) {showInputDialog = "3";//默认速度break;}if(showInputDialog.length() > 1) {continue;}char[] a = showInputDialog.toCharArray();if(a[0] >= '1' && a[0] <= '5') {break;}}speed = Integer.parseInt(showInputDialog) * 50;SnakeJPanel snakeJPanel = new SnakeJPanel(speed);//创建一个JFrame窗口,将游戏面板添加进行窗口中JFrame jFrame = new JFrame();//设置窗口的某些属性jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jFrame.setSize(920, 750);jFrame.add(snakeJPanel);jFrame.setLocationRelativeTo(null);jFrame.setVisible(true);}}

四、游戏启动

设置游戏速度

游戏界面

 俄罗斯方块

游戏规则:

由小方块组成的不同形状的板块陆续从屏幕上方落下来,玩家通过调整板块的位置和方向,使它们在屏幕底部拼出完整的一条或几条。这些完整的横条会随即消失,给新落下来的板块腾出空间,与此同时,玩家得到分数奖励。没有被消除掉的方块不断堆积起来,一旦堆到屏幕顶端,玩家便告输,游戏结束。

整体代码分为三个模块:方格模块,七种图形模块,俄罗斯方块主模块。

小方块类:Cell

package com.zhao.demo.block;import java.awt.image.BufferedImage;
import java.util.Objects;/*** @author xiaoZhao* @date 2022/5/7* @describe*  小方块类*   方法: 左移、右移、下落*/
public class Cell {// 行private int row;// 列private int col;private BufferedImage image;public Cell() {}public Cell(int row, int col, BufferedImage image) {this.row = row;this.col = col;this.image = image;}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;}@Overridepublic String toString() {return "Cell{" +"row=" + row +", col=" + col +", image=" + image +'}';}@Overridepublic boolean equals(Object o) {if (this == o) {return true;}if (!(o instanceof Cell)) {return false;}Cell cell = (Cell) o;return getRow() == cell.getRow() &&getCol() == cell.getCol() &&Objects.equals(getImage(), cell.getImage());}@Overridepublic int hashCode() {return Objects.hash(getRow(), getCol(), getImage());}//左移动一格public void left(){col--;}//右移动一格public void right(){col++;}//下移动一格public void down(){row++;}
}

 四方格图形的父类:Tetromino

package com.zhao.demo.block;import com.zhao.demo.shape.*;/*** @author xiaoZhao* @date 2022/5/11* @describe 编写四方格父类*/
public class Tetromino {public Cell[] cells = new Cell[4];//旋转的状态protected State[] states;//声明旋转次数protected int count = 10000;//左移方法public void moveLeft() {for (Cell cell : cells) {cell.left();}}//右移方法public void moveRight() {for (Cell cell : cells) {cell.right();}}//单元格下落public void moveDrop() {for (Cell cell : cells) {cell.down();}}//编写随机生成四方格public static Tetromino randomOne() {int num = (int) (Math.random() * 7);Tetromino tetromino = null;switch (num) {case 0:tetromino = new I();break;case 1:tetromino = new J();break;case 2:tetromino = new L();break;case 3:tetromino = new O();break;case 4:tetromino = new S();break;case 5:tetromino = new T();break;case 6:tetromino = new Z();break;}return tetromino;}//顺时针旋转的方法public void rotateRight() {if (states.length == 0) {return;}//旋转次数+1count++;State s = states[count % states.length];Cell cell = cells[0];int row = cell.getRow();int col = cell.getCol();cells[1].setRow(row + s.row1);cells[1].setCol(col + s.col1);cells[2].setRow(row + s.row2);cells[2].setCol(col + s.col2);cells[3].setRow(row + s.row3);cells[3].setCol(col + s.col3);}//逆时针旋转的方法public void rotateLeft() {if (states.length == 0) {return;}//旋转次数+1count--;State s = states[count % states.length];Cell cell = cells[0];int row = cell.getRow();int col = cell.getCol();cells[1].setRow(row + s.row1);cells[1].setCol(col + s.col1);cells[2].setRow(row + s.row2);cells[2].setCol(col + s.col2);cells[3].setRow(row + s.row3);cells[3].setCol(col + s.col3);}//四方格旋转状态的内部类protected class State {//存储四方格各元素的位置int row0, col0, row1, col1, row2, col2, row3, col3;public State() {}public State(int row0, int col0, int row1, int col1, int row2, int col2, int row3, int col3) {this.row0 = row0;this.col0 = col0;this.row1 = row1;this.col1 = col1;this.row2 = row2;this.col2 = col2;this.row3 = row3;this.col3 = col3;}public int getRow0() {return row0;}public void setRow0(int row0) {this.row0 = row0;}public int getCol0() {return col0;}public void setCol0(int col0) {this.col0 = col0;}public int getRow1() {return row1;}public void setRow1(int row1) {this.row1 = row1;}public int getCol1() {return col1;}public void setCol1(int col1) {this.col1 = col1;}public int getRow2() {return row2;}public void setRow2(int row2) {this.row2 = row2;}public int getCol2() {return col2;}public void setCol2(int col2) {this.col2 = col2;}public int getRow3() {return row3;}public void setRow3(int row3) {this.row3 = row3;}public int getCol3() {return col3;}public void setCol3(int col3) {this.col3 = col3;}@Overridepublic String toString() {return "State{" +"row0=" + row0 +", col0=" + col0 +", row1=" + row1 +", col1=" + col1 +", row2=" + row2 +", col2=" + col2 +", row3=" + row3 +", col3=" + col3 +'}';}}
}

 

七种图形类:I、J、L、O、S、T、Z

 I

package com.zhao.demo.shape;import com.zhao.demo.App.Tetris;
import com.zhao.demo.block.Cell;
import com.zhao.demo.block.Tetromino;/*** @author xiaoZhao* @date 2022/5/11* @describe*/
public class I extends Tetromino {public I() {cells[0] = new Cell(0,4, Tetris.I);cells[1] = new Cell(0,3, Tetris.I);cells[2] = new Cell(0,5, Tetris.I);cells[3] = new Cell(0,6, Tetris.I);//共有两种旋转状态states =new State[2];//初始化两种状态的相对坐标states[0]=new State(0,0,0,-1,0,1,0,2);states[1]=new State(0,0,-1,0,1,0,2,0);}}

J

package com.zhao.demo.shape;import com.zhao.demo.App.Tetris;
import com.zhao.demo.block.Cell;
import com.zhao.demo.block.Tetromino;/*** @author xiaoZhao* @date 2022/5/11* @describe*/
public class J extends Tetromino {public J() {cells[0] = new Cell(0,4, Tetris.J);cells[1] = new Cell(0,3, Tetris.J);cells[2] = new Cell(0,5, Tetris.J);cells[3] = new Cell(1,5, Tetris.J);states=new State[4];states[0]=new State(0,0,0,-1,0,1,1,1);states[1]=new State(0,0,-1,0,1,0,1,-1);states[2]=new State(0,0,0,1,0,-1,-1,-1);states[3]=new State(0,0,1,0,-1,0,-1,1);}
}

 L

package com.zhao.demo.shape;import com.zhao.demo.App.Tetris;
import com.zhao.demo.block.Cell;
import com.zhao.demo.block.Tetromino;/*** @author xiaoZhao* @date 2022/5/11* @describe*/
public class L extends Tetromino {public L() {cells[0] = new Cell(0,4, Tetris.L);cells[1] = new Cell(0,3, Tetris.L);cells[2] = new Cell(0,5, Tetris.L);cells[3] = new Cell(1,3, Tetris.L);states=new State[4];states[0]=new State(0,0,0,-1,0,1,1,-1);states[1]=new State(0,0,-1,0,1,0,-1,-1);states[2]=new State(0,0,0,1,0,-1,-1,1);states[3]=new State(0,0,1,0,-1,0,1,1);}
}

O

package com.zhao.demo.shape;import com.zhao.demo.App.Tetris;
import com.zhao.demo.block.Cell;
import com.zhao.demo.block.Tetromino;/*** @author xiaoZhao* @date 2022/5/11* @describe*/
public class O extends Tetromino {public O() {cells[0] = new Cell(0, 4, Tetris.O);cells[1] = new Cell(0, 5, Tetris.O);cells[2] = new Cell(1, 4, Tetris.O);cells[3] = new Cell(1, 5, Tetris.O);//无旋转状态states = new State[0];}
}

package com.zhao.demo.shape;import com.zhao.demo.App.Tetris;
import com.zhao.demo.block.Cell;
import com.zhao.demo.block.Tetromino;/*** @author xiaoZhao* @date 2022/5/11* @describe*/
public class S extends Tetromino {public S() {cells[0] = new Cell(0,4, Tetris.S);cells[1] = new Cell(0,5, Tetris.S);cells[2] = new Cell(1,3, Tetris.S);cells[3] = new Cell(1,4, Tetris.S);//共有两种旋转状态states =new State[2];//初始化两种状态的相对坐标states[0]=new State(0,0,0,1,1,-1,1,0);states[1]=new State(0,0,1,0,-1,-1,0,-1);}
}

package com.zhao.demo.shape;import com.zhao.demo.App.Tetris;
import com.zhao.demo.block.Cell;
import com.zhao.demo.block.Tetromino;/*** @author xiaoZhao* @date 2022/5/11* @describe*/
public class T extends Tetromino {public T() {cells[0] = new Cell(0,4, Tetris.T);cells[1] = new Cell(0,3, Tetris.T);cells[2] = new Cell(0,5, Tetris.T);cells[3] = new Cell(1,4, Tetris.T);states=new State[4];states[0]=new State(0,0,0,-1,0,1,1,0);states[1]=new State(0,0,-1,0,1,0,0,-1);states[2]=new State(0,0,0,1,0,-1,-1,0);states[3]=new State(0,0,1,0,-1,0,0,1);}
}

package com.zhao.demo.shape;import com.zhao.demo.App.Tetris;
import com.zhao.demo.block.Cell;
import com.zhao.demo.block.Tetromino;/*** @author xiaoZhao* @date 2022/5/11* @describe*/
public class Z extends Tetromino {public Z() {cells[0] = new Cell(1,4, Tetris.Z);cells[1] = new Cell(0,3, Tetris.Z);cells[2] = new Cell(0,4, Tetris.Z);cells[3] = new Cell(1,5, Tetris.Z);//共有两种旋转状态states =new State[2];//初始化两种状态的相对坐标states[0]=new State(0,0,-1,-1,-1,0,0,1);states[1]=new State(0,0,-1,1,0,1,1,0);}
}

俄罗斯方块游戏主类:Tetris

package com.zhao.demo.App;import com.zhao.demo.block.Cell;
import com.zhao.demo.block.Tetromino;import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.security.cert.Certificate;/*** @author xiaoZhao* @date 2022/5/11* @describe 俄罗斯方块游戏主类*/
public class Tetris extends JPanel {//正在下落的方块private Tetromino currentOne = Tetromino.randomOne();//将要下落的方块private Tetromino nextOne = Tetromino.randomOne();//游戏主区域private Cell[][] wall = new Cell[18][9];//声明单元格的值private static final int CELL_SIZE = 48;//游戏分数池int[] scores_pool = {0, 1, 2, 5, 10};//当前游戏的分数private int totalScore = 0;//当前消除的行数private int totalLine = 0;//游戏三种状态 游戏中、暂停、结束public static final int PLING = 0;public static final int STOP = 1;public static final int OVER = 2;//当前游戏状态值private int game_state;//显示游戏状态String[] show_state = {"P[pause]", "C[continue]", "S[replay]"};//载入方块图片public static BufferedImage I;public static BufferedImage J;public static BufferedImage L;public static BufferedImage O;public static BufferedImage S;public static BufferedImage T;public static BufferedImage Z;public static BufferedImage background;static {try {I = ImageIO.read(new File("images/I.png"));J = ImageIO.read(new File("images/J.png"));L = ImageIO.read(new File("images/L.png"));O = ImageIO.read(new File("images/O.png"));S = ImageIO.read(new File("images/S.png"));T = ImageIO.read(new File("images/T.png"));Z = ImageIO.read(new File("images/Z.png"));background = ImageIO.read(new File("images/background.png"));} catch (IOException e) {e.printStackTrace();}}@Overridepublic void paint(Graphics g) {g.drawImage(background, 0, 0, null);//平移坐标轴g.translate(22, 15);//绘制游戏主区域paintWall(g);//绘制正在下落的四方格paintCurrentOne(g);//绘制下一个将要下落的四方格paintNextOne(g);//绘制游戏得分paintSource(g);//绘制当前游戏状态paintState(g);}public void start() {game_state = PLING;KeyListener l = new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {int code = e.getKeyCode();switch (code) {case KeyEvent.VK_DOWN:sortDropActive();break;case KeyEvent.VK_LEFT:moveleftActive();break;case KeyEvent.VK_RIGHT:moveRightActive();break;case KeyEvent.VK_UP:rotateRightActive();break;case KeyEvent.VK_SPACE:hadnDropActive();break;case KeyEvent.VK_P://判断当前游戏状态if (game_state == PLING) {game_state = STOP;}break;case KeyEvent.VK_C:if (game_state == STOP) {game_state = PLING;}break;case KeyEvent.VK_S://重新开始game_state = PLING;wall = new Cell[18][9];currentOne = Tetromino.randomOne();nextOne = Tetromino.randomOne();totalScore = 0;totalLine = 0;break;}}};//将窗口设置为焦点this.addKeyListener(l);this.requestFocus();while (true) {if (game_state == PLING) {try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}if (camDrop()) {currentOne.moveDrop();} else {landToWall();destroyLine();if (isGameOver()) {game_state = OVER;} else {//游戏没有结束currentOne = nextOne;nextOne = Tetromino.randomOne();}}}repaint();}}//创建顺时针旋转public void rotateRightActive() {currentOne.rotateRight();if (outOFBounds() || coincide()) {currentOne.rotateLeft();}}//瞬间下落public void hadnDropActive() {while (true) {//判断能否下落if (camDrop()) {currentOne.moveDrop();} else {break;}}//嵌入到墙中landToWall();destroyLine();if (isGameOver()) {game_state = OVER;} else {//游戏没有结束currentOne = nextOne;nextOne = Tetromino.randomOne();}}//按键一次,下落一格public void sortDropActive() {if (camDrop()) {//当前四方格下落一格currentOne.moveDrop();} else {landToWall();destroyLine();if (isGameOver()) {game_state = OVER;} else {//游戏没有结束currentOne = nextOne;nextOne = Tetromino.randomOne();}}}//单元格嵌入墙中private void landToWall() {Cell[] cells = currentOne.cells;for (Cell cell : cells) {int row = cell.getRow();int col = cell.getCol();wall[row][col] = cell;}}//判断四方格能否下落public boolean camDrop() {Cell[] cells = currentOne.cells;for (Cell cell : cells) {int row = cell.getRow();int col = cell.getCol();//判断是否到达底部if (row == wall.length - 1) {return false;} else if (wall[row + 1][col] != null) {return false;}}return true;}//消除行public void destroyLine() {int line = 0;Cell[] cells = currentOne.cells;for (Cell cell : cells) {int row = cell.getRow();if (isFullLine(row)) {line++;for (int i = row; i > 0; i--) {System.arraycopy(wall[i - 1], 0, wall[i], 0, wall[0].length);}wall[0] = new Cell[9];}}//分数池获取分数,累加到总分totalScore += scores_pool[line];//总行数totalLine += line;}//判断当前行是否已经满了public boolean isFullLine(int row) {Cell[] cells = wall[row];for (Cell cell : cells) {if (cell == null) {return false;}}return true;}//判断游戏是否结束public boolean isGameOver() {Cell[] cells = nextOne.cells;for (Cell cell : cells) {int row = cell.getRow();int col = cell.getCol();if (wall[row][col] != null) {return true;}}return false;}private void paintState(Graphics g) {if (game_state == PLING) {g.drawString(show_state[PLING], 500, 660);} else if (game_state == STOP) {g.drawString(show_state[STOP], 500, 660);} else {g.drawString(show_state[OVER], 500, 660);g.setColor(Color.RED);g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 60));g.drawString("GAME OVER!", 30, 400);}}private void paintSource(Graphics g) {g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 30));g.drawString("分数: " + totalScore, 500, 250);g.drawString("行数: " + totalLine, 500, 430);}private void paintNextOne(Graphics g) {Cell[] cells = nextOne.cells;for (Cell cell : cells) {int x = cell.getCol() * CELL_SIZE + 370;int y = cell.getRow() * CELL_SIZE + 25;g.drawImage(cell.getImage(), x, y, null);}}private void paintCurrentOne(Graphics g) {Cell[] cells = currentOne.cells;for (Cell cell : cells) {int x = cell.getCol() * CELL_SIZE;int y = cell.getRow() * CELL_SIZE;g.drawImage(cell.getImage(), x, y, null);}}private void paintWall(Graphics g) {for (int i = 0; i < wall.length; i++) {for (int j = 0; j < wall[i].length; 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);}}}}//判断是否出界public boolean outOFBounds() {Cell[] cells = currentOne.cells;for (Cell cell : cells) {int col = cell.getCol();int row = cell.getRow();if (row < 0 || row > wall.length - 1 || col < 0 || col > wall[0].length-1) {return true;}}return false;}//按键一次,左移一次public void moveleftActive() {currentOne.moveLeft();//判断是否越界或重合if (outOFBounds() || coincide()) {currentOne.moveRight();}}//按键一次,右移一次public void moveRightActive() {currentOne.moveRight();//判断是否越界或重合if (outOFBounds() || coincide()) {currentOne.moveLeft();}}//判断是否重合public boolean coincide() {Cell[] cells = currentOne.cells;for (Cell cell : cells) {int row = cell.getRow();int col = cell.getCol();if (wall[row][col] != null) {return true;}}return false;}public static void main(String[] args) {JFrame jFrame = new JFrame("俄罗斯方块");//创建游戏界面Tetris panel = new Tetris();jFrame.add(panel);//设置可见jFrame.setVisible(true);//设置窗口大小jFrame.setSize(810, 940);//设置剧中jFrame.setLocationRelativeTo(null);//设置窗口关闭时停止jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//游戏主要开始逻辑panel.start();}
}

三、效果展示

游戏开始,方快下落,右边区域展示即将下落的方块图、分数、消除的行数以及游戏切换的状态。

按下空格键,方块瞬间下落, 按下P键游戏暂停,消除一行分数为1(此处由分数池进行控制)

  按下C键游戏继续。

方块占满,游戏结束,此时可以按下S键重新开始游戏。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/141504.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

重复性工作自动化解决方案——影刀

以前&#xff0c;影刀是一个邂逅的初见小工具&#xff0c;新奇在里头&#xff0c;踌躇在外头&#xff1b; 现在&#xff0c;影刀是一个稳定的职场贾维斯&#xff0c;高效在里头&#xff0c;悠闲在外头&#xff1b; 以后&#xff0c;影刀是一个潜力的知己老司机&#xff0c;有序…

2019年五一杯数学建模A题让标枪飞解题全过程文档及程序

2020年五一杯数学建模 A题 让标枪飞 原题再现 标枪的投掷是一项历史悠久的田径比赛项目。标枪投掷距离的远近受到运动员水平&#xff08;出手速度、出手角、初始攻角、出手高度、出手时标枪的初始俯仰角速度等&#xff09;&#xff0c;标枪的技术参数&#xff08;标枪的长度、…

网络运维Day14

监控概述 监控的目的 报告系统运行状况每一部分必须同时监控内容包括吞吐量、反应时间、使用率等提前发现问题进行服务器性能调整前&#xff0c;知道调整什么找出系统的瓶颈在什么地方 监控的资源类别 公开数据 Web、FTP、SSH、数据库等应用服务TCP或UDP端口 私有数据 CPU、内…

互联网Java工程师面试题·微服务篇·第三弹

目录 34、什么是端到端微服务测试&#xff1f; 35、Container 在微服务中的用途是什么&#xff1f; 36、什么是微服务架构中的 DRY&#xff1f; 37、什么是消费者驱动的合同&#xff08;CDC&#xff09;&#xff1f; 38、Web&#xff0c;RESTful API 在微服务中的作用是什…

Mysql-库的操作

1.创建数据库 CREATE DATABASE [IF NOT EXISTS] name name后可以加 CHARACTER SET 或者是 charsetname COLLATE collation_name &#xff08;mysql数据库不区分大小写&#xff09; 说明&#xff1a; name表示想创建的库的名字大写的表示关键字 [] 是可选项 CHARACTER SET…

python3+requests+unittest实战系列【二】

前言&#xff1a;上篇文章python3requestsunittest&#xff1a;接口自动化测试&#xff08;一&#xff09;已经介绍了基于unittest框架的实现接口自动化&#xff0c;但是也存在一些问题&#xff0c;比如最明显的测试数据和业务没有区分开&#xff0c;接口用例不便于管理等&…

CSS特效009:音频波纹加载律动

总第 009 篇文章&#xff0c; 查看专栏目录 本专栏记录的是经常使用的CSS示例与技巧&#xff0c;主要包含CSS布局&#xff0c;CSS特效&#xff0c;CSS花边信息三部分内容。其中CSS布局主要是列出一些常用的CSS布局信息点&#xff0c;CSS特效主要是一些动画示例&#xff0c;CSS花…

Python高级语法----Python C扩展与性能优化

文章目录 1. 编写Python C扩展模块示例代码编译和运行运行结果2. 利用Cython优化性能示例代码编译和运行运行结果3. Python性能分析工具示例代码分析结果1. 编写Python C扩展模块 Python C扩展模块允许你将C语言代码集成到Python程序中,以提高性能。这对于计算密集型任务特别…

物联网AI MicroPython学习之语法 bluetooth蓝牙

学物联网&#xff0c;来万物简单IoT物联网&#xff01;&#xff01; bluetooth 介绍 该模块为板上的蓝牙控制器提供了相关接口。目前支持低功耗蓝牙 (BLE)的Central&#xff08;中央&#xff09;, Peripheral&#xff08;外设&#xff09;, Broadcaster&#xff08;广播者&…

计算机视觉(CV)技术的优势和挑战

计算机视觉技术在很多领域具有很大的优势,例如: 自动化:计算机视觉技术可以帮助实现自动化生产和检测,省去了人力成本和时间成本。 准确性:计算机视觉技术可以提高生产和检测的准确性,降低了人工操作产生的误差。 速度:计算机视觉技术可以实现高速速度的生产和检测,提高…

深度学习 机器视觉 人脸识别系统 - opencv python 计算机竞赛

文章目录 0 前言1 机器学习-人脸识别过程人脸检测人脸对其人脸特征向量化人脸识别 2 深度学习-人脸识别过程人脸检测人脸识别Metric Larning 3 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 深度学习 机器视觉 人脸识别系统 该项目…

从头开始的卷积神经网络

VGG-16 卷积神经网络。来源&#xff1a;LearnOpenCV 参考资料&#xff1a;这篇文章可以在 Kaggle Notebook &#x1f9e0; Convolutional Neural Network From Scratch上更好地阅读。路易斯费尔南多托雷斯 一、说明 本文详细介绍在tf2.0上&#xff0c;使用ceras实现基本的神经…

C++字典树算法:找出强数对的最大异或值 II

涉及知识点 数学 字典树 题目 给你一个下标从 0 开始的整数数组 nums 。如果一对整数 x 和 y 满足以下条件&#xff0c;则称其为 强数对 &#xff1a; |x - y| < min(x, y) 你需要从 nums 中选出两个整数&#xff0c;且满足&#xff1a;这两个整数可以形成一个强数对&…

mysql主从复制-使用心得

文章目录 前言环境配置主库从库 STATEMENTbinloggtidlog-errorDistSQL总结 前言 mysql 主从复制使用感受&#xff0c;遇到一些问题的整理&#xff0c;也总结了一些排查问题技巧。 环境 mysql5.7 配置 附&#xff1a;千万级数据快速插入配置可以参考&#xff1a;mysql千万数…

HarmonyOS应用开发者高级认证(88分答案)

看好选择题&#xff0c;每个2分多答对2个刚好88分&#xff0c;祝你顺利。 其它帮扶选择题。 一、判断 只要使用端云一体化的云端资源就需要支付费用&#xff08;错&#xff09;所有使用Component修饰的自定义组件都支持onPageShow&#xff0c;onBackPress和onPageHide生命周期…

vue3配置环境变量,小白简单易学

环境变量的意义就是防止我们更新打包的时候写错变量&#xff0c;合并代码这些一系列问题 首先看看效果 左边是本地测试环境&#xff0c;右边是打包后的生产环境&#xff0c;写这个环境变量的好处就是&#xff0c;你在本地开发的时候变量随便改&#xff0c;不会影响生产环境&am…

07【保姆级】-GO语言的程序流程控制【if switch for while 】

之前我学过C、Java、Python语言时总结的经验&#xff1a; 先建立整体框架&#xff0c;然后再去抠细节。先Know how&#xff0c;然后know why。先做出来&#xff0c;然后再去一点点研究&#xff0c;才会事半功倍。适当的囫囵吞枣。因为死抠某个知识点很浪费时间的。对于GO语言&a…

2024 款:最新前端技术趋势

Hello&#xff0c;大家好&#xff0c;我是 Sunday。 上一次的时候聊了 那么些已经落后的前端开发技术 。但是光知道什么技术落后了是不够的&#xff0c;咱们还得知道 前端最新的技术趋势是什么。所以&#xff0c;今天这篇文章&#xff0c;咱们就来聊一聊&#xff0c;2023 最新…

Spring后端HttpClient实现微信小程序登录

这是微信官方提供的时序图。我们需要关注的是前后端的交互&#xff0c;以及服务端如何收发网络请求。 小程序端 封装基本网络请求 我们先封装一个基本的网络请求。 const baseUrl"localhost:8080" export default{sendRequsetAsync } /* e url&#xff1a;目标页…

对比国内主流开源 SQL 审核平台 Yearning vs Archery

Yearning, Archery 和 Bytebase 是目前国内最主流的三个开源 SQL 审核平台。其中 Yearning 和 Archery 是社区性质的项目&#xff0c;而 Bytebase 则是商业化产品。通常调研 Bytebase 的用户也会同时比较 Yearning 和 Archery。 下面我们就来展开对比一下 Yearning 和 Archery…