Смекни!
smekni.com

Java-гра Тетріс (стр. 3 из 3)

s[i][j] = stakan.getCell(y+i,j+x-1);

}

for (int i=0; i<s.length; i++) for (int j=0; j<s[0].length; j++) {

if (s[i][j]*pat[i][j]>0) {

return false;

}

}

return true;

}

public boolean canRight(Glass stakan) {

if (x==stakan.getWidth()-pat[0].length) return false;

int [][] s = new int[pat.length][pat[0].length];

for (int i=0; i<s.length; i++) for (int j=0; j<s[0].length; j++) {

s[i][j] = stakan.getCell(y+i,j+x+1);

}

for (int i=0; i<s.length; i++) for (int j=0; j<s[0].length; j++) {

if (s[i][j]*pat[i][j]>0) {

return false;

}

}

return true;

}

public boolean canRotate(Glass stakan) {

if (x+pat.length>stakan.getWidth()) return false;

int[][] tmpPat = new int[pat[0].length][pat.length];

for (int i=0; i<pat.length; i++) for (int j=0; j<pat[0].length; j++) {

tmpPat[j][i] = pat[i][j];

}

int[][] tPat = new int[tmpPat.length][tmpPat[0].length];

for (int i=0; i<tPat.length; i++) for (int j=0; j<tPat[0].length; j++) {

tPat[i][j] = tmpPat[tPat.length-i-1][j];

}

int [][] s = new int[tPat.length][tPat[0].length];

for (int i=0; i<s.length; i++) for (int j=0; j<s[0].length; j++) {

s[i][j] = stakan.getCell(y+i,j+x);

}

for (int i=0; i<s.length; i++) for (int j=0; j<s[0].length; j++) {

if (s[i][j]*tPat[i][j]>0) {

return false;

}

}

return true;

}

public void moveLeft() {

if (x>0) {

x--;

}

}

public void moveRight() {

if (x<10-pat[0].length) {

x++;

}

}

}

Файл Glass.java

package Tetr;

import java.awt.*;

/**

*

* @author irdis_13

*/

public class Glass {

private int[] x = {0, 10, 10, 310, 310, 320, 320, 0};

private int[] y = {0, 0, 600, 600, 0, 0, 610, 610};

private int[][] cells = new int[21][10];

public Glass() {

clearGlass();

}

public void clearGlass() {

for (int i = 0; i<cells.length; i++) {

for (int j = 0; j<cells[i].length; j++) {

cells[i][j] = (i==cells.length-1) ? 10 : 0;

}

}

}

public int getHeight() {

return cells.length;

}

public int getWidth() {

return cells[0].length;

}

public int getCell(int i, int j) {

return cells[i][j];

}

public void draw(Graphics g) {

g.setColor(Color.GRAY);

g.fillRect(10,0,300,600);

g.setColor(Color.BLUE);

g.fillPolygon(x,y,x.length);

for (int i = 0; i<cells.length-1; i++) {

for (int j = 0; j<cells[i].length; j++) {

drawCell(g,i,j);

}

}

}

public void drawCell(Graphics g, int i, int j) {

g.setColor(Figure.COLORS[cells[i][j]]);

g.fillRect(j*30+11,i*30+1,28,28);

}

public int acceptFigure(Figure f){

for (int i=0; i<f.getHeight(); i++) {

for (int j=0; j<f.getWidth(); j++) {

int xx = f.getX()+j, yy = f.getY()+i;

if (f.getCell(i,j)!=0) { // клетка не пуста

cells[yy][xx] = f.getCell(i,j);

} }

}

int lines = clearFullLines();

if (lines>0) return lines;

if (f.getY()==0) return -1;

return 0;

}

private int clearFullLines() {

int linesCount = 0;

lineLoop: for (int i=1; i<20; i++){

for (int j=0; j<10; j++) {

if (cells[i][j]==0) {

continue lineLoop;

}

}

linesCount++;

for (int j=i; j>=1; j--) {

cells[j]=cells[j-1];

}

cells[0] = new int[10];

for (int j=0; j<10; j++) {

cells[0][j]=0;

}

}

return linesCount;

}

}

Файл PlaySound.java

package Tetr;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.sound.midi.*;

import java.io.*;

/**

*

* @author irdis_13

*/

