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();
}
}
}