吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4901|回复: 2
收起左侧

[Java 转载] (新人第三弹)有关前面给予俄罗斯方块添加背景音乐的部分源码

[复制链接]
笨笨的蛋蛋 发表于 2014-11-13 21:28
哈哈,我终于凑够3个帖子了。。。。(这个音乐文件自己添加吧)
俄罗斯方块帖子:http://www.52pojie.cn/forum.php? ... p;page=1#pid6992033


import java.io.*;
import javax.media.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.*;

import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.imageio.ImageIO;

public class Tetris extends JPanel {
        private Cell[][] wall;
        private Tetromino tetromino;
        private Tetromino nextOne;
        private int lines;
        private int scores;
       
        private static  BufferedImage gameOver;
        private static  BufferedImage background;
        public static  BufferedImage T;
        public static  BufferedImage L;
        public static  BufferedImage J;
        public static  BufferedImage O;
        public static  BufferedImage S;
        public static  BufferedImage Z;
        public static  BufferedImage I;
       
       
        static {
                try {
                        gameOver = ImageIO.read(Tetris.class.getResource("game-over.png"));
                        background = ImageIO.read(Tetris.class.getResource("TETRIS.png"));
                        T = ImageIO.read(Tetris.class.getResource("T.png"));
                        L = ImageIO.read(Tetris.class.getResource("L.png"));
                        J = ImageIO.read(Tetris.class.getResource("J.png"));
                        O = ImageIO.read(Tetris.class.getResource("O.png"));
                        S = ImageIO.read(Tetris.class.getResource("S.png"));
                        Z = ImageIO.read(Tetris.class.getResource("Z.png"));
                        I = ImageIO.read(Tetris.class.getResource("I.png"));
                       
                }catch(Exception e) {
                        e.printStackTrace();
                }
        }
       
        public void paint(Graphics g) {
                g.drawImage(background,0,0,null);
                g.translate(15,15);
                paintWall(g);
                paintTetromino(g);
                paintNextOne(g);
                paintScore(g);
                if(gameover) {
                        g.drawImage(gameOver,0,0,null);
                }
                System.out.println(interval);
        }
       
        private static final int ROWS = 20;
        private static final int COLS = 10;
        private static final int CELL_SIZE=26;
       
        public void paintWall(Graphics g) {
                //wall = new Cell[ROWS][COLS];   被这句坑死了,方块落到下面直接就没了,,属于重新构造wall,而action方法中有wall的构造了。
                for(int row=0;row<wall.length;row ++) {
                        Cell[] lines = wall[row];
                        for(int col=0;col<lines.length;col ++) {
                                Cell cell = lines[col];
                                int x = col*CELL_SIZE;   //搞错了。搞成cell.getCol()了
                                int y = row*CELL_SIZE;
                                if(cell==null) {
                                        g.drawRect(x, y, CELL_SIZE, CELL_SIZE);
                                }else {
                                        g.drawImage(cell.getImage(),x-1,y-1,null);
                                }
                        }
                }
        }
       
        public void paintTetromino(Graphics g) {
                if(tetromino == null) {
                        return;
                }
                Cell[] cells = tetromino.cells;
                for(int i=0;i<cells.length;i++) {
                        Cell cell = cells;
                        int x = cell.getCol()*CELL_SIZE;
                        int y = cell.getRow()*CELL_SIZE;
                        g.drawImage(cell.getImage(),x-1,y-1,null);
                }
        }
       
        public void paintNextOne(Graphics g) {
                if(nextOne == null) {
                        return;
                }
                Cell[] cells = nextOne.cells;
                for(int i=0;i<cells.length;i++) {
                        Cell cell = cells;
                        int x = (cell.getCol()+10)*CELL_SIZE;
                        int y = (cell.getRow()+1)*CELL_SIZE;
                        g.drawImage(cell.getImage(),x-1,y-1,null);
                }
        }
       
        private final static int FONT_COLOR = 0x776699;
        private final static int FONT_SIZE = 30;
       
