Hello!
I’m working on a very basic platform engine. This is my first attempt so please excuse me for sloppy programming pratice and such
I’ve got a working platform engine. A player that moves and a “world” loaded from a file.
My problem is that the movement and physics isn’t that good and i really don’t like how it looks.
I’d like to know what to do with the code to get a smooth movement and physics that works properly!
I’ve implemented collision detecion, which I think works okay but if someone thinks diffrently please explain what I should do to make it better.
If someone helps me out with this it would help my out alot
So time for some code
My Frame class (The main class)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
public class Frame extends JFrame implements Runnable,KeyListener {
private static final long serialVersionUID = 1L;
// SCREEN VARIABLES
int screenWidth =640;
int screenHeight =480;
// world objeccts and variables
Map map;
Block blocks;
// Graphicsh
BufferedImage backBuffer;
ImageEntity background;
Graphics2D g2d;
// animated sprite variable
//AnimatedSprite player;
// Hero variables
AnimatedSprite hero;
Hero player;
int playerX = 320;
int playerY = 100;
int moveX,moveY;
boolean collision = false;
int test = 0;
// world physics variables
long TimeStart,TimeEnd;
// Fps variables
int frameCount = 0;
int frameRate = 0;
long startTime = System.currentTimeMillis();
int desierdRate = 60;
// Threads
Thread gameloop;
public static void main(String[] args) {
new Frame();
}
Frame () {
// Basic GUI STUFF
super ("The Diary");
setSize (screenWidth,screenHeight);
setVisible (true);
setResizable (false);
setDefaultCloseOperation (3);
// Create a backbuffer so the screen wont flicker like hell!
backBuffer = new BufferedImage (screenWidth,screenHeight,BufferedImage.TYPE_INT_RGB);
g2d = backBuffer.createGraphics();
background = new ImageEntity (this);
background.load("bluespace.png");
// Hero
player = new Hero (32,32);
player.showBounds(true);
// player.setGravity(5);
//map
map = new Map("maze.txt");
// skapar och startar min tråd
gameloop = new Thread (this);
gameloop.start();
addKeyListener (this);
}
public void run () {
Thread t = Thread.currentThread();
try {
map.createMap();
} catch (IOException e) {
e.printStackTrace();
}
while (t == gameloop) {
try {
Thread.sleep(1000 / desierdRate);
} catch (InterruptedException e) {
e.printStackTrace();
}
gameUpdate ();
repaint ();
}
}
// Game update here is it where the magics happends
void gameUpdate () {
// räknar ut FPS
log ("FPS :"+ frameRate);
frameCount++;
if (System.currentTimeMillis() > startTime + 1000) {
startTime = System.currentTimeMillis();
frameRate = frameCount;
frameCount = 0;
}
// backgrouunds bilden
g2d.setColor(Color.GRAY);
g2d.fill(new Rectangle (0,0,screenWidth-1,screenHeight-1));
// rita ut alla blocks
for (Block b : map.getBlocks()) {
b.draw(g2d);
}
checkCollision () ;
player.draw(g2d);
player.move();
// Draw debug info
g2d.setColor(Color.WHITE);
g2d.drawString("FPS: "+ frameRate, 50, 50);
g2d.drawString("Player X" + player.positionX, 50,70);
g2d.drawString("Player Y"+ player.positionY, 50, 80);
g2d.drawString ("Collison:" + player.collision,50,100);
g2d.drawString("Falling: " + player.falling, 50, 110);
}
private boolean checkCollision() {
for (int n = 0; n < map.getBlocks().size();n++) {
if (player.getBounds().intersects(map.getBlocks().get(n).getBounds())) {
return player.collision = true;
}
}
return player.collision = false;
}
public void paint (Graphics g) {
//paint the backBuffer
g.drawImage(backBuffer, 0, 0, this);
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
player.setX(-5);
break;
case KeyEvent.VK_RIGHT:
player.setX(5);
break;
case KeyEvent.VK_UP :
player.setGravity(0);
player.setY(-50);
break;
case KeyEvent.VK_SPACE:
player.positionX = 150;
player.positionY = 150;
break;
}
}
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
player.setX(0);
//player.setGravity(5);
break;
case KeyEvent.VK_RIGHT:
player.setX(0);
//player.setGravity(5);
break;
case KeyEvent.VK_UP:
player.setY(0);
player.setGravity(5);
break;
}
}
public void keyTyped(KeyEvent e) {
}
public void log(String s) {
//System.out.println (s);
}
}
My Hero class :
import java.awt.*;
public class Hero {
int positionX,positionY,x,y;
int width,height;
int gravity;
boolean drawBounds;
boolean collision,falling ;
Map m;
// konstruktör
Hero (int width,int height) {
positionX = 150;
positionY = 150;
this.width = width;
this.height = height;
drawBounds = false;
}
// riitar ut objectet
public void draw (Graphics2D g2d) {
g2d.setColor(Color.BLACK);
g2d.fillRect(positionX, positionY, width, height);
if (drawBounds == true) {
g2d.setColor(Color.RED);
g2d.drawRect(positionX + x , positionY +y, width, height);
}
}
// collision detections rectangle som är en aning före obejctet!
public Rectangle getBounds () {
Rectangle r ;
r = new Rectangle (positionX +x ,positionY +y ,width,height);
return r;
}
// en setter som talar om om man vill visa collsionrectangle
public void showBounds(boolean b) {
drawBounds = b;
}
public void setX (int x) {
this.x = x;
}
public void setY (int y) {
this.y = y;
}
public void move () {
if (collision == true) {
setGravity (0);
x = 0;
y = 0;
}
if (y > 0) {
falling = true;
} else {
falling = false;
}
if (x > 0 || x< 0 && collision == false) {
setGravity (5);
}
positionX +=x;
positionY +=y;
gravity();
}
public void setGravity (int gravity){
this.gravity = gravity;
}
private void gravity () {
setY (gravity);
}
}
If needed my block class and world class
import java.awt.*;
import javax.swing.ImageIcon;
public class Block {
int width,height,x,y;
Image BLOCK_WALL;
public Block (int x, int y) {
this.x = x;
this.y = y;
width = 20;
height = 20;
BLOCK_WALL = new ImageIcon ("D:/Programmering/Projekt/Workspace/Framework/src/block_wall.png").getImage();
}
public void draw (Graphics2D g2d) {
g2d.drawImage(BLOCK_WALL, x, y, width,height,null);
g2d.setColor(Color.RED);
g2d.drawRect(x, y, width, height);
}
public Rectangle getBounds() {
Rectangle r;
r = new Rectangle (x,y,width,height);
return r;
}
}
import java.util.*;
import java.io.*;
public class Map {
ArrayList <Block> blocks;
String filePath;
public Map (String path) {
filePath= path;
blocks = new ArrayList<Block>();
}
public void createMap () throws IOException {
ArrayList <String> lines = new ArrayList <String>();
BufferedReader r= new BufferedReader (new FileReader (filePath));
while (true) {
String line = r.readLine();
if (line == null) {
r.close();
break;
}
else {
lines.add(line);
}
}
for (int y = 0; y < lines.size(); y++) {
for (int x = 0; x<lines.get(y).length();x++) {
// laddar in mapppen med x och y värden
char mark = lines.get(y).charAt(x);
if (mark == '#') {
blocks.add(new Block (x*20,(y*20)+25));
}
}
}
}
public ArrayList <Block> getBlocks () {
return blocks;
}
}
I’m new to this fourm so please excuse me if I’ve posted in the wrong forum and for my unbelievable long post x)
Thank in advance!