Public paste
Undefined
By: Guest | Date: Jul 6 2010 09:29 | Format: None | Expires: never | Size: 3.16 KB | Hits: 776

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