        public void paintScore(Graphics g) {
                int x = 290;
                int y = 160;
                g.setColor(new Color(FONT_COLOR));
                Font font = g.getFont();
                font  = new Font(font.getName(),font.getStyle(),FONT_SIZE);
                g.setFont(font);
                String str = "SCORE:" +scores;
                g.drawString(str, x, y);
                y += 56;
                str = "LINES:"+lines;
                g.drawString(str, x, y);
                y +=56;
                String str1 = "LV:"+level + " "+"SP:"+speed;
                if(pause) {
                        g.drawString("[C]Continues",x,y);
                } else {
                        //Font font1 = g.getFont();
                        //g.setFont(new Font(font.getName(),font.getStyle(),30));
                        g.drawString(str1, x, y);
                        //font = font1;
                }
                if(gameover) {
                       
                        g.drawString("[S]Start", x, y);
                }
               
               
        }
       
        Player player = null;
    URL url = null;
    File file = null;

    public Tetris() {
            file = new File("如果没有你.mp3");
             try {
                  url = file.toURI().toURL();
            player = Manager.createPlayer(url);
           // player.getDuration();
            //player.prefetch();
            //player.start();
        } catch (NoPlayerException e2) {
        }catch(MalformedURLException e1) {
        }catch (IOException e3) {}
               
                player.addControllerListener(new ControllerListener() {
                        public synchronized void controllerUpdate(ControllerEvent event) {
            if(event instanceof EndOfMediaEvent) {
                    player.setMediaTime(new Time(0));
                    player.start();
                    return;
            }
           
            if(event instanceof PrefetchCompleteEvent) {
                    player.start();
                    return;
            }
    }
                });
                player.prefetch();
    }



       
        public static void main(String[] args) {
                JFrame frame = new JFrame();
                Tetris tetris = new Tetris();
                frame.setSize(530,580);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.add(tetris);
                tetris.action();
        }
       

        public void moveLeftAction() {
                tetromino.moveLeft();
                if(outOfBounds()||coincide()) {
                        tetromino.moveRight();
                }
        }
       
        public void moveRightAction() {
                tetromino.moveRight();
                if(outOfBounds()||coincide()) {
                        tetromino.moveLeft();
                }
        }
       
        private boolean outOfBounds() {
                Cell[] cells = tetromino.cells;
                for(int i=0;i<cells.length;i++) {
                        Cell cell = cells;
                        int col = cell.getCol();
                        int row = cell.getRow();
                        if(col<0||col>=COLS||row>=ROWS) {
                                return true;
                        }
                }
                return false;
        }
       
        private boolean coincide() {
                Cell[] cells = tetromino.cells;
                for(int i=0;i<cells.length;i++) {
                        Cell cell = cells;
                        int col = cell.getCol();
                        int row = cell.getRow();
                        if(col>=0&&col<=COLS&&row>=0&&row<ROWS&&wall[row][col]!=null) {
                                return true;
                        }
                }
                return false;
        }
       
        private boolean canDrop() {
                Cell[] cells = tetromino.cells;
                for(int i=0;i<cells.length;i++) {
                        Cell cell = cells;
                        int row = cell.getRow();
                        if(row==(ROWS-1)) {
                                return false;
                        }
                }
               
                for(Cell cell:cells) {
                        int row = cell.getRow() +1;
                        int col = cell.getCol();
                        if(row>0&&row<ROWS&&col>=0&&col<COLS&&wall[row][col]!=null) {
                                return false;
                        }
                }
                return true;
        }
       
        private void landIntoWall() {
                Cell[] cells = tetromino.cells;
                for(Cell cell:cells) {
                        int row = cell.getRow();
                        int col = cell.getCol();
                        wall[row][col] = cell;
                }
        }
       
        private boolean fullCells(int row) {
                Cell[] line = wall[row];
                for(Cell cell:line) {
                        if(cell==null)
                                return false;
                }
                return true;
        }
       
