Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualburnt_casadilla

Posted 12 August 2012 - 06:06 PM

i added the xpos ypos xspeed and yspeed parameters to the mainBall and changed the variables in the keyDown and run method but it still did nothing

[source lang="java"]import java.applet.Applet;import java.awt.Color;import java.awt.Component;import java.awt.Event;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.util.ArrayList;import java.util.concurrent.TimeUnit;public class game extends Applet implements Runnable{ static final int WIDTH = 450; static final int HEIGHT = 450; private Image dbImage; private Graphics dbg; public static long NEW_DOT_FREQ = TimeUnit.SECONDS.toMillis(3); public long lastUpdateTime; public long timeSinceLastNewDot; public ArrayList<Ball> BALLS; Color[] color = {Color.red, Color.blue, Color.green, Color.yellow, Color.magenta, Color.black}; int colorIndex; static final int NUM_OF_BALLS = 4; int i; int t; MainBall mainBall = new MainBall(100, 100, 10, 10, 100, 100, 0, 0); Thread updateTime = new updateTime(); public void start() { lastUpdateTime = System.currentTimeMillis(); Thread th = new Thread(this); th.start();//start main game updateTime.start(); } public void updateGame() { //Get the current time long currentTime = System.currentTimeMillis(); //Calculate how much time has passed since the last update long elapsedTime = currentTime - lastUpdateTime; //Store this as the most recent update time lastUpdateTime = currentTime; //Create a new dot if enough time has passed //Update the time since last new dot was drawn timeSinceLastNewDot += elapsedTime; if (timeSinceLastNewDot >= NEW_DOT_FREQ) { int newX = randomNumber(); int newY = randomNumber(); debugPrint("New dot created at x:" + newX + ", y:" + newY + "."); BALLS.add(new Ball(newX, newY, 20, 20)); timeSinceLastNewDot = 0; } } private void debugPrint(String value) { System.out.println(value); } public class updateTime extends Thread implements Runnable { public void run() { for(t = 0; ; t++) { try { Thread.sleep(1000); } catch(InterruptedException e){} } } } public int randomNumber() { return (int)(Math.random() * 400); } public int getRandomColor() { return (int)(Math.random() * 6); } public class MainBall { int x; int y; int width; int height; int xpos = 100; int ypos = 100; int xspeed = 0; int yspeed = 0; public MainBall(int x, int y, int width, int height, int xpos, int ypos, int xspeed, int yspeed) { this.x = 100; this.y = 100; this.width = 10; this.height = 10; this.xpos = 100; this.ypos = 100; this.xspeed = 0; this.yspeed = 0; } public void paintMainBall(Graphics g) { g.setColor(Color.black); g.fillOval(x, y, width, height); g.drawString(xpos + ", " + ypos, 20, 40); } }//mainBall class Ball { int x; int y; int width; int height; public Ball(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; }//end ball public void paint(Graphics g) { g.setColor(color[getRandomColor()]); g.fillOval(x, y, width, height); } //end paint } //ball class public void update(Graphics g) //double buffer don't touch!! { 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); } public boolean keyDown (Event e, int key) { if(key == Event.LEFT) { mainBall.xspeed = -5; mainBall.yspeed = 0; } if(key == Event.RIGHT) { mainBall.xspeed = 5; mainBall.yspeed = 0; } if(key == Event.UP) { mainBall.yspeed = -5; mainBall.xspeed = 0; } if(key == Event.DOWN) { mainBall.yspeed = 5; mainBall.xspeed = 0; } return true; } public void run() { while(true) { repaint(); if (mainBall.xpos < 1) { mainBall.xpos = 449; } if (mainBall.xpos > 449) { mainBall.xpos = 1; } if (mainBall.ypos < 1) { mainBall.ypos = 449; } if (mainBall.ypos > 449) { mainBall.ypos = 1; } mainBall.ypos += mainBall.yspeed; mainBall.xpos += mainBall.xspeed; try { Thread.sleep(20); } catch(InterruptedException ex){} } } public void init() { this.setSize(WIDTH, HEIGHT); BALLS = new ArrayList<Ball>(); } public void paint(Graphics g) { g.drawString("time: " + t, 20, 20); mainBall.paintMainBall(g); for (Ball ball : BALLS) { ball.paint(g); } updateGame(); }}[/source]

#1burnt_casadilla

Posted 12 August 2012 - 05:50 PM

i added the xpos ypos xspeed and yspeed parameters to the mainBall and changed the variables in the keyDown and run method but it still did nothing

PARTNERS