java游戏代码.docx
- 文档编号:8608272
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:49
- 大小:29.66KB
java游戏代码.docx
《java游戏代码.docx》由会员分享,可在线阅读,更多相关《java游戏代码.docx(49页珍藏版)》请在冰豆网上搜索。
java游戏代码
java游戏代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTetrix extends JFrame implements Runnable {
private ImageIcon iconLogo;
private JPanel nextPanel, scorePanel, opPanel, gamePanel, hidedOpPanel;
private JLabel labLevel, labLine, labScore;
private final JTextArea keyfocus; // 键盘的操作焦点,隐藏于游戏画面之后
private JButton btnNew, btnPause, btnQuit;
private showNextPiece nextPieceArea; // 预览下一方块
private final gameOpBoard gameOpArea; // 游戏画面
private Container c;
private int Level, Score, LineRemoved; // 等级、得分、移除总行数
private double interval; // 暂停时间,用于控制速度
private boolean pause; // 是否暂停
private Thread loopThread; // 游戏回圈执行绪
static final long serialVersionUID = 1;
public JTetrix() {
c = getContentPane();
//iconLogo = new ImageIcon(JTetrix.class.getResource("logo.jpg"));
Level = 1;
Score = 0;
LineRemoved = 0;
// 预览下一个方块的面版配置
nextPanel = new JPanel();
nextPanel.setLayout(null);
nextPanel.setBorder(BorderFactory.createTitledBorder("下一个"));
nextPanel.setLocation(10, 10);
nextPanel.setSize(150, 120);
nextPieceArea = new showNextPiece(5, 6, 15, 15);
nextPieceArea.generateNextPiece(); // 先产生第一片待取
nextPieceArea.setLocation(40, 20);
nextPanel.add(nextPieceArea);
// 等级、得分面版配置
scorePanel = new JPanel();
scorePanel.setBorder(BorderFactory.createTitledBorder("等级 / 得分"));
scorePanel.setLocation(10, 140);
scorePanel.setSize(150, 160);
scorePanel.add(new JLabel(" 速度等级 "));
scorePanel.add(labLevel = new JLabel("1", SwingConstants.CENTER));
scorePanel.add(new JLabel(" 移除行数 "));
scorePanel.add(labLine = new JLabel("0", SwingConstants.CENTER));
scorePanel.add(new JLabel(" 目前得分 "));
scorePanel.add(labScore = new JLabel("0", SwingConstants.CENTER));
// 功能面版配置
opPanel = new JPanel();
opPanel.setBorder(BorderFactory.createTitledBorder(""));
opPanel.setLocation(10, 320);
opPanel.setSize(150, 130);
opPanel.add(btnNew = new JButton("开新游戏"));
opPanel.add(btnPause = new JButton("暂停游戏"));
opPanel.add(btnQuit = new JButton("关闭游戏"));
// 游戏操作画面面版配置
gameOpArea = new gameOpBoard(10, 22, 18, 18);
gameOpArea.setLocation(20, 20);
gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix()); // 先取出一个方块
nextPieceArea.generateNextPiece(); // 预览窗格产生下一个方块预览
gamePanel = new JPanel();
gamePanel.setLayout(null);
gamePanel.setLocation(180, 20);
gamePanel.setSize(12 * 18, 24 * 18);
gamePanel.setBorder(BorderFactory.createTitledBorder(""));
gamePanel.add(gameOpArea);
// 隐藏的操作面版配置
hidedOpPanel = new JPanel();
hidedOpPanel.setLayout(new BorderLayout());
hidedOpPanel.setLocation(200, 40);
hidedOpPanel.setSize(10 * 18, 22 * 18);
keyfocus = new JTextArea();
keyfocus.setEditable(false);
hidedOpPanel.add(keyfocus);
// 加入面版至视窗
c.setLayout(null);
c.add(nextPanel);
c.add(scorePanel);
c.add(opPanel);
c.add(gamePanel);
c.add(hidedOpPanel);
// 键盘事件处理
keyfocus.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT)
gameOpArea.moveRight();
else if (key == KeyEvent.VK_LEFT)
gameOpArea.moveLeft();
else if (key == KeyEvent.VK_UP)
gameOpArea.RotateRL
(1);
else if (key == KeyEvent.VK_DOWN)
gameOpArea.RotateRL(0);
else if (key == KeyEvent.VK_SPACE) {
gameOpArea.soonMoveDown();
}
}
});
// 开新游戏
btnNew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newGame();
keyfocus.requestFocus();
}
});
// 暂停游戏
btnPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pause = true;
JOptionPane.showOptionDialog(null, "程式名称:
\n JTetrix v0.1\n"
+ "程式设计:
\n younganne\n"
+ "简介:
\n 一个用Java写的俄罗斯方块游戏\n", "关于JTetrix",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
pause = false;
interval = 2000 / Level;
loopThread.interrupt();
keyfocus.requestFocus();
}
});
// 关闭游戏
btnQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 启动游戏回圈执行绪
loopThread = new Thread(this);
loopThread.start();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JTetrix v0.1");
setSize(420, 500);
setVisible(true);
}
// 游戏回圈的执行绪执行对象
public void run() {
while (true) { // 游戏回圈
try {
// 是否暂停
if (pause)
interval = 999999999;
else
interval = 2000 / Level;
if (gameOpArea.isUpdated()) {// 由游戏画面的阵列是否更新来判断是否取出下一个
gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix());
nextPieceArea.generateNextPiece();
// 更新等级、得分等资讯
Score = gameOpArea.getScore();
labScore.setText("" + Score);
LineRemoved = gameOpArea.getLineRemoved();
labLine.setText("" + LineRemoved);
Level = (int) ((Score + 100) / 100); // 每一百分升级一次
labLevel.setText("" + Level);
}
Thread.sleep((int) interval);
gameOpArea.MoveDown(); // 不断下移
} catch (InterruptedException e) {
}
}
}
// 开新游戏
public void newGame() {
Level = 1;
Score = 0;
LineRemoved = 0;
labScore.setText("" + Score);
labLine.setText("" + LineRemoved);
labLevel.setText("" + Level);
gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix()); // 先取出一个方块
gameOpArea.newState();
nextPieceArea.generateNextPiece(); // 预览窗格产生下一个方块预览
}
public static void main(String[] args) {
JTetrix frm = new JTetrix();
}
}
// 方块的资料结构与操作方法
class TetrixPiece {
private int pieceType; // 方块样式
private int[][] coordinates = new int[4][2]; // 四个方块,记录X与Y
public TetrixPiece() {
initialize((int) (Math.random() * 7 + 1));
}
public TetrixPiece(int type) {
initialize(type);
}
// 向左转动
public void rotateLeft() {
if (pieceType == 5) // 不转动正方形方块
return;
int tmp;
for (int i = 0; i < 4; i++) {
tmp = getXCoord(i);
setXCoord(i, getYCoord(i));
setYCoord(i, -tmp);
}
}
// 向右转动
public void rotateRight() {
if (pieceType == 5) // 不转动正方形方块
return;
int tmp;
for (int i = 0; i < 4; i++) {
tmp = getXCoord(i);
setXCoord(i, -getYCoord(i));
setYCoord(i, tmp);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTetrix extends JFrame implements Runnable {
private ImageIcon iconLogo;
private JPanel nextPanel, scorePanel, opPanel, gamePanel, hidedOpPanel;
private JLabel labLevel, labLine, labScore;
private final JTextArea keyfocus; // 键盘的操作焦点,隐藏于游戏画面之后
private JButton btnNew, btnPause, btnQuit;
private showNextPiece nextPieceArea; // 预览下一方块
private final gameOpBoard gameOpArea; // 游戏画面
private Container c;
private int Level, Score, LineRemoved; // 等级、得分、移除总行数
private double interval; // 暂停时间,用于控制速度
private boolean pause; // 是否暂停
private Thread loopThread; // 游戏回圈执行绪
static final long serialVersionUID = 1;
public JTetrix() {
c = getContentPane();
//iconLogo = new ImageIcon(JTetrix.class.getResource("logo.jpg"));
Level = 1;
Score = 0;
LineRemoved = 0;
// 预览下一个方块的面版配置
nextPanel = new JPanel();
nextPanel.setLayout(null);
nextPanel.setBorder(BorderFactory.createTitledBorder("下一个"));
nextPanel.setLocation(10, 10);
nextPanel.setSize(150, 120);
nextPieceArea = new showNextPiece(5, 6, 15, 15);
nextPieceArea.generateNextPiece(); // 先产生第一片待取
nextPieceArea.setLocation(40, 20);
nextPanel.add(nextPieceArea);
// 等级、得分面版配置
scorePanel = new JPanel();
scorePanel.setBorder(BorderFactory.createTitledBorder("等级 / 得分"));
scorePanel.setLocation(10, 140);
scorePanel.setSize(150, 160);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 游戏 代码