        private void deleteRow(int row) {
                for(int i=row;i>=1;i--) {
                        System.arraycopy(wall[i-1], 0, wall, 0, COLS); //这地方把i写成row了,后面的
                }
                Arrays.fill(wall[0], null);
        }
        int[] scoreState = {0,1,5,10,20};
        private void destoryLines() {
                int lines = 0;
                for(int row=0;row<wall.length;row ++) {
                        if(fullCells(row)) {
                                deleteRow(row);
                                lines ++;
                        }
                }
                this.lines  += lines;
                this.scores += scoreState[lines];
                //等级调速
                if(this.lines==5) {
                        level =levels[1];
                        interval = 500;
                        speed = 2;
                        timer.cancel();
                        continueAction();
                }else if(this.lines ==10) {
                        level = levels[2];
                        interval = 450;
                        speed = 3;
                        timer.cancel();
                        continueAction();
                }else if(this.lines==15) {
                        level = levels[3];
                        interval = 380;
                        speed =4;
                        timer.cancel();
                        continueAction();
                }else if(this.lines==20) {
                        level = levels[4];
                        interval = 300;
                        speed =5;
                        timer.cancel();
                        continueAction();
                }else if(this.lines ==25) {
                        level = levels[5];
                        interval = 220;
                        speed = 6;
                        timer.cancel();
                        continueAction();
                }else if(this.lines==30) {
                        level = levels[6];
                        interval = 150;
                        speed = 7;
                        timer.cancel();
                        continueAction();
                }else if(this.lines ==40) {
                        level ="超神";
                        interval = 80;
                        speed = 8;
                        timer.cancel();
                        continueAction();
                }
        }
       
        public void softDropAction() {
                if(canDrop()) {
                        tetromino.softDrop();
                }else {
                        landIntoWall();
                        destoryLines();
                        checkGameOver();
                        tetromino = nextOne;
                        nextOne = Tetromino.randomOne();
                       
                }
        }
       
        public void hardDropAction() {
                while(canDrop()) {
                        tetromino.softDrop();
                }
                landIntoWall();
                destoryLines();
                checkGameOver();
                tetromino = nextOne;
                nextOne = Tetromino.randomOne();
        }
       
        public void rotateRightAction() {
                tetromino.rotateRight();
                if(outOfBounds()||coincide()) {
                        tetromino.rotateLeft();
                }
        }
       
        private boolean gameover;
        private boolean pause;
        private Timer timer;
        private TimerTask timerTask;
        private long interval=600;
       
        public void startAction() {
                scores = 0;
                lines = 0;
                gameover = false;
                pause = false;
                clearWall();
               
                timer = new Timer();
                timerTask = new TimerTask() {
                        public void run() {
                                softDropAction();
                                repaint();
                        }
                };
                timer.schedule(timerTask,1000,interval);
        }
       
        public void pauseAction() {
                pause = true;
                timer.cancel();
        }
       
        public void continueAction() {
                timer = new Timer();
               
                timer.schedule(new TimerTask() {
                        public void run() {
                                softDropAction();
                                repaint();
                               
                        }
                },100,interval);
                pause = false;
        }
       
        public void checkGameOver() {
                if(wall[0][4]!=null) {
                        timer.cancel();
                        gameover = true;
                }
        }
       
        public void clearWall() {
                for(Cell[] lines:wall) {
                        Arrays.fill(lines, null);
                }
        }
       
       
        String[] levels = {"初级","中级","高级","皇级","帝级","圣级","神级"};
        private String level = levels[0];
        private int speed = 1;
       
        public void action() {
                wall = new Cell[ROWS][COLS];
                tetromino = Tetromino.randomOne();
                nextOne = Tetromino.randomOne();
                startAction();
                KeyAdapter k = new KeyAdapter() {
                        public void keyPressed(KeyEvent e) {
                                int key = e.getKeyCode();
                                if(key==KeyEvent.VK_Q) {
                                        System.exit(0);
                                }
                                if(pause) {
                                        if(key==KeyEvent.VK_C){
                                                continueAction();
                                        }
                                        return;
                                }
                                if(gameover) {
                                        if(key==KeyEvent.VK_S){
                                                startAction();
                                        }
                                        return;
                                }
                                switch(key) {
                                case KeyEvent.VK_LEFT:
                                        moveLeftAction();
                                        break;
                                case KeyEvent.VK_RIGHT:
                                        moveRightAction();
                                        break;
                                case KeyEvent.VK_DOWN:
                                        softDropAction();
                                        break;
                                case KeyEvent.VK_UP:
                                        rotateRightAction();
                                        break;
                                case KeyEvent.VK_SPACE:
                                        hardDropAction();
                                        break;
                                case KeyEvent.VK_P:
                                        pauseAction();
                                        break;
                                }
                                repaint();
                        }
                };
                this.requestFocus();
                this.addKeyListener(k);
               
        }
       
}


发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 笨笨的蛋蛋 发表于 2014-11-13 21:33
我记得我自己玩不到皇级。。。
触类旁通CDC 发表于 2014-11-13 21:34
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 07:00

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表