JPong

Thomas Darimont

Erfahrenes Mitglied
Servus!

Hab heute ein wenig Langeweile gehabt und hab mal einen kleinen Pong Clone gebastelt ...

könnt ja mal ein wenig damit herumspielen, vielleicht postet ja jemand sogar die Handy Implementierung ;-)

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 * MyPong.java
 *
 * Created on 24. Juli 2003, 21:10
 */

/**
 *
 * @author  Administrator
 */
public class MyPong extends javax.swing.JFrame {
    
    private Graphics grp = null;
    private boolean running = true;
    private Thread t = null;
    private Image screen  = null;
    private Graphics2D gra = null;
    
    private Kugel k = null;
    
    int x = 0;
    int y = 0;
       
    /** Creates new form MyPong */
    public MyPong() {
        initComponents();
        initGame();
    }
    
    public void initGame(){if (!running){
        while(true) {
            try{
                Thread.currentThread().sleep(100);
            }catch(InterruptedException ie){
                ie.printStackTrace();
            }
        }
    }
    
    grp = jPanel1.getGraphics();
    screen = createImage(400,300);
    gra = (Graphics2D)screen.getGraphics();
    k = new Kugel(150,150, 1, 1);
    t = new Thread(new Game());
    t.start();
    }
    
    
    /** 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.
     */
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();

        setTitle("MyPong");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setMinimumSize(new java.awt.Dimension(400, 300));
        jPanel1.setPreferredSize(new java.awt.Dimension(400, 300));
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel1MouseClicked(evt);
            }
        });
        jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jPanel1MouseMoved(evt);
            }
        });

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }
    
    private void jPanel1MouseMoved(java.awt.event.MouseEvent evt) {
        // Add your handling code here:
        
        y = evt.getY();
        
        
    }
    
    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
        // Add your handling code here:
        
        running = running ^ true;
        //System.out.println(running);
    }
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new MyPong().show();
    }
    
    private void drawField(){
        gra.drawRect(5,5, 394, 294);
        gra.drawOval(175, 125, 50, 50);
        gra.drawLine(200,5, 200, 294);
        gra.drawLine(5, 150, 394, 150);
        
    }
    
    private void toScreen(){
        grp.drawImage(screen,0,0,400,300,this);
    }
    
    private void drawKugel(){
        
        k.koord.x += 2 * k.xricht;
        k.koord.y += 2 * k.yricht;
        
        if (( ( k.koord.x >= 380 ) || ( k.koord.x <= 390 ) ) &&  ( Math.abs(y - k.koord.y ) < 20 )){ //Treffer!
            
            if( k.koord.x + 10 > 390 )
                k.xricht *= -1;
        }
        
        if (k.koord.x + 5 < 23)
            k.xricht *= -1;
        
        if( k.koord.y + 4 > 300 )
            k.yricht *= -1;
        else if (k.koord.y + 4 < 14)
            k.yricht *= -1;
        
        
        gra.fillOval(k.koord.x - 3, k.koord.y - 3, 5,5);
        
    }
    
    private void drawPlayer(){
        gra.fillRect( 380 , ( ( y < 278 ) && (y > 10) ) ? y : (y == 278) ? 278 : 10,  10, 20);
    }
    
    private void drawComputer(){
        gra.fillRect(10, k.koord.y - 10 , 10,20);
    }
    
    class Game implements Runnable{
        
        public void run(){
            
            while(true){ 
                
                if (!running){
                    while(!running) {
                        try{
                            Thread.currentThread().sleep(100);
                        }catch(InterruptedException ie){
                            ie.printStackTrace();
                        }
                    }
                } else {
                    
                    while(running){
                        
                        
                        
                        gra.clearRect(0,0,400,300);
                        
                        drawField();
                        
                        drawPlayer();
                        
                        drawKugel();
                        
                        drawComputer();
                        
                        toScreen();
                        
                        try{
                            Thread.currentThread().sleep(10);
                        }catch(InterruptedException ie){
                            System.out.println(ie);
                            ie.printStackTrace();
                        }
                    }
                }
            }
        }
        
    }
    
    class Kugel{
        
        public Point koord = null;
        public int xricht = 0; // -1, 0 , 1
        public int yricht = 0; // -1, 0 , 1
        
        public Kugel(int x, int y, int xr, int yr){
            koord = new Point(x,y);
            xricht = xr;
            yricht = yr;
            
        }
        
    }
    
    
    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    // End of variables declaration
    
}

Gruss Tom
 
Mittagspausen Version ... ;-)

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 *MyPong.java
 *
 *Createdon24.Juli2003,21:10
 */
/**
 *
 *@authorAdministrator
 */
