Advertisement
Guest User

Tron

a guest
Apr 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.38 KB | None | 0 0
  1. package core;
  2.  
  3. import java.util.Scanner;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6.  
  7. import org.newdawn.slick.AppGameContainer;
  8. import org.newdawn.slick.BasicGame;
  9. import org.newdawn.slick.Color;
  10. import org.newdawn.slick.GameContainer;
  11. import org.newdawn.slick.Graphics;
  12. import org.newdawn.slick.Image;
  13. import org.newdawn.slick.Input;
  14. import org.newdawn.slick.SlickException;
  15.  
  16. public class HelloWorld extends BasicGame {
  17.  
  18.     Image img;
  19.     Image image;
  20.     int positionX = 100;
  21.     int positionY = 100;
  22.     int boostJ = 0;
  23.     int boostK = 0;
  24.     int tailleducarre = 10;
  25.     boolean z = false;
  26.     boolean d = false;
  27.     boolean q = false;
  28.     boolean s = false;
  29.     boolean UP = false;
  30.     boolean DOWN = false;
  31.     boolean LEFT = false;
  32.     boolean RIGHT = false;
  33.     byte direction1 = 0;
  34.     byte direction2 = 0;
  35.     byte Futuredirection1 = 0;
  36.     byte Futuredirection2 = 0;
  37.     int gamewonJ = 0;
  38.     int gamewonK = 0;
  39.     int Xmax = 1290;
  40.     int Xmin = 100;
  41.     int Ymax = 690;
  42.     int Ymin = 100;
  43.     int vittesse = 1;
  44.     int changementX1 = 0;
  45.     int changementY1 = 0;
  46.     int changementX2 = 0;
  47.     int changementY2 = 0;
  48.    
  49.    
  50.    
  51.     // initialisation des joueurs et de la grille
  52.  
  53.     Joueur j = new Joueur(positionX + tailleducarre * 5, positionY + tailleducarre * 5);
  54.     Joueur k = new Joueur(positionX + tailleducarre * 10, positionY + tailleducarre * 10);
  55.     Grid grid = new Grid(100);
  56.    
  57.  
  58.     public HelloWorld(String gamename) {
  59.         super(gamename);
  60.     }
  61.  
  62.     @Override
  63.     public void init(GameContainer gc) throws SlickException {
  64.         img = new Image("Design.png");
  65.         // image= new Image("croix.png");
  66.  
  67.         // chercher une case parmis la liste de case
  68.  
  69.         for (Case b : grid.listOfCase) {
  70.  
  71.             if (b.x == 20 && b.y == 30) {
  72.  
  73.                 System.out.println("voici la case de coordonées : " + b.x + " " + b.y);
  74.  
  75.             }
  76.         }
  77.  
  78.     }
  79.  
  80.     @Override
  81.     public void update(GameContainer gc, int i) throws SlickException {
  82.  
  83.         // déplacement du joueur J en faisant varier la polarité de la direction
  84.  
  85.         boolean pressD = gc.getInput().isKeyDown(Input.KEY_D);
  86.  
  87.         if (pressD) {
  88.             if (!d) {
  89.  
  90.                 d = true;
  91.                 Futuredirection1 = 2;
  92.  
  93.             }
  94.         } else {
  95.  
  96.             d = false;
  97.         }
  98.  
  99.         boolean pressQ = gc.getInput().isKeyDown(Input.KEY_Q);
  100.  
  101.         if (pressQ) {
  102.             if (!q) {
  103.  
  104.                 q = true;
  105.                 Futuredirection1 = 4;
  106.  
  107.             }
  108.         } else {
  109.  
  110.             q = false;
  111.  
  112.         }
  113.  
  114.         boolean pressZ = gc.getInput().isKeyDown(Input.KEY_Z);
  115.  
  116.         if (pressZ) {
  117.             if (!z) {
  118.  
  119.                 z = true;
  120.                 Futuredirection1 = 1;
  121.  
  122.             }
  123.         } else {
  124.  
  125.             z = false;
  126.  
  127.         }
  128.  
  129.         boolean pressS = gc.getInput().isKeyDown(Input.KEY_S);
  130.  
  131.         if (pressS) {
  132.             if (!s) {
  133.  
  134.                 s = true;
  135.                 Futuredirection1 = 3;
  136.  
  137.             }
  138.         } else {
  139.  
  140.             s = false;
  141.  
  142.         }
  143.  
  144.         boolean pressSPACE = gc.getInput().isKeyDown(Input.KEY_SPACE);
  145.         if (pressSPACE) {
  146.         }
  147.  
  148.         // déplacement du joueur K en faisant varier la polarité de la direction
  149.  
  150.         boolean pressRIGHT = gc.getInput().isKeyDown(Input.KEY_RIGHT);
  151.  
  152.         if (pressRIGHT) {
  153.             if (!RIGHT) {
  154.  
  155.                 RIGHT = true;
  156.                 Futuredirection2 = 2;
  157.  
  158.             }
  159.         } else {
  160.  
  161.             RIGHT = false;
  162.         }
  163.  
  164.         boolean pressLEFT = gc.getInput().isKeyDown(Input.KEY_LEFT);
  165.  
  166.         if (pressLEFT) {
  167.             if (!LEFT) {
  168.  
  169.                 LEFT = true;
  170.                 Futuredirection2 = 4;
  171.  
  172.             }
  173.         } else {
  174.  
  175.             LEFT = false;
  176.         }
  177.  
  178.         boolean pressUP = gc.getInput().isKeyDown(Input.KEY_UP);
  179.  
  180.         if (pressUP) {
  181.             if (!UP) {
  182.  
  183.                 UP = true;
  184.                 Futuredirection2 = 1;
  185.  
  186.             }
  187.         } else {
  188.  
  189.             UP = false;
  190.         }
  191.  
  192.         boolean pressDOWN = gc.getInput().isKeyDown(Input.KEY_DOWN);
  193.  
  194.         if (pressDOWN) {
  195.             if (!DOWN) {
  196.  
  197.                 DOWN = true;
  198.                 Futuredirection2 = 3;
  199.  
  200.             }
  201.         } else {
  202.  
  203.             DOWN = false;
  204.         }
  205.         boolean pressCTRL = gc.getInput().isKeyDown(Input.KEY_RCONTROL);
  206.         if (pressCTRL) {
  207.  
  208.         }
  209.  
  210.         if (Futuredirection1 != direction1 ) {
  211.             final int coordonéX = j.x ;
  212.             final int coordonéY = j.y;
  213.             System.out.println(coordonéX);
  214.             System.out.println(coordonéY);
  215.            
  216.         }
  217.    
  218.        
  219.        
  220.        
  221.         // Directions prises par le joueur : Nord = 1 ; Est = 2 ; Sud = 3 ; Ouest = 4
  222.  
  223.         switch (direction1) {
  224.  
  225.         case 1:
  226.             j.y = j.y - vittesse;
  227.  
  228.             break;
  229.         case 2:
  230.             j.x = j.x + vittesse;
  231.             break;
  232.         case 3:
  233.  
  234.             j.y = j.y + vittesse;
  235.  
  236.             break;
  237.         case 4:
  238.  
  239.             j.x = j.x - vittesse;
  240.  
  241.             break;
  242.         }
  243.  
  244.         if (Futuredirection1 != 8) {
  245.  
  246.             if (j.y % tailleducarre == 0 && j.x % tailleducarre == 0) {
  247.  
  248.                 direction1 = Futuredirection1;
  249.                 Futuredirection1 = 8;
  250.  
  251.             }
  252.         }
  253.  
  254.         switch (direction2) {
  255.  
  256.         case 1:
  257.             k.y = k.y - vittesse;
  258.             break;
  259.         case 2:
  260.             k.x = k.x + vittesse;
  261.             break;
  262.         case 3:
  263.             k.y = k.y + vittesse;
  264.             break;
  265.         case 4:
  266.             k.x = k.x - vittesse;
  267.             break;
  268.  
  269.         }
  270.  
  271.         if (Futuredirection2 != 8) {
  272.  
  273.             if (k.y % tailleducarre == 0 && k.x % tailleducarre == 0) {
  274.  
  275.                 direction2 = Futuredirection2;
  276.                 Futuredirection2 = 8;
  277.             }
  278.         }
  279.         //limites du terrain
  280.        
  281.             //limitation pour le joueur 1
  282.             if (j.x >= Xmax) {
  283.                 direction1 = 4;
  284.             }
  285.             if (j.x <= Xmin) {
  286.                 direction1 = 2;
  287.             }
  288.             if (j.y >= Ymax) {
  289.                 direction1 = 1;
  290.             }
  291.             if (j.y <= Ymin) {
  292.                 direction1 = 3;
  293.             }
  294.            
  295.             // limitation pour le joueur 2
  296.             if (k.x >= Xmax) {
  297.                 direction2 = 4;
  298.             }
  299.             if (k.x <= Xmin) {
  300.                 direction2 = 2;
  301.             }
  302.             if (k.y >= Ymax) {
  303.                 direction2 = 1;
  304.             }
  305.             if (k.y <= Ymin) {
  306.                 direction2 = 3;
  307.             }
  308.            
  309.        
  310.        
  311.        
  312.        
  313.         if (j.y % tailleducarre == 0 && j.x % tailleducarre == 0) {
  314.             grid.listOfDeadlyCase.add(new Case(j.x, j.y));
  315.         }
  316.  
  317.         // fin de partie
  318.  
  319.         for (Case b : grid.listOfDeadlyCase) {
  320.  
  321.             if (b.x == j.x && b.y == j.y) {
  322.  
  323.                 // fin de partie pour le joueur J
  324.                 System.out.println("le joueur K gagne la partie. GG !");
  325.                 gamewonJ = 10;
  326.             }
  327.  
  328.             if (b.x == k.x && b.y == k.y) {
  329.  
  330.                 // fin de partie pour le joueur k
  331.                 System.out.println("le joueur J gagne la partie. GG !");
  332.                 gamewonK = 10;
  333.             }
  334.         }
  335.     }
  336.  
  337.     @Override
  338.     public void render(GameContainer gc, Graphics g) throws SlickException {
  339.  
  340.         // dessin des images
  341.         g.drawString("La grille !", 20, 20);
  342.         img.draw(positionX, positionY, 1200, 600);
  343.         g.drawRect(k.x, k.y, tailleducarre, tailleducarre);
  344.         g.drawRect(j.x, j.y, tailleducarre, tailleducarre);
  345.         g.setColor(Color.orange);
  346.         g.drawRect(j.x, j.y, tailleducarre, tailleducarre);
  347.        
  348.        
  349.        
  350.         //rendue du tracé du joueur
  351.         g.setColor(Color.orange);
  352.        
  353.         if (direction1 == 3 ) {
  354.         for( int i =0; i<10 ; i++ ){
  355.         g.drawGradientLine(100.0f, 100.0f+i, Color.orange, j.x, j.y+i,Color.orange);
  356.             }
  357.         }
  358.        
  359.         if( direction1 == 2 ) {
  360.         for( int o = 0; o<10 ; o++) {
  361.         g.drawGradientLine(100.0f+o, 100.0f, Color.orange, j.x+o, j.y,Color.orange);
  362.            
  363.             }
  364.         }
  365.         //g.drawImage(image, 100f, 100f, Color.black);
  366.  
  367.         //g.setColor( new Color( 128, 128, 128 ) );g.setColor( Color.white );new Color(128, 128, 128);g.fillRect(100, 100, 10, 10);g.drawLine(100, 150, 300, 350);g.setColor(new Color(0, 0, 0, .5f));
  368.        
  369.  
  370.         // affichage de la victoire d'un joueur
  371.         if (gamewonJ == 10) {
  372.             g.setColor(Color.white);
  373.             g.drawString("le joueur j gagne ", 825, 825);
  374.             //g.clear();
  375.         }
  376.         if (gamewonK == 10) {
  377.             g.drawString("le joueur k gagne ", 825, 825);
  378.             //g.clear();
  379.         }
  380.     }
  381.  
  382.     public static void main(String[] args) {
  383.         try {
  384.             AppGameContainer appgc;
  385.             appgc = new AppGameContainer(new HelloWorld("Simple Slick Game"));
  386.             appgc.setDisplayMode(1920, 1080, false);
  387.             appgc.start();
  388.         } catch (SlickException ex) {
  389.             Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
  390.         }
  391.     }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement