CatchMe Quelltext
Home Nach oben

 

Home
Nach oben
Zeichnen Quelltext
Ball Quelltext
CatchMe Quelltext

 

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.math.*;

/*Catch me ist ein Spiel in dem man versuchen muss, den Ball im Paneel zu catchen.
Drücken Sie mit der linken Mousetaste auf den Ball und Sie haben ihn gefangen.
Na dann viel Spass !!
*/

public class Applet1 extends Applet implements Runnable 
{
    int x;
    int y;
    int max_x=0;
    int max_y=0;
    int feeld_x0=6;
    int feeld_x1;
    int feeld_y0=34;
    int feeld_y1;
    int n;
    int button1_y;
    int button2_y;
    int button3_y;
    int list1_y=5;
    int score_y;
    int time_y;
    int level_y;
    int message_y;
    long timeOut=60000;
    int verschiebung;
    int groesse;
    int pause;
    int score=2000;
    int level=1;
    boolean gameover=false;
    Thread counter;
    Button button_newGame=new Button("New Game");
    Button button_stop=new Button("Stop");
    Button button_start=new Button("Start");
    List lilst_size=new List(5,false);
    
    
    private int zufallZahl(int multiplikator)
    {
        Float f = new Float(Math.random()*multiplikator);
        return f.intValue(); 
    }
    
    //Run-methode des Thread überschreiben
    public void run()
    {
        Graphics g = getGraphics();
        while (true)
        {
            try
            {
                g.setColor(getBackground());
                g.fillOval(x,y,groesse,groesse);
                g.setColor(Color.blue);
                berechneKoordinaten();
                g.fillOval(x,y,groesse,groesse);
                g.setColor(Color.black);
                g.drawString("Cetch Me",(max_x+feeld_x0)/2-25, 22);
                status(g);
                counter.sleep(pause);
                timeOut-=pause;
            }
            catch (Exception e)
            {
                y=y;
            }
        } //end while
    }
    
    public void berechneKoordinaten()
    {
        n=Math.abs((n+zufallZahl(3)-1)%8);
        switch(n)
        {
            case 0:
            {
                x+=verschiebung;
                break;
            }
            case 1:
            {
                x+=verschiebung;
                y+=verschiebung;
                break;
            }
            case 2:
            {
                y+=verschiebung;
                break;
            }
            case 3:
            {
                x-=verschiebung;
                y+=verschiebung;
                break;
            }
            case 4:
            {
                x-=verschiebung;
                break;
            }
            case 5:
            {
                x-=verschiebung;
                y-=verschiebung;
                break;
            }
            case 6:
            {
                y-=verschiebung;
                break;
            }
            case 7:
            {
                x+=verschiebung;
                y-=verschiebung;
                break;
            }
            default:
            {
                break;
            }
        }
        if (x>=feeld_x1) 
        {
            x=feeld_x1;
            n=4;
        }
        if (x<feeld_x0) 
        {
            x=feeld_x0;
            n=0;
        }
        if (y>feeld_y1) 
        {
            y=feeld_y1;
            n=6;
        }
        if (y<feeld_y0) 
        {
            y=feeld_y0;
            n=2;
        }
    }
    
    //Setings for a new game
    public void newGame()
    {
        gameover=false;
        pause=50;
        score=2000;
        level=1;
        timeOut=60000;
        x=max_x/2;
        y=max_y/2;
        n=zufallZahl(8);
        groesse=(lilst_size.getSelectedIndex()+2)*5;
        verschiebung=groesse/3;
    }
    
    public void initFeeld()
    {
        feeld_x1=max_x - groesse - 5;
        feeld_y1=max_y - groesse - 5;
        feeld_x0=lilst_size.getSize().width + 11;
        feeld_y0=6;
        list1_y=5;
        button1_y=lilst_size.getSize().height + 7;
        button2_y=button1_y + 2 + button_newGame.getSize().height;
        button3_y=button2_y + 2 + button_stop.getSize().height;
        score_y=button3_y + button_start.getSize().height + 20;
        time_y=score_y + 12;
        level_y=time_y + 12;
        message_y=level_y + 12;
        
        button_newGame.setSize(lilst_size.getSize().width,button_newGame.getSize().height);
        button_stop.setSize(lilst_size.getSize().width,button_stop.getSize().height);
        button_start.setSize(button_stop.getSize().width,button_start.getSize().height);
    }
    
