This commit is contained in:
Henrik Bygren 2023-09-28 08:22:09 +02:00
commit 6996b5a925
8 changed files with 194 additions and 0 deletions

12
.classpath Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="resources"/>
<classpathentry kind="lib" path="/home/bygren/Dev/work/Programmering/Programmering-2/Graphics/se.egy.graphics.v7.1.2-test.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
/.DS_Store
*target*
*.class
/doc/
/lib/
# eclipse specific git ignore
.DS_Store .
metadata
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.loadpath
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch

17
.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>game-test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=1.8

BIN
resources/playerImg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -0,0 +1,99 @@
package se.egyweb.game;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import se.egy.graphics.*;
public class Game implements KeyListener{
private boolean gameRunning = true;
private int width = 800;
private int height = 600;
private GameScreen gameScreen = new GameScreen("Game", width, height, true); // false vid testkörning
private ImgContainer player;
private HashMap<String, Boolean> keyDown = new HashMap<>();
public Game(){
gameScreen.setKeyListener(this);
keyDown.put("left", false);
keyDown.put("right", false);
keyDown.put("esc", false);
loadImages();
gameLoop();
}
public void loadImages(){
player = new ImgContainer(500, 0, "/playerImg.png");
}
public void update(){
if(keyDown.get("right"))
player.setX(player.getX() + 5);
if(keyDown.get("left")){
player.setX(player.getX() - 12);
}
if(keyDown.get("esc")) {
gameRunning = false;
System.exit(0);
}
if(player.getX()+player.getWidth() > width) {
player.setX(width - player.getWidth());
}
if(player.getX() < 0) {
player.setX(0);
}
}
public void render(){
gameScreen.render(player);
}
public void gameLoop(){
while(gameRunning){
update();
render();
// Fördröjning
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}
/** Spelets tangentbordslyssnare */
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT)
keyDown.put("left", true);
else if(key == KeyEvent.VK_RIGHT)
keyDown.put("right", true);
else if(key == KeyEvent.VK_ESCAPE)
keyDown.put("esc", true);
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT)
keyDown.put("left", false);
else if(key == KeyEvent.VK_RIGHT)
keyDown.put("right", false);
}
public static void main(String[] args) {
new Game();
}
}

View File

@ -0,0 +1,29 @@
package se.egyweb.game;
import se.egy.graphics.*;
public class GameTest {
public static void main(String[] args) {
// Skapar ett spelfönster
GameScreen gameScreen = new GameScreen("Test", 1024, 768, false);
// Skapar och laddar in en bild med en x,y-koordinat.
ImgContainer ic = new ImgContainer(200, 100, "playerImg.png");
for(int i = 0; i < 100; i++) {
gameScreen.render(ic); // renderar bilden
try{ Thread.sleep(20);}catch(Exception e){}; // pausar i 20 ms
// Förflyttar bilden genom att öka x,y - koordinaterna med 5
int newX = ic.getX() + 4;
int newY = ic.getY() + 4;
ic.setX(newX);
ic.setY(newY);
}
//System.exit(0);
}
}