commit 19ea61ea967f9dd42bc3fabde6ad275d421e12b4 Author: Victor Date: Thu Nov 15 17:12:38 2018 +0200 Initial diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..0aa3114 --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project GalaxyInPixels. + + + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..21b1da1 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1420 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..0d33bfa --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=869151fb +build.xml.script.CRC32=8b5b96c4 +build.xml.stylesheet.CRC32=8064a381@1.63.0.46 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=869151fb +nbproject/build-impl.xml.script.CRC32=38f21b9e +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties new file mode 100644 index 0000000..6138315 --- /dev/null +++ b/nbproject/private/private.properties @@ -0,0 +1,2 @@ +compile.on.save=true +user.properties.file=C:\\Users\\aNNiMON\\Documents\\NetBeansProjects\\_NBModules\\OwnLangSupport_Old2\\build\\testuserdir\\build.properties diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml new file mode 100644 index 0000000..6807a2b --- /dev/null +++ b/nbproject/private/private.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..295a404 --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,71 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/GalaxyInPixels.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=com.annimon.gipgame.GalaxyInPixels +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=windows-1251 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..b20d43c --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + GalaxyInPixels + + + + + + + + + diff --git a/src/com/annimon/gipgame/Background.java b/src/com/annimon/gipgame/Background.java new file mode 100644 index 0000000..e4d3b5e --- /dev/null +++ b/src/com/annimon/gipgame/Background.java @@ -0,0 +1,56 @@ +package com.annimon.gipgame; + +import java.awt.Color; +import java.awt.Graphics; + +public class Background { + + private int yBackground, squareSize; + private final BackgroundCreator creator; + + public Background() { + yBackground = 0; + squareSize = 20; + creator = new BackgroundCreator(); + } + + public void draw(Graphics g) { + final int[][] array = creator.getBackground(); + final int rowWidth = array[0].length; + final int width = g.getClipBounds().width; + final int height = g.getClipBounds().height; + squareSize = width / rowWidth + 1; + creator.setHeight(height / squareSize + 4); + + // Fill rectangles. + for(int y = -1; y < array.length - 2; y++) { + int[] row = array[2 + y]; + + int top = y * squareSize + yBackground; + if (top > height) break; + + for (int x = 0; x < rowWidth; x++) { + int left = x * squareSize; + + g.setColor(new Color(row[x])); + g.fillRect(left, top, squareSize, squareSize); + } + + } + } + + public void update(int addValue) { + if (addValue > 200) { + // When game is finished, speed up background. + yBackground += 2; + addValue /= 2; + } + yBackground++; + + // Remove last and add new row in background. + if (yBackground >= squareSize) { + creator.updateBackground(addValue); + yBackground = 0; + } + } +} diff --git a/src/com/annimon/gipgame/BackgroundCreator.java b/src/com/annimon/gipgame/BackgroundCreator.java new file mode 100644 index 0000000..3e4537c --- /dev/null +++ b/src/com/annimon/gipgame/BackgroundCreator.java @@ -0,0 +1,94 @@ +package com.annimon.gipgame; + +import java.util.ArrayList; +import java.util.List; + + +public class BackgroundCreator { + + private static final boolean ANTI_ALIAS = true; + + private static final int WIDTH = 5; + private int height = 12; + + private final List background; + + public BackgroundCreator() { + background = new ArrayList<>(); + + createBackground(); + } + + public void setHeight(int height) { + this.height = height; + } + + public int[][] getBackground() { + int[][] array = background.toArray(new int[0][]); + if (!ANTI_ALIAS) return array; + + // Smooth background colors. + int[][] antialias = new int[height][WIDTH]; + for(int y = 0; y < height; y++) { + for (int x = 0; x < WIDTH; x++) { + int top = (y > 0) ? array[y - 1][x] : 0; + int bottom = (y < (height - 1)) ? array[y + 1][x] : 0; + int left = (x > 0) ? array[y][x - 1] : 0; + int right = (x < (WIDTH - 1)) ? array[y][x + 1] : 0; + int center = array[y][x]; + + antialias[y][x] = getAntiAliasColor(new int[] {top, bottom, left, right, center}); + } + } + return antialias; + } + + private int getAntiAliasColor(int[] colors) { + int red = 0; + int green = 0; + int blue = 0; + + for (int i = 0; i < 4; i++) { + int color = colors[i]; + red += (color >> 16) & 0xFF; + green += (color >> 8) & 0xFF; + blue += color & 0xFF; + } + + red /= 4; + green /= 4; + blue /= 4; + + red += (colors[4] >> 16) & 0xFF; + green += (colors[4] >> 8) & 0xFF; + blue += colors[4] & 0xFF; + + return 0xFF000000 | (red << 16) | (green << 8) | blue; + } + + public void updateBackground(int addValue) { + background.remove(height - 1); + background.add(0, createRow(addValue)); + } + + + private void createBackground() { + for (int i = 0; i < height; i++) { + background.add(createRow(0)); + } + } + + private int[] createRow(int addValue) { + int[] row = new int[WIDTH]; + for (int i = 0; i < WIDTH; i++) { + row[i] = Util.randomSpaceColor(15 + (addValue / 2)); + } + if (Util.rand(8) == 5) { + // Create star. + int x = Util.rand(WIDTH); + int add = (addValue / 2); + row[x] = Util.randomColor(10 + add, 60 + add); + } + return row; + } +} diff --git a/src/com/annimon/gipgame/Bonus.java b/src/com/annimon/gipgame/Bonus.java new file mode 100644 index 0000000..dd904d0 --- /dev/null +++ b/src/com/annimon/gipgame/Bonus.java @@ -0,0 +1,62 @@ +package com.annimon.gipgame; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.geom.Rectangle2D; + +public class Bonus { + + private static int BONUS_SIZE = 16; + private static int HORIZ_RANGE = 320; + private static int HEIGHT = 480; + + public static void initBonusSize(int width, int height) { + HEIGHT = height; + + BONUS_SIZE = width / 30; + HORIZ_RANGE = width - BONUS_SIZE; + } + + + private int color; + private float speed; + private Rectangle2D.Float bonusRect; + + public Bonus() { + + bonusRect = new Rectangle2D.Float(0, 0, BONUS_SIZE, BONUS_SIZE); + initNewPosition(); + } + + public void draw(Graphics g) { + g.setColor(new Color(color, true)); + g.fillRect((int) bonusRect.x, (int) bonusRect.y, + (int) bonusRect.width, (int) bonusRect.height); + } + + public void update(PlayerShip player) { + bonusRect.y += speed; + if (bonusRect.y > HEIGHT) { + initNewPosition(); + player.changeScore(-1); + } else { + if (Util.rand(50) == 5) changeSpeed(); + } + if (player.isCollide(bonusRect)) { + initNewPosition(); + player.changeScore(1); + } + } + + private void initNewPosition() { + color = Util.randomColor(128, 255) | 0xA0000000; + changeSpeed(); + + bonusRect.x = Util.rand(HORIZ_RANGE); + bonusRect.y = - BONUS_SIZE - Util.rand(HEIGHT); + } + + private void changeSpeed() { + speed = Util.rand(0.2f, 4f); + } +} diff --git a/src/com/annimon/gipgame/Fps.java b/src/com/annimon/gipgame/Fps.java new file mode 100644 index 0000000..9b0d140 --- /dev/null +++ b/src/com/annimon/gipgame/Fps.java @@ -0,0 +1,33 @@ +package com.annimon.gipgame; + +public class Fps { + + private static final int MAX_FPS = 30; + private static final int MAX_DELAY = 1000 / MAX_FPS; + + private static long currentFps; + private static long counter = 0, startTime = 0; + private static long startTimeForMeasureDelay = 0; + + public static String getFpsAsString() { + counter++; + if (startTime == 0) { + startTime = System.currentTimeMillis(); + } + if ( (System.currentTimeMillis() - startTime) >= 1000) { + currentFps = counter; + counter = 0; + startTime = System.currentTimeMillis(); + } + return Long.toString(currentFps); + } + + public static void startMeasuringDelay() { + startTimeForMeasureDelay = System.currentTimeMillis(); + } + + public static long getDelay() { + long delay = System.currentTimeMillis() - startTimeForMeasureDelay; + return (delay > MAX_DELAY ? 0 : MAX_DELAY - delay); + } +} diff --git a/src/com/annimon/gipgame/GalaxyInPixels.java b/src/com/annimon/gipgame/GalaxyInPixels.java new file mode 100644 index 0000000..ee04294 --- /dev/null +++ b/src/com/annimon/gipgame/GalaxyInPixels.java @@ -0,0 +1,26 @@ +package com.annimon.gipgame; + +import java.awt.BorderLayout; +import javax.swing.JFrame; +import static javax.swing.JFrame.EXIT_ON_CLOSE; + +public class GalaxyInPixels extends JFrame { + + public static void main(String[] args) { + new GalaxyInPixels().setVisible(true); + } + + public GalaxyInPixels() { + super("Galaxy In Pixels"); + setResizable(false); + setUndecorated(true); + setAlwaysOnTop(true); + setBounds(0, 0, 0, 0); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setLayout(new BorderLayout(0, 0)); + + add(new GamePanel()); + pack(); + } + +} diff --git a/src/com/annimon/gipgame/GamePanel.java b/src/com/annimon/gipgame/GamePanel.java new file mode 100644 index 0000000..5211a31 --- /dev/null +++ b/src/com/annimon/gipgame/GamePanel.java @@ -0,0 +1,112 @@ +package com.annimon.gipgame; + +import java.awt.AWTEvent; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Toolkit; +import java.awt.event.KeyEvent; +import javax.swing.JPanel; + +public class GamePanel extends JPanel implements Runnable { + + private static final int BONUS_COUNT = 10; + + private Background background; + private PlayerShip player; + private Bonus[] bonus; + + private Thread thread; + + private int xStep; + private int width, height; + + public GamePanel() { + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + width = (int) (screenSize.getWidth() * 300 / 683); + height = (int) screenSize.getHeight(); + setPreferredSize( new Dimension(width, height) ); + setFocusable(true); + enableEvents(AWTEvent.KEY_EVENT_MASK); + + initView(); + } + + private void initView() { + background = new Background(); + player = new PlayerShip(); + player.init(width, height); + + Bonus.initBonusSize(width, height); + bonus = new Bonus[BONUS_COUNT]; + for (int i = 0; i < BONUS_COUNT; i++) { + bonus[i] = new Bonus(); + } + + xStep = 0; + + thread = new Thread(this); + thread.start(); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.setColor(Color.BLACK); + g.fillRect(0, 0, width, height); + + background.draw(g); + player.draw(g); + if (!player.gameIsFinished()) { + for (int i = 0; i < BONUS_COUNT; i++) { + bonus[i].draw(g); + } + } + } + + @Override + public void processKeyEvent(KeyEvent event) { + final int key = event.getKeyCode(); + if (event.getID() == KeyEvent.KEY_PRESSED) { + final int MOVE_STEP = 5; + if (key == KeyEvent.VK_LEFT) { + xStep = MOVE_STEP; + } else if (key == KeyEvent.VK_RIGHT) { + xStep = -MOVE_STEP; + } else if (key == KeyEvent.VK_ESCAPE) { + System.exit(0); + } + if (event.isShiftDown()) xStep *= 3; + } else if (event.getID() == KeyEvent.KEY_RELEASED) { + if ( (key == KeyEvent.VK_LEFT) || (key == KeyEvent.VK_RIGHT) ) { + xStep = 0; + } + } + } + + private void update() { + player.updateX(xStep); + background.update(player.getScore()); + + if (player.gameIsFinished()) return; + + for (int i = 0; i < BONUS_COUNT; i++) { + bonus[i].update(player); + } + } + + @Override + public void run() { + while(Thread.currentThread() == thread) { + Fps.startMeasuringDelay(); + update(); + repaint(); + try { + Thread.sleep(Fps.getDelay()); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + } + +} diff --git a/src/com/annimon/gipgame/PlayerShip.java b/src/com/annimon/gipgame/PlayerShip.java new file mode 100644 index 0000000..8404b34 --- /dev/null +++ b/src/com/annimon/gipgame/PlayerShip.java @@ -0,0 +1,118 @@ +package com.annimon.gipgame; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.geom.AffineTransform; +import java.awt.geom.Rectangle2D; +import java.awt.image.AffineTransformOp; +import java.awt.image.BufferedImage; + + + +public class PlayerShip { + + private static final int WIDTH = 5, HEIGHT = 4; + private static final Color[] SHIP_COLORS = { + Color.WHITE, + Color.GREEN, + Color.BLUE, + Color.CYAN, + Color.YELLOW, + Color.DARK_GRAY, + Color.MAGENTA, + Color.RED, + Color.BLACK, + new Color(0x00, true) + }; + + private int SHIP_WIDTH, SHIP_HEIGHT; + private int TEXT_SIZE; + + private int x, y; + private int scrWidth, scrHeight; + private int score; + private BufferedImage shipBitmap; + + public PlayerShip() { + score = 0; + } + + public void init(int w, int h) { + scrWidth = w; + scrHeight = h; + + SHIP_WIDTH = w / 16; + SHIP_HEIGHT = h / 20; + TEXT_SIZE = 12; + + createPlayerShip(SHIP_COLORS[0].getRGB()); + + x = (w - SHIP_WIDTH) / 2; + y = h - SHIP_HEIGHT * 2; + } + + public int getScore() { + return score; + } + + public boolean gameIsFinished() { + return ( score > (SHIP_COLORS.length * 20) ); + } + + public void draw(Graphics g) { + // When game is finished we don't need to draw ship. + if (gameIsFinished()) { + drawText(g, "Game is finished", true); + return; + } + // Draw ship. + g.drawImage(shipBitmap, x, y, null); + // Draw score. + drawText(g, String.valueOf(score), false); + } + + public void updateX(int value) { + x -= value; + + if (x < 0) x = 0; + else if (x > ( scrWidth - SHIP_WIDTH )) { + x = scrWidth - SHIP_WIDTH; + } + } + + public boolean isCollide(Rectangle2D.Float rectangle) { + return (rectangle.intersects(x, y, SHIP_WIDTH, SHIP_HEIGHT)); + } + + public void changeScore(int value) { + if (gameIsFinished()) return; + + score += value; + if (score < 0) score = 0; + else if (score % 20 == 0) { + // Change color of the ship by score level. + int shipLevel = Math.min(score / 20, SHIP_COLORS.length - 1); + createPlayerShip(SHIP_COLORS[shipLevel].getRGB()); + } + } + + private void createPlayerShip(int color) { + //color |= 0xFF000000; + BufferedImage bmp = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + bmp.setRGB(2, 0, color); + bmp.setRGB(1, 1, color); bmp.setRGB(3, 1, color); + bmp.setRGB(1, 2, color); bmp.setRGB(3, 2, color); + bmp.setRGB(0, 3, color); bmp.setRGB(4, 3, color); + // Stretch bitmap to calculated size. + shipBitmap = new BufferedImage(SHIP_WIDTH, SHIP_HEIGHT, BufferedImage.TYPE_INT_ARGB); + AffineTransform at = new AffineTransform(); + at.scale(SHIP_WIDTH / (float) WIDTH, SHIP_HEIGHT / (float) HEIGHT); + AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); + shipBitmap = scaleOp.filter(bmp, shipBitmap); + } + + private void drawText(Graphics g, String text, boolean vertCenter) { + g.setColor(Color.YELLOW); + g.drawString(text, scrWidth / 2 - (text.length()), 10); + } +} diff --git a/src/com/annimon/gipgame/Util.java b/src/com/annimon/gipgame/Util.java new file mode 100644 index 0000000..ceb0679 --- /dev/null +++ b/src/com/annimon/gipgame/Util.java @@ -0,0 +1,42 @@ +package com.annimon.gipgame; + +import java.util.Random; + +public class Util { + + private static Random rnd = new Random(); + + public static int rand(int to) { + return rnd.nextInt(to); + } + + public static int rand(int from, int to) { + return rnd.nextInt(to - from) + from; + } + + public static float rand(float from, float to) { + return rnd.nextFloat() * (to - from) + from; + } + + public static int randomColor(int from, int to) { + if (from > 230) from = 230; + if (to > 255) to = 255; + + int red = rand(from, to); + int green = rand(from, to); + int blue = rand(from, to); + + return 0xFF000000 | (red << 16) | (green << 8) | blue; + } + + public static int randomSpaceColor(int to) { + if (to > 240) to = 240; + + int red = rand(to / 2); + int green = rand(red / 2, to / 2); + int blue = rand(red, to); + + return 0xFF000000 | (red << 16) | (green << 8) | blue; + } + +}