(新人第三弹)有关前面给予俄罗斯方块添加背景音乐的部分源码
哈哈,我终于凑够3个帖子了。。。。(这个音乐文件自己添加吧)俄罗斯方块帖子:http://www.52pojie.cn/forum.php?mod=viewthread&tid=304433&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 staticBufferedImage gameOver;
private staticBufferedImage background;
public staticBufferedImage T;
public staticBufferedImage L;
public staticBufferedImage J;
public staticBufferedImage O;
public staticBufferedImage S;
public staticBufferedImage Z;
public staticBufferedImage 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; 被这句坑死了,方块落到下面直接就没了,,属于重新构造wall,而action方法中有wall的构造了。
for(int row=0;row<wall.length;row ++) {
Cell[] lines = wall;
for(int col=0;col<lines.length;col ++) {
Cell cell = lines;
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("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("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!=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!=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 = cell;
}
}
private boolean fullCells(int row) {
Cell[] line = wall;
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, 0, wall, 0, COLS); //这地方把i写成row了,后面的
}
Arrays.fill(wall, 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;
//等级调速
if(this.lines==5) {
level =levels;
interval = 500;
speed = 2;
timer.cancel();
continueAction();
}else if(this.lines ==10) {
level = levels;
interval = 450;
speed = 3;
timer.cancel();
continueAction();
}else if(this.lines==15) {
level = levels;
interval = 380;
speed =4;
timer.cancel();
continueAction();
}else if(this.lines==20) {
level = levels;
interval = 300;
speed =5;
timer.cancel();
continueAction();
}else if(this.lines ==25) {
level = levels;
interval = 220;
speed = 6;
timer.cancel();
continueAction();
}else if(this.lines==30) {
level = levels;
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!=null) {
timer.cancel();
gameover = true;
}
}
public void clearWall() {
for(Cell[] lines:wall) {
Arrays.fill(lines, null);
}
}
String[] levels = {"初级","中级","高级","皇级","帝级","圣级","神级"};
private String level = levels;
private int speed = 1;
public void action() {
wall = new Cell;
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);
}
}
我记得我自己玩不到皇级。。。{:1_908:} 前排拿下,顶
页:
[1]