Thread: Ping pong game
View Single Post
  #4 (permalink)  
Old 03-19-2008, 02:52 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
// <applet code="FullBallRx" width="400" height="400"></applet> import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class FullBallRx extends Applet implements Runnable { //position of the ball int x_pos = 12; int y_pos = 12; //position of the ball ** slider ** int sliderx_pos = 1; int slidery_pos = 150; //ball size int radius = 5; //size of slider int swidth = 60; int sheight = 1; Rectangle box = new Rectangle(swidth, sheight); // int realwidth = sliderx_pos + swidth; //balldirection int x_speed = 6; int y_speed = 8; //slider direcion int sliderx_speed = 0; //applet size int appletsize_top = 10; int appletsize_bottom = 160; int appletsize_left = 10; int appletsize_right = 340; //random movement Random rnd = new Random (); //Drawing image off screen private Image dbImage; private Graphics dbg; Thread thread; boolean running = false; public void init() { enableEvents(AWTEvent.KEY_EVENT_MASK); setFocusable(true); } public void start () { // define a new thread running = true; thread = new Thread (this); // start this thread thread.start (); System.out.println("start"); } public void stop() { running = false; if(thread != null) thread.interrupt(); thread = null; System.out.println("stop"); } public void run () { while (running) { // lower ThreadPriority // Thread.currentThread().setPriority(Thread.MIN_PRIORITY); // change direction if ball will hit any side on the // next advance/stop. The ball position is at its center. if (x_pos - radius + x_speed < appletsize_left || x_pos + radius + x_speed > appletsize_right ) { x_speed *= -1; } if (y_pos - radius + y_speed < appletsize_top) { y_speed *= -1; } if(y_pos + radius + y_speed > appletsize_bottom ) { y_speed *= -1; } //change direction of slider // Moved to processKeyEvent method. //logic for bouncing off slider // We know where the ball and slider are so we // should be able to calculate a possible collision. // Look ahead to the next position of the ball for // the possible collision. // Assumption: consider collisions only on the top // of the slider. if(x_pos + radius + x_speed > sliderx_pos && x_pos - radius + x_speed < sliderx_pos + swidth) { // Ball is in vertical plane of slider. // Check for possible vertical contact. if(y_pos + radius + y_speed > slidery_pos) { // We have an imminent collision -> bounce! y_speed *= -1; } } x_pos += x_speed; y_pos += y_speed; //repaint applet repaint(); try { // Stop thread for the speed Thread.sleep (50); } catch (InterruptedException ex) { running = false; break; } // set ThreadPriority to maximum value // Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } //create an image off screen and loads it into the applet public void update (Graphics g) { // initialize buffer if (dbImage == null) { dbImage = createImage (this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics (); } // clear screen in background dbg.setColor (getBackground ()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); // draw elements in background dbg.setColor (getForeground()); paint (dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); } public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; // set color g.setColor (Color.red); // paint a filled colored circle g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); box.setLocation(sliderx_pos, slidery_pos); g2.setColor (Color.blue); g2.draw(box); } protected void processKeyEvent(KeyEvent e) { int keyCode = e.getKeyCode(); // linke Cursortaste if (keyCode == KeyEvent.VK_LEFT) { // Ball bewegt sich dann nach links if (sliderx_pos -10 > appletsize_left) sliderx_pos = sliderx_pos -10; } // rechte Cursortaste else if (keyCode == KeyEvent.VK_RIGHT) { if (sliderx_pos + swidth +10 < appletsize_right) sliderx_pos = sliderx_pos +10; } } }
Reply With Quote