有人说过
写代码不但要勤,要沉迷进去,更重要的是要有感动,有感动,就能写出精彩动人的代码。写代码要融入感情,它才生动鲜活起来,才能入神!不然再怎么写,都不能到最高境界,充其量也不过是个机器。这是玄妙而又实实在在的存在。
这个人说的好。。写的代码也帮。。向他学习
- 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;
-
- 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(220, 275);
- frame.setTitle("Tetris内测版");
- // frame.setUndecorated(true);
- frame.setVisible(true);
- frame.setResizable(false);
-
- }
- }
-
- // 创建一个俄罗斯方块类
- class Tetrisblok extends JPanel implements KeyListener {
-
- // blockType 代表方块类型
- // turnState代表方块状态
- private int blockType;
- private int score = 0;
-
- 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];
-
- // 方块的形状 第一组代表方块类型有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, 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 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 } } };
-
- // 生成新方块的方法
- public void newblock() {
- blockType = (int) (Math.random() * 1000) % 7;
- turnState = (int) (Math.random() * 1000) % 4;
- x = 4;
- y = 0;
- if (gameover(x, y) == 1) {
-
- newmap();
- drawwall();
- score = 0;
- JOptionPane.showMessageDialog(null, "GAME OVER");
- }
- }
-
- // 画围墙
- 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();
- 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();
- }
-
-
复制代码 |
|