Initial
This commit is contained in:
commit
19ea61ea96
73
build.xml
Normal file
73
build.xml
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!-- You may freely edit this file. See commented blocks below for -->
|
||||||
|
<!-- some examples of how to customize the build. -->
|
||||||
|
<!-- (If you delete it and reopen the project it will be recreated.) -->
|
||||||
|
<!-- By default, only the Clean and Build commands use this build script. -->
|
||||||
|
<!-- Commands such as Run, Debug, and Test only use this build script if -->
|
||||||
|
<!-- the Compile on Save feature is turned off for the project. -->
|
||||||
|
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
|
||||||
|
<!-- in the project's Project Properties dialog box.-->
|
||||||
|
<project name="GalaxyInPixels" default="default" basedir=".">
|
||||||
|
<description>Builds, tests, and runs the project GalaxyInPixels.</description>
|
||||||
|
<import file="nbproject/build-impl.xml"/>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
There exist several targets which are by default empty and which can be
|
||||||
|
used for execution of your tasks. These targets are usually executed
|
||||||
|
before and after some main targets. They are:
|
||||||
|
|
||||||
|
-pre-init: called before initialization of project properties
|
||||||
|
-post-init: called after initialization of project properties
|
||||||
|
-pre-compile: called before javac compilation
|
||||||
|
-post-compile: called after javac compilation
|
||||||
|
-pre-compile-single: called before javac compilation of single file
|
||||||
|
-post-compile-single: called after javac compilation of single file
|
||||||
|
-pre-compile-test: called before javac compilation of JUnit tests
|
||||||
|
-post-compile-test: called after javac compilation of JUnit tests
|
||||||
|
-pre-compile-test-single: called before javac compilation of single JUnit test
|
||||||
|
-post-compile-test-single: called after javac compilation of single JUunit test
|
||||||
|
-pre-jar: called before JAR building
|
||||||
|
-post-jar: called after JAR building
|
||||||
|
-post-clean: called after cleaning build products
|
||||||
|
|
||||||
|
(Targets beginning with '-' are not intended to be called on their own.)
|
||||||
|
|
||||||
|
Example of inserting an obfuscator after compilation could look like this:
|
||||||
|
|
||||||
|
<target name="-post-compile">
|
||||||
|
<obfuscate>
|
||||||
|
<fileset dir="${build.classes.dir}"/>
|
||||||
|
</obfuscate>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
For list of available properties check the imported
|
||||||
|
nbproject/build-impl.xml file.
|
||||||
|
|
||||||
|
|
||||||
|
Another way to customize the build is by overriding existing main targets.
|
||||||
|
The targets of interest are:
|
||||||
|
|
||||||
|
-init-macrodef-javac: defines macro for javac compilation
|
||||||
|
-init-macrodef-junit: defines macro for junit execution
|
||||||
|
-init-macrodef-debug: defines macro for class debugging
|
||||||
|
-init-macrodef-java: defines macro for class execution
|
||||||
|
-do-jar: JAR building
|
||||||
|
run: execution of project
|
||||||
|
-javadoc-build: Javadoc generation
|
||||||
|
test-report: JUnit report generation
|
||||||
|
|
||||||
|
An example of overriding the target for project execution could look like this:
|
||||||
|
|
||||||
|
<target name="run" depends="GalaxyInPixels-impl.jar">
|
||||||
|
<exec dir="bin" executable="launcher.exe">
|
||||||
|
<arg file="${dist.jar}"/>
|
||||||
|
</exec>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
Notice that the overridden target depends on the jar target and not only on
|
||||||
|
the compile target as the regular run target does. Again, for a list of available
|
||||||
|
properties which you can use, check the target you are overriding in the
|
||||||
|
nbproject/build-impl.xml file.
|
||||||
|
|
||||||
|
-->
|
||||||
|
</project>
|
1420
nbproject/build-impl.xml
Normal file
1420
nbproject/build-impl.xml
Normal file
File diff suppressed because it is too large
Load Diff
8
nbproject/genfiles.properties
Normal file
8
nbproject/genfiles.properties
Normal file
@ -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
|
2
nbproject/private/private.properties
Normal file
2
nbproject/private/private.properties
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
compile.on.save=true
|
||||||
|
user.properties.file=C:\\Users\\aNNiMON\\Documents\\NetBeansProjects\\_NBModules\\OwnLangSupport_Old2\\build\\testuserdir\\build.properties
|
7
nbproject/private/private.xml
Normal file
7
nbproject/private/private.xml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
|
||||||
|
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
|
||||||
|
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
|
||||||
|
<group/>
|
||||||
|
</open-files>
|
||||||
|
</project-private>
|
71
nbproject/project.properties
Normal file
71
nbproject/project.properties
Normal file
@ -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
|
15
nbproject/project.xml
Normal file
15
nbproject/project.xml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||||
|
<type>org.netbeans.modules.java.j2seproject</type>
|
||||||
|
<configuration>
|
||||||
|
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
|
||||||
|
<name>GalaxyInPixels</name>
|
||||||
|
<source-roots>
|
||||||
|
<root id="src.dir"/>
|
||||||
|
</source-roots>
|
||||||
|
<test-roots>
|
||||||
|
<root id="test.src.dir"/>
|
||||||
|
</test-roots>
|
||||||
|
</data>
|
||||||
|
</configuration>
|
||||||
|
</project>
|
56
src/com/annimon/gipgame/Background.java
Normal file
56
src/com/annimon/gipgame/Background.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
94
src/com/annimon/gipgame/BackgroundCreator.java
Normal file
94
src/com/annimon/gipgame/BackgroundCreator.java
Normal file
@ -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<int[]> 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;
|
||||||
|
}
|
||||||
|
}
|
62
src/com/annimon/gipgame/Bonus.java
Normal file
62
src/com/annimon/gipgame/Bonus.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
33
src/com/annimon/gipgame/Fps.java
Normal file
33
src/com/annimon/gipgame/Fps.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
26
src/com/annimon/gipgame/GalaxyInPixels.java
Normal file
26
src/com/annimon/gipgame/GalaxyInPixels.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
112
src/com/annimon/gipgame/GamePanel.java
Normal file
112
src/com/annimon/gipgame/GamePanel.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
118
src/com/annimon/gipgame/PlayerShip.java
Normal file
118
src/com/annimon/gipgame/PlayerShip.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
42
src/com/annimon/gipgame/Util.java
Normal file
42
src/com/annimon/gipgame/Util.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user