    /**Initialize the applet*/
    public void init() 
    {
        max_x=getSize().width;
        max_y=getSize().height;
        counter=null;
        lilst_size.addItem("Punktgröße = 10");
        lilst_size.addItem("Punktgröße = 15");
        lilst_size.addItem("Punktgröße = 20");
        lilst_size.addItem("Punktgröße = 25");
        lilst_size.addItem("Punktgröße = 30");
        lilst_size.addItem("Punktgröße = 35");
        lilst_size.select(3);
        newGame();
        
        //Listbox für größenwahl des Balles
        lilst_size.addItemListener(new ItemListener ()
        {
            public void itemStateChanged(ItemEvent i)
            {
                groesse=(lilst_size.getSelectedIndex()+2)*5;
                verschiebung=groesse/3;
                initFeeld();
            }
        });
        
        //Button for new game 
        button_newGame.addActionListener (new ActionListener()
        {
            public void actionPerformed (ActionEvent e)
            {
                repaint();
                newGame();
            }
        });
        
        //Button to stop the thread
        button_stop.addActionListener (new ActionListener()
        {
            public void actionPerformed (ActionEvent e)
            {
                stop();
            }
        });
        
        //Button to start the thread
        button_start.addActionListener (new ActionListener()
        {
            public void actionPerformed (ActionEvent e)
            {
                if (counter==null)
                {
                    start();
                    x=max_x/2;
                    y=max_y/2;
                }
            }
        });
        
        //Mouse event to look about koordinats 
        this.addMouseListener (new MouseAdapter ()
        {
            public void mousePressed (MouseEvent e)
            {
                int a=e.getX();
                int b=e.getY();
                int v=x+groesse;
                int w=y+groesse;
                if (counter!=null)
                {
                    if ( a>x && a<v && b>y && b<w)
                    {
                        stop();
                        level++;
                        timeOut+=60000;
                        score+=2000;
                        Graphics g=getGraphics();
                        g.drawString("You made it!!", (max_x+feeld_x0)/2-25, feeld_y1/2+11);
                        g.drawString("Press [Start] for next level",(max_x+feeld_x0)/2-25, feeld_y1/2+22);
                        pause=pause-20;
                        if (pause<10) pause=10;
                    } 
                    else 
                    {
                        Graphics g=getGraphics();
                        g.drawString("Try it again!!", 5, message_y);
                        score-=100;
                        if (score<0) gameover=true;
                    }
                }
            } //end mousePressed
        }); //end Listener
        add(lilst_size);
        add(button_newGame);
        add(button_stop);
        add(button_start);
    }
    
    //Zeichnen des Objektes
    public void paint (Graphics g)
    {
        initFeeld();
        if (gameover!=true && timeOut>0) 
        {
            button_newGame.setLocation(5,button1_y);
            button_stop. setLocation(5,button2_y);
            button_start. setLocation(5,button3_y);
            lilst_size. setLocation(5,list1_y);
            g.setColor(Color.black);
            g.drawString("Score: " + score ,5, score_y);
            g.drawString("Time: " + timeOut/1000+"s",5,time_y);
            g.drawString("Level: " + level , 5, level_y);
            g.drawRect(button_stop.getSize().width+10 , 5 , feeld_x1-feeld_x0+groesse+1 , max_y-10);
            g.setColor(Color.blue);
            g.fillOval(x,y,groesse,groesse);
            if (timeOut<=10000)
            {
                g.setColor(Color.red);
                g.drawString("Hurry up !! ",5, message_y);
            }
            if (timeOut<20000 && timeOut>10000)
            {
                g.setColor(Color.red);
                g.drawString("Am I to fast 4 Y ?? ", 5, message_y);
            }
        } 
        else 
        {
            g.drawString("You lost the Game", max_x/3, max_y/2);
            g.setColor(Color.red);
            g.drawString("Your total Score: " +score , max_x/3, max_y/2+20);
        }
    }
    
    public void status(Graphics g)
    {
        if (gameover!=true && timeOut>0) 
        {
            Color col;
            col=g.getColor();
            g.setColor(Color.green);
            //getBackground());
            g.fill3DRect(5,score_y-12,feeld_x0-10,37,true);
            g.setColor(col);
            g.drawString("Score: " + score ,5, score_y);
            g.drawString("Time: " + timeOut/1000+"s",5,time_y);
            g.drawString("Level: " + level , 5, level_y);
            if (timeOut<=10000)
            {
                g.setColor(Color.red);
                g.drawString("Hurry up !! ",5, message_y);
            }
            if (timeOut<20000 && timeOut>10000)
            {
                g.setColor(Color.red);
                g.drawString("Am I to fast 4 Y ?? ", 5, message_y);
            }
        } 
        else 
        {
            g.drawString("You lost the Game", max_x/3, max_y/2);
            g.setColor(Color.red);
            g.drawString("Your total Score: " +score , max_x/3, max_y/2+20);
            stop();
            newGame();
        }
    }
    
    public void start ()
    {
        Graphics g = getGraphics();
        g.setColor(getBackground());
        g.fillOval(x,y,groesse,groesse);
        g.setColor(Color.blue);
        if (counter==null)
        {
            counter=new Thread(this);
            counter.start();
        }
    }
    
    public void stop ()
    {
        if (counter!=null)
        {
            counter.stop();
            counter=null;
        }
    }
}