Skip to content

Instantly share code, notes, and snippets.

@aem
Created October 13, 2016 19:33
Show Gist options
  • Save aem/0e39d0f42818791f576903d2cc047f9e to your computer and use it in GitHub Desktop.
Save aem/0e39d0f42818791f576903d2cc047f9e to your computer and use it in GitHub Desktop.
import java.awt.Color;
import javalib.funworld.World;
import javalib.worldimages.OverlayImages;
import javalib.worldimages.Posn;
import javalib.worldimages.RectangleImage;
import javalib.worldimages.TextImage;
import javalib.worldimages.WorldEnd;
import javalib.worldimages.WorldImage;
class SnakeWorld extends World {
static final int WIDTH = 600;
static final int HEIGHT = 600;
static final int BLOCK_SIZE = 20;
ILoFood food;
ILoBlock blocks;
boolean hasStarted;
int score;
//constructor
SnakeWorld(ILoFood food, ILoBlock blocks, boolean hasStarted, int score) {
super();
this.food = food;
this.blocks = blocks;
this.hasStarted = hasStarted;
this.score = score;
}
/**
* FIELDS
* ... this.food ... ILoFood
* ... this.blocks ... ILoBlock
* ... this.hasStarted ... boolean
* ... this.score ... int
* METHODS
* ... this.worldEnds() WorldEnd
* ... this.onKeyEvent(String) ... World
* ... this.onTick() ... World
* ... this.makeImage() ... WorldImage
* ... this.drawScore() ... WorldImage
* METHODS OF FIELDS
* ... this.blocks.getFirst() ... Block
* ... this.blocks.getRest() ... ILoBlock
* ... this.blocks.changeDirection(String) ... ILoBlock
* ... this.blocks.eat(SnakeWorld) ... SnakeWorld
* ... this.blocks.isOnFood(ILoFood) ... boolean
* ... this.blocks.updateLoc(Block) ... ILoBlock
* ... this.blocks.addBlock() ... ILoBlock
* ... this.blocks.onBlock(Block) ... boolean
* ... this.blocks.drawBlocks() ... WorldImages
* ... this.food.getFirst() ... Food
* ... this.food.getRest() ... ILoFood
* ... this.food.addNew(Block) ... ILoFood
* ... this.food.drawFood() ... WorldImage
*/
public static void main(String[] args) {
Block b1 = new Block(true,
"up",
new CartPt(310, 310),
new CartPt(310, 290));
Food f1 = new Food();
Food f2 = new Food();
Food f3 = new Food();
ILoBlock emptyB = new MtLoB();
ILoBlock blocks = new ConsLoB(b1, emptyB);
ILoFood emptyF = new MtLoF();
ILoFood food = new ConsLoF(f1,
new ConsLoF(f2,
new ConsLoF(f3, emptyF)));
SnakeWorld sw = new SnakeWorld(food, blocks, false, 0);
sw.bigBang(600, 600, 0.1);
}
//called on each tick to determine if the game should end.
//the game ends if the snake hits the side of the world or itself
public WorldEnd worldEnds() {
return new WorldEnd(this.blocks.getFirst().outOfBounds()
|| this.blocks.onBlock(this.blocks.getFirst()),
new OverlayImages(
new RectangleImage(new Posn(0, 0),
WIDTH,
HEIGHT,
Color.white),
new TextImage(new Posn(150, 50),
"You lost! Final score: " + score,
Color.black)));
}
//called each time a key is pressed. used to change the direction
//of the snake if an arrow key is pressed, and to start the game if
//this SnakeWorld's hasStarted flag is false
public World onKeyEvent(String s) {
if (!this.hasStarted) {
return new SnakeWorld(this.food, this.blocks, true, 0);
}
else if (s.equals("left") ||
s.equals("right") ||
s.equals("up") ||
s.equals("down")) {
return new SnakeWorld(this.food,
this.blocks.changeDirection(s),
true,
this.score);
}
else {
return this;
}
}
//called on each tick. calls the ILoBlock method eat(SnakeWorld s) to do
//all of the processing and updating of locations and score
public World onTick() {
return this.blocks.eat(this);
}
//called each tick to render the image that appears on the screen.
//if the game hasn't started this method renders a static welcome screen,
//otherwise it overlays the background, food, snake, and score on 1 image
public WorldImage makeImage() {
if (!this.hasStarted) {
return new OverlayImages(
new RectangleImage(new Posn(0, 0),
WIDTH,
HEIGHT,
Color.white),
new OverlayImages(
new TextImage(new Posn(150, 50),
"Press any key to start...",
Color.black),
new RectangleImage(new Posn(310, 310),
BLOCK_SIZE,
BLOCK_SIZE,
Color.black)));
}
else {
return new OverlayImages(
new RectangleImage(new Posn(0, 0),
WIDTH,
HEIGHT,
Color.white),
new OverlayImages(this.food.drawFood(),
new OverlayImages(this.blocks.drawBlocks(),
this.drawScore())));
}
}
//draws the user's current score in a WorldImage
WorldImage drawScore() {
return new TextImage(new Posn(25, 10),
"Score: " + this.score,
Color.gray);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment