Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Little snake game.
- *
- * @@@@@@@@ Compile on computer to have it work properly @@@@@@@
- *
- * Z = snake / P = Player / # = door / $ = gold
- * Collect the gold and get through the door, without having the snakes eaten you
- * Made by Katharina, inspired by "Java ist auch eine Insel"-book
- **/
- import java.util.Scanner;
- import java.awt.Point;
- public class ZZZnake {
- public static void main(String[] args) {
- //(Feld: 0/0...39/9)
- Point player = new Point( (int)(Math.random() * 38), (int)(Math.random() * 9) );
- Point snake1 = new Point( (int)(Math.random() * 38), (int)(Math.random() * 9) );
- Point snake2 = new Point( (int)(Math.random() * 38), (int)(Math.random() * 9) );
- Point door = new Point( (int)(Math.random() * 38), (int)(Math.random() * 9) );
- Point gold = new Point( (int)(Math.random() * 38), (int)(Math.random() * 9) );
- Point p = new Point(0,0);
- boolean rich = false; //collected gold
- int count = 0; // player steps
- int lvl = 3; //how many steps the player takes until snake starts moving
- Scanner mov = new Scanner(System.in);
- for(int i = 0; i < 50; i++) {
- System.out.println("\n"); //"clearing" console
- }
- while(true) {
- //draw
- for(int y = 0; y < 10; y++) {
- for(int x = 0; x < 40; x++) {
- p.setLocation(x,y);
- if(player.equals(p))
- System.out.print('P');
- else if(snake1.equals(p))
- System.out.print('Z');
- else if(snake2.equals(p))
- System.out.print('Z');
- else if(door.equals(p))
- System.out.print('#');
- else if(gold.equals(p))
- System.out.print('$');
- else
- System.out.print('.');
- }
- System.out.println();
- }
- //move W-A-S-D
- switch (mov.next()) {
- case "w":
- player.y = Math.max(0, player.y - 1); //max, so player cant get beyond the field
- count++;
- break;
- case "s":
- player.y = Math.min(9, player.y + 1); //min, so player cant get beyond the field
- count++;
- break;
- case "a":
- player.x = Math.max(0, player.x - 1);
- count++;
- break;
- case "d":
- player.x = Math.min(39, player.x + 1);
- count++;
- default:
- break;
- }
- //move the snake towards the player
- if(player.x < snake1.x && (count % lvl == 0))
- snake1.x--;
- else if(player.x > snake1.x && (count % lvl == 0))
- snake1.x++;
- if(player.y < snake1.y && (count % lvl == 0))
- snake1.y--;
- else if(player.y > snake1.y && (count % lvl == 0))
- snake1.y++;
- if(player.x < snake2.x && (count % lvl == 0))
- snake2.x--;
- else if(player.x > snake2.x && (count % lvl == 0))
- snake2.x++;
- if(player.y < snake2.y && (count % lvl == 0))
- snake2.y--;
- else if(player.y > snake2.y && (count % lvl == 0))
- snake2.y++;
- //set's the status
- if(player.equals(snake1)) {
- System.out.println("You lost. Try again!");
- return;
- }
- if(player.equals(snake2)) {
- System.out.println("You lost. Try again!");
- return;
- }
- if(player.equals(gold)) {
- rich = true;
- gold.setLocation(-1,-1); //moving the gold beyond the field after collecting it
- }
- if(player.equals(door) && rich == true) {
- System.out.println("You won!");
- return;
- }
- for(int i = 0; i < 50; i++) {
- System.out.println("\n"); // "clearing" console
- }
- } //end while
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement