Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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: ctomni231 (posted 2014-07-18 06:55:21, viewed 79 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   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399   400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439   440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479   480   481   482   483   484   485   486   487   488   489   490   491   492   493   494   495   496   497   498   499   500   501   502   503   504   505   506   507   508   509   510  
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Game extends Applet implements Runnable, KeyListener{

   private boolean[] keys = new boolean[6];
   
   public final int SIZE = 127;
   public final String FILE = "";
    
   public int counter;
    public int scrollx;
    
    public int main;
    public int posx;
    public int posy;
    public int limit;
    
    public boolean jumping;
    public boolean hit;
    public boolean hitLeft;
    public int grav;
    public int move;
    
    public int goalx;
    public boolean win;
    public int wC;
    
    public int[] path = new int[SIZE];
    public int[] px = new int[SIZE];
    public int[] py = new int[SIZE];
    public int[] psx = new int[SIZE];
    public int[] psy = new int[SIZE];
    
    public int[] enem = new int[SIZE];
    public int[] ex = new int[SIZE];
    public int[] ey = new int[SIZE];
    public int[] eg = new int[SIZE];
    public boolean[] el = new boolean[SIZE];
    
    //File IO stuff
    private String basePath;
    private Scanner scan;
    private String data;
    private String[] split;

    @Override
    public void start() {
        enableEvents(16);
        addKeyListener(this);
        new Thread(this).start();
    }

    public void run() {
        BufferedImage image = new BufferedImage(80*10, 60*10, 1);
        Graphics g = image.getGraphics();
        Graphics g2 = null;
        
        for(int i = 0; i < path.length; i++){
           path[i] = -1;
           enem[i] = -1;
        }
        
        scrollx = 0;
        grav = 0;
        move = 1;
        jumping = false; 
        
        main = 0;
        posx = 100;
        posy = 100;
        limit = 10;
        
        goalx = 1500;
        win = false;
        
        hit = false;
        hitLeft = false;
        wC = 255;
        
        //File I/O Stuff
        basePath = new File("").getAbsolutePath();
        if(new File(basePath.replace('\\','/')+"/setting.txt").exists()){
           int blockcount = 0;
           int enemycount = 0;
           try {
            scan = new Scanner(new File(basePath.replace('\\','/')+"/setting.txt"));
         
            while(scan.hasNextLine()){
               data = scan.nextLine();
               data = data.toUpperCase();
               
               if(data.startsWith("<"))
                  data = data.substring(1);
               
               if(data.startsWith("P")){
                  split = data.split(" ");
                  for(int i = 0; i < split.length; i++){
                     split[i] = turnToNumber(split[i]);
                     if(i == 1)
                        posx = Integer.parseInt(split[i]);
                     else if(i == 2)
                        posy = Integer.parseInt(split[i]);
                     else if(i == 3)
                        limit = Integer.parseInt(split[i]);
                  }
               }
               
               if(data.startsWith("G")){
                  split = data.split(" ");
                  for(int i = 0; i < split.length; i++){
                     split[i] = turnToNumber(split[i]);
                     if(i == 1)
                        goalx = Integer.parseInt(split[i]);
                  }
               }
               
               if(data.startsWith("E")){
                  split = data.split(" ");
                  enem[enemycount] = 0;
                  ex[enemycount] = 0;
                  ey[enemycount] = 0;
                  el[enemycount] = false;
                  for(int i = 0; i < split.length; i++){
                     split[i] = turnToNumber(split[i]);
                     if(i == 1)
                        ex[enemycount] = Integer.parseInt(split[i]);
                     else if(i == 2)
                        ey[enemycount] = Integer.parseInt(split[i]);
                     else if(i == 3)
                        el[enemycount] = Integer.parseInt(split[i]) >= 1;
                  }
                  enemycount++;
               }
               
               if(data.startsWith("B")){
                  split = data.split(" ");
                  path[blockcount] = 0;
                  px[blockcount] = -25;
                  py[blockcount] = -25;
                  psx[blockcount] = 100;
                  psy[blockcount] = 25;
                  for(int i = 0; i < split.length; i++){
                     split[i] = turnToNumber(split[i]);
                     if(i == 1)
                        px[blockcount] = Integer.parseInt(split[i]);
                     else if(i == 2)
                        py[blockcount]= Integer.parseInt(split[i]);
                     else if(i == 3)
                        psx[blockcount] = Integer.parseInt(split[i]);
                     else if(i == 4)
                        psy[blockcount] = Integer.parseInt(split[i]);
                  }
                  blockcount++;
               }
            }
         
           } catch (FileNotFoundException e) {
              //Exception already handled...
         }
        }else{ 
            path[0] = 0;
            px[0] = 50;
            py[0] = 200;
            psx[0] = 500;
            psy[0] = 25;
            
            path[1] = 0;
            px[1] = 120;
            py[1] = 300;
            psx[1] = 500;
            psy[1] = 25;
            
            path[2] = 0;
            px[2] = 600;
            py[2] = 200;
            psx[2] = 500;
            psy[2] = 25;
            
            path[3] = 0;
            px[3] = 1200;
            py[3] = 200;
            psx[3] = 500;
            psy[3] = 25;
            
            path[4] = 0;
            px[4] = 1000;
            py[4] = 225;
            psx[4] = 500;
            psy[4] = 25;
            
            path[5] = 1;
            px[5] = 1000;
            py[5] = 205;
            psx[5] = 50;
            psy[5] = 25;
            
            path[6] = 2;
            px[6] = 1020;
            py[6] = 105;
            psx[6] = 50;
            psy[6] = 25;
            
            enem[0] = 0;
            ex[0] = 150;
            ey[0] = 10;
            eg[0] = 0;
            el[0] = false;
            
            enem[1] = 0;
            ex[1] = 200;
            ey[1] = 290;
            eg[1] = 0;
            el[1] = false;
            
            enem[2] = 0;
            ex[2] = 950;
            ey[2] = 10;
            eg[2] = 0;
            el[2] = false;
        }
        
        while(true){
           
           counter++;
           if(counter > 50)
              counter = 0;
           
           g.setColor(Color.RED);
           if(win)
              g.setColor(Color.GREEN);
           if(wC == 0)
              g.setColor(Color.BLACK);
           g.fillRect(goalx+scrollx, 0, 5, 600);
           
           for(int i = 0; i < path.length; i++){
              if(path[i] == -1)
                 continue;
              
              if(path[i] == 1){
                 py[i] += (counter < 25) ? 1 : -1;
                 g.setColor(Color.ORANGE);
              }else if(path[i] == 2){
                 py[i] += (counter < 25) ? -1 : 1;
                 g.setColor(Color.ORANGE);
              }else{
                 g.setColor(Color.BLACK);
              }
                 
              
              g.fillRect(px[i]+scrollx, py[i], psx[i], psy[i]);
              
              if(grav > 0 && posy+10 > py[i] && posy+10 < py[i]+psy[i] &&
                    posx > px[i] && posx < px[i]+psx[i]){
                 grav = 0;
                 posy--;
                 jumping = false;
                 hit = false;
              }
              
              if(grav < 0 && posy < py[i]+psy[i] && posy > py[i] &&
                    posx > px[i] && posx < px[i]+psx[i]){
                 grav = 0;
              }
              
              if(posx > px[i] && posx < px[i]+psx[i] && posy < py[i]+psy[i] && posy > py[i]){
                 posx++;
                 if(keys[2]){
                    keys[2] = false;
                 }
              }
              
              if(posx+10 > px[i] && posx+10 < px[i]+psx[i] && posy < py[i]+psy[i] && posy > py[i]){
                 posx--;
                 if(keys[3]){
                    keys[3] = false;
                 }
              }
                 
           }
           
           for(int i = 0; i < enem.length; i++){
              if(enem[i] == -1)
                 continue;
              
              g.setColor(Color.GRAY);
              g.fillRect(ex[i]+scrollx, ey[i], 10, 10);
              
              for(int j = 0; j < path.length; j++){
                 if(path[j] == -1)
                    continue;
                 
                 if(eg[i] > 0 && ey[i]+10 > py[j] && ey[i]+10 < py[j]+psy[j] &&
                        ex[i] > px[j] && ex[i] < px[j]+psx[j]){
                     eg[i] = 0;
                     ey[i]--;
                  }
                 
                 if(ex[i]+10 == px[j] + psx[j] && ey[i]+15 > py[j] && ey[i]+15 < py[j]+psy[j])
                    el[i] = true;
                 else if(ex[i] == px[j] && ey[i]+15 > py[j] && ey[i]+15 < py[j]+psy[j])
                    el[i] = false;
                 
                 if(ex[i]+10 > px[j] && ex[i]+10 < px[j]+psx[j] && ey[i] < py[j]+psy[j] && ey[i] > py[j])
                    el[i] = true;
                 if(ex[i] > px[j] && ex[i] < px[j]+psx[j] && ey[i] < py[j]+psy[j] && ey[i] > py[j])
                    el[i] = false;
              }
              
              //Hit logic
              if(grav > 10 && posx > ex[i]-10 && posx < ex[i]+10 && posy > ey[i]-10 && posy < ey[i]){
                 ey[i] = 0;
                 ex[i] = 900-scrollx;
                 grav = -30;
              }
              
              else if(!hit && posx > ex[i]-10 && posx < ex[i]+10 && posy == ey[i]){
                 hit = true;
                 hitLeft = !el[i];
                 grav = -30;
              }
              
              if(el[i])
                 ex[i]--;
              else
                 ex[i]++;
              
           }
           
           if(hit){
              if(hitLeft)
                 posx--;
              else
                 posx++;
           }
           
           if(main == 0){
              
              g.setColor(Color.RED);
              if(keys[2] || keys[3])
                 g.setColor(Color.YELLOW);
              if(hit && counter < 25)
                 g.setColor(Color.BLACK);
              if(jumping && counter < 25)
                 g.setColor(Color.GREEN);
              
              if(!jumping && !keys[2] && !keys[3])
                 g.fillRect(posx+scrollx, posy-counter/25, 10, 10);
              else
                 g.fillRect(posx+scrollx, posy, 10, 10);
                 
              
           }
           
           if(posx >= goalx){
              if(wC > 220){
                 if(counter%15 < 5)
                    g.setColor(Color.BLUE);
                 else
                    g.setColor(Color.GREEN);
                 g.fillRect(0, 0, 80*10, 60*10);
              }
              win = true;
           }
           
           
           
           if(posx+scrollx < 300)
              scrollx += move/limit;
           if(posx+scrollx > 500)
              scrollx -= move/limit;
           
           
           if(keys[2] && !hit){
              posx -= move/limit;
              if(move < 25)
                 move += 2;
           }
           if(keys[3] && !hit){
              posx += move/limit;
              if(move < 25)
                 move += 2;
           }
           if(!jumping && !hit && keys[4] && grav == 0){
              grav = -50;
              jumping = true;
              keys[4] = false;
           }
           if(keys[5] && wC == 0){
              win = false;
              wC = 255;
              posx = 0;
              posy = 0;
              scrollx = 0;
           }
           
           if(move > 0)
              move--;
           
           //Logic
           posy += grav/10;
           grav++;
           
           for(int i = 0; i < enem.length; i++){
              if(enem[i] == -1)
                 continue;
              
              ey[i] += eg[i]/10;
               eg[i]++;
           }
           
           if(posy >= 600){
              hit = false;
              posy = 0;
           }
           
           if(wC == 0){
              g.setColor(Color.WHITE);
              g.drawString("WINNER! Now exit the game! :P", 300, 350);
           }
           
           if(g2 == null){
                g2 = getGraphics();
                requestFocus();
            }else
                g2.drawImage(image, 0, 0, 80*10, 60*10, null);
            
           if(win && wC > 0)
              wC--;
           
           
           
            g.setColor(new Color(wC,wC,wC));
            g.fillRect(0, 0, 80*10, 60*10);
        }
        
    }
    
    public String turnToNumber(String string){
       String temp = "";
       for(int i = 0; i < string.length(); i++){
          if(string.substring(i, i+1).matches("[0-9-]"))
             temp += string.substring(i, i+1);
       }
       return temp;
    }

    // to run in window, uncomment below
    public static void main(String[] args) throws Throwable {
        javax.swing.JFrame frame = new javax.swing.JFrame("Mario Clone");
        frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        Game applet = new Game();
        applet.setPreferredSize(new java.awt.Dimension(800, 600));
        frame.add(applet, java.awt.BorderLayout.CENTER);
        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        Thread.sleep(250);
        applet.start();
    }

   @Override
   public void keyPressed(KeyEvent e) {
      if(e.getKeyCode() == 38)
         keys[0] = true;
      else if(e.getKeyCode() == 40)
         keys[1] = true;
      else if(e.getKeyCode() == 37)
         keys[2] = true;
      else if(e.getKeyCode() == 39)
         keys[3] = true;
      else if(e.getKeyCode() == 90)
         keys[4] = true;
      else if(e.getKeyCode() == 88)
         keys[5] = true;
   }

   @Override
   public void keyReleased(KeyEvent e) {
      if(e.getKeyCode() == 38)
         keys[0] = false;
      else if(e.getKeyCode() == 40)
         keys[1] = false;
      else if(e.getKeyCode() == 37)
         keys[2] = false;
      else if(e.getKeyCode() == 39)
         keys[3] = false;
      else if(e.getKeyCode() == 90)
         keys[4] = false;
      else if(e.getKeyCode() == 88)
         keys[5] = false;
      
   }

   @Override
   public void keyTyped(KeyEvent arg0) {
      // TODO Auto-generated method stub
      
   }

}





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
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!