aboutsummaryrefslogtreecommitdiff
path: root/Занимательное программирование/2/2_vertical_scroll/core
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-12-15 00:36:33 +0100
committerEugen Wissner <belka@caraus.de>2025-12-15 00:36:33 +0100
commit2878f1e34c2c2e19d5b7f6fd368dbf9ec0c6277f (patch)
tree3964cbb7e85992f8f44840d5edacafadf805ba0f /Занимательное программирование/2/2_vertical_scroll/core
parentc1147629f7aae2ee90ccd7c9f1ccbf106361d486 (diff)
downloadbook-exercises-2878f1e34c2c2e19d5b7f6fd368dbf9ec0c6277f.tar.gz
Добавил вторую главу занимательного программирования
Diffstat (limited to 'Занимательное программирование/2/2_vertical_scroll/core')
-rw-r--r--Занимательное программирование/2/2_vertical_scroll/core/build.gradle5
-rw-r--r--Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/DiagonalScreen.java81
-rw-r--r--Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/GameScreen.java59
-rw-r--r--Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/HorizontalScreen.java54
-rw-r--r--Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/MainMenuScreen.java117
-rw-r--r--Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/Modelling.java21
-rw-r--r--Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/VerticalScreen.java54
7 files changed, 391 insertions, 0 deletions
diff --git a/Занимательное программирование/2/2_vertical_scroll/core/build.gradle b/Занимательное программирование/2/2_vertical_scroll/core/build.gradle
new file mode 100644
index 0000000..c2fa637
--- /dev/null
+++ b/Занимательное программирование/2/2_vertical_scroll/core/build.gradle
@@ -0,0 +1,5 @@
+[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
+
+sourceSets.main.java.srcDirs = [ "src/" ]
+
+eclipse.project.name = appName + "-core"
diff --git a/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/DiagonalScreen.java b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/DiagonalScreen.java
new file mode 100644
index 0000000..5828ce0
--- /dev/null
+++ b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/DiagonalScreen.java
@@ -0,0 +1,81 @@
+package net.caraus.modelling;
+
+import java.util.Iterator;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Input.Keys;
+import com.badlogic.gdx.utils.ScreenUtils;
+import com.badlogic.gdx.math.Rectangle;
+
+public class DiagonalScreen extends GameScreen {
+
+ public DiagonalScreen(final Modelling game) {
+ super(game);
+
+ Rectangle nextRectangle = new Rectangle();
+ nextRectangle.x = -SCREEN_WIDTH;
+ nextRectangle.y = 0;
+ backgrounds.add(nextRectangle);
+
+ nextRectangle = new Rectangle();
+ nextRectangle.x = 0;
+ nextRectangle.y = -SCREEN_HEIGHT;
+ backgrounds.add(nextRectangle);
+
+ nextRectangle = new Rectangle();
+ nextRectangle.x = -SCREEN_WIDTH;
+ nextRectangle.y = -SCREEN_HEIGHT;
+ backgrounds.add(nextRectangle);
+
+ nextRectangle = new Rectangle();
+ nextRectangle.x = 0;
+ nextRectangle.y = 0;
+ backgrounds.add(nextRectangle);
+ }
+
+ @Override
+ public void render(float delta) {
+ ScreenUtils.clear(0, 0, 0.2f, 1);
+
+ camera.update();
+ batch.setProjectionMatrix(camera.combined);
+
+ float xStep = SCREEN_WIDTH / 10;
+ float yStep = SCREEN_HEIGHT / 10;
+
+ backgrounds.get(0).x = backgrounds.get(0).x + xStep * delta;
+ backgrounds.get(1).x = backgrounds.get(1).x + xStep * delta;
+ backgrounds.get(2).x = backgrounds.get(2).x + xStep * delta;
+ backgrounds.get(3).x = backgrounds.get(3).x + xStep * delta;
+
+ backgrounds.get(0).y = backgrounds.get(0).y + yStep * delta;
+ backgrounds.get(1).y = backgrounds.get(1).y + yStep * delta;
+ backgrounds.get(2).y = backgrounds.get(2).y + yStep * delta;
+ backgrounds.get(3).y = backgrounds.get(3).y + yStep * delta;
+
+ if (backgrounds.get(0).x >= 0) {
+ for (Iterator<Rectangle> iter = backgrounds.iterator(); iter.hasNext(); ) {
+ Rectangle background = iter.next();
+
+ background.x = background.x - SCREEN_WIDTH;
+ }
+ }
+ if (backgrounds.get(1).y >= 0) {
+ for (Iterator<Rectangle> iter = backgrounds.iterator(); iter.hasNext(); ) {
+ Rectangle background = iter.next();
+
+ background.y = background.y - SCREEN_HEIGHT;
+ }
+ }
+ batch.begin();
+ for (Rectangle background: backgrounds) {
+ batch.draw(backgroundImage, background.x, background.y);
+ }
+ batch.end();
+
+ if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
+ game.setScreen(new MainMenuScreen(game));
+ dispose();
+ }
+ }
+}
diff --git a/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/GameScreen.java b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/GameScreen.java
new file mode 100644
index 0000000..c739459
--- /dev/null
+++ b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/GameScreen.java
@@ -0,0 +1,59 @@
+package net.caraus.modelling;
+
+import com.badlogic.gdx.graphics.OrthographicCamera;
+import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Screen;
+import com.badlogic.gdx.utils.Array;
+import com.badlogic.gdx.math.Rectangle;
+
+public abstract class GameScreen implements Screen {
+ protected final Modelling game;
+
+ protected OrthographicCamera camera;
+ protected SpriteBatch batch;
+ protected Array<Rectangle> backgrounds;
+ protected Texture backgroundImage;
+
+ static final int SCREEN_WIDTH = 609;
+ static final int SCREEN_HEIGHT = 456;
+ static final int STEP = 100;
+
+ public GameScreen(final Modelling game) {
+ this.game = game;
+ backgroundImage = new Texture(Gdx.files.internal("background.bmp"));
+
+ camera = new OrthographicCamera();
+ camera.setToOrtho(false, SCREEN_WIDTH, SCREEN_HEIGHT);
+
+ batch = new SpriteBatch();
+ backgrounds = new Array<Rectangle>();
+ }
+
+ @Override
+ public void resize(int width, int height) {
+ }
+
+ @Override
+ public void show() {
+ }
+
+ @Override
+ public void hide() {
+ }
+
+ @Override
+ public void pause() {
+ }
+
+ @Override
+ public void resume() {
+ }
+
+ @Override
+ public void dispose () {
+ backgroundImage.dispose();
+ batch.dispose();
+ }
+}
diff --git a/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/HorizontalScreen.java b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/HorizontalScreen.java
new file mode 100644
index 0000000..9d35c78
--- /dev/null
+++ b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/HorizontalScreen.java
@@ -0,0 +1,54 @@
+package net.caraus.modelling;
+
+import java.util.Iterator;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Input.Keys;
+import com.badlogic.gdx.utils.ScreenUtils;
+import com.badlogic.gdx.math.Rectangle;
+
+public class HorizontalScreen extends GameScreen {
+
+ public HorizontalScreen(final Modelling game) {
+ super(game);
+
+ Rectangle nextRectangle = new Rectangle();
+ nextRectangle.x = -SCREEN_WIDTH;
+ nextRectangle.y = 0;
+ backgrounds.add(nextRectangle);
+
+ nextRectangle = new Rectangle();
+ nextRectangle.x = 0;
+ nextRectangle.y = 0;
+ backgrounds.add(nextRectangle);
+ }
+
+ @Override
+ public void render(float delta) {
+ ScreenUtils.clear(0, 0, 0.2f, 1);
+
+ camera.update();
+ batch.setProjectionMatrix(camera.combined);
+
+ backgrounds.get(0).x = backgrounds.get(0).x + STEP * Gdx.graphics.getDeltaTime();
+ backgrounds.get(1).x = backgrounds.get(1).x + STEP * Gdx.graphics.getDeltaTime();
+
+ if (backgrounds.get(0).x >= 0) {
+ for (Iterator<Rectangle> iter = backgrounds.iterator(); iter.hasNext(); ) {
+ Rectangle background = iter.next();
+
+ background.x = background.x - SCREEN_WIDTH;
+ }
+ }
+ batch.begin();
+ for (Rectangle background: backgrounds) {
+ batch.draw(backgroundImage, background.x, background.y);
+ }
+ batch.end();
+
+ if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
+ game.setScreen(new MainMenuScreen(game));
+ dispose();
+ }
+ }
+}
diff --git a/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/MainMenuScreen.java b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/MainMenuScreen.java
new file mode 100644
index 0000000..56136c1
--- /dev/null
+++ b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/MainMenuScreen.java
@@ -0,0 +1,117 @@
+package net.caraus.modelling;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Screen;
+import com.badlogic.gdx.graphics.OrthographicCamera;
+import com.badlogic.gdx.scenes.scene2d.Actor;
+import com.badlogic.gdx.scenes.scene2d.Stage;
+import com.badlogic.gdx.scenes.scene2d.ui.Table;
+import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
+import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
+import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
+import com.badlogic.gdx.utils.ScreenUtils;
+import com.badlogic.gdx.graphics.g2d.BitmapFont;
+
+public class MainMenuScreen implements Screen {
+ final Modelling game;
+
+ BitmapFont font;
+ OrthographicCamera camera;
+
+ private TextButton horizontalButton;
+ private TextButton verticalButton;
+ private TextButton crossButton;
+
+ private Stage stage;
+ private Table table;
+
+ public MainMenuScreen(final Modelling game) {
+ this.game = game;
+
+ font = new BitmapFont(); // use libGDX's default Arial font.
+ camera = new OrthographicCamera();
+ camera.setToOrtho(false, 800, 480);
+
+ stage = new Stage();
+ Gdx.input.setInputProcessor(stage);
+
+ table = new Table();
+ table.setFillParent(true);
+ table.setDebug(true);
+ stage.addActor(table);
+
+ createButtons();
+ }
+
+ private void createButtons() {
+ TextButtonStyle style = new TextButtonStyle();
+ style.font = font;
+
+ table.row().width(150);
+
+ horizontalButton = new TextButton("Waagerecht", style);
+ horizontalButton.addListener(new ChangeListener() {
+ public void changed(ChangeEvent event, Actor actor) {
+ game.setScreen(new HorizontalScreen(game));
+ dispose();
+ }
+ });
+ table.add(horizontalButton);
+
+ verticalButton = new TextButton("Senkrecht", style);
+ verticalButton.addListener(new ChangeListener() {
+ public void changed(ChangeEvent event, Actor actor) {
+ game.setScreen(new VerticalScreen(game));
+ dispose();
+ }
+ });
+ table.add(verticalButton);
+
+ crossButton = new TextButton("Diagonal", style);
+ crossButton.addListener(new ChangeListener() {
+ public void changed(ChangeEvent event, Actor actor) {
+ game.setScreen(new DiagonalScreen(game));
+ dispose();
+ }
+ });
+ table.add(crossButton);
+ }
+
+ @Override
+ public void render(float delta) {
+ ScreenUtils.clear(0, 0, 0.2f, 1);
+
+ camera.update();
+ game.batch.setProjectionMatrix(camera.combined);
+
+ stage.act(delta);
+ stage.draw();
+ }
+
+ @Override
+ public void resize(int width, int height) {
+ stage.getViewport().update(width, height, true);
+ }
+
+ @Override
+ public void show() {
+ }
+
+ @Override
+ public void hide() {
+ }
+
+ @Override
+ public void pause() {
+ }
+
+ @Override
+ public void resume() {
+ }
+
+ @Override
+ public void dispose() {
+ stage.dispose();
+ font.dispose();
+ }
+}
diff --git a/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/Modelling.java b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/Modelling.java
new file mode 100644
index 0000000..5949186
--- /dev/null
+++ b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/Modelling.java
@@ -0,0 +1,21 @@
+package net.caraus.modelling;
+
+import com.badlogic.gdx.Game;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+
+public class Modelling extends Game {
+ public SpriteBatch batch;
+
+ public void create() {
+ batch = new SpriteBatch();
+ this.setScreen(new MainMenuScreen(this));
+ }
+
+ public void render() {
+ super.render();
+ }
+
+ public void dispose() {
+ batch.dispose();
+ }
+}
diff --git a/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/VerticalScreen.java b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/VerticalScreen.java
new file mode 100644
index 0000000..2e56977
--- /dev/null
+++ b/Занимательное программирование/2/2_vertical_scroll/core/src/net/caraus/modelling/VerticalScreen.java
@@ -0,0 +1,54 @@
+package net.caraus.modelling;
+
+import java.util.Iterator;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Input.Keys;
+import com.badlogic.gdx.utils.ScreenUtils;
+import com.badlogic.gdx.math.Rectangle;
+
+public class VerticalScreen extends GameScreen {
+
+ public VerticalScreen(final Modelling game) {
+ super(game);
+
+ Rectangle nextRectangle = new Rectangle();
+ nextRectangle.x = 0;
+ nextRectangle.y = -SCREEN_HEIGHT;
+ backgrounds.add(nextRectangle);
+
+ nextRectangle = new Rectangle();
+ nextRectangle.x = 0;
+ nextRectangle.y = 0;
+ backgrounds.add(nextRectangle);
+ }
+
+ @Override
+ public void render(float delta) {
+ ScreenUtils.clear(0, 0, 0.2f, 1);
+
+ camera.update();
+ batch.setProjectionMatrix(camera.combined);
+
+ backgrounds.get(0).y = backgrounds.get(0).y + STEP * Gdx.graphics.getDeltaTime();
+ backgrounds.get(1).y = backgrounds.get(1).y + STEP * Gdx.graphics.getDeltaTime();
+
+ if (backgrounds.get(0).y >= 0) {
+ for (Iterator<Rectangle> iter = backgrounds.iterator(); iter.hasNext(); ) {
+ Rectangle background = iter.next();
+
+ background.y = background.y - SCREEN_HEIGHT;
+ }
+ }
+ batch.begin();
+ for (Rectangle background: backgrounds) {
+ batch.draw(backgroundImage, background.x, background.y);
+ }
+ batch.end();
+
+ if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
+ game.setScreen(new MainMenuScreen(game));
+ dispose();
+ }
+ }
+}