Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

175 Neutral

About PsychotikRabbit

  • Rank

Personal Information

  • Interests

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. PsychotikRabbit

    Is this memory leakage?

    Yes you are right you need to call close. Another approach:   public class InputManager { private final Scanner scanner; public InputManager() { scanner = new Scanner(System.in); } //This should be called when you don't want to read input anymore. public void shutdown() { scanner.close(); } public String menu(){ String menuoption = scanner.next(); //Do some stuff... return menuoption; } public void info(){ String info = scanner.next(); //Do some stuff... } } You could then use your class like this:   public static void main(String[] args) { InputManager inputManager = new InputManager(); //Do whatever you want with the user input. String option = inputManager.menu(); System.out.println("Option: " + option); //Before exiting the application you free resources inputManager.shutdown(); }
  2. PsychotikRabbit

    Is this memory leakage?

    Best practice would be to re-use the same scanner instance. If the menu() and info() method must remain static then you can do something like this:   private static final Scanner scanner = new Scanner(System.in); //You should specify either if the method is public or private. private static String menu(){ String menuoption = scanner.next(); //Do some stuff... return menuoption; } //You should specify either if the method is public or private. private static void info(){ String info = scanner.next(); //Do some stuff... }
  3. I don't think it's bad to use instanceof for this part of the code. Storing a string for the name would be perfect if it would be used as an unique Identifiant. Though, all your paddles will be name "paddle" and all your walls will be named "wall". I think you must adjust your code with the functionnalities that you are looking for. Therefore, as you don't need 1000 different entities and you have to implement collision detection differently on each of your entity, it is a good way to do it.
  4. It's not a bad way, but you can improve it. Instead of adding an entityName, when you check collision you can verify the type of the obstacle instance:   //If there is an obstacle if(obstacle != null) { if(obstacle instanceof Paddle) { this.invGoingLeft(); } if(obstacle instanceof Wall) { this.invGoingDown(); } }   Also in the second snippet of code your posted you have two if   if(obstacle.entityName == "Wall")   and   if(obstacle.entityName != "Wall")   Consider using "else" instead of the second condition. Also if you don't want to get lost in your code and follow standards go read about Indentation at  http://en.wikipedia.org/wiki/Indent_style . It is a simple game, I think this collision detection is good for a pong game, though if you start making bigger projects, you might consider more advanced techniques which will fits your needs. As for now it's very good to start somewhere less complicated to get more experience.
  5. Since this is a ball specific collision implementation, you could add in the ball's update method some code to manage it.   //ball update method public void update() { //Check collision Entity obstacle = CollisionDetection.collidesWith(this); //If there is an obstacle if(obstacle != null) { if(obstacle.getX() < getX()) { //Bouce Right; } else { //Bounce Left; } if(obstacle.getY() < getY() { //Bounce Bottom } else { //Bounce Top } } }   Of course you can adapt this code to your needs.
  6. PsychotikRabbit

    Why does this code not work?

    OperatingRoom[] rooms = new OperatingRoom[3]; for(int i = 0; i < rooms.length; i++) { System.out.println("Op room #" + i); rooms = new OperatingRoom(); rooms.fillRoom(opRoom); //What is opRoom ? I guess you have a variabe not included in your snippet. rooms.display(); }
  7. It's simple, you can use Entity as the base class. Entity is anything that has a position,a size and is renderable in the game. Then for your "living things" you can create a MovingEntity class which extends from Entity. Now with that, all your entities/obstacles descend from Entity.   Entity.java --------------------------- public class Entity { int position x; int position y; int width; int height; //Here go your getters/setters and contructors }   MovableEntity.java --------------------------------------- public class MovableEntity extends Entity { int speed; //Here goes your setters/getters/constructors public void update() { //Detect collision and move } } Now your walls can now extends from Entity or directly be directly a Entity. Your ball/paddle/ai can extends from MovableEntity or be directly a MovableEntity.
  8. You can change your CollisionDetection class to an utility class, with static-only methods and therefore you could access it from anywhere. As CJ_COIMBRA said, you could create a new collection of Entity object and store them in the CollisionDetection class.   public class CollisionDetection { private static ArrayList<Entity> entities = new ArrayList<Entity>(); public static boolean collides(Entity e) { return collidesWith(e) != null; } public static Entity collidesWith(Entity e) { Entity obstacle = null; for(Entity entity: entities) { //If its not the same entity if(entity!= e) { if(e.getRect().intersects(entity.getRect()) { obstacle = entity; } } } return obstacle; } //Add obstacle to detect public static void addEntity(Entity entity) { entities.add(entity); } //Remove obstacle to detect public static void removeEntity(Entity entity) { entities.remove(entity); } } You could consider your walls and other obstacles as Entity. With this utility class you can initialize the detection in your init method like this:   CollisionDetection.addEntity(ball); CollisionDetection.addEntity(topWall); etc... Then in the entities that you need to manage collision (like the ball) in the update method, after updating the position of the ball you can detect collision with any other entities and handle it.   //Detect if the ball (this) collides with any other entities boolean collision = CollisionDetection.collides(this); //Detect with which entity the ball collides //If its not colliding with anything it will return null. //Its your job to ensure that it will not collide with more than one entity at the time //by reacting on collision. Entity obstacle = CollisionDetection.collidesWith(this);    
  9. PsychotikRabbit

    Slick2D on a JPanel

    Slick does provide a render, update and init method in the BasicGame class. In the render method there is two given parameters (GameContainer gc, Graphics g). The Graphics object seems to provides similar drawing method as java2d graphics. I beleive you can adapt easily your rendering class to use Slick 2D engine with that Graphics. Here's an example of how to draw a rectangle in the Slick2D engine :   public void render(GameContainer gc, Graphics g) throws SlickException { g.drawRect(0,0,20,20); }
  10. PsychotikRabbit

    Magnifier Tool

    Instead of creating a screenshot with a timer can't you just use the original image and scale it when its painted ? I did not write this code, it seems to do what you need to do by scaling the image with AffineTransform on its paint. Of course you would adjust the scaling depending on the zoom of your magnifier. protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); int w = getWidth(); int h = getHeight(); int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double x = (w - scale * imageWidth)/2; double y = (h - scale * imageHeight)/2; AffineTransform at = AffineTransform.getTranslateInstance(x,y); at.scale(scale, scale); g2.drawRenderedImage(image, at); } With this it won't flicker anymore because you won't need to use setVisible and to take a bunch of screenshots every second.
  11. PsychotikRabbit

    Chess Movement Using Mouse Listeners (Java)

    I've already worked on something similar, it's just mathematics. MouseEvent can provide you the mouse position of the click (MouseEvent.getX(), MouseEvent.getY()). We're lucky that you are making a chess game because of chessboard. To get the selected square location you can simply loop through all rows and column and check if the position of the mouse is within the square. I wrote a little method which would find a square , I didn't test it but I think it can help you out. private final int NUMBER_OF_ROWS = 8; private final int NUMBER_OF_COLUMNS = 8; private final int SQUARE_SIZE = 32; public Square getSquareAt(int x, int y) { for(int col = 0; col < NUMBER_OF_COLUMNS ; ++col ) { for(int row = 0; row < NUMBER_OF_ROWS; ++row ) { int squareX = row * SQUARE_SIZE; int squareY = col * SQUARE_SIZE; Rectangle rectangle = new Rectangle(squareX,squareY, SQUARE_SIZE, SQUARE_SIZE); if(rectangle.contains(x, y)) { return chessBoard[col][row]; } } } return null; } I let java.awt detect if the coordinate of the mouse is within the bounds of the square. Edit: That will work if there is one unique mouse listener listening on the chessboard panel. (container of all squares).
  12. PsychotikRabbit

    Chess Movement Using Mouse Listeners (Java)

    You are calling the method to highlight solution squares on the mouseReleased event instead of the mousePressed. MousePressed is the trigger for your "drag" and MouseReleased is the trigger for your "drop". What do you need to know exactly about mouse listeners ?
  13. PsychotikRabbit

    Networking to Slow - Java Multiplayer Pong

    I didn't test it but I'm pretty sure it would improve the network time. You won't need to open an output stream on the socket to write a string and to open an input stream on the socket to read a string. It would improve both CPU and network time. You could write a small unit test to test out the time sending and receiving a string by streams and another test for the time sending deserialized object and receiving the byte array.
  14. PsychotikRabbit

    Networking to Slow - Java Multiplayer Pong

    You could also send/receive a byte array instead of using character stream as you seems to be only sending Numbers. I know it prolly won't improve a lot the speed but optimization is always good. Go read about java object serialization/deserialization. You will save time by not creating a String and parsing it as you just want to send numbers.
  15. PsychotikRabbit

    PaintComponent runs slow and flickers in Java

    For the timer you can try differents values to test the stability of your games but as I said 25 frames per second should be enough. And is the repaint() method call is really necessary in your paintComponent method ? @Override public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; if(Map.redLevel == true){ g2d.drawImage(background, himmelX, himmelY, null); g2d.drawImage(background2, himmelX2, himmelY2, null); g2d.drawImage(lava, grassX+80, grassY+355, null); g2d.drawImage(lava, grassX+680, grassY+130, null); repaint(); } }
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!