Public paste
Undefined
By: Guest | Date: Jul 6 2010 11:49 | Format: None | Expires: never | Size: 3.51 KB | Hits: 769

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