俄罗斯方块 java程序源代码在eclipse能运行 925187692@qq.com 能发一下吗
2个回答
展开全部
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Tetris extends JFrame {
public Tetris() {
Tetrisblok a = new Tetrisblok();
addKeyListener(a);
add(a);
}
public static void main(String[] args) {
Tetris frame = new Tetris();
JMenuBar menu = new JMenuBar();
frame.setJMenuBar(menu);
JMenu game = new JMenu("游戏");
JMenuItem newgame = game.add("新游戏");
JMenuItem pause = game.add("暂停");
JMenuItem goon = game.add("继续");
JMenuItem exit = game.add("退出");
JMenu help = new JMenu("帮助");
JMenuItem about = help.add("关于");
menu.add(game);
menu.add(help);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(310, 360);
frame.setLocation(300, 200);
frame.setTitle("Tetris内测版0.1");
// frame.setUndecorated(true);
frame.setVisible(true);
frame.setResizable(false);
}
}
// 创建一个俄罗斯方块类
class Tetrisblok extends JPanel implements KeyListener {
// blockType 代表方块类型
// turnState代表方块状态
private int score = 0, score1 = 0, max = 0;
private int blockType, turnState;
private int bt, ts;
private int x, y;
private int i = 0;
int j = 0;
int flag = 0;
// 定义已经放下的方块x=0-11,y=0-21;
int[][] map = new int[13][23];
// 方块的形状 第一组代表方块类型有S、Z、L、J、I、O、T 7种 第二组 代表旋转几次 第三四组为 方块矩阵
private final int shapes[][][] = new int[][][] {
// i
{ { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 } },
// s
{ { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,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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 } } };
// 生成新方块的方法
public void newblock() {
bt = (int) (Math.random() * 1000) % 7;
ts = (int) (Math.random() * 1000) % 4;
x = 4;
y = 0;
}
//传递方块
public void diliver() {
blockType = bt;
turnState = ts;
}
// 画围墙
public void drawwall() {
for (i = 0; i < 12; i++) {
map[i][21] = 2;
}
for (j = 0; j < 22; j++) {
map[11][j] = 2;
map[0][j] = 2;
}
}
// 初始化地图
public void newmap() {
for (i = 0; i < 12; i++) {
for (j = 0; j < 22; j++) {
map[i][j] = 0;
}
}
}
// 初始化构造方法
Tetrisblok() {
newblock();
diliver();
newblock();
newmap();
drawwall();
Timer timer = new Timer(1000, new TimerListener());
timer.start();
}
// 旋转的方法
public void turn() {
int tempturnState = turnState;
turnState = (turnState + 1) % 4;
if (blow(x, y, blockType, turnState) == 1) {
}
if (blow(x, y, blockType, turnState) == 0) {
turnState = tempturnState;
}
repaint();
}
// 左移的方法
public void left() {
if (blow(x - 1, y, blockType, turnState) == 1) {
x = x - 1;
}
repaint();
}
// 右移的方法
public void right() {
if (blow(x + 1, y, blockType, turnState) == 1) {
x = x + 1;
}
repaint();
}
// 下落的方法
public void down() {
if (blow(x, y + 1, blockType, turnState) == 1) {
//y++;
while (true) {
y++;
/*
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
if (blow(x, y + 1, blockType, turnState) == 0) {
break;
}
}
//delline();
}
if (blow(x, y + 1, blockType, turnState) == 0) {
add(x, y, blockType, turnState);
delline();
//newblock();
}
repaint();
}
// 是否合法的方法
public int blow(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] == 1) && (map[x
+ b + 1][y + a] == 1))
|| ((shapes[blockType][turnState][a * 4 + b] == 1) && (map[x
+ b + 1][y + a] == 2))) {
return 0; // 不合法
}
}
}
return 1; // 合法
}
// 消行的方法
public void delline() {
int c = 0;
int time = 0;
for (int b = 0; b < 22; b++) {
for (int a = 0; a < 12; a++) {
if (map[a][b] == 1) {
c = c + 1;
if (c == 10) {
repaint();
/*
try {
Thread.sleep(1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
*/
time++;
for (int d = b; d > 0; d--) {
for (int e = 0; e < 11; e++) {
map[e][d] = map[e][d - 1];
}
}
}
}
}
c = 0;
}
switch (time) {
case 1:
score += 10;
break;
case 2:
score += 30;
break;
case 3:
score += 60;
break;
case 4:
score += 100;
break;
default:
score += 0;
}
}
// 判断你挂的方法
public int gameover(int x, int y) {
if (blow(x, y, blockType, turnState) == 0) {
score1 = score;
if (max < score) {
max = score;
}
return 1;
}
return 0;
}
// 把当前添加map
public void add(int x, int y, int blockType, int turnState) {
diliver();
newblock();
int j = 0;
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (map[x + b + 1][y + a] == 0) {
map[x + b + 1][y + a] = shapes[blockType][turnState][j];
}
j++;
}
}
}
// 画方块的的方法
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 画当前方块
for (j = 0; j < 16; j++) {
if (shapes[blockType][turnState][j] == 1) {
g.fill3DRect((j % 4 + x + 1) * 14, (j / 4 + y) * 14, 14, 14, true);
//flag = 1;
}
if (shapes[bt][ts][j] == 1) {
//g.fill3DRect((j % 4 + x + 1) * 14, (j / 4 + y) * 14, 14, 14, true);
g.fill3DRect((j%4+14)*14, (j/4+1)*14, 14, 14, true);
}
}
// 画已经固定的方块
for (j = 0; j < 22; j++) {
for (i = 0; i < 12; i++) {
if (map[i][j] == 1) {
g.fillRect(i * 14, j * 14, 14, 14);
}
if (map[i][j] == 2) {
g.setColor(new Color(220, 100, 50));
g.drawRect(i * 14, j * 14, 14, 14);
//g.setColor(c);
} else {
Color c = g.getColor();
g.setColor(new Color(150, 150, 150));
g.drawRect(i*14, j*14, 14, 14);
g.setColor(c);
}
}
}
g.setColor(Color.black);
g.drawString("score=" + score, 200, 80);
g.drawString("最高分:" + max, 200, 100);
g.drawString("上次得分:" + score1, 200, 120);
g.drawString("抵制不良游戏,", 200, 150);
g.drawString("拒绝盗版游戏。", 200, 170);
g.drawString("注意自我保护,", 200, 190);
g.drawString("谨防受骗上当。", 200, 210);
g.drawString("适度游戏益脑,", 200, 230);
g.drawString("沉迷游戏伤身。", 200, 250);
g.drawString("合理安排时间,", 200, 270);
g.drawString("享受健康生活。", 200, 290);
}
// 键盘监听
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
down();
break;
case KeyEvent.VK_UP:
turn();
break;
case KeyEvent.VK_RIGHT:
right();
break;
case KeyEvent.VK_LEFT:
left();
break;
}
}
// 无用
public void keyReleased(KeyEvent e) {
}
// 无用
public void keyTyped(KeyEvent e) {
}
// 定时器监听
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (blow(x, y + 1, blockType, turnState) == 1) {
y = y + 1;
//delline();
}
if (blow(x, y + 1, blockType, turnState) == 0) {
add(x, y, blockType, turnState);
delline();
//newblock();
}
if (gameover(x, y) == 1) {
newmap();
drawwall();
score = 0;
JOptionPane.showMessageDialog(null, "GAME OVER");
}
repaint();
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Tetris extends JFrame {
public Tetris() {
Tetrisblok a = new Tetrisblok();
addKeyListener(a);
add(a);
}
public static void main(String[] args) {
Tetris frame = new Tetris();
JMenuBar menu = new JMenuBar();
frame.setJMenuBar(menu);
JMenu game = new JMenu("游戏");
JMenuItem newgame = game.add("新游戏");
JMenuItem pause = game.add("暂停");
JMenuItem goon = game.add("继续");
JMenuItem exit = game.add("退出");
JMenu help = new JMenu("帮助");
JMenuItem about = help.add("关于");
menu.add(game);
menu.add(help);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(310, 360);
frame.setLocation(300, 200);
frame.setTitle("Tetris内测版0.1");
// frame.setUndecorated(true);
frame.setVisible(true);
frame.setResizable(false);
}
}
// 创建一个俄罗斯方块类
class Tetrisblok extends JPanel implements KeyListener {
// blockType 代表方块类型
// turnState代表方块状态
private int score = 0, score1 = 0, max = 0;
private int blockType, turnState;
private int bt, ts;
private int x, y;
private int i = 0;
int j = 0;
int flag = 0;
// 定义已经放下的方块x=0-11,y=0-21;
int[][] map = new int[13][23];
// 方块的形状 第一组代表方块类型有S、Z、L、J、I、O、T 7种 第二组 代表旋转几次 第三四组为 方块矩阵
private final int shapes[][][] = new int[][][] {
// i
{ { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 } },
// s
{ { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 } },
// z
{ { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1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;
}
//传递方块
public void diliver() {
blockType = bt;
turnState = ts;
}
// 画围墙
public void drawwall() {
for (i = 0; i < 12; i++) {
map[i][21] = 2;
}
for (j = 0; j < 22; j++) {
map[11][j] = 2;
map[0][j] = 2;
}
}
// 初始化地图
public void newmap() {
for (i = 0; i < 12; i++) {
for (j = 0; j < 22; j++) {
map[i][j] = 0;
}
}
}
// 初始化构造方法
Tetrisblok() {
newblock();
diliver();
newblock();
newmap();
drawwall();
Timer timer = new Timer(1000, new TimerListener());
timer.start();
}
// 旋转的方法
public void turn() {
int tempturnState = turnState;
turnState = (turnState + 1) % 4;
if (blow(x, y, blockType, turnState) == 1) {
}
if (blow(x, y, blockType, turnState) == 0) {
turnState = tempturnState;
}
repaint();
}
// 左移的方法
public void left() {
if (blow(x - 1, y, blockType, turnState) == 1) {
x = x - 1;
}
repaint();
}
// 右移的方法
public void right() {
if (blow(x + 1, y, blockType, turnState) == 1) {
x = x + 1;
}
repaint();
}
// 下落的方法
public void down() {
if (blow(x, y + 1, blockType, turnState) == 1) {
//y++;
while (true) {
y++;
/*
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
if (blow(x, y + 1, blockType, turnState) == 0) {
break;
}
}
//delline();
}
if (blow(x, y + 1, blockType, turnState) == 0) {
add(x, y, blockType, turnState);
delline();
//newblock();
}
repaint();
}
// 是否合法的方法
public int blow(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] == 1) && (map[x
+ b + 1][y + a] == 1))
|| ((shapes[blockType][turnState][a * 4 + b] == 1) && (map[x
+ b + 1][y + a] == 2))) {
return 0; // 不合法
}
}
}
return 1; // 合法
}
// 消行的方法
public void delline() {
int c = 0;
int time = 0;
for (int b = 0; b < 22; b++) {
for (int a = 0; a < 12; a++) {
if (map[a][b] == 1) {
c = c + 1;
if (c == 10) {
repaint();
/*
try {
Thread.sleep(1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
*/
time++;
for (int d = b; d > 0; d--) {
for (int e = 0; e < 11; e++) {
map[e][d] = map[e][d - 1];
}
}
}
}
}
c = 0;
}
switch (time) {
case 1:
score += 10;
break;
case 2:
score += 30;
break;
case 3:
score += 60;
break;
case 4:
score += 100;
break;
default:
score += 0;
}
}
// 判断你挂的方法
public int gameover(int x, int y) {
if (blow(x, y, blockType, turnState) == 0) {
score1 = score;
if (max < score) {
max = score;
}
return 1;
}
return 0;
}
// 把当前添加map
public void add(int x, int y, int blockType, int turnState) {
diliver();
newblock();
int j = 0;
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (map[x + b + 1][y + a] == 0) {
map[x + b + 1][y + a] = shapes[blockType][turnState][j];
}
j++;
}
}
}
// 画方块的的方法
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 画当前方块
for (j = 0; j < 16; j++) {
if (shapes[blockType][turnState][j] == 1) {
g.fill3DRect((j % 4 + x + 1) * 14, (j / 4 + y) * 14, 14, 14, true);
//flag = 1;
}
if (shapes[bt][ts][j] == 1) {
//g.fill3DRect((j % 4 + x + 1) * 14, (j / 4 + y) * 14, 14, 14, true);
g.fill3DRect((j%4+14)*14, (j/4+1)*14, 14, 14, true);
}
}
// 画已经固定的方块
for (j = 0; j < 22; j++) {
for (i = 0; i < 12; i++) {
if (map[i][j] == 1) {
g.fillRect(i * 14, j * 14, 14, 14);
}
if (map[i][j] == 2) {
g.setColor(new Color(220, 100, 50));
g.drawRect(i * 14, j * 14, 14, 14);
//g.setColor(c);
} else {
Color c = g.getColor();
g.setColor(new Color(150, 150, 150));
g.drawRect(i*14, j*14, 14, 14);
g.setColor(c);
}
}
}
g.setColor(Color.black);
g.drawString("score=" + score, 200, 80);
g.drawString("最高分:" + max, 200, 100);
g.drawString("上次得分:" + score1, 200, 120);
g.drawString("抵制不良游戏,", 200, 150);
g.drawString("拒绝盗版游戏。", 200, 170);
g.drawString("注意自我保护,", 200, 190);
g.drawString("谨防受骗上当。", 200, 210);
g.drawString("适度游戏益脑,", 200, 230);
g.drawString("沉迷游戏伤身。", 200, 250);
g.drawString("合理安排时间,", 200, 270);
g.drawString("享受健康生活。", 200, 290);
}
// 键盘监听
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
down();
break;
case KeyEvent.VK_UP:
turn();
break;
case KeyEvent.VK_RIGHT:
right();
break;
case KeyEvent.VK_LEFT:
left();
break;
}
}
// 无用
public void keyReleased(KeyEvent e) {
}
// 无用
public void keyTyped(KeyEvent e) {
}
// 定时器监听
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (blow(x, y + 1, blockType, turnState) == 1) {
y = y + 1;
//delline();
}
if (blow(x, y + 1, blockType, turnState) == 0) {
add(x, y, blockType, turnState);
delline();
//newblock();
}
if (gameover(x, y) == 1) {
newmap();
drawwall();
score = 0;
JOptionPane.showMessageDialog(null, "GAME OVER");
}
repaint();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询