Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (775)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: safater (posted 2013-01-23 09:49:40, viewed 248 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306  
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class Game extends JPanel implements KeyListener , MouseListener , Runnable {
   private static final long serialVersionUID = 1L;
   Thread t;
   ImageIcon hero;
   BufferedImage Hero;
   ImageIcon floor;
   ImageIcon tiles;
   BufferedImage heroMissile[];
   AffineTransform oldTrans;
   AffineTransform newTrans;
   private int fireCounter;
   private double heroAngle;
   private double missileAngel[];
   private Point mousePosition;
   private boolean move = false;
   private boolean moveD = false;
   private boolean fire = false;
   private int heroTempMissileX [];
   private int heroTempMissileY [];
   private double mouseX;
   private double mouseY;
   private ImageIcon foe[];
   private Rectangle r1;
   private int heroX;
   private int heroY;
   private int currentMap;
   private boolean drawMap1 = true;
   private int repaintCount;
   private double nDirectionX;
   private double nDirectionY;
   private double nDirectionX2[];
   private double nDirectionY2[];
   private ImageIcon reloading;
   private Rectangle border[];
   private int firedOne;
   private int borderCounter;
   private Rectangle mr1;
   private Rectangle mr2;
   private Rectangle mr3;
   private boolean m1hitirepeatm1hit = false;
   private boolean m2hitirepeatm2hit = false;
   private boolean m3hitirepeatm3hit = false;
   private int borderCount;
   private int[][] map1 = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1} , {1,0,0,1,0,0,0,0,0,1,0,0,0,1}
   , {1,0,0,1,0,0,0,0,0,1,0,0,0,1} , {1,0,0,1,0,0,0,0,0,1,0,0,0,1} , {1,0,0,1,0,0,0,0,0,1,0,0,0,1} ,
   {1,0,0,1,0,0,0,0,0,1,0,0,0,1}, {0,0,0,1,0,0,0,0,0,1,0,0,0,1} , {0,0,0,1,0,0,0,0,0,1,0,0,0,1} ,
   {1,0,0,0,0,0,1,0,0,0,0,0,0,1} , {1,0,0,0,0,0,1,0,0,0,0,0,0,1} , {1,1,0,0,0,0,1,0,0,0,0,0,1,1} , 
   {1,1,1,0,0,0,1,0,0,0,0,1,1,1} , {1,1,1,1,0,0,1,0,0,0,1,1,1,1} , {1,1,1,1,1,1,1,1,1,1,1,1,1,1}};;
   public Game(){
      repaintCount = 1;
      fireCounter = 10;
      foe = new ImageIcon[2];
      borderCounter = 0;
      foe[0] = new ImageIcon("foe90cw.png");
      foe[1] = new ImageIcon("foe225cw.png");
      border = new Rectangle[196];
      heroMissile = new BufferedImage[3];
      heroMissile[0] = new BufferedImage(10 , 10 ,BufferedImage.TYPE_INT_ARGB);
      heroMissile[1] = new BufferedImage(10 , 10 ,BufferedImage.TYPE_INT_ARGB);
      heroMissile[2] = new BufferedImage(10 , 10 ,BufferedImage.TYPE_INT_ARGB);
      heroTempMissileX = new int[3];
      heroTempMissileY = new int[3];
      nDirectionX2 = new double[3];
      nDirectionY2 = new double[3];
      missileAngel = new double[3];
      reloading = new ImageIcon("reloading.png");
      firedOne = 0;
      oldTrans = new AffineTransform();
      newTrans = new AffineTransform();
      currentMap = 0;
      heroAngle = 0;
      Hero = new BufferedImage(50 , 50 ,BufferedImage.TYPE_INT_ARGB);
      tiles = new ImageIcon("mapS.png");
      try{
         Hero = ImageIO.read(new File("hero.png"));
         heroMissile[0] = ImageIO.read(new File("heroMissile.png"));
         heroMissile[1] = ImageIO.read(new File("heroMissile2.png"));
         heroMissile[2] = ImageIO.read(new File("heroMissile3.png"));
      }catch(Exception e){}
      Toolkit tools = Toolkit.getDefaultToolkit();
      Image image = tools.getImage("cursor.png");
      Point p = new Point(0 , 0);
      Cursor cursor = tools.createCustomCursor(image, p, "aim");
      setCursor(cursor);
      heroX = 70;
      heroY = 570;
      hero = new ImageIcon("hero.png");
      floor = new ImageIcon("floor.png");
      this.setSize(700,700);
      t = new Thread(this , "loop");
      t.start();
      this.addKeyListener(this);
   }
   public void paintComponent(Graphics g){
      int x1 = 0;
      int y1 = 0;
      borderCounter = 0;
      super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        floor.paintIcon(this, g, 0, 0);
        foe[1].paintIcon(this, g, 490, 100);
        foe[0].paintIcon(this, g, 100, 300);
        if(!fire)
           reloading.paintIcon(this, g, 50, 50);
      if(firedOne != -1){
         for(int loopstah = 0;loopstah < 3;loopstah++){
            nDirectionX2[loopstah] = Math.sin((float) missileAngel[loopstah]) * -20;
            nDirectionY2[loopstah] = Math.cos((float) missileAngel[loopstah]) * (+20);
            heroTempMissileX[loopstah] += nDirectionX2[loopstah];
            heroTempMissileY[loopstah] += nDirectionY2[loopstah];
            if(loopstah == 0)
               mr1 = new Rectangle((Integer)heroTempMissileX[0], (Integer)heroTempMissileX[0] + 9, 13 ,13);
            if(loopstah == 1)   
               mr2 = new Rectangle((Integer)heroTempMissileX[1], (Integer)heroTempMissileX[1] + 9, 13 ,13);
            if(loopstah == 2)
               mr3 = new Rectangle((Integer)heroTempMissileX[2], (Integer)heroTempMissileX[2] + 9, 13 ,13);
         }
      }
        if(drawMap1){
           repaintCount++;
         for(int i = 0; i < 700;i += 50){
            x1++;
            for(int j = 0; j < 700; j += 50){
               y1++;
               
               if(map1[x1 - 1][y1 - 1] == 1){
                  borderCounter++;
                  tiles.paintIcon(this, g2, i, j);
                  border [borderCounter - 1] = new Rectangle(i , j ,50 ,50);
               }
            }y1 = 0;
         }if(repaintCount == 2){
            borderCount = borderCounter;
         }
      }
      if(firedOne >= 0){
         if(!checkCollisionHeroMissile1(mr1)){
            if(!m1hitirepeatm1hit)
               g2.drawImage(heroMissile[0], heroTempMissileX[0] + 9, heroTempMissileY[0], null);
         }
        }
        if(firedOne >= 1){
         if(!checkCollisionHeroMissile2(mr2)){
            if(!m1hitirepeatm1hit)
               g2.drawImage(heroMissile[1], heroTempMissileX[1] + 9, heroTempMissileY[1], null);
         }
        }
        if(firedOne >= 2){
         if(!checkCollisionHeroMissile3(mr3)){
            if(!m1hitirepeatm1hit)
               g2.drawImage(heroMissile[2], heroTempMissileX[2] +  9, heroTempMissileY[2], null);
         }
        }
        heroAngle = Math.atan2((heroY + 25) - mouseY , (heroX + 25) - mouseX) + Math.PI/2;
        nDirectionX = Math.sin((float) heroAngle) * -10;
      nDirectionY = Math.cos((float) heroAngle) * (+10);
        g2.rotate(heroAngle , heroX  + 25, heroY + 25);
        hero.paintIcon(this, g2, heroX, heroY);
      
   }
   public void run() {
      while(true){
         int tempHeroX;
         int tempHeroY;
         fireCounter++;
         if(fireCounter > 60){
            fire = true;
            fireCounter = 0;
            firedOne = 0;
            m1hitirepeatm1hit = false;
            m2hitirepeatm2hit = false;
            m3hitirepeatm3hit = false;
         }
         mousePosition = MouseInfo.getPointerInfo().getLocation();
         mouseX = mousePosition.getX();
         mouseY = mousePosition.getY();
         tempHeroX = heroX;
         tempHeroY = heroY;
         if(move)
         {
            
            heroX =  (heroX + (int) nDirectionX);
            heroY =  (heroY + (int) nDirectionY);
            r1 = new Rectangle(heroX , heroY , 50 , 50);
            if(checkCollision()){
               heroX  = tempHeroX;
               heroY  = tempHeroY;
            }
            
         }
         if(moveD){
            heroX =  (heroX - (int) nDirectionX / 2);
            heroY =  (heroY - (int) nDirectionY / 2);
            r1 = new Rectangle(heroX , heroY , 50 , 50);
            if(checkCollision()){
               heroX  = tempHeroX;
               heroY  = tempHeroY;
            }
            
         }
         repaint();
         
         try {
            Thread.sleep(25L);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
   public void mouseClicked(MouseEvent arg0) {
   }
   public void mouseEntered(MouseEvent arg0) {
   }
   public void mouseExited(MouseEvent arg0) {
   }
   public void mousePressed(MouseEvent e) {
      if(fire){
         if(firedOne > 2){
            fire = false;
         }else{
            heroTempMissileX[firedOne] = heroX + 17;
            heroTempMissileY[firedOne] = heroY + 25;
            missileAngel[firedOne] = Math.atan2((heroY + 25) - mouseY , (heroX + 25) - mouseX) + Math.PI/2;
            repaint();
            firedOne++;
         }
      }
   }
   public void mouseReleased(MouseEvent arg0) {
   }
   public void keyPressed(KeyEvent e) {
      if(e.getKeyChar() == 'w'){
         move = true;
      }if(e.getKeyChar() == 's'){
         moveD = true;
      }
   }
   public void keyReleased(KeyEvent e) {
      if(e.getKeyChar() == 'w'){
         move = false;
      }if(e.getKeyChar() == 's'){
         moveD = false;
      }
   }
   public void keyTyped(KeyEvent arg0) {
   }
   public void setMap(){
      if(currentMap == 1)
         drawMap1 = true;
   }
   public boolean checkCollision(){
      for(int i = 0;i < borderCounter;i++){
         if(r1.intersects(border[i]))
            return true;
      }
      return false;
   }
   public boolean checkCollisionHeroMissile1(Rectangle r1){
      for(int i = 0;i < borderCount;i++){
         if(r1.intersects(border[i])){
            m1hitirepeatm1hit = true;
            return true;
         }
      }
      return false;
   }
   public boolean checkCollisionHeroMissile2(Rectangle r1){
      for(int i = 0;i < borderCount;i++){
         if(r1.intersects(border[i])){
            m2hitirepeatm2hit = true;
            return true;
         }
      }
      return false;
   }
   public boolean checkCollisionHeroMissile3(Rectangle r1){
      for(int i = 0;i < borderCount;i++){
         if(r1.intersects(border[i])){
            m3hitirepeatm3hit = true;
            return true;
         }
      }
      return false;
   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
EgonOlsen (1869 views)
2018-06-10 19:43:48

EgonOlsen (1901 views)
2018-06-10 19:43:44

EgonOlsen (1258 views)
2018-06-10 19:43:20

DesertCoockie (1703 views)
2018-05-13 18:23:11

nelsongames (1378 views)
2018-04-24 18:15:36

nelsongames (2003 views)
2018-04-24 18:14:32

ivj94 (2753 views)
2018-03-24 14:47:39

ivj94 (1955 views)
2018-03-24 14:46:31

ivj94 (3045 views)
2018-03-24 14:43:53

Solater (1090 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!