- import java.applet.*;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.Random;
- public class Game extends Applet implements Runnable, KeyListener {
- Random rd = new Random();
- int ball_x_pos = 250;
- int ball_y_pos = 125;
- int mouse_x_pos = 0;
- int mouse_y_pos = 0;
- int appletsize_x = 700;
- int appletsize_y = 400;
- int trefferanzahl = 5;
- int radius = 10;
- int balken_x_pos = 30;
- int balken_y_pos = mouse_y_pos;
- int balken_width = 20;
- int balken_height = 100;
- int ball_x_speed = 0;
- int ball_y_speed = 0;
- Image background;
- private Image dbImage;
- private Graphics dbg;
- Button start;
- Button Pause;
- public void init() {
- this.setLayout(null);
- addKeyListener(this);
- background = getImage(getCodeBase(), "bg.jpg");
- start = new Button("Spiel starten");
- Pause = new Button("Pause");
- start.setBounds(250, 125, 80, 50);
- add(start);
- add(Pause);
- }
- public boolean action(Event e, Object args) {
- if (e.target == start) {
- ball_x_speed = 1 + rd.nextInt(trefferanzahl);
- ball_y_speed = 1 + rd.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_y_pos = y;
- return true;
- }
- public void run() {
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- while (true) {
- if ( ball_y_pos >= mouse_y_pos + radius &&
- ball_y_pos <= mouse_y_pos + balken_height + radius &&
- ball_x_pos <= balken_width + balken_x_pos + radius) {
- System.out.println("Ball getroffen!");
- trefferanzahl++;
- if (trefferanzahl >= 10) { trefferanzahl = 5; }
- ball_y_speed = 1 + rd.nextInt(trefferanzahl);
- ball_x_speed = 1 + rd.nextInt(trefferanzahl);
- }
- if (ball_x_pos > appletsize_x - radius) {
- ball_x_speed *= -1;
- }
- if (ball_y_pos > appletsize_y - radius) {
- ball_y_speed *= -1;
- }
- if (ball_x_pos < radius) {
- ball_x_pos = 350;
- ball_y_pos = 200;
- }
- if (ball_y_pos < radius) {
- ball_y_speed *= -1;
- }
- ball_x_pos += ball_x_speed;
- ball_y_pos += ball_y_speed;
- if (ball_x_pos <= radius) {
- ball_x_pos = radius + 2;
- ball_x_speed = ball_x_speed * -1;
- }
- if (ball_y_pos <= radius) {
- ball_y_pos = radius + 2;
- ball_y_speed = ball_y_speed * -1;
- }
- 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_x_pos - radius, ball_y_pos - radius, 2 * radius, 2 * radius);
- g.setColor(Color.black);
- g.fillRect(balken_x_pos, mouse_y_pos, balken_width, balken_height);
- }
- @Override
- public void keyPressed(KeyEvent e) {
- // TODO Auto-generated method stub
- System.out.println("hallo");
- }
- @Override
- public void keyReleased(KeyEvent e) {
- // TODO Auto-generated method stub
- System.out.println("hallo");
- }
- @Override
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- System.out.println("hallo");
- }
- /*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:36 | Format: None | Expires: never | Size: 4.36 KB | Hits: 931
Latest pastes
18 hours ago
1 days ago
1 days ago
1 days ago
2 days ago