3一个JAVA编写的俄罗斯方块游戏代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
} }
// 创建一个俄罗斯方块类 cla来自百度文库s Tetrisblok extends JPanel implements KeyListener {
// blockType 代表方块类型 // turnState 代表方块状态 private int blockType; private int score = 0;
} 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; }
c = c + 1; if (c == 10) {
score += 10; for (int d = b; d > 0; d--) {
for (int e = 0; e < 11; e++) { map[e][d] = map[e][d - 1];
} } } } } c = 0; } }
// 判断你挂的方法 public int gameover(int x, int y) {
newmap(); drawwall(); score = 0; JOptionPane.showMessageDialog(null, "GAME OVER"); } }
// 画围墙 public void drawwall() {
for (i = 0; i < 12; i++) { map[i][21] = 2;
private int turnState;
private int x;
private int y;
private int i = 0;
int j = 0; int flag = 0; // 定义已经放下的方块 x=0-11,y=0-21; int[][] map = new int[13][23];
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("关于");
} ; if (blow(x, y + 1, blockType, turnState) == 0) {
add(x, y, blockType, turnState); newblock(); delline(); } ; repaint(); }
// 是否合法的方法 public int blow(int x, int y, int blockType, int turnState) {
return 0; } } }
return 1; }
// 消行的方法 public void delline() {
int c = 0; for (int b = 0; b < 22; b++) {
for (int a = 0; a < 12; a++) { if (map[a][b] == 1) {
如图
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.*; import javax.swing.Timer;
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))) {
if (blow(x + 1, y, blockType, turnState) == 1) { x = x + 1;
} ; repaint(); }
// 下落的方法 public void down() {
if (blow(x, y + 1, blockType, turnState) == 1) { y = y + 1; delline();
// 生成新方块的方法 public void newblock() {
blockType = (int) (Math.random() * 1000) % 7; turnState = (int) (Math.random() * 1000) % 4; x = 4; y = 0; if (gameover(x, y) == 1) {
} }
// 初始化构造方法 Tetrisblok() {
newblock(); newmap(); drawwall(); Timer timer = new Timer(1000, new TimerListener()); timer.start(); }
// 旋转的方法 public void turn() {
// 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, 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 } }, // j { { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 1, 0, 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 } } };
menu.add(game); menu.add(help); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(220, 275); frame.setTitle("Tetris 内测版"); // frame.setUndecorated(true); frame.setVisible(true); frame.setResizable(false);
// 方块的形状 第一组代表方块类型有 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 } },
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++;
if (blow(x, y, blockType, turnState) == 0) { return 1;
} return 0; }
// 把当前添加 map public void add(int x, int y, int blockType, int turnState) {
int j = 0; for (int a = 0; a < 4; a++) {
turnState = tempturnState; } repaint(); }
// 左移的方法 public void left() {
if (blow(x - 1, y, blockType, turnState) == 1) {
x = x - 1; } ; repaint(); }
// 右移的方法 public void right() {
int tempturnState = turnState; turnState = (turnState + 1) % 4; if (blow(x, y, blockType, turnState) == 1) { } if (blow(x, y, blockType, turnState) == 0) {
相关文档
最新文档