From 2596b913fc5ee50e20bc94f0b8bfe4d95372cb81 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Mon, 11 May 2020 12:17:05 +0200 Subject: [PATCH] added new thread for client connection and added resize method --- core/src/netwerkprog/game/MainGame.java | 35 +++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/core/src/netwerkprog/game/MainGame.java b/core/src/netwerkprog/game/MainGame.java index bbe8398..5a215bd 100644 --- a/core/src/netwerkprog/game/MainGame.java +++ b/core/src/netwerkprog/game/MainGame.java @@ -19,7 +19,10 @@ public class MainGame extends ApplicationAdapter { float yPos = 500; float xUpdate; float yUpdate; + float screenWidth; + float screenHeight; private FrameRate frameRate; + private Client client; @Override public void create() { @@ -28,6 +31,8 @@ public class MainGame extends ApplicationAdapter { float ratio = (float) Gdx.graphics.getWidth() / Gdx.graphics.getHeight(); xUpdate = ratio; yUpdate = ratio; + screenWidth = Gdx.graphics.getWidth(); + screenHeight = Gdx.graphics.getHeight(); frameRate = new FrameRate(); // play music @@ -40,14 +45,21 @@ public class MainGame extends ApplicationAdapter { } private void connectToServer() { - Client client = new Client("localhost", Server.PORT); - client.start(); + client = new Client("localhost", Server.PORT); + try { + client.start(); + } catch (Exception e) { + System.out.println("There was an error connecting : " + e.getMessage()); + } } + /** + * render method that is called after the update method + */ @Override public void render() { update(); - Gdx.gl.glClearColor(xPos/Gdx.graphics.getWidth(), 0, 0, 1); + Gdx.gl.glClearColor(xPos / Gdx.graphics.getWidth(), 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); batch.draw(img, xPos, yPos); @@ -66,15 +78,28 @@ public class MainGame extends ApplicationAdapter { private void updatePos() { yPos += yUpdate; xPos += xUpdate; - if (yPos > Gdx.graphics.getHeight() - img.getHeight() || yPos < 0) { + + if (yPos > screenHeight - img.getHeight() || yPos < 0) { yUpdate = -yUpdate; } - if (xPos > Gdx.graphics.getWidth() - img.getWidth() || xPos < 0) { + if (xPos > screenWidth - img.getWidth() || xPos < 0) { xUpdate = -xUpdate; } } + @Override + public void resize(int width, int height) { + super.resize(width, height); + screenHeight = height; + screenWidth = width; + } + + @Override + public void resume() { + super.resume(); + } + @Override public void dispose() { batch.dispose();