public class PlaySound implements ActionListener{

public PlaySound(String s){

play(s);

}

protected void play(String file) {

try{

File f = new File(file);

Sequencer sequencer = MidiSystem.getSequencer();

if (sequencer == null) {

System.err.println("Sequenser not supported");

}

sequencer.open();

Sequence seq = MidiSystem.getSequence(f);

sequencer.setSequence(seq);

sequencer.start();

sequencer.setLoopCount(1000);

}catch(Exception e){

System.err.println(e);

}

}

public void actionPerformed(ActionEvent e) {

throw new UnsupportedOperationException("Not supported yet.");

}

}

Файл TetrisFrame.java

package Tetr;

import Tetr.TetrisGamePanel;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

/**

*

* @author irdis_13

*/

public class TetrisFrame extends javax.swing.JFrame {

/** Creates new form TetrisFrame */

public TetrisFrame() {

initComponents();

tetrisGamePanel1.grabFocus();

}

// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents

private void initComponents() {

buttonPanel1 = buttonPanel1 = new ButtonPanel(this);

menuBar = new javax.swing.JMenuBar();

jMenu1 = new javax.swing.JMenu();

menuNewGame = new javax.swing.JMenuItem();

menuPauseGame = new javax.swing.JMenuItem();

jSeparator1 = new javax.swing.JSeparator();

menuFileExit = new javax.swing.JMenuItem();

tetrisGamePanel1 = new Tetr.TetrisGamePanel();

menuBar1 = new javax.swing.JMenuBar();

jMenu2 = new javax.swing.JMenu();

menuNewGame1 = new javax.swing.JMenuItem();

menuPauseGame1 = new javax.swing.JMenuItem();

jSeparator2 = new javax.swing.JSeparator();

menuFileExit1 = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setFocusable(false);

jMenu1.setText("File");

menuNewGame.setText("Item");

jMenu1.add(menuNewGame);

menuPauseGame.setText("Item");

jMenu1.add(menuPauseGame);

jMenu1.add(jSeparator1);

menuFileExit.setText("Item");

jMenu1.add(menuFileExit);

menuBar.add(jMenu1);

tetrisGamePanel1.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(java.awt.event.KeyEvent evt) {

tetrisGamePanel1KeyPressed(evt);

}

});

menuBar1.setFocusable(false);

jMenu2.setText("File");

jMenu2.setFocusable(false);

menuNewGame1.setText("Start New Game");

menuNewGame1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

menuNewGame1ActionPerformed(evt);

}

});

jMenu2.add(menuNewGame1);

menuPauseGame1.setText("Stop Game");

menuPauseGame1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

menuPauseGame1ActionPerformed(evt);

}

});

jMenu2.add(menuPauseGame1);

jMenu2.add(jSeparator2);

menuFileExit1.setText("Exit");

menuFileExit1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

menuFileExit1ActionPerformed(evt);

}

});

jMenu2.add(menuFileExit1);

menuBar1.add(jMenu2);

setJMenuBar(menuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(153, 153, 153)

.addComponent(buttonPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(tetrisGamePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 485, javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(buttonPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(tetrisGamePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 612, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

pack();

}// </editor-fold>//GEN-END:initComponents

private void tetrisGamePanel1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tetrisGamePanel1KeyPressed

TetrisGamePanel p = (TetrisGamePanel)tetrisGamePanel1;

switch (evt.getKeyCode()) {

case KeyEvent.VK_LEFT:

p.figureMoveLeft();

break;

case KeyEvent.VK_RIGHT:

p.figureMoveRight();

break;

case KeyEvent.VK_UP:

p.figureRotate();

break;

case KeyEvent.VK_DOWN:

p.figureMoveDown();

break;

case KeyEvent.VK_F2:

p.startNewGame();

break;

case KeyEvent.VK_F3:

p.gamePauseResume();

break;

default: return;

}

repaint();

}//GEN-LAST:event_tetrisGamePanel1KeyPressed

private void menuNewGame1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuNewGame1ActionPerformed

TetrisGamePanel p = (TetrisGamePanel)tetrisGamePanel1;

p.startNewGame();

}//GEN-LAST:event_menuNewGame1ActionPerformed

private void menuPauseGame1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuPauseGame1ActionPerformed

TetrisGamePanel p = (TetrisGamePanel)tetrisGamePanel1;

p.stopGame();

}//GEN-LAST:event_menuPauseGame1ActionPerformed

private void menuFileExit1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFileExit1ActionPerformed

System.exit(0);

}//GEN-LAST:event_menuFileExit1ActionPerformed

