Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- public class SecondControl extends Applet implements Runnable, KeyListener {
- int key;
- boolean gravity = true;
- boolean bounce = true;
- double x;
- double y;
- double xSpeed;
- double ySpeed;
- boolean keyLock = false;
- boolean going = false;
- static int WIDTH = 1000;
- static int HEIGHT = 600;
- static int DELAY = 10;
- static int SIZE = 20;
- static double FRICTION = .01;
- static double SPEED_CHANGE = 2.0;
- static double BOUNCE_DAMPENER = .3;
- static double GRAVITY = .14;
- Thread t;
- Image img;
- Graphics gra;
- public void init() {
- addKeyListener(this);
- img = createImage(WIDTH, HEIGHT);
- gra = img.getGraphics();
- t = new Thread(this);
- t.start();
- going = true;
- }
- public void run() {
- while(true) {
- keyLock = true;
- x+=xSpeed;
- y+=ySpeed;
- //gravity accelerates downwards
- if(gravity) {
- ySpeed+=GRAVITY;
- }
- //friction slowly decelerates the ball
- if(xSpeed < -1* FRICTION){
- xSpeed+=FRICTION;
- } else if(xSpeed > FRICTION) {
- xSpeed-=FRICTION;
- } else {
- xSpeed = 0;
- }
- if(ySpeed < -1* FRICTION){
- ySpeed+=FRICTION;
- } else if(ySpeed > FRICTION) {
- ySpeed-=FRICTION;
- } else {
- ySpeed = 0;
- }
- //wall behavior
- if(x<0) {
- if(bounce) {
- x = 0;
- xSpeed = (float)(-1+BOUNCE_DAMPENER)*xSpeed;
- } else {
- x = WIDTH-SIZE;
- xSpeed = (float)(1-BOUNCE_DAMPENER)*xSpeed;
- }
- } else if(x>(WIDTH-SIZE)) {
- if(bounce) {
- x = WIDTH-SIZE;
- xSpeed = (float)(-1+BOUNCE_DAMPENER)*xSpeed;
- } else {
- x = 0;
- xSpeed = (float)(1-BOUNCE_DAMPENER)*xSpeed;
- }
- }
- if(y<0) {
- if(bounce) {
- y = 0;
- ySpeed = (float)(-1+BOUNCE_DAMPENER)*ySpeed;
- } else {
- y = HEIGHT-SIZE;
- ySpeed = (float)(1-BOUNCE_DAMPENER)*ySpeed;
- }
- } else if(y>(HEIGHT-SIZE)) {
- if(bounce) {
- y = HEIGHT-SIZE;
- ySpeed = (float)(-1+BOUNCE_DAMPENER)*ySpeed;
- } else {
- y = 0;
- ySpeed = (float)(1-BOUNCE_DAMPENER)*ySpeed;
- }
- }
- keyLock = false;
- repaint();
- try {
- t.sleep(DELAY);
- } catch(InterruptedException exc) { ; }
- }
- }
- public void update(Graphics g) {
- paint(g);
- }
- public void paint(Graphics g) {
- gra.setColor(Color.white);
- gra.fillRect(0,0,WIDTH, HEIGHT);
- gra.setColor(Color.blue);
- gra.fillOval((int)Math.floor(x),(int)Math.floor(y),SIZE,SIZE);
- g.drawImage(img, 0, 0, this);
- }
- public void keyPressed(KeyEvent evt) {
- key = evt.getKeyCode();
- if(keyLock == false) {
- switch(key){
- case KeyEvent.VK_DOWN: {
- ySpeed+=SPEED_CHANGE;
- break;
- }
- case KeyEvent.VK_UP: {
- ySpeed-=SPEED_CHANGE;
- break;
- }
- case KeyEvent.VK_RIGHT: {
- xSpeed+=SPEED_CHANGE;
- break;
- }
- case KeyEvent.VK_LEFT: {
- xSpeed-=SPEED_CHANGE;
- break;
- }
- case KeyEvent.VK_G: {
- gravity=!gravity;
- break;
- }
- case KeyEvent.VK_P: {
- if(going) {
- t.suspend();
- going = false;
- } else {
- t.resume();
- going = true;
- }
- break;
- }
- case KeyEvent.VK_B: {
- bounce=!bounce;
- break;
- }
- }
- }
- }
- public void keyReleased(KeyEvent evt) { ; }
- public void keyTyped(KeyEvent evt) { ; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement