# Am I doing this right?

Printable View

• 02-29-2012, 07:16 AM
stuffses
Am I doing this right?
I've started a simple 2D shooter using swing, and I wanted to see if I was doing it efficiently and simply, before I start using the same methods all over my game.
I will only post the Player class, because it is were most of the action happens.

Here is the Player class:
Code:

```package main; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.geom.AffineTransform; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Timer; public class Player {         int x;         int y;         double angle; //in radians         Player(int ix, int iy) {                 startTimer();                 x = ix;                 y = iy;         }         // called every frame         void advance() {                 //move player                 movement();         }         void movement() {                 // distance from mouse to player                 double distX = Main.myListener.getX() - x;                 double distY = Main.myListener.getY() - y;                 //only run if player is far from mouse                 if (distX > 5 || distX < -5) {                         x += distX * 0.08;                 }                 if (distY > 5 || distY < -5) {                         y += distY * 0.08;                 }                                 //calculate angle                 angle = Math.atan2(Main.myListener.getY() - y, Main.myListener.getX() - x);         }         // called every frame         public void draw(Graphics g) {                 // create a G2D version of g                 Graphics2D g2d = (Graphics2D) g;                                 //antialiasing                 g2d.addRenderingHints(new RenderingHints(                                 RenderingHints.KEY_ANTIALIASING,                                 RenderingHints.VALUE_ANTIALIAS_ON));                 // save the current transforms                 AffineTransform saveXform = g2d.getTransform();                 // holds transforms                 AffineTransform at = new AffineTransform();                 // set transforms here                 at.translate((int) x, (int) y);                 at.rotate(angle);                 // apply the transforms to the graphics object                 g2d.transform(at);                 // draw player                 g2d.fillRect(-20, -20, 40, 40);                 g2d.fillRect(15, -7, 15, 14);                 // reset the transforms                 g2d.setTransform(saveXform);         }         // initialize the timer         @SuppressWarnings("serial")         void startTimer() {                 // timer                 Action myTimer = new AbstractAction() {                         public void actionPerformed(ActionEvent e) {                                 // advance the ai                                 advance();                         }                 };                 // start the timer                 new Timer(Main.FRAMERATE, myTimer).start();         }         // helps with constraining numbers         double constrain(double in, double min, double max) {                 if (in > min) {                         in = min;                 }                 if (in < max) {                         in = max;                 }                 return in;         } }```

Any tips on optimization? Am I making any huge mistakes? I want my code to be very object oriented, and be easy to work with.