- import java.applet.*;
- import java.awt.*;
- import java.util.Random;
- public class Game extends Applet implements Runnable {
- Random rnd = new Random();
- int mouse_pos_x = 0;
- int mouse_pos_y = 0;
- int ball_pos_x = 350;
- int ball_pos_y = 200;
- int radius = 10;
- int ball_speed_x = 0;
- int ball_speed_y = 0;
- int balken_pos_x = 30;
- int balken_pos_y = 0;
- int balken_width = 5;
- int balken_height = 100;
- int balken2_pos_x = 670;
- int balken2_pos_y = 0;
- int balken2_width = 5;
- int balken2_height = 100;
- int size_x = 700;
- int size_y = 400;
- int trefferanzahl = 5;
- private Image dbImage;
- private Graphics dbg;
- Image background;
- Button start;
- Button Pause;
- public void init() {
- this.setLayout(null);
- background = getImage(getCodeBase(), "bg.jpg");
- start = new Button("Spiel starten");
- Pause = new Button("Pause");
- start.setBounds(350, 200, 80, 20);
- add(start);
- add(Pause);
- }
- public boolean action(Event e, Object args) {
- if (e.target == start) {
- ball_speed_x = 1 + rnd.nextInt(trefferanzahl);
- ball_speed_y = 1 + rnd.nextInt(trefferanzahl);
- start.setVisible(false);
- }
- return false;
- }
- public void start() {
- Thread th = new Thread(this);
- th.start();
- }
- public void stop() {
- }
- public void destroy() {
- }
- public boolean mouseMove(Event e, int x, int y) {
- mouse_pos_y = y;
- return true;
- }
- public void run() {
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- while (true) {
- ball_pos_x += ball_speed_x;
- ball_pos_y += ball_speed_y;
- if ( ball_pos_y >= mouse_pos_y + radius &&
- ball_pos_y <= mouse_pos_y + balken_height + radius &&
- ball_pos_x <= balken_width + balken_pos_x + radius) {
- System.out.println("Ball getroffen!");
- trefferanzahl++;
- if (trefferanzahl >= 10) { trefferanzahl = 5; }
- ball_speed_y = 1 + rnd.nextInt(trefferanzahl);
- ball_speed_x = 1 + rnd.nextInt(trefferanzahl);
- }
- if ( ball_pos_y >= balken2_pos_y + radius &&
- ball_pos_y >= balken2_pos_y - balken2_height + radius &&
- ball_pos_x >= balken2_width - balken2_pos_x + radius) {
- System.out.println("Ball getroffen!");
- trefferanzahl++;
- if (trefferanzahl >= 10) { trefferanzahl = 5; }
- ball_speed_y = 1 + rnd.nextInt(trefferanzahl);
- ball_speed_x = 1 + rnd.nextInt(trefferanzahl);
- }
- if (ball_pos_x > size_x - radius) {ball_speed_x *= -1;}
- if (ball_pos_x < radius) {ball_speed_x *= -1;}
- if (ball_pos_y > size_y - radius) {ball_speed_y *= -1;}
- if (ball_pos_y < radius) {ball_speed_y *= -1;}
- balken2_pos_y = ball_pos_y - 50;
- repaint();
- try {Thread.sleep(20);} catch (InterruptedException ex) {}
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- }
- }
- public void paint(Graphics g) {
- g.drawImage(background, 0, 0, this);
- g.setColor(Color.black);
- g.fillOval(ball_pos_x - radius, ball_pos_y - radius, 2 * radius, 2 * radius);
- g.setColor(Color.black);
- g.fillRect(balken_pos_x, mouse_pos_y, balken_width, balken_height);
- g.setColor(Color.black);
- g.fillRect(balken2_pos_x, balken2_pos_y, balken2_width, balken2_height);
- }
- /*public void update(Graphics g) {
- if (dbImage == null) {
- dbImage = createImage(this.getSize().width, this.getSize().height);
- dbg = dbImage.getGraphics();
- }
- dbg.setColor(getBackground());
- dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
- dbg.setColor(getForeground());
- paint(dbg);
- g.drawImage(dbImage, 0, 0, this);
- }*/
- }
Undefined
By: Guest | Date: Jul 6 2010 11:49 | Format: None | Expires: never | Size: 3.51 KB | Hits: 855
Latest pastes
50 minutes ago
10 hours ago
1 days ago
2 days ago
2 days ago