Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- APPLICATION
- ------------------------
- import javax.swing.*;
- public class Application {
- public static String path ="C:\\Users\\jarek\\OneDrive\\NUIG Private\\(2) Semester 2 2019\\Next Generation Technologies II CT255\\Assignment 3\\";
- private Application(){
- JFrame frame = new JFrame("Ihsan The Defender");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- GamePanel gamePanel= new GamePanel();
- frame.add(gamePanel);
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setResizable(false);
- frame.setVisible(true);
- new Thread(gamePanel).start();
- }
- public static void main (String args[]){
- new Application();
- }
- }
- GAMEPANEL
- ------------------------
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.security.Key;
- import java.util.ArrayList;
- public class GamePanel extends JPanel implements Runnable, KeyListener{
- private static final Dimension DESIRED_SIZE = new Dimension(600,700);
- private String path = Application.path;
- Image gameOverImg = new ImageIcon(path+"//images//gameover1.png").getImage();
- private Ihsan ihsan;
- private ArrayList <David> davids = new ArrayList<>();
- private int enemies=5;
- private boolean pause=false;
- private boolean gameOver=false;
- GamePanel(){
- ihsan = new Ihsan(this);
- for(int i=0; i<enemies; i++){
- davids.add(new David(this));
- }
- setFocusable(true);
- requestFocusInWindow();
- addKeyListener(this);
- }
- @Override
- public void run() {
- while (!pause || !gameOver){
- repaint();
- for(David david:davids){
- david.move();
- }
- synchronized ("bulletLock") {
- for (Bullet bullet : Bullet.bullets) {
- bullet.move();
- }
- }
- try{Thread.sleep(30);}
- catch (InterruptedException e){}
- }
- }
- public void paint(Graphics g){
- Graphics2D g2d = (Graphics2D) g.create();
- g2d.setColor(Color.GRAY);
- g2d.fillRect(0,0 ,getWidth(), getHeight());
- for(David david : davids){
- g2d.drawImage(david.getImg(), david.getX(), david.getY(), null);
- }
- g2d.drawImage(ihsan.getImg(), ihsan.getX(), ihsan.getY(), null);
- synchronized ("bulletLock") {
- for (Bullet bullet : Bullet.bullets) {
- g2d.drawImage(bullet.getImg(), bullet.getX(), bullet.getY(), null);
- }
- }
- if(gameOver){
- g2d.drawImage(gameOverImg,0,getHeight()/4,null);
- }
- }
- @Override
- public void keyPressed(KeyEvent e) {
- int key=e.getKeyCode();
- if (key==KeyEvent.VK_D || key==KeyEvent.VK_RIGHT){
- ihsan.move(4,0);
- System.out.println("Right Key");
- }
- if (key==KeyEvent.VK_A || key== KeyEvent.VK_LEFT){
- ihsan.move(-4,0);
- System.out.println("Left Key");
- }
- if(key==KeyEvent.VK_SPACE){
- Bullet.bullets.add(new Bullet(this,ihsan.getX()+(ihsan.getWidth()/2), ihsan.getY()));
- }
- }
- @Override
- public void keyTyped(KeyEvent e) { }
- @Override
- public void keyReleased(KeyEvent e) { }
- public boolean getGameOver(){
- return gameOver;
- }
- @Override
- public Dimension getPreferredSize(){
- return DESIRED_SIZE;
- }
- public void setGameOver(boolean gameOver) {
- this.gameOver = gameOver;
- }
- }
- BULLET
- ------------------------
- import javax.swing.*;
- import java.awt.*;
- import java.util.ArrayList;
- public class Bullet {
- //Environment
- public static ArrayList<Bullet> bullets = new ArrayList<>();
- private String path = Application.path;
- private GamePanel gp;
- //properties
- private int x,y;
- private int width,height;
- private int yVector;
- private Image image;
- Bullet(GamePanel gp, int x, int y){
- image = new ImageIcon(path+"\\images\\javaicon.png").getImage();
- width=image.getWidth(null);
- height=image.getHeight(null);
- this.gp=gp;
- this.x=x;
- this.y=y;
- yVector=5;
- }
- public void move(){
- synchronized ("bulletLock") {
- if (y < -height) {
- bullets.remove(this);
- }
- }
- y-=5;
- }
- public Image getImg(){
- return image;
- }
- public int getX(){
- return x;
- }
- public int getY(){
- return y;
- }
- }
- IHSAN
- ------------------------
- import javax.swing.*;
- import java.awt.*;
- import java.util.ArrayList;
- public class Ihsan {
- //environment
- String path = Application.path;
- private int jpWidth, jpHeight;
- GamePanel gp;
- //properties
- private Image image;
- private int x,y;
- private int width, height;
- Ihsan(GamePanel gp){
- this.gp=gp;
- //load the image
- image = new ImageIcon(path+"\\images\\ihsan.png").getImage();
- //get panel size
- //jpWidth=jp.getWidth returns 0 hence :
- jpWidth=gp.getPreferredSize().width;
- jpHeight=gp.getPreferredSize().height;
- //get ihsan size
- width=image.getWidth(null);
- height=image.getHeight(null);
- //set ihsan position
- x=jpWidth/2-width/2;
- y=jpHeight-height;
- Thread t = new Thread();
- t.start();
- }
- public void move(int xVector, int yVector){
- if(gp.getGameOver()){return;}
- if(x<=0-width/2 && xVector<0){return;}
- if(x>=jpWidth-width/2 && xVector>0){return;}
- x+=xVector;
- y+=yVector;
- }
- public Image getImg(){
- return image;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public int getHeight(){
- return height;
- }
- public int getWidth(){
- return width;
- }
- }
- DAVID
- ------------------------
- import javax.swing.*;
- import java.awt.*;
- import java.util.Random;
- public class David {
- // environment
- private GamePanel gamePanel;
- Random rand = new Random();
- String path = Application.path;
- private int jpWidth, jpHeight;
- // properties
- private Image image;
- private int x,y;
- private int yVector=1, xVector;
- private int width, height;
- David(GamePanel gamePanel){
- this.gamePanel=gamePanel;
- // load the image
- image = new ImageIcon(path+"\\images\\david.png").getImage();
- // get david size
- width=image.getWidth(null);
- height=image.getHeight(null);
- // get GamePanel's dimensions
- jpWidth=gamePanel.getPreferredSize().width;
- jpHeight=gamePanel.getPreferredSize().height;
- // random position
- // Math.random for better random-start-position:
- x= (int) (Math.random()*(jpWidth-width));
- // x=rand.nextInt(jpWidth-width);
- y=-rand.nextInt(height)-height;
- }
- private int i=0;
- public void move(){
- y+=yVector;
- if(i>20){
- xVector = rand.nextInt(3) - 1;
- i = 0;
- }else {i++;}
- // overwrite xVector if about to hit the border
- if(x<1){xVector=1;}
- if(x+width>jpWidth-1){xVector=-1;}
- x+=xVector;
- if(y+height>jpHeight){gamePanel.setGameOver(true);}
- }
- public Image getImg(){
- return image;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public int getWidth() {return width;}
- public int getHeight() {return height;}
- }
- //by Jaroslaw Janas
Advertisement
Add Comment
Please, Sign In to add comment