Public paste
Undefined
By: Guest | Date: Jul 6 2010 10:19 | Format: None | Expires: never | Size: 3.01 KB | Hits: 766

  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 rd = new Random();
  8.  
  9.         int mouse_x_pos = 0;
  10.         int mouse_y_pos = 0;
  11.        
  12.         int ball_x_pos = 350;
  13.         int ball_y_pos = 200;
  14.         int radius = 10;
  15.         int ball_x_speed = 0;
  16.         int ball_y_speed = 0;
  17.        
  18.         int balken_x_pos = 30;
  19.         int balken_y_pos = mouse_y_pos;
  20.         int balken_width = 10;
  21.         int balken_height = 100;
  22.  
  23.         int appletsize_x = 700;
  24.         int appletsize_y = 400;
  25.        
  26.         int trefferanzahl = 5;
  27.         Image background;
  28.         private Image dbImage;
  29.         private Graphics dbg;
  30.         Button start;
  31.         Button Pause;
  32.  
  33.         public void init() {
  34.                 this.setLayout(null);
  35.                 background = getImage(getCodeBase(), "bg.jpg");
  36.                 start = new Button("Spiel starten");
  37.                 Pause = new Button("Pause");
  38.                 start.setBounds(350, 200, 80, 20);
  39.                 add(start);
  40.                 add(Pause);
  41.         }
  42.  
  43.         public boolean action(Event e, Object args) {
  44.                 if (e.target == start) {
  45.                         ball_x_speed = 1 + rd.nextInt(trefferanzahl);
  46.                         ball_y_speed = 1 + rd.nextInt(trefferanzahl);
  47.                         start.setVisible(false);
  48.                 }
  49.                 return false;
  50.         }
  51.  
  52.         public void start() {
  53.                 Thread th = new Thread(this);
  54.                 th.start();
  55.         }
  56.  
  57.         public void stop() {
  58.         }
  59.  
  60.         public void destroy() {
  61.         }
  62.  
  63.         public boolean mouseMove(Event e, int x, int y) {
  64.                 mouse_y_pos = y;
  65.                 return true;
  66.         }
  67.  
  68.         public void run() {
  69.  
  70.                 Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  71.                 while (true) {
  72.                         ball_x_pos += ball_x_speed;
  73.                         ball_y_pos += ball_y_speed;
  74.                         if (    ball_y_pos >= mouse_y_pos + radius &&
  75.                                         ball_y_pos <= mouse_y_pos + balken_height + radius &&
  76.                                         ball_x_pos <= balken_width + balken_x_pos + radius) {
  77.                                 System.out.println("Ball getroffen!");
  78.                                 trefferanzahl++;
  79.                                 if (trefferanzahl >= 10) { trefferanzahl = 5; }
  80.                                 ball_y_speed = 1 + rd.nextInt(trefferanzahl);
  81.                                 ball_x_speed = 1 + rd.nextInt(trefferanzahl);
  82.                         }
  83.                         if (ball_x_pos > appletsize_x - radius) {ball_x_speed *= -1;}
  84.                         if (ball_x_pos < radius) {ball_x_speed *= -1;}
  85.                         if (ball_y_pos > appletsize_y - radius) {ball_y_speed *= -1;}
  86.                         if (ball_y_pos < radius) {ball_y_speed *= -1;}
  87.                        
  88.                        
  89.                         if(balken_y_pos >= appletsize_y - balken_height) {
  90.                                 balken_y_pos = appletsize_y - balken_height;
  91.                         }
  92.                         repaint();
  93.                         try {
  94.                                 Thread.sleep(20);
  95.                         } catch (InterruptedException ex) {
  96.                         }
  97.                         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  98.                 }
  99.         }
  100.  
  101.         public void paint(Graphics g) {
  102.                 g.drawImage(background, 0, 0, this);
  103.  
  104.                 g.setColor(Color.black);
  105.                 g.fillOval(ball_x_pos - radius, ball_y_pos - radius, 2 * radius, 2 * radius);
  106.  
  107.                 g.setColor(Color.black);
  108.                 g.fillRect(balken_x_pos, mouse_y_pos, balken_width, balken_height);
  109.         }
  110.  
  111.         /*public void update(Graphics g) {
  112.                 if (dbImage == null) {
  113.                         dbImage = createImage(this.getSize().width, this.getSize().height);
  114.                         dbg = dbImage.getGraphics();
  115.                 }
  116.                 dbg.setColor(getBackground());
  117.                 dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
  118.  
  119.                 dbg.setColor(getForeground());
  120.                 paint(dbg);
  121.  
  122.                 g.drawImage(dbImage, 0, 0, this);
  123.         }*/
  124. }