- import java.applet.*;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.awt.event.MouseListener;
- import java.util.Random;
- public class Game extends Applet implements Runnable, KeyListener, MouseListener, MouseMotionListener {
- 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 tastaturhoch = 38;
- int player2y = 100;
- int tastaturrunter = 40;
- int appletsize_x = 700;
- int appletsize_y = 400;
- int mousepressed = 0;
- int trefferanzahl = 15;
- 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;
- int punktespieler1 = 0;
- int punktespieler2 = 0;
- Image background;
- private Image dbImage;
- private Graphics dbg;
- public void init() {
- this.setLayout(null);
- addKeyListener(this);
- addMouseListener(this);
- addMouseMotionListener(this);
- setFocusable(true);
- background = getImage(getCodeBase(), "bg.jpg");
- }
- public void mousePressed(MouseEvent arg0) {
- if ( mousepressed == 0){
- ball_x_speed = 1 + rd.nextInt(trefferanzahl);
- ball_y_speed = 1 + rd.nextInt(trefferanzahl);
- }
- mousepressed++;
- }
- 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 mouseMoved(MouseEvent arg0) {
- // TODO Auto-generated method stub
- mouse_y_pos = arg0.getY();
- }
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == tastaturhoch) {
- player2y = player2y - 35;
- }
- if(e.getKeyCode() == tastaturrunter){
- player2y = player2y + 35;
- }
- }
- public void keyReleased(KeyEvent e) {
- // TODO Auto-generated method stub
- System.out.println("hallo");
- }
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- System.out.println("hallo");
- }
- 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 >= 25) { trefferanzahl = 15; }
- ball_y_speed = 1 + rd.nextInt(trefferanzahl);
- ball_x_speed = 1 + rd.nextInt(trefferanzahl);
- }
- if ( ball_y_pos >= player2y + radius &&
- ball_y_pos <= player2y + balken_height + radius &&
- ball_x_pos >= 670) {
- System.out.println("Ball getroffen!");
- trefferanzahl++;
- if (trefferanzahl >= 25) { trefferanzahl = 15; }
- 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;
- }
- if (ball_x_pos >= 675) { ball_x_speed = 0; ball_y_speed = 0; ball_x_pos = 350; ball_y_pos = 200; punktespieler1++; mousepressed = 0;}
- if (ball_x_pos <= 25) { ball_x_speed = 0; ball_y_speed = 0; ball_x_pos = 350; ball_y_pos = 200; punktespieler2++; mousepressed = 0;}
- 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);
- g.setColor(Color.black);
- g.fillRect(670,player2y, balken_width, balken_height );
- }
- public void mouseEntered(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- public void mouseExited(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- public void mouseReleased(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- public void mouseDragged(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- public void mouseClicked(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- /* 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 13:35 | Format: None | Expires: never | Size: 6.16 KB | Hits: 879
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago