- BreakerBall
- --------------------------------------------------------------------------------------------------------------------------
- package breakerball;
- import static com.sun.java.accessibility.util.AWTEventMonitor.addWindowListener;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.*;
- import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
- public class BreakerBall extends JFrame {//650 700
- static BreakerBall w1 ;
- static play2 p = new play2();
- private BreakerBall(){
- super(" BreakerBall");
- addWindowListener(new java.awt.event.WindowAdapter() {
- public void windowOpened(java.awt.event.WindowEvent evt) {
- w1 = new BreakerBall();
- }
- public void windowClosing(java.awt.event.WindowEvent evt) {
- System.out.println("AS2525");
- }
- });
- }
- public static void main(String[] args) {
- w1 = new BreakerBall();
- w1.setResizable(false);
- w1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- p.setVisible(true);
- p.jButton1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Input();
- }
- });
- p.jButton2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Input2();
- }
- });
- }
- private static void Input(){
- try{
- int r = Integer.parseInt(p.jTextField1.getText()) ;
- int c = Integer.parseInt(p.jTextField2.getText()) ;
- w1.setBounds(100,100,650,700);
- w1.setVisible(true);
- Play p1 = new Play(r,c) ;
- p1.balldirX = -(p.jSlider1.getValue()) ;
- p1.balldirY = p1.balldirY + -(p.jSlider1.getValue()) ;
- w1.add(p1);
- }catch(Exception e){
- JOptionPane.showMessageDialog(null, "無效整數");
- e.printStackTrace();
- }
- }
- private static void Input2(){
- try{
- w1.setBounds(100,100,650,700);
- w1.setVisible(true);
- play3 p2 = new play3() ;
- p2.balldirX = -(p.jSlider1.getValue()) ;
- p2.balldirY = p2.balldirY + -(p.jSlider1.getValue()) ;
- w1.add(p2);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- Play
- --------------------------------------------------------------------------------------------------------------------------
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package breakerball;
- import static breakerball.BreakerBall.w1;
- import static com.sun.java.accessibility.util.AWTEventMonitor.addWindowListener;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import java.io.File;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.UnsupportedAudioFileException;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- public class Play extends JPanel implements ActionListener, KeyListener {
- File Clip = new File("drum1.wav");
- File a = new File("pianoC.wav");
- int score = 0;
- boolean play = false;
- int totalbricks; //總磚
- int playerX = 325, playerX2 = 150;
- int ballposX = 180;
- int ballposY = 380;
- int balldirX = 0;
- int balldirY = -1;
- Timer timer;
- Brick Brick;
- public Play(int r, int c) {
- addKeyListener(this);
- Brick = new Brick(r, c);
- totalbricks = r * c;
- this.setFocusable(true);//設置可透過鍵盤獲得值
- this.setFocusTraversalKeysEnabled(true);//設置焦點穿越鍵啟用
- timer = new Timer(5, this);// ActionListener , KeyListener
- timer.start();
- }
- public void paint(Graphics G) {//Graphics2D 重製 類別繪圖
- //背景
- G.setColor(Color.BLACK);
- G.fillRect(1, 1, 650, 700);
- Brick.draw((Graphics2D) G);
- //邊界
- G.setColor(Color.RED);
- G.fillRect(0, 0, 5, 695);
- G.fillRect(0, 0, 645, 5);
- G.fillRect(641, 0, 5, 695);
- //Font
- G.setColor(Color.RED);
- G.setFont(new Font("標楷體", 3, 21));
- G.drawString("分數 :" + score, 10, 650);
- //槳
- G.setColor(Color.GREEN);
- G.fillRect(playerX, 600, 100, 12);
- //球
- G.setColor(Color.YELLOW);
- G.fillOval(ballposX, ballposY, 15, 15);
- //Over
- if (ballposY > 670) {
- ballposX = 120;
- ballposY = 380;
- playerX = 500;
- play = false;
- timer.stop();
- G.setColor(Color.RED);
- G.setFont(new Font("標楷體", 3, 48));
- G.drawString("遊戲結束重新開始 Enter", 50, 450);
- }
- if (totalbricks == 0) {
- ballposX = 120;
- ballposY = 380;
- playerX = 500;
- play = false;
- timer.stop();
- G.setColor(Color.RED);
- G.setFont(new Font("標楷體", 3, 48));
- G.drawString("你贏了分數" + score, 50, 450);
- }
- G.dispose();
- }
- public void actionPerformed(ActionEvent e) {
- if (play) {//鍵盤開始
- if (new Rectangle(ballposX, ballposY, 15, 15).intersects(new Rectangle(playerX, 600, 100, 8))) { //交叉
- Plsy2(Clip);
- balldirY = -balldirY;
- } //Y 變負 往內
- ballposX += balldirX;
- ballposY += balldirY;
- if (ballposX < 0) {
- balldirX = -balldirX; //X 變正 往外
- }
- if (ballposY < 0) {
- balldirY = -balldirY; //Y 變正 往外
- }
- if (ballposX > 630) {
- balldirX = -balldirX; //X 變負 往內
- }
- }
- for (int i = 0; i < Brick.map.length; i++) {
- for (int j = 0; j < Brick.map[0].length; j++) {
- if (Brick.map[i][j] > 0) {
- int BrickX = j * Brick.BrickWinght + 80;
- int BrickY = i * Brick.BrickHinght + 80;
- int BrickWidth = Brick.BrickWinght;
- int BrickHight = Brick.BrickHinght;
- Rectangle ball = new Rectangle(ballposX, ballposY, 15, 15);
- Rectangle Rect = new Rectangle(BrickX, BrickY, BrickWidth, BrickHight);
- if (ball.intersects(Rect)) {
- Plsy2(a);
- totalbricks--;
- score += 5;
- Brick.setBrickvaule(i, j, 0);
- if (ballposX <= Rect.x || ballposX >= Rect.x + Rect.width) // <= 方塊 =>
- {
- balldirX = -balldirX; //反彈
- } else {
- balldirY = -balldirY;
- }
- }
- }
- }
- }
- this.repaint();//重製
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if (timer.isRunning()) {
- if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
- if (playerX > 520) {
- playerX = 520;
- } else {
- MoveRight(1);
- }
- } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
- if (playerX < 13) {
- playerX = 13;
- } else {
- MoveLeft(1);
- }
- }
- }
- if (play == false && e.getKeyCode() == KeyEvent.VK_ENTER) {
- score = 0;
- ballposX = 180;
- ballposY = 395;
- playerX = 400;
- play = true;
- balldirX = -balldirX;
- balldirY = -balldirY;
- totalbricks = 21;
- Brick = new Brick(3, 7);
- timer.start();
- repaint();
- }
- }
- private void MoveRight(int i) {
- play = true;
- if (i == 1) {
- playerX += 30;
- } else {
- playerX2 += 30;
- }
- }
- private void MoveLeft(int i) {
- play = true;
- if (i == 1) {
- playerX -= 30;
- } else {
- playerX2 -= 30;
- }
- }
- public static void Plsy2(File Cli) {
- try {
- Clip a = AudioSystem.getClip();
- a.open(AudioSystem.getAudioInputStream(Cli));
- a.start();
- Thread.sleep(0);//間隔
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- Brick
- --------------------------------------------------------------------------------------------------------------------------
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package breakerball;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Stroke;
- /**
- *
- * @author user
- */
- public class Brick {
- int[][] map ;
- int BrickWinght ;
- int BrickHinght ;
- Brick(int r , int com ){
- map = new int[r][com] ;
- for(int i = 0 ; i < map.length ; i++){
- for(int j = 0 ; j < map[0].length ; j++){
- map[i][j] = 1 ;
- }
- }
- BrickWinght = 500/com ;//7
- BrickHinght = 120/r ;//3
- }
- public void draw(Graphics2D G){
- for(int i = 0 ; i < map.length ; i++){
- for(int j = 0 ; j < map[0].length ; j++){
- if(map[i][j] > 0){
- G.setColor(Color.ORANGE);
- G.fillRect(j* BrickWinght+80, i* BrickHinght+80, BrickWinght, BrickHinght);
- G.setColor(Color.black);
- G.setStroke(new BasicStroke(3));
- G.drawRect(j* BrickWinght+80, i* BrickHinght+80,BrickWinght, BrickHinght);
- }
- }
- }
- }
- public void setBrickvaule(int r , int c , int vaule){
- map[r][c] = vaule ;
- }
- }
- play2
- --------------------------------------------------------------------------------------------------------------------------
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package breakerball;
- import static breakerball.Play.Plsy2;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.io.File;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- /**
- *
- * @author 1
- */
- public class play3 extends JPanel implements ActionListener , KeyListener{
- File Clip2 = new File("small_drum1.wav");
- int score = 0 , score2 = 0 ;
- boolean play = false ;
- int playerX = 325 , playerX2 = 365;
- int ballposX = 350 ;
- int ballposY = 350 ;
- int balldirX = 0;
- int balldirY = -1 ;
- Timer timer ;
- public play3(){
- addKeyListener(this);
- this.setFocusable(true);//設置可透過鍵盤獲得值
- this.setFocusTraversalKeysEnabled(true);//設置焦點穿越鍵啟用
- timer = new Timer(15, this);// ActionListener , KeyListener
- timer.start();
- }
- public void paint(Graphics G){
- //背景
- G.setColor(Color.BLACK);
- G.fillRect(1, 1, 650, 700);
- //邊界
- G.setColor(Color.RED);
- G.fillRect(0, 0,5, 695);
- G.fillRect(641, 0, 5, 695);
- //界線
- G.setColor(Color.WHITE);
- G.fillRect(6, 350,633, 3);
- G.drawOval(220, 248, 200, 200);
- //槳
- G.setColor(Color.RED);
- G.fillRect(playerX2, 60, 100, 12);
- G.setColor(Color.GREEN);
- G.fillRect(playerX, 600, 100, 12);
- //球
- G.setColor(Color.YELLOW);
- G.fillOval(ballposX, ballposY, 15, 15);
- //分數
- G.setFont(new java.awt.Font("華康粗黑體(P)", 0, 18));
- G.drawString("分數 "+score2, 10, 340);
- G.drawString("分數 "+score, 570, 375);
- if(score > 15){
- G.drawString("玩家一獲勝 ", 350, 375);
- timer.stop();
- }else if(score2 >15){
- G.drawString("玩家二獲勝 ", 350, 340);
- timer.stop();
- }
- G.dispose();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(play){
- Rectangle R = new Rectangle(ballposX, ballposY, 15, 15) ;
- if(R.intersects(new Rectangle(playerX, 600, 100, 8))){ //交叉
- balldirY = -balldirY ;
- Plsy2(Clip2); } //Y 變負 往內
- if(R.intersects(new Rectangle(playerX2, 60, 100, 8))){ //交叉
- balldirY = -balldirY ;
- Plsy2(Clip2);} //Y 變負 往內
- ballposX += balldirX ;
- ballposY += balldirY ;
- if(ballposX < 0)
- balldirX = -balldirX; //X 變正 往外
- if(ballposX > 630)
- balldirX = -balldirX ; //X 變負 往內
- if(ballposY <= 16){
- ballposX = -balldirX ;
- ballposY = -balldirY ;
- ballposX = 355 ;
- ballposY = 75 ;
- timer.start();
- play = false ;
- playerX = 325 ;
- playerX2 = 355;
- score ++;
- }
- if(ballposY >= 630){
- ballposX = -balldirX ;
- ballposY = -balldirY ;
- ballposX = 355 ;
- ballposY = 585 ;
- timer.start();
- play = false ;
- playerX = 325 ;
- playerX2 = 355;
- score2 ++;
- }
- }
- this.repaint();
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if(timer.isRunning()){
- if(e.getKeyCode() == KeyEvent.VK_RIGHT){
- if(playerX > 520)
- playerX = 520 ;
- else
- MoveRight(1);
- }else if (e.getKeyCode() == KeyEvent.VK_LEFT){
- if(playerX < 13)
- playerX = 13 ;
- else
- MoveLeft(1);
- }
- if(e.getKeyCode() == KeyEvent.VK_D){
- if(playerX2 > 520)
- playerX2 = 520 ;
- else
- MoveRight(2);
- }else if (e.getKeyCode() == KeyEvent.VK_A){
- if(playerX2 < 13)
- playerX2 = 13 ;
- else
- MoveLeft(2);
- }
- }
- if(e.getKeyCode() == KeyEvent.VK_ENTER){//重來
- Reset();
- score = 0; score2 = 0;
- }
- }
- private void MoveRight(int i) {
- play = true ;
- if(i == 1)
- playerX += 30 ;
- else
- playerX2 += 30 ;
- }
- private void MoveLeft(int i) {
- play = true ;
- if(i == 1)
- playerX -= 30 ;
- else
- playerX2 -= 30 ;
- }
- private void Reset(){
- ballposX = -balldirX ;
- ballposY = -balldirY ;
- ballposX = 350 ;
- ballposY = 350 ;
- timer.start();
- play = false ;
- playerX = 325 ;
- playerX2 = 355;
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- }
- play2
- -------------------------------------------------------------------------------------------------------------------------
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package breakerball;
- import java.io.File;
- import javax.swing.ImageIcon;
- /**
- *
- * @author user
- */
- public class play2 extends javax.swing.JFrame {
- /**
- * Creates new form play2
- */
- public play2() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jTextField1 = new javax.swing.JTextField();
- jTextField2 = new javax.swing.JTextField();
- jSlider1 = new javax.swing.JSlider();
- jButton2 = new javax.swing.JButton();
- jButton1 = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setBounds(new java.awt.Rectangle(450, 450, 0, 0));
- setResizable(false);
- addWindowListener(new java.awt.event.WindowAdapter() {
- public void windowClosing(java.awt.event.WindowEvent evt) {
- formWindowClosing(evt);
- }
- public void windowOpened(java.awt.event.WindowEvent evt) {
- formWindowOpened(evt);
- }
- });
- addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyPressed(java.awt.event.KeyEvent evt) {
- formKeyPressed(evt);
- }
- public void keyTyped(java.awt.event.KeyEvent evt) {
- formKeyTyped(evt);
- }
- });
- getContentPane().setLayout(new java.awt.GridLayout(2, 2));
- jTextField1.setFont(new java.awt.Font("標楷體", 0, 36)); // NOI18N
- jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
- jTextField1.setText("列數");
- getContentPane().add(jTextField1);
- jTextField2.setFont(new java.awt.Font("標楷體", 0, 36)); // NOI18N
- jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
- jTextField2.setText("行數");
- getContentPane().add(jTextField2);
- jSlider1.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
- jSlider1.setMajorTickSpacing(3);
- jSlider1.setMaximum(8);
- jSlider1.setMinimum(1);
- jSlider1.setOrientation(javax.swing.JSlider.VERTICAL);
- jSlider1.setPaintLabels(true);
- jSlider1.setPaintTicks(true);
- jSlider1.setValue(2);
- getContentPane().add(jSlider1);
- jButton2.setFont(new java.awt.Font("Arial", 0, 36)); // NOI18N
- jButton2.setText("2P Start");
- jButton2.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyPressed(java.awt.event.KeyEvent evt) {
- jButton2KeyPressed(evt);
- }
- });
- getContentPane().add(jButton2);
- jButton1.setFont(new java.awt.Font("Aharoni", 0, 36)); // NOI18N
- jButton1.setText("Start");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyPressed(java.awt.event.KeyEvent evt) {
- jButton1KeyPressed(evt);
- }
- });
- getContentPane().add(jButton1);
- jLabel1.setFont(new java.awt.Font("Arial Black", 0, 48)); // NOI18N
- jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- jLabel1.setText("XIN");
- getContentPane().add(jLabel1);
- pack();
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- }
- private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {
- // TODO add your handling code here:
- }
- private void formKeyTyped(java.awt.event.KeyEvent evt) {
- }
- private void formWindowClosing(java.awt.event.WindowEvent evt) {
- }
- private void formWindowOpened(java.awt.event.WindowEvent evt) {
- // TODO add your handling code here:
- }
- private void jButton2KeyPressed(java.awt.event.KeyEvent evt) {
- }
- private void formKeyPressed(java.awt.event.KeyEvent evt) {
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(play2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(play2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(play2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(play2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new play2().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- public javax.swing.JButton jButton1;
- public javax.swing.JButton jButton2;
- private javax.swing.JLabel jLabel1;
- public javax.swing.JSlider jSlider1;
- public javax.swing.JTextField jTextField1;
- public javax.swing.JTextField jTextField2;
- // End of variables declaration
- }
- play3
- --------------------------------------------------------------------------------------------------------------------------
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package breakerball;
- import static breakerball.Play.Plsy2;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.io.File;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- /**
- *
- * @author 1
- */
- public class play3 extends JPanel implements ActionListener , KeyListener{
- File Clip2 = new File("small_drum1.wav");
- int score = 0 , score2 = 0 ;
- boolean play = false ;
- int playerX = 325 , playerX2 = 365;
- int ballposX = 350 ;
- int ballposY = 350 ;
- int balldirX = 0;
- int balldirY = -1 ;
- Timer timer ;
- public play3(){
- addKeyListener(this);
- this.setFocusable(true);//設置可透過鍵盤獲得值
- this.setFocusTraversalKeysEnabled(true);//設置焦點穿越鍵啟用
- timer = new Timer(15, this);// ActionListener , KeyListener
- timer.start();
- }
- public void paint(Graphics G){
- //背景
- G.setColor(Color.BLACK);
- G.fillRect(1, 1, 650, 700);
- //邊界
- G.setColor(Color.RED);
- G.fillRect(0, 0,5, 695);
- G.fillRect(641, 0, 5, 695);
- //界線
- G.setColor(Color.WHITE);
- G.fillRect(6, 350,633, 3);
- G.drawOval(220, 248, 200, 200);
- //槳
- G.setColor(Color.RED);
- G.fillRect(playerX2, 60, 100, 12);
- G.setColor(Color.GREEN);
- G.fillRect(playerX, 600, 100, 12);
- //球
- G.setColor(Color.YELLOW);
- G.fillOval(ballposX, ballposY, 15, 15);
- //分數
- G.setFont(new java.awt.Font("華康粗黑體(P)", 0, 18));
- G.drawString("分數 "+score2, 10, 340);
- G.drawString("分數 "+score, 570, 375);
- if(score > 15){
- G.drawString("玩家一獲勝 ", 350, 375);
- timer.stop();
- }else if(score2 >15){
- G.drawString("玩家二獲勝 ", 350, 340);
- timer.stop();
- }
- G.dispose();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(play){
- Rectangle R = new Rectangle(ballposX, ballposY, 15, 15) ;
- if(R.intersects(new Rectangle(playerX, 600, 100, 8))){ //交叉
- balldirY = -balldirY ;
- Plsy2(Clip2); } //Y 變負 往內
- if(R.intersects(new Rectangle(playerX2, 60, 100, 8))){ //交叉
- balldirY = -balldirY ;
- Plsy2(Clip2);} //Y 變負 往內
- ballposX += balldirX ;
- ballposY += balldirY ;
- if(ballposX < 0)
- balldirX = -balldirX; //X 變正 往外
- if(ballposX > 630)
- balldirX = -balldirX ; //X 變負 往內
- if(ballposY <= 16){
- ballposX = -balldirX ;
- ballposY = -balldirY ;
- ballposX = 355 ;
- ballposY = 75 ;
- timer.start();
- play = false ;
- playerX = 325 ;
- playerX2 = 355;
- score ++;
- }
- if(ballposY >= 630){
- ballposX = -balldirX ;
- ballposY = -balldirY ;
- ballposX = 355 ;
- ballposY = 585 ;
- timer.start();
- play = false ;
- playerX = 325 ;
- playerX2 = 355;
- score2 ++;
- }
- }
- this.repaint();
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if(timer.isRunning()){
- if(e.getKeyCode() == KeyEvent.VK_RIGHT){
- if(playerX > 520)
- playerX = 520 ;
- else
- MoveRight(1);
- }else if (e.getKeyCode() == KeyEvent.VK_LEFT){
- if(playerX < 13)
- playerX = 13 ;
- else
- MoveLeft(1);
- }
- if(e.getKeyCode() == KeyEvent.VK_D){
- if(playerX2 > 520)
- playerX2 = 520 ;
- else
- MoveRight(2);
- }else if (e.getKeyCode() == KeyEvent.VK_A){
- if(playerX2 < 13)
- playerX2 = 13 ;
- else
- MoveLeft(2);
- }
- }
- if(e.getKeyCode() == KeyEvent.VK_ENTER){//重來
- Reset();
- score = 0; score2 = 0;
- }
- }
- private void MoveRight(int i) {
- play = true ;
- if(i == 1)
- playerX += 30 ;
- else
- playerX2 += 30 ;
- }
- private void MoveLeft(int i) {
- play = true ;
- if(i == 1)
- playerX -= 30 ;
- else
- playerX2 -= 30 ;
- }
- private void Reset(){
- ballposX = -balldirX ;
- ballposY = -balldirY ;
- ballposX = 350 ;
- ballposY = 350 ;
- timer.start();
- play = false ;
- playerX = 325 ;
- playerX2 = 355;
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- }
2017年7月29日 星期六
(Java) Breaker Ball (單人與雙人)
訂閱:
張貼留言 (Atom)



沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。