Chapter 11—Arrays and ArrayLists The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Ja va Arrays and ArrayLists C H A P T E R 1 1 Little boxes, on a hillside, little boxes made of ticky-tacky Little boxes, little boxes, little boxes, all the same There’s a green one and a pink one and a blue one and a yellow one And they're all made out of ticky-tacky and they all look just the same —Malvina Reynolds, “Little Boxes,” 1962 CS101 @ Özyeğin University Slides are adapted from the originals available at http://www-cs-faculty.stanford.edu/~eroberts/books/ArtAndScienceOfJava/ Lecture Slides, Part III 1
Java. The Art and Science of. An Introduction. to Computer Science. ERIC S. ROBERTS. C H A P T E R 1 1. Arrays and ArrayLists. Little boxes, on a hillside, little boxes made of ticky-tacky Little boxes, little boxes, little boxes, all the same - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Chapter 11—Arrays and ArrayLists
The Art and Science of
An Introductionto Computer ScienceERIC S. ROBERTS
Java
Arrays and ArrayListsC H A P T E R 1 1
Little boxes, on a hillside, little boxes made of ticky-tackyLittle boxes, little boxes, little boxes, all the sameThere’s a green one and a pink one and a blue one and a yellow oneAnd they're all made out of ticky-tacky and they all look just the same
—Malvina Reynolds, “Little Boxes,” 1962
CS101 @ Özyeğin University
Slides are adapted from the originals available athttp://www-cs-faculty.stanford.edu/~eroberts/books/ArtAndScienceOfJava/
SimpleSnakepublic void initializeSnakeBody() { int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; for(int i = 0; i < snakeBody.length; i++) { snakeBody[i] = createSnakeBodyPartCenteredAt(xCenter, yCenter); xCenter -= (BODY_PART_WIDTH/Math.sqrt(2)); yCenter -= (BODY_PART_WIDTH/Math.sqrt(2)); add(snakeBody[i]); }}
public GOval createSnakeBodyPartCenteredAt(int xCenter, int yCenter) { GOval circle = new GOval(xCenter - BODY_PART_WIDTH/2, yCenter - BODY_PART_WIDTH/2, BODY_PART_WIDTH, BODY_PART_WIDTH); circle.setFilled(true); circle.setFillColor(Color.BLUE); return circle;}
6
SimpleSnakepublic void initializeVelocities() { for(int i = 0; i < xVelocities.length; i++) { xVelocities[i] = 3; } for(int i = 0; i < yVelocities.length; i++) { yVelocities[i] = 3; }}
public void moveSnake() { for(int i = 0; i < snakeBody.length; i++) { snakeBody[i].move(xVelocities[i], yVelocities[i]); }}public void checkBounds() { for(int i = 0; i < snakeBody.length; i++) { double x = snakeBody[i].getX(); double y = snakeBody[i].getY(); if(x < 0 || x > getWidth() - BODY_PART_WIDTH) { xVelocities[i] *= -1; } if(y < 0 || y > getHeight() - BODY_PART_WIDTH) { yVelocities[i] *= -1; } }} 7
SimpleSnake v.2• In this problem we will implement yet another simple version
of the snake game.
• P.S. You may need to click on the window to request focus so that key events will be captured.
8
SimpleSnake v.2• In this version, there is a single, constant SPEED value. The
head of the snake has a direction, which can be one of NORTH, EAST, SOUTH, WEST.
• After each move, a body part replaces the part in the front.
snakedirection
9
SimpleSnake v.2• In this version, there is a single, constant SPEED value. The
head of the snake has a direction, which can be one of NORTH, EAST, SOUTH, WEST.
• After each move, a body part replaces the part in the front.
snakedirection
10
SimpleSnake v.2• Moving the snake parts:
• Each part element gets the location of the element in front of it.
• Head element is moved according to its direction.
0 1 2 3 4 5 6 7
11
SimpleSnakepublic class SimpleSnakeV2 extends GraphicsProgram { private final int BODY_PART_WIDTH = 20; private final int SNAKE_LENGTH = 12; private final int SPEED = BODY_PART_WIDTH;
private GOval[] snakeBody = new GOval[SNAKE_LENGTH];
private final int NORTH = 1; private final int EAST = 2; private final int SOUTH = 3; private final int WEST = 4; // Direction of the head is one of {N, W, S, E} private int direction = EAST;
public void addGrid() { for(int i = 0; i < getWidth(); i += BODY_PART_WIDTH) { add(new GLine(i, 0, i, getHeight())); } for(int i = 0; i < getHeight(); i += BODY_PART_WIDTH) { add(new GLine(0, i, getWidth(), i)); } }
13
14
public void moveSnake() { // Each body part replaces the one in front of it for(int i = snakeBody.length - 1; i >= 1; i--) { GOval prevPart = snakeBody[i-1]; snakeBody[i].setLocation(prevPart.getX(), prevPart.getY()); } // Move the head if(direction == EAST) { snakeBody[0].move(SPEED, 0); } else if(direction == WEST) { snakeBody[0].move(-SPEED, 0); } else if(direction == NORTH) { snakeBody[0].move(0, -SPEED); } else if(direction == SOUTH) { snakeBody[0].move(0, SPEED); } }
15
// The method below is invoked whenever the user presses a key // You do not need to spend too much time understanding this part public void keyPressed(KeyEvent keyEvent) { // VK_UP: key code for up arrow key // VK_RIGHT: key code for right arrow key // VK_DOWN: key code for down arrow key // VK_LEFT: key code for left arrow key if(keyEvent.getKeyCode() == KeyEvent.VK_UP) { direction = NORTH; } else if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) { direction = EAST; } else if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN) { direction = SOUTH; } else if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT) { direction = WEST; } }
public void init() { setSize(600, 400); addKeyListeners(); // required for key interaction }}