這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)利用Java編寫一個(gè)俄羅斯方塊小游戲,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為東營(yíng)企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè),東營(yíng)網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
Java游戲俄羅斯方塊的實(shí)現(xiàn)實(shí)例
java小游戲主要理解應(yīng)用java Swing,awt等基礎(chǔ)組件的知識(shí),通過本例應(yīng)當(dāng)掌握面向?qū)ο蟮闹R(shí)。
實(shí)現(xiàn)代碼:
package cn.hncu.games;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Tetris extends JFrame {
private TetrisPanel tp;
public Tetris() {
// 添加菜單,這里只是意思一下,留待大家自己做更詳細(xì)的
// 菜單條
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
// 菜單
JMenu menuGame = new JMenu("游戲");
menubar.add(menuGame);
// 菜單項(xiàng)
JMenuItem mi1 = new JMenuItem("新游戲");
mi1.setActionCommand("new");
JMenuItem mi2 = new JMenuItem("暫停");
mi2.setActionCommand("pause");
JMenuItem mi3 = new JMenuItem("繼續(xù)");
mi3.setActionCommand("continue");
JMenuItem mi4 = new JMenuItem("退出");
mi4.setActionCommand("exit");
menuGame.add(mi1);
menuGame.add(mi2);
menuGame.add(mi3);
menuGame.add(mi4);
//菜單項(xiàng)監(jiān)聽
MenuListener menuListener = new MenuListener();
mi1.addActionListener(menuListener);
mi2.addActionListener(menuListener);
mi3.addActionListener(menuListener);
mi4.addActionListener(menuListener);
// 版本菜單
JMenu menuHelp = new JMenu("幫助");
menubar.add(menuHelp);
menuHelp.add("版本所有@湖南城院 QQ:666688888");
setLocation(700, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(220, 275);
setResizable(false);
tp = new TetrisPanel();
getContentPane().add(tp);
// 讓整個(gè)畫布添加鍵盤監(jiān)聽
// tp.addKeyListener(tp.listener);//不行,畫布不方便獲得鍵盤焦點(diǎn)
this.addKeyListener(tp.listener); // 讓框架來監(jiān)聽鍵盤
}
public static void main(String[] args) {
Tetris te = new Tetris();
te.setVisible(true);
}
class MenuListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equalsIgnoreCase("new")){
getContentPane().remove(tp);
tp = new TetrisPanel();
getContentPane().add(tp);
getContentPane().validate();//校驗(yàn)當(dāng)前容器,有刷新功能
}else if(e.getActionCommand().equalsIgnoreCase("pause")){
tp.getTimer().stop();
}else if(e.getActionCommand().equalsIgnoreCase("continue")){
tp.getTimer().restart();
}else if(e.getActionCommand().equalsIgnoreCase("exit")){
System.exit(0);
}
}
}
}
class TetrisPanel extends JPanel {
private int map[][] = new int[13][23];// map[列號(hào)][行號(hào)]。真正的方塊區(qū)是:21行*10列。邊框(2列,1行)
// 方塊的形狀:
// 第一維代表方塊類型(包括7種:S、Z、L、J、I、O、T)
// 第二維代表旋轉(zhuǎn)次數(shù)
// 第三四維代表方塊矩陣
// shapes[type][turnState][i] i--> block[i/4][i%4]
int shapes[][][] = new int[][][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, 0, 0, 0, 0, 0, 0, 0, 0 } },
// O
{ { 1, 1, 0, 0, 1, 1, 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 },
{ 1, 1, 0, 0, 1, 1, 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 } },
// L
{ { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
// T
{ { 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 } } };
private int type;
private int turnState;
private int x, y;// 當(dāng)前塊的位置---左上角的坐標(biāo)
private int score = 0;
private Timer timer = null;
private int delay = 1000;
TimerListener listener = null;
public TetrisPanel() {
newGame();
nextBlock();
listener = new TimerListener();
timer = new Timer(delay, listener);
timer.start();
}
private void newGame() {
// 初始化游戲地圖
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 21; j++) {
if (i == 0 || i == 11) {// 邊框
map[i][j] = 3;
} else {
map[i][j] = 0;
}
}
map[i][21] = 3;
}
score = 0;
}
private void nextBlock() {
type = (int) (Math.random() * 1000) % 7; // type=5;
turnState = (int) (Math.random() * 1000) % 4; // turnState=3;
x = 4;
y = 0;
if (crash(x, y, type, turnState) == 0) {
timer.stop();
int op = JOptionPane.showConfirmDialog(null,
"Game Over!....笨蛋,敢再來一局嗎?!");
if (op == JOptionPane.YES_OPTION) {
newGame();
} else if (op == JOptionPane.NO_OPTION) {
System.exit(0);
}
}
}
private void down() {
if (crash(x, y + 1, type, turnState) == 0) {// 判斷當(dāng)前塊往下落一格后是否和地圖存在填充塊完全重合---注意實(shí)參:y+1
add(x, y, type, turnState);// 把該塊加到地圖---形成堆積塊
nextBlock();
} else {
y++;
}
repaint();
}
private void left() {
if (x >= 0) {
x -= crash(x - 1, y, type, turnState);
}
repaint();
}
private void right() {
if (x < 8) {
x += crash(x + 1, y, type, turnState);
}
repaint();
}
private void turn() {
if (crash(x, y, type, (turnState + 1) % 4) == 1) {
turnState = (turnState + 1) % 4;
}
repaint();
}
// 讓一個(gè)塊堆積,其實(shí)是把當(dāng)前塊中的填充塊信息記錄到map[][]中
private void add(int x, int y, int type, int turnState) {
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (shapes[type][turnState][a * 4 + b] == 1) {
map[x + b + 1][y + a] = 1;
}
}
}
tryDelLine();
}
// 消塊
private void tryDelLine() {
// 從上往下,一行行依次遍歷,如果某一行的map[i][j]值全是1,則把這一行消掉---上一行往下落
for (int b = 0; b < 21; b++) {
int c = 1;
for (int a = 0; a < 12; a++) {
c &= map[a][b];
}
if (c == 1) {// 全是1--下落一行
score += 10;
for (int d = b; d > 0; d--) {
for (int e = 0; e < 11; e++) {
map[e][d] = map[e][d - 1];
}
}
// 更改游戲的難度(加快下落速度)
delay /= 2;
timer.setDelay(delay);
}
}
}
private int crash(int x, int y, int blockType, int turnState) {
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if ((shapes[blockType][turnState][a * 4 + b] & map[x + b + 1][y
+ a]) == 1) {// 和填充塊或框架重合,都算碰撞
return 0; // 碰撞了---方塊的填充塊和地圖中的填充塊完全重合
}
}
}
return 1;// 沒有碰撞
}
// 表現(xiàn)層
@Override
public void paint(Graphics g) {
super.paint(g);// 清除殘影
// 畫當(dāng)前塊
for (int j = 0; j < 16; j++) {
if (shapes[type][turnState][j] == 1) {
g.setColor(Color.green);
g.fillRect((j % 4 + x + 1) * 10, (j / 4 + y) * 10, 10, 10);
}
}
/*
* for(int a=0;a<4;a++){ for(int b=0;b<4;b++){
* if(shapes[type][turnState][a*4+b]==1){ g.fillRect((b+x+1)*10,
* (a+y)*10, 10, 10); } } }
*/
// 畫地圖(整個(gè)游戲的方塊區(qū)和邊框)
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 22; j++) {
if (map[i][j] == 1) {
g.setColor(Color.red);
g.fillRect(i * 10, j * 10, 10, 10);// 填充
g.setColor(Color.yellow);
g.drawRect(i * 10, j * 10, 10, 10);// 格線
} else if (map[i][j] == 3) {
g.setColor(Color.red);
g.drawRect(i * 10, j * 10, 10, 10);
}
}
}
// 顯示分?jǐn)?shù),同時(shí)為版面美觀,在界面上再加點(diǎn)東西
// 畫方塊區(qū)右側(cè)部分
g.setColor(Color.blue);
g.setFont(new Font("aa", Font.BOLD, 18));
g.drawString("score=" + score, 130, 20);
g.setFont(new Font("aa", Font.PLAIN, 13));
g.drawString("拒絕盜版游戲", 130, 70);
g.drawString("注意自我保護(hù)", 130, 90);
g.drawString("謹(jǐn)防受騙上當(dāng)。", 125, 110);
g.drawString("適度游戲益腦,", 125, 130);
g.drawString("沉迷游戲傷身。", 125, 150);
g.drawString("合理安排時(shí)間,", 125, 170);
g.drawString("享受健康生活。", 125, 190);
}
class TimerListener extends KeyAdapter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
down();
}
@Override
public void keyPressed(KeyEvent e) {
// System.out.println("aaaaa");
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
down();
break;
case KeyEvent.VK_LEFT:
left();
break;
case KeyEvent.VK_RIGHT:
right();
break;
case KeyEvent.VK_UP:
turn();
}
}
}
public Timer getTimer() {
return timer;
}
}上述就是小編為大家分享的利用Java編寫一個(gè)俄羅斯方塊小游戲了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞名稱:利用Java編寫一個(gè)俄羅斯方塊小游戲
當(dāng)前地址:http://www.chinadenli.net/article34/ishspe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站改版、微信公眾號(hào)、建站公司、品牌網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)