Public paste
Undefined
By: Guest | Date: Jul 1 2010 12:12 | Format: None | Expires: never | Size: 4.04 KB | Hits: 832

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