Guest User

Ihsan Invaders

a guest
Jan 21st, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.77 KB | None | 0 0
  1. APPLICATION
  2. ------------------------
  3. import javax.swing.*;
  4.  
  5. public class Application {
  6.     public static String path ="C:\\Users\\jarek\\OneDrive\\NUIG Private\\(2) Semester 2 2019\\Next Generation Technologies II CT255\\Assignment 3\\";
  7.     private Application(){
  8.         JFrame frame = new JFrame("Ihsan The Defender");
  9.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10.         GamePanel gamePanel= new GamePanel();
  11.         frame.add(gamePanel);
  12.         frame.pack();
  13.         frame.setLocationRelativeTo(null);
  14.         frame.setResizable(false);
  15.         frame.setVisible(true);
  16.         new Thread(gamePanel).start();
  17.     }
  18.  
  19.     public static void main (String args[]){
  20.         new Application();
  21.     }
  22. }
  23.  
  24. GAMEPANEL
  25. ------------------------
  26. import javax.swing.*;
  27. import java.awt.*;
  28. import java.awt.event.KeyEvent;
  29. import java.awt.event.KeyListener;
  30. import java.security.Key;
  31. import java.util.ArrayList;
  32.  
  33. public class GamePanel extends JPanel implements Runnable, KeyListener{
  34.     private static final Dimension DESIRED_SIZE = new Dimension(600,700);
  35.     private String path = Application.path;
  36.     Image gameOverImg = new ImageIcon(path+"//images//gameover1.png").getImage();
  37.     private Ihsan ihsan;
  38.     private ArrayList <David> davids = new ArrayList<>();
  39.     private int enemies=5;
  40.     private boolean pause=false;
  41.     private boolean gameOver=false;
  42.  
  43.     GamePanel(){
  44.         ihsan = new Ihsan(this);
  45.         for(int i=0; i<enemies; i++){
  46.             davids.add(new David(this));
  47.         }
  48.         setFocusable(true);
  49.         requestFocusInWindow();
  50.         addKeyListener(this);
  51.     }
  52.  
  53.     @Override
  54.     public void run() {
  55.         while (!pause || !gameOver){
  56.             repaint();
  57.             for(David david:davids){
  58.                 david.move();
  59.             }
  60.             synchronized ("bulletLock") {
  61.                 for (Bullet bullet : Bullet.bullets) {
  62.                     bullet.move();
  63.                 }
  64.             }
  65.  
  66.             try{Thread.sleep(30);}
  67.             catch (InterruptedException e){}
  68.         }
  69.     }
  70.  
  71.     public void paint(Graphics g){
  72.         Graphics2D g2d = (Graphics2D) g.create();
  73.         g2d.setColor(Color.GRAY);
  74.         g2d.fillRect(0,0 ,getWidth(), getHeight());
  75.  
  76.         for(David david : davids){
  77.             g2d.drawImage(david.getImg(), david.getX(), david.getY(), null);
  78.         }
  79.         g2d.drawImage(ihsan.getImg(), ihsan.getX(), ihsan.getY(), null);
  80.  
  81.         synchronized ("bulletLock") {
  82.             for (Bullet bullet : Bullet.bullets) {
  83.                 g2d.drawImage(bullet.getImg(), bullet.getX(), bullet.getY(), null);
  84.             }
  85.         }
  86.  
  87.         if(gameOver){
  88.             g2d.drawImage(gameOverImg,0,getHeight()/4,null);
  89.         }
  90.     }
  91.  
  92.  
  93.     @Override
  94.     public void keyPressed(KeyEvent e) {
  95.         int key=e.getKeyCode();
  96.  
  97.         if (key==KeyEvent.VK_D || key==KeyEvent.VK_RIGHT){
  98.             ihsan.move(4,0);
  99.             System.out.println("Right Key");
  100.         }
  101.  
  102.         if (key==KeyEvent.VK_A || key== KeyEvent.VK_LEFT){
  103.             ihsan.move(-4,0);
  104.             System.out.println("Left Key");
  105.         }
  106.  
  107.         if(key==KeyEvent.VK_SPACE){
  108.             Bullet.bullets.add(new Bullet(this,ihsan.getX()+(ihsan.getWidth()/2), ihsan.getY()));
  109.         }
  110.     }
  111.  
  112.     @Override
  113.     public void keyTyped(KeyEvent e) { }
  114.  
  115.     @Override
  116.     public void keyReleased(KeyEvent e) { }
  117.  
  118.     public boolean getGameOver(){
  119.         return gameOver;
  120.     }
  121.  
  122.     @Override
  123.     public Dimension getPreferredSize(){
  124.         return DESIRED_SIZE;
  125.     }
  126.  
  127.     public void setGameOver(boolean gameOver) {
  128.         this.gameOver = gameOver;
  129.     }
  130. }
  131.  
  132.  
  133. BULLET
  134. ------------------------
  135. import javax.swing.*;
  136. import java.awt.*;
  137. import java.util.ArrayList;
  138.  
  139. public class Bullet {
  140.     //Environment
  141.     public static ArrayList<Bullet> bullets = new ArrayList<>();
  142.     private String path = Application.path;
  143.     private GamePanel gp;
  144.     //properties
  145.     private int x,y;
  146.     private int width,height;
  147.     private int yVector;
  148.     private Image image;
  149.  
  150.  
  151.     Bullet(GamePanel gp, int x, int y){
  152.         image = new ImageIcon(path+"\\images\\javaicon.png").getImage();
  153.         width=image.getWidth(null);
  154.         height=image.getHeight(null);
  155.         this.gp=gp;
  156.         this.x=x;
  157.         this.y=y;
  158.         yVector=5;
  159.     }
  160.  
  161.     public void move(){
  162.         synchronized ("bulletLock") {
  163.             if (y < -height) {
  164.                 bullets.remove(this);
  165.             }
  166.         }
  167.         y-=5;
  168.     }
  169.  
  170.     public Image getImg(){
  171.         return image;
  172.     }
  173.  
  174.     public int getX(){
  175.         return x;
  176.     }
  177.  
  178.     public int getY(){
  179.         return y;
  180.     }
  181. }
  182.  
  183.  
  184. IHSAN
  185. ------------------------
  186. import javax.swing.*;
  187. import java.awt.*;
  188. import java.util.ArrayList;
  189.  
  190. public class Ihsan {
  191.     //environment
  192.     String path = Application.path;
  193.     private int jpWidth, jpHeight;
  194.     GamePanel gp;
  195.     //properties
  196.     private Image image;
  197.     private int x,y;
  198.     private int width, height;
  199.  
  200.     Ihsan(GamePanel gp){
  201.         this.gp=gp;
  202.         //load the image
  203.         image = new ImageIcon(path+"\\images\\ihsan.png").getImage();
  204.         //get panel size
  205.         //jpWidth=jp.getWidth returns 0 hence :
  206.         jpWidth=gp.getPreferredSize().width;
  207.         jpHeight=gp.getPreferredSize().height;
  208.         //get ihsan size
  209.         width=image.getWidth(null);
  210.         height=image.getHeight(null);
  211.         //set ihsan position
  212.         x=jpWidth/2-width/2;
  213.         y=jpHeight-height;
  214.         Thread t = new Thread();
  215.         t.start();
  216.     }
  217.  
  218.     public void move(int xVector, int yVector){
  219.         if(gp.getGameOver()){return;}
  220.         if(x<=0-width/2 && xVector<0){return;}
  221.         if(x>=jpWidth-width/2 && xVector>0){return;}
  222.         x+=xVector;
  223.         y+=yVector;
  224.     }
  225.  
  226.     public Image getImg(){
  227.         return image;
  228.     }
  229.  
  230.     public int getX() {
  231.         return x;
  232.     }
  233.  
  234.     public int getY() {
  235.         return y;
  236.     }
  237.  
  238.     public int getHeight(){
  239.         return height;
  240.     }
  241.  
  242.     public int getWidth(){
  243.         return width;
  244.     }
  245. }
  246.  
  247.  
  248. DAVID
  249. ------------------------
  250. import javax.swing.*;
  251. import java.awt.*;
  252. import java.util.Random;
  253.  
  254. public class David {
  255. //    environment
  256.     private GamePanel gamePanel;
  257.     Random rand = new Random();
  258.     String path = Application.path;
  259.     private int jpWidth, jpHeight;
  260. //    properties
  261.     private Image image;
  262.     private int x,y;
  263.     private int yVector=1, xVector;
  264.     private int width, height;
  265.  
  266.     David(GamePanel gamePanel){
  267.         this.gamePanel=gamePanel;
  268. //        load the image
  269.         image = new ImageIcon(path+"\\images\\david.png").getImage();
  270. //        get david size
  271.         width=image.getWidth(null);
  272.         height=image.getHeight(null);
  273. //        get GamePanel's dimensions
  274.         jpWidth=gamePanel.getPreferredSize().width;
  275.         jpHeight=gamePanel.getPreferredSize().height;
  276. //        random position
  277. //        Math.random for better random-start-position:
  278.         x= (int) (Math.random()*(jpWidth-width));
  279. //        x=rand.nextInt(jpWidth-width);
  280.         y=-rand.nextInt(height)-height;
  281.     }
  282.  
  283.     private int i=0;
  284.     public void move(){
  285.         y+=yVector;
  286.         if(i>20){
  287.             xVector = rand.nextInt(3) - 1;
  288.             i = 0;
  289.         }else {i++;}
  290. //        overwrite xVector if about to hit the border
  291.         if(x<1){xVector=1;}
  292.         if(x+width>jpWidth-1){xVector=-1;}
  293.  
  294.         x+=xVector;
  295.         if(y+height>jpHeight){gamePanel.setGameOver(true);}
  296.     }
  297.  
  298.     public Image getImg(){
  299.         return image;
  300.     }
  301.  
  302.     public int getX() {
  303.         return x;
  304.     }
  305.  
  306.     public int getY() {
  307.         return y;
  308.     }
  309.  
  310.     public int getWidth() {return width;}
  311.  
  312.     public int getHeight() {return height;}
  313. }
  314.  
  315. //by Jaroslaw Janas
Advertisement
Add Comment
Please, Sign In to add comment