Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualj-locke

Posted 11 August 2012 - 11:18 PM

Alright, sorry but I needed to support my gaming habit for a while. I just put this together and I did confirm that it runs. If you have any questions about why I did something, please do feel free to ask because I can't/won't say that everything is the best approach to a problem. I put it together to show you things being done based on time but not centered around running several threads within my program.

[source lang="java"]import java.applet.Applet;import java.awt.*;import java.awt.geom.Ellipse2D;import java.util.ArrayList;import java.util.concurrent.TimeUnit;public class GameWithTime extends Applet implements Runnable{ //Sets NEW_DOT_FREQUENCY as 3 seconds private static long NEW_DOT_FREQUENCY = TimeUnit.SECONDS.toMillis(3); private static final int SCREEN_WIDTH = 400; private static final int SCREEN_HEIGHT = 400; //Stores what the time was when updateGame last ran private long lastUpdateTime = 0; //Tracks how much time has passed since the last new dot was added private long timeSinceLastNewDot = 0; //List of all the enemy dots private ArrayList<Dot> enemyDots; private Thread animator; private boolean isRunning; //Initialize things for the game public void init() { debugPrint("Initializing values."); //Put 4 dots in the list initially enemyDots = new ArrayList<Dot>(); enemyDots.add(new Dot(randomValue(), randomValue(), 10, 10)); enemyDots.add(new Dot(randomValue(), randomValue(), 10, 20)); enemyDots.add(new Dot(randomValue(), randomValue(), 20, 10)); enemyDots.add(new Dot(randomValue(), randomValue(), 20, 20)); } public void start() { lastUpdateTime = System.currentTimeMillis(); isRunning = true; animator = new Thread(this); animator.start(); } public void stop() { isRunning = false; } //Applet is closing, clean up public void destroy() { } public void run() { while (isRunning) { repaint(); try { Thread.sleep(15); } catch (InterruptedException e) { } } } //This update and paint approach is an alternative to double buffering to avoid flickering public void update(Graphics g) { //Update variables updateGame(); //Draw things to the screen //Clear the screen g.setColor(Color.BLACK); g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); g.setColor(Color.cyan); //Draw each dot in the list for (Dot dot : enemyDots) { dot.draw(g); } } //This update and paint approach is an alternative to double buffering to avoid flickering public void paint(Graphics g) { update(g); } private 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; //Process inputs updateVariablesBasedOnUserInput(); //Create a new dot if enough time has passed //Update the time since last new dot was drawn timeSinceLastNewDot += elapsedTime; //See if enough time has passed to create a new dot debugPrint(Long.toString(timeSinceLastNewDot)); if (timeSinceLastNewDot >= NEW_DOT_FREQUENCY) { //Generate random x and y values for new dot int newX = randomValue(); int newY = randomValue(); //Add a new dot to the collection of dots debugPrint("New dot created at x:" + newX + ", y:" + newY + "."); enemyDots.add(new Dot(newX, newY, 20, 20)); //Reset time since a new dot was created timeSinceLastNewDot = 0; } } private void updateVariablesBasedOnUserInput() { //Set speeds/velocities based on user inputs } //Returns a new random value each time a call is made private int randomValue() { return (int) (Math.random() * 400); } //Prints passed value to the console private void debugPrint(String value) { System.out.println(value); } private class Dot extends Ellipse2D.Float { public Dot(int xValue, int yValue, int width, int height) { this.x = xValue; this.y = yValue; this.width = width; this.height = height; } public void draw(Graphics graphics) { graphics.drawOval((int) x, (int) y, (int) width, (int) height); } }}[/source]

#1j-locke

Posted 11 August 2012 - 11:06 PM

Alright, sorry but I needed to support my gaming habit for a while. I just put this together and I did confirm that it runs. If you have any questions about why I did something, please do feel free to ask because I can't/won't say that everything is the best approach to a problem. I put it together to show you things being done based on time but not centered around running several threads within my program.

[source lang="java"]import java.applet.Applet;import java.awt.*;import java.awt.geom.Ellipse2D;import java.util.ArrayList;import java.util.concurrent.TimeUnit;public class GameWithTime extends Applet implements Runnable{ //Sets NEW_DOT_FREQUENCY as 3 seconds private static long NEW_DOT_FREQUENCY = TimeUnit.SECONDS.toMillis(3); private static final int SCREEN_WIDTH = 400; private static final int SCREEN_HEIGHT = 400; //Stores what the time was when updateGame last ran private long lastUpdateTime = 0; //Tracks how much time has passed since the last new dot was added private long timeSinceLastNewDot = 0; //List of all the enemy dots private ArrayList<Dot> enemyDots; private Thread animator; private boolean isRunning; //Initialize things for the game public void init() { debugPrint("Initializing values."); //Put 3 dots in the list initially enemyDots = new ArrayList<Dot>(3); enemyDots.add(new Dot(randomValue(), randomValue(), 10, 10)); enemyDots.add(new Dot(randomValue(), randomValue(), 10, 20)); enemyDots.add(new Dot(randomValue(), randomValue(), 20, 10)); enemyDots.add(new Dot(randomValue(), randomValue(), 20, 20)); } public void start() { lastUpdateTime = System.currentTimeMillis(); isRunning = true; animator = new Thread(this); animator.start(); } public void stop() { isRunning = false; } //Applet is closing, clean up public void destroy() { } public void run() { while (isRunning) { repaint(); try { Thread.sleep(15); } catch (InterruptedException e) { } } } //This update and paint approach is an alternative to double buffering to avoid flickering public void update(Graphics g) { //Update variables updateGame(); //Draw things to the screen //Clear the screen g.setColor(Color.BLACK); g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); g.setColor(Color.cyan); //Draw each dot in the list for (Dot dot : enemyDots) { dot.draw(g); } } //This update and paint approach is an alternative to double buffering to avoid flickering public void paint(Graphics g) { update(g); } private 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; //Process inputs updateVariablesBasedOnUserInput(); //Create a new dot if enough time has passed //Update the time since last new dot was drawn timeSinceLastNewDot += elapsedTime; //See if enough time has passed to create a new dot debugPrint(Long.toString(timeSinceLastNewDot)); if (timeSinceLastNewDot >= NEW_DOT_FREQUENCY) { //Generate random x and y values for new dot int newX = randomValue(); int newY = randomValue(); //Add a new dot to the collection of dots debugPrint("New dot created at x:" + newX + ", y:" + newY + "."); enemyDots.add(new Dot(newX, newY, 20, 20)); //Reset time since a new dot was created timeSinceLastNewDot = 0; } } private void updateVariablesBasedOnUserInput() { //Set speeds/velocities based on user inputs } //Returns a new random value each time a call is made private int randomValue() { return (int) (Math.random() * 400); } //Prints passed value to the console private void debugPrint(String value) { System.out.println(value); } private class Dot extends Ellipse2D.Float { public Dot(int xValue, int yValue, int width, int height) { this.x = xValue; this.y = yValue; this.width = width; this.height = height; } public void draw(Graphics graphics) { graphics.drawOval((int) x, (int) y, (int) width, (int) height); } }}[/source]

PARTNERS