/**

* @param args the command line arguments

*/

public void continueButtonPressed(ActionEvent evt) {

TetrisGamePanel p = (TetrisGamePanel)tetrisGamePanel1;

p.resumeGame();

}

public void pauseButtonPressed(ActionEvent evt) {

TetrisGamePanel p = (TetrisGamePanel)tetrisGamePanel1;

p.pauseGame();

}

// Variables declaration - do not modify//GEN-BEGIN:variables

private Tetr.ButtonPanel buttonPanel1;

private javax.swing.JMenu jMenu1;

private javax.swing.JMenu jMenu2;

private javax.swing.JSeparator jSeparator1;

private javax.swing.JSeparator jSeparator2;

private javax.swing.JMenuBar menuBar;

private javax.swing.JMenuBar menuBar1;

private javax.swing.JMenuItem menuFileExit;

private javax.swing.JMenuItem menuFileExit1;

private javax.swing.JMenuItem menuNewGame;

private javax.swing.JMenuItem menuNewGame1;

private javax.swing.JMenuItem menuPauseGame;

private javax.swing.JMenuItem menuPauseGame1;

private Tetr.TetrisGamePanel tetrisGamePanel1;

}

Файл TetrisGamePanel.java

package Tetr;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

*

* @author irdis_13

*/

public class TetrisGamePanel extends javax.swing.JPanel implements GamePanel,ActionListener {

private Glass stakan;

private Figure fig = null;

private int score = 0;

private int gameState;

private int[] DELAY = {500, 450, 400, 350, 300, 250, 200, 150, 100, 50};

private int level = 1;

Timer t = new javax.swing.Timer(DELAY[level], this);

public TetrisGamePanel() {

stakan = new Glass();

startNewGame();

t.start();

}

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

stakan.draw(g);

fig.draw(g);

g.setFont(new Font("Times New Roman", Font.BOLD, 24));

g.setColor(Color.BLACK);

g.drawString("Счет: " + score, 350, 250);

g.drawString("Уровень: " + level, 350, 300);

}

public int getScore() {

return score;

}

public void startNewGame() {

score = 0;

level = 1;

fig = new Figure();

stakan = new Glass();

gameState = PLAY;

}

public void pauseGame() {

if (gameState == PLAY) {

gameState = PAUSE;

}

}

public void resumeGame() {

if (gameState == PAUSE) {

gameState = PLAY;

}

}

public void stopGame() {

gameState = STOP;

}

public int getState() {

return gameState;

}

public void figureMoveRight(){

if (fig.canRight(stakan)& gameState==PLAY)

fig.moveRight();

}

public void figureMoveLeft(){

if (fig.canLeft(stakan)& gameState==PLAY)

fig.moveLeft();

}

public void figureMoveDown(){

t.setDelay(DELAY[level]/20);

}

public void figureRotate(){

if (fig.canRotate(stakan)& gameState==PLAY)

fig.rotate();

}

public void gamePauseResume(){

if (gameState==PLAY) {

gameState = PAUSE;

}

else

if (gameState==PAUSE) {

gameState = PLAY;

}

}

public void actionPerformed(ActionEvent e) {

if (gameState != PLAY) {

return;

}

int bonus;

if (fig.canDown(stakan)) {

fig.moveDown();

} else {

if ((bonus = stakan.acceptFigure(fig)) < 0) {

stopGame();

}

if (bonus > 0) {

switch (bonus) {

case 4:

score += 20;

case 3:

score += 15;

case 2:

score += 10;

case 1:

score += 5;

}

int newLevel = score / 100 + 1;

if (newLevel > level && level < 9) {

level++;

}

}

if (gameState == PLAY) {

fig = new Figure();

t.setDelay(DELAY[level]);

}

}

repaint();

}

// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents

private void initComponents() {

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

this.setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 400, Short.MAX_VALUE)

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 300, Short.MAX_VALUE)

);

}// </editor-fold>//GEN-END:initComponents

}


Додаток Д Результат роботи програми

Результат роботи програми зображено на рисунку 5

Рисунок 5 – Результат роботи програми