Advertisement
Darano

snake

Dec 16th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. /**
  2.  * Little snake game.
  3.  *
  4.  * @@@@@@@@ Compile on computer to have it work properly @@@@@@@
  5.  *
  6.  * Z = snake / P = Player / # = door / $ = gold
  7.  * Collect the gold and get through the door, without having the snakes eaten you
  8.  * Made by Katharina, inspired by "Java ist auch eine Insel"-book
  9.  **/
  10.  
  11.  
  12.  
  13. import java.util.Scanner;
  14. import java.awt.Point;
  15.  
  16.  
  17. public class ZZZnake {
  18.     public static void main(String[] args) {
  19.         //(Feld: 0/0...39/9)
  20.         Point player = new Point( (int)(Math.random() * 38), (int)(Math.random() * 9) );
  21.         Point snake1 = new Point( (int)(Math.random() * 38), (int)(Math.random() * 9) );
  22.         Point snake2 = new Point( (int)(Math.random() * 38), (int)(Math.random() * 9) );
  23.  
  24.         Point door = new Point( (int)(Math.random() * 38), (int)(Math.random() * 9) );
  25.         Point gold = new Point( (int)(Math.random() * 38), (int)(Math.random() * 9) );
  26.         Point p = new Point(0,0);
  27.         boolean rich = false;            //collected gold
  28.         int count = 0; // player steps
  29.         int lvl = 3; //how many steps the player takes until snake starts moving
  30.         Scanner mov = new Scanner(System.in);
  31.        
  32.        
  33.         for(int i = 0; i < 50; i++) {
  34.         System.out.println("\n");  //"clearing" console
  35.         }
  36.        
  37.         while(true) {
  38.         //draw
  39.             for(int y = 0; y < 10; y++) {
  40.                 for(int x = 0; x < 40; x++) {
  41.                     p.setLocation(x,y);
  42.                     if(player.equals(p))
  43.                         System.out.print('P');
  44.                     else if(snake1.equals(p))
  45.                         System.out.print('Z');
  46.                     else if(snake2.equals(p))
  47.                         System.out.print('Z');
  48.                     else if(door.equals(p))
  49.                         System.out.print('#');
  50.                     else if(gold.equals(p))
  51.                         System.out.print('$');
  52.                     else
  53.                         System.out.print('.'); 
  54.                 }
  55.                 System.out.println();
  56.             }
  57.            
  58.         //move W-A-S-D
  59.            
  60.             switch (mov.next()) {
  61.             case "w":
  62.                 player.y = Math.max(0, player.y - 1); //max, so player cant get beyond the field
  63.                 count++;
  64.                 break;
  65.             case "s":
  66.                 player.y = Math.min(9, player.y + 1); //min, so player cant get beyond the field
  67.                 count++;
  68.                 break;
  69.             case "a":
  70.                 player.x = Math.max(0, player.x - 1);
  71.                 count++;
  72.                 break;
  73.             case "d":
  74.                 player.x = Math.min(39, player.x + 1);
  75.                 count++;
  76.             default:
  77.                 break;
  78.                 }
  79.        
  80.            
  81.         //move the snake towards the player
  82.             if(player.x < snake1.x && (count % lvl == 0))
  83.                 snake1.x--;
  84.             else if(player.x > snake1.x && (count % lvl == 0))
  85.                 snake1.x++;
  86.             if(player.y < snake1.y && (count % lvl == 0))
  87.                 snake1.y--;
  88.             else if(player.y > snake1.y && (count % lvl == 0))
  89.                 snake1.y++;
  90.            
  91.             if(player.x < snake2.x && (count % lvl == 0))
  92.                 snake2.x--;
  93.             else if(player.x > snake2.x && (count % lvl == 0))
  94.                 snake2.x++;
  95.             if(player.y < snake2.y && (count % lvl == 0))
  96.                 snake2.y--;
  97.             else if(player.y > snake2.y && (count % lvl == 0))
  98.                 snake2.y++;
  99.            
  100.         //set's the status
  101.             if(player.equals(snake1)) {
  102.                 System.out.println("You lost. Try again!");
  103.                 return;
  104.             }
  105.             if(player.equals(snake2)) {
  106.                 System.out.println("You lost. Try again!");
  107.                 return;
  108.             }
  109.             if(player.equals(gold)) {
  110.                 rich = true;
  111.                 gold.setLocation(-1,-1); //moving the gold beyond the field after collecting it
  112.             }
  113.             if(player.equals(door) && rich == true) {
  114.                 System.out.println("You won!");
  115.                 return;
  116.             }
  117.            
  118.             for(int i = 0; i < 50; i++) {
  119.                 System.out.println("\n"); // "clearing" console
  120.             }
  121.         } //end while
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement