Public paste
Undefined
By: Guest | Date: Jul 6 2010 11:36 | Format: None | Expires: never | Size: 4.36 KB | Hits: 842

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  5. import java.util.Random;
  6.  
  7.  
  8. public class Game extends Applet implements Runnable, KeyListener {
  9.  
  10.     Random rd = new Random();
  11.     int ball_x_pos = 250;
  12.     int ball_y_pos = 125;
  13.     int mouse_x_pos = 0;
  14.     int mouse_y_pos = 0;
  15.     int appletsize_x = 700;
  16.     int appletsize_y = 400;
  17.     int trefferanzahl = 5;
  18.     int radius = 10;
  19.     int balken_x_pos = 30;
  20.     int balken_y_pos = mouse_y_pos;
  21.     int balken_width = 20;
  22.     int balken_height = 100;
  23.     int ball_x_speed = 0;
  24.     int ball_y_speed = 0;
  25.     Image background;
  26.     private Image dbImage;
  27.     private Graphics dbg;
  28.     Button start;
  29.     Button Pause;
  30.  
  31.     public void init() {
  32.         this.setLayout(null);
  33.         addKeyListener(this);
  34.         background = getImage(getCodeBase(), "bg.jpg");
  35.         start = new Button("Spiel starten");
  36.         Pause = new Button("Pause");
  37.         start.setBounds(250, 125, 80, 50);
  38.      
  39.         add(start);
  40.         add(Pause);
  41.     }
  42.  
  43.  
  44.     public boolean action(Event e, Object args) {
  45.         if (e.target == start) {
  46.             ball_x_speed = 1 + rd.nextInt(trefferanzahl);
  47.             ball_y_speed = 1 + rd.nextInt(trefferanzahl);
  48.             start.setVisible(false);
  49.         }
  50.         return false;
  51.     }
  52.  
  53.     public void start() {
  54.         Thread th = new Thread(this);
  55.         th.start();
  56.     }
  57.  
  58.     public void stop() {
  59.     }
  60.  
  61.     public void destroy() {
  62.     }
  63.  
  64.     public boolean mouseMove(Event e, int x, int y) {
  65.         mouse_y_pos = y;
  66.         return true;
  67.     }
  68.  
  69.     public void run() {
  70.  
  71.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  72.         while (true) {
  73.             if (    ball_y_pos >= mouse_y_pos + radius &&
  74.                     ball_y_pos <= mouse_y_pos + balken_height + radius &&
  75.                     ball_x_pos <= balken_width + balken_x_pos + radius) {
  76.                 System.out.println("Ball getroffen!");
  77.                 trefferanzahl++;
  78.                 if (trefferanzahl >= 10) { trefferanzahl = 5; }
  79.                 ball_y_speed = 1 + rd.nextInt(trefferanzahl);
  80.                 ball_x_speed = 1 + rd.nextInt(trefferanzahl);
  81.             }
  82.  
  83.             if (ball_x_pos > appletsize_x - radius) {
  84.                 ball_x_speed *= -1;
  85.  
  86.             }
  87.             if (ball_y_pos > appletsize_y - radius) {
  88.                 ball_y_speed *= -1;
  89.             }
  90.             if (ball_x_pos < radius) {
  91.                 ball_x_pos = 350;
  92.                 ball_y_pos = 200;
  93.             }
  94.  
  95.             if (ball_y_pos < radius) {
  96.                 ball_y_speed *= -1;
  97.             }
  98.             ball_x_pos += ball_x_speed;
  99.             ball_y_pos += ball_y_speed;
  100.  
  101.             if (ball_x_pos <= radius) {
  102.                 ball_x_pos = radius + 2;
  103.                 ball_x_speed = ball_x_speed * -1;
  104.             }
  105.             if (ball_y_pos <= radius) {
  106.                 ball_y_pos = radius + 2;
  107.                 ball_y_speed = ball_y_speed * -1;
  108.             }
  109.             repaint();
  110.             try {
  111.                 Thread.sleep(20);
  112.             } catch (InterruptedException ex) {
  113.             }
  114.             Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  115.         }
  116.     }
  117.  
  118.     public void paint(Graphics g) {
  119.         g.drawImage(background, 0, 0, this);
  120.  
  121.         g.setColor(Color.black);
  122.         g.fillOval(ball_x_pos - radius, ball_y_pos - radius, 2 * radius, 2 * radius);
  123.  
  124.         g.setColor(Color.black);
  125.         g.fillRect(balken_x_pos, mouse_y_pos, balken_width, balken_height);
  126.     }
  127.         @Override
  128.         public void keyPressed(KeyEvent e) {
  129.                 // TODO Auto-generated method stub
  130.                 System.out.println("hallo");
  131.         }
  132.         @Override
  133.         public void keyReleased(KeyEvent e) {
  134.                 // TODO Auto-generated method stub
  135.                 System.out.println("hallo");
  136.         }
  137.         @Override
  138.         public void keyTyped(KeyEvent e) {
  139.                 // TODO Auto-generated method stub
  140.                 System.out.println("hallo");
  141.         }
  142.  
  143.     /*public void update(Graphics g) {
  144.         if (dbImage == null) {
  145.             dbImage = createImage(this.getSize().width, this.getSize().height);
  146.             dbg = dbImage.getGraphics();
  147.         }
  148.         dbg.setColor(getBackground());
  149.         dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
  150.  
  151.         dbg.setColor(getForeground());
  152.         paint(dbg);
  153.  
  154.         g.drawImage(dbImage, 0, 0, this);
  155.     }*/
  156. }