public  class MyPong extends javax.swing.JFrame{
    private Graphics grp= null ;
    private  boolean running= true ;
    private Thread t = null ;
    private Image screen= null ;
    private Graphics2D gra= null ;
    private  int score=0;
    private Kugel k= null ;
    int x=0;
    int y=0;
    /**Creates new formMyPong*/
    public MyPong(){
        initComponents();
        initGame();
    }
    public  void initGame(){ if (!running){
        while ( true ){
            try {
                Thread.currentThread().sleep(100);
            } catch (InterruptedException ie){
                ie.printStackTrace();
            }
        }
    }
    grp=jPanel1.getGraphics();
    screen=createImage(400,300);
    gra=(Graphics2D)screen.getGraphics();
    k= new Kugel(150,150,1,1);
    t= new Thread( new Game());
    t.start();
    }
    /**Thismethodiscalledfromwithintheconstructorto
     *initializetheform.
     *WARNING:DoNOTmodify this code.Thecontentof this methodis
     *alwaysregeneratedbytheFormEditor.
     */
    private  void initComponents(){
        jPanel1= new javax.swing.JPanel();
        setTitle("MyPong");
        addWindowListener( new java.awt.event.WindowAdapter(){
            public  void windowClosing(java.awt.event.WindowEvent evt){
                exitForm(evt);
            }
        });
        jPanel1.setMinimumSize( new java.awt.Dimension(400,300));
        jPanel1.setPreferredSize( new java.awt.Dimension(400,300));
        jPanel1.addMouseListener( new java.awt.event.MouseAdapter(){
            public  void mouseClicked(java.awt.event.MouseEvent evt){
                jPanel1MouseClicked(evt);
            }
        });
        jPanel1.addMouseMotionListener( new java.awt.event.MouseMotionAdapter(){
            public  void mouseMoved(java.awt.event.MouseEvent evt){
                jPanel1MouseMoved(evt);
            }
        });
        getContentPane().add(jPanel1,java.awt.BorderLayout.CENTER);
        pack();
    }
    private  void jPanel1MouseMoved(java.awt.event.MouseEvent evt){
        //Addyourhandlingcodehere:
        y=evt.getY();
    }
    private  void jPanel1MouseClicked(java.awt.event.MouseEvent evt){
        //Addyourhandlingcodehere:
        running=running^true;
        //System.out.println(running);
    }
    /**ExittheApplication*/
    private  void exitForm(java.awt.event.WindowEvent evt){
        System.exit(0);
    }
    /**
     *@paramargsthecommandlinearguments
     */
    public  static  void main(String args[]){
        new MyPong().show();
    }
    private  void drawField(){
        gra.drawRect(5,5,394,294);
        gra.drawOval(175,125,50,50);
        gra.drawLine(200,5,200,294);
        gra.drawLine(5,150,394,150);
    }
    private  void toScreen(){
        grp.drawImage(screen,0,0,400,300, this );
    }
    private  void drawKugel(){
        k.koord.x+=2*k.xricht;
        k.koord.y+=2*k.yricht;
        if (((k.koord.x>=380)||(k.koord.x<=390))&&(Math.abs(y-k.koord.y)<20)){//Treffer!
            if (k.koord.x+10>390)
            k.xricht*=-1;
        }
        if (k.koord.x+5<23)
        k.xricht*=-1;
        if (k.koord.y+4>300)
        k.yricht*=-1;
        else  if (k.koord.y+4<14)
        k.yricht*=-1;
        gra.fillOval(k.koord.x-3,k.koord.y-3,5,5);
    }
    private  void drawPlayer(){
        gra.fillRect(380,((y<278)&&(y>10))?y: (y==278)?278:10,10,20);
    }
    private  void drawComputer(){
        gra.fillRect(10,k.koord.y-10,10,20);
    }
    private  void drawScore(){
        if (k.koord.x>400){
            k.koord.x=200;
            k.koord.y=150;
            score++;
            Toolkit.getDefaultToolkit().beep();
        }
        gra.drawString("Score:"+String.valueOf(score),30,20);
    }
    class Game implements Runnable{
        public  void run(){
            while ( true ){
                if (!running){
                    while (!running){
                        try{
                            Thread.currentThread().sleep(100);
                        } catch (InterruptedExceptionie){
                            ie.printStackTrace();
                        }
                    }
                } else {
                    while (running){
                        gra.clearRect(0,0,400,300);
                        drawField();
                        drawPlayer();
                        drawKugel();
                        drawComputer();
                        drawScore();
                        toScreen();
                        try {
                            Thread.currentThread().sleep(10);
                        } catch (InterruptedException ie){
                            System.out.println(ie);
                            ie.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    class Kugel{
        public Pointkoord= null ;
        public  int xricht=0;//-1,0,1
        public  int yricht=0;//-1,0,1
        public Kugel( int x, int y, int xr, int yr){
            koord= new Point(x,y);
            xricht=xr;
            yricht=yr;
        }
    }
    //Variablesdeclaration- do notmodify
    private javax.swing.JPanel jPanel1;
    //Endofvariablesdeclaration
}
 
Zuletzt bearbeitet:
Ping Pong mit Java3D?

Hallo

Weiß hier irgendjemand ob es schon irgendwo eine Java3D Variante des JPong gibt? Wenn möglich mit Quellcode ;-)

Gruß Timo
 
Zurück