Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
buildscript {


repositories {
mavenLocal()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
google()
}
dependencies {


}
}

allprojects {
apply plugin: "eclipse"
apply plugin: "idea"

version = '1.0'
ext {
appName = "my-gdx-game"
gdxVersion = '1.9.9'
roboVMVersion = '2.3.6'
box2DLightsVersion = '1.4'
ashleyVersion = '1.7.0'
aiVersion = '1.8.0'
}

repositories {
mavenLocal()
mavenCentral()
jcenter()
google()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}

project(":desktop") {
apply plugin: "java"


dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"

}
}

project(":core") {
apply plugin: "java"


dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"

}
}

tasks.eclipse.doLast {
delete ".project"
}
Binary file added core/assets/asteroids64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added core/assets/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added core/assets/bullet32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added core/assets/ship64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added core/assets/star16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apply plugin: "java"

sourceCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

sourceSets.main.java.srcDirs = [ "src/" ]


eclipse.project {
name = appName + "-core"
}
101 changes: 101 additions & 0 deletions core/src/com/mygdx/game/Background.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.mygdx.game;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;

public class Background {

private class Star {
private float x, y;
private float speed;

public Star() {
this.x = MathUtils.random(0, 1280);
this.y = MathUtils.random(0, 720);
this.speed = MathUtils.random(50.0f, 120.0f);
}

public void update(float dt) {
x -= speed * dt;
if (x < -16) {
x = 1280.0f;
y = MathUtils.random(0, 720);
speed = MathUtils.random(50.0f, 120.0f);
}
}
}

private class Asteroid {
private float x, y;
private float speed;

public Asteroid() {
this.x = MathUtils.random(800, 1280);
this.y = MathUtils.random(64, 720);
this.speed = MathUtils.random(100.0f, 200.0f);
}

public void update(float dt) {
x -= speed * dt;
if (x < -32 || collision()) {
x = 1280.0f;
y = MathUtils.random(64, 720);
speed = MathUtils.random(100.0f, 1200.0f);
}
}

public boolean collision(){
if (hero.position.x-32<x+16 && hero.position.x+32>x-16 && hero.position.y-32<y+16 && hero.position.y+32>y-16){
return true;
}
return false;
}
}

private Texture texture;
private Hero hero;
private Texture textureStar;
private Texture textureAsteroid;
private Star[] stars;
private Asteroid[] asteroids;

public Background(Hero hero) {
this.hero = hero;
this.texture = new Texture("bg.png");
this.textureStar = new Texture("star16.png");
this.textureAsteroid = new Texture("asteroids64.png");
this.stars = new Star[400];
this.asteroids = new Asteroid[10];
for (int i = 0; i < stars.length; i++) {
stars[i] = new Star();
}
for (int i = 0; i < asteroids.length; i++) {
asteroids[i] = new Asteroid();
}
}

public void render(SpriteBatch batch) {
batch.draw(texture, 0, 0);
for (int i = 0; i < stars.length; i++) {
float scale = stars[i].speed / 150.0f;
if (MathUtils.random(0, 500) < 2) {
scale *= 1.5f;
}
batch.draw(textureStar, stars[i].x - 8, stars[i].y - 8, 8, 8, 16, 16, scale, scale, 0, 0, 0, 16, 16, false, false);
}
for (int i = 0; i < asteroids.length; i++) {
batch.draw(textureAsteroid, asteroids[i].x - 32, asteroids[i].y - 32, 32, 32, 64, 64, 1, 1, 0.0f, 0, 0, 64, 64, false, false);

}
}

public void update(float dt) {
for (int i = 0; i < stars.length; i++) {
stars[i].update(dt);
}
for (int i = 0; i < asteroids.length; i++) {
asteroids[i].update(dt);
}
}
}
72 changes: 72 additions & 0 deletions core/src/com/mygdx/game/Hero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class Hero {
private Texture texture;
protected Vector2 position;
private float speed;

public Hero() {
texture = new Texture("ship64.png");
position = new Vector2(640.0f, 360.0f);
speed = 300.0f;
}

public void render(SpriteBatch batch) {
batch.draw(texture, position.x - 32.0f, position.y - 32.0f);
}

public void update(float dt) {
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
if (position.x<=1248.0f) {
position.x += speed * dt;
}
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
if (position.x>=32.0f) {
position.x -= speed * dt;
}
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
if (position.y>=720.0f) {
position.y = 0.0f;
}
position.y += speed * dt;
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
if (position.y<=0.0f) {
position.y = 720.0f;
}
position.y -= speed * dt;
}
if (Gdx.input.isTouched()) {
if (Gdx.input.getX() > position.x) {
if (position.x<=1248.0f) {
position.x += speed * dt;
}
}
if (Gdx.input.getX() < position.x) {
if (position.x>=32.0f) {
position.x -= speed * dt;
}
}
if (720.0f - Gdx.input.getY() > position.y) {
if (position.y>=720.0f) {
position.y = 0.0f;
}
position.y += speed * dt;
}
if (720.0f - Gdx.input.getY() < position.y) {
if (position.y<=0.0f) {
position.y = 720.0f;
}
position.y -= speed * dt;
}
}
}
}
52 changes: 52 additions & 0 deletions core/src/com/mygdx/game/MyGdxGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Background background;
Hero hero;

// Разобраться с кодом
// Не давать кораблю вылетать за лвую и правую сторону экрана
// Верхнюю и нижнюю сторону экрана корабль должен пролетать насквозь
// * Добавить астероид, который летает по экрану по типу звезд
// и проверять столкновение этого астероида с кораблем, при столкновении
// "пересоздавать" астероид

// Варианты игры: Гонки, Герои 3, Марио, RTS, tower defence, косм. стрелялка вид (сбоку/сверху)
// worms, battle toads

@Override
public void create() {
batch = new SpriteBatch();
hero = new Hero();
background = new Background(hero);
}

@Override
public void render() {
float dt = Gdx.graphics.getDeltaTime();
update(dt);
Gdx.gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
background.render(batch);
hero.render(batch);
batch.end();
}

public void update(float dt) {
background.update(dt);
hero.update(dt);
}

@Override
public void dispose() {
batch.dispose();
}
}
55 changes: 55 additions & 0 deletions desktop/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
apply plugin: "java"

sourceCompatibility = 1.6
sourceSets.main.java.srcDirs = [ "src/" ]

project.ext.mainClassName = "com.mygdx.game.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../core/assets");

task run(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
}

task debug(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
debug = true
}

task dist(type: Jar) {
from files(sourceSets.main.output.classesDir)
from files(sourceSets.main.output.resourcesDir)
from {configurations.compile.collect {zipTree(it)}}
from files(project.assetsDir);

manifest {
attributes 'Main-Class': project.mainClassName
}
}

dist.dependsOn classes

eclipse {
project {
name = appName + "-desktop"
linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/core/assets'
}
}

task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
doLast {
def classpath = new XmlParser().parse(file(".classpath"))
new Node(classpath, "classpathentry", [ kind: 'src', path: 'assets' ]);
def writer = new FileWriter(file(".classpath"))
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.setPreserveWhitespace(true)
printer.print(classpath)
}
}
14 changes: 14 additions & 0 deletions desktop/src/com/mygdx/game/desktop/DesktopLauncher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mygdx.game.desktop;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.MyGdxGame;

public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = 1280;
config.height = 720;
new LwjglApplication(new MyGdxGame(), config);
}
}
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.gradle.daemon=true
org.gradle.jvmargs=-Xms128m -Xmx1500m
org.gradle.configureondemand=false
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Fri Jun 09 23:06:52 EDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip
Loading