Ads 468x60px

freak2code is the blog about latest geek news,software reviews ,trends in technology and more informative stuff......

Monday, 13 August 2012

How can I improve on this?


How can I improve on this?

I'm not sure if this is the right thread, but I'm not sure where to post. I just want information on things like if I'm writing it in a bad way or maybe theres an easier way.
PLEASE DON'T GIVE ME THE AWNSER. I don't wanna be spoon fed. Give me a simple nudge so I get my brain thinking its a much better learning technique. The code in it self runs fine although my crappy collision detection which I used pointers for sucks.

Basically I just want to know why my code sucks.
How I can improve it.
If I'm using methods or calling stuff or other things I don't need.
I know the walls are dumb an I'll make a new class for level and make a code in there so I can create walls much easier.

I might be confusing you guy, but I'm only wondering how to improve my code and organize it better so I can keep on coding a very simple game. I don't know what it is even going to do yet. It's all for learning as a I have a bigger idea for a game that isn't out of my way.

Screen.java

Code :

package com.projectgaming.game1;
 
import java.awt.Color;
import java.awt.event.KeyListener;
 
import javax.swing.JFrame;
 
public class Screen {
 
 private static final long serialVersionUID = 1L;
 
 public static int w = 800;
 public static int h = 600;
 
 public static void main(String[] args){
  Game g = new Game();
  JFrame frame = new JFrame();
  frame.add(g);
  frame.pack();
  frame.setResizable(false);
  frame.setSize(w, h);
  frame.setTitle("Game Test");
  frame.setBackground(Color.BLACK);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLocationRelativeTo(null);
 }
}



Game.java
Code :

package com.projectgaming.game1;
 
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 
import javax.swing.*;
 
public class Game extends JPanel implements Runnable, KeyListener{
 
 private static final long serialVersionUID = 1L;
 
 public Rectangle character;
 public Rectangle floor;
 public Rectangle wall1;
 public Rectangle wall2;
 public Rectangle wall3;
 public Rectangle wall4;
 
 public int keyUp = KeyEvent.VK_W;
 public int keyDown = KeyEvent.VK_S;
 public int keyRight = KeyEvent.VK_D;
 public int keyLeft = KeyEvent.VK_A;
 
 public int characterWidth = 24;
 public int characterHeight = 36;
 public int characterX = 400;
 public int characterY = 300;
 
 public int floorWidth = 400;
 public int floorHeight= 300;
 public int floorX = 200;
 public int floorY= 200;
 
 public int wall1Width = 400;
 public int wall1Height = 10;
 public int wall1X = 200;
 public int wall1Y = 200;
 
 public int wall2Width = 10;
 public int wall2Height = 300;
 public int wall2X = 600;
 public int wall2Y = 200;
 
 public int wall3Width = 410;
 public int wall3Height = 10;
 public int wall3X = 200;
 public int wall3Y = 500;
 
 public int wall4Width = 10;
 public int wall4Height = 300;
 public int wall4X = 200;
 public int wall4Y = 200;
 
 public int movementSpeed = 1;
 public int movementFrame = 0;
 
 public boolean running = true;
 public boolean objectsDefined = false;
 
 public boolean alive = true;
 
 public boolean up = false;
 public boolean down = false;
 public boolean right = false;
 public boolean left = false;
 public boolean moving = false;
 
 public Thread game;
 
 public Game(){
  super();
  game = new Thread(this);
  game.start();
 
  defineObjects();
  setFocusable(true);
  addKeyListener(this);
 }
 
 public void keyPressed(KeyEvent e){
  //System.out.println("Key pressed");
  if(e.getKeyCode() == keyUp){
   up = true;
  }
 
  if(e.getKeyCode() == keyDown){
   down = true;
  }
 
  if(e.getKeyCode() == keyLeft){
   left = true;
  }
 
  if(e.getKeyCode() == keyRight){
   right = true;
  }
 }
 public void keyReleased(KeyEvent e){
  if(e.getKeyCode() == keyUp){
   up = false;
  }
 
  if(e.getKeyCode() == keyDown){
   down = false;
  }
 
  if(e.getKeyCode() == keyLeft){
   left = false;
  }
 
  if(e.getKeyCode() == keyRight){
   right = false;
  }
 }
 public void keyTyped(KeyEvent e){}
 
 public static void main(String[] args){
  new Game();
 }
 
 public void defineObjects(){
  character = new Rectangle(characterX, characterY, characterWidth, characterHeight);
  floor = new Rectangle(floorX, floorY, floorWidth, floorHeight);
  wall1 = new Rectangle(wall1X, wall1Y, wall1Width, wall1Height);
  wall2 = new Rectangle(wall2X, wall2Y, wall2Width, wall2Height);
  wall3 = new Rectangle(wall3X, wall3Y, wall3Width, wall3Height);
  wall4 = new Rectangle(wall4X, wall4Y, wall4Width, wall4Height);
 
  objectsDefined = true;
 
  repaint();
 }
 public void paintComponent(Graphics g){
  super.paintComponent(g);
 
  this.setBackground(Color.BLACK);
 
  if(objectsDefined){
   g.setColor(Color.RED);
   g.fillRect(floor.x, floor.y, floor.width, floor.height);
 
   g.setColor(Color.BLUE);
   g.fillRect(wall1.x, wall1.y, wall1.width, wall1.height);
   g.fillRect(wall2.x, wall2.y, wall2.width, wall2.height);
   g.fillRect(wall3.x, wall3.y, wall3.width, wall3.height);
   g.fillRect(wall4.x, wall4.y, wall4.width, wall4.height);
 
   if(alive){
    g.setColor(Color.WHITE);
    g.fillRect(character.x, character.y, character.width, character.height);
   }
  }
  repaint();
 }
 
 public void run() {
  while(running){
 
   Point rightFoot = new Point(character.x, (character.y + 1) + character.height);
   Point leftFoot = new Point(character.x + character.width, character.y + character.height);
   Point rightHand = new Point(character.x, character.y - 1);
   Point leftHand = new Point(character.x - 1, character.y);
   //Movement
   if(movementFrame >= movementSpeed){
     if(up){
      if(wall1.contains(rightHand) || wall1.contains(leftHand)){}else{
       character.y -= 1;
      }
     }
 
     if(down){
      if(wall1.contains(rightFoot) || wall1.contains(leftFoot)){}else{
       character.y += 1;
      }
     }
 
     if(left){
      if(wall1.contains(leftHand) || wall1.contains(leftFoot)){}else{
       character.x -= 1;
      }
     }
 
     if(right){
      if(wall1.contains(rightHand) || wall1.contains(rightFoot)){}else{
       character.x += 1;
      }
     } 
     movementFrame = 0;
    }else{
     movementFrame += 1;
    }
   try {
    Thread.sleep(1);
   } catch (Exception e) {
   }
 
   repaint();
  }
 }
}

0 comments:

Post a Comment

Recent Posts