Professional Documents
Culture Documents
CPEOOP 122 - Endterm Project - Docoument - Asistin, Panesa
CPEOOP 122 - Endterm Project - Docoument - Asistin, Panesa
Overview:
The idea developed into a single-player version where a snake grows longer each time it consumes a piece of
food, typically apples. The primary challenge is to prevent the snake from colliding with obstacles and itself, a
task that becomes progressively harder as the snake lengthens. It is characterized by their simple yet engaging
gameplay, which involves steering (using arrows) a constantly growing snake to avoid collisions. From its
origins in arcade games to its widespread popularity on mobile devices, the snake game remains a timeless
classic, enjoyed by players of all ages.
Objective: Guide the snake to consume food without colliding with obstacles or its own body. Controls:
Typically controlled using arrow keys to steer the direction of the snake. Growth Mechanism: Each time the
snake consumes food, it grows longer, increasing the challenge. Collision: Colliding with obstacles or the
snake's own body results in game over. Scoring: The player's score typically increases with each food item
consumed or the snake's length.
Source Code
I. App.java
CpEHDL | Laboratory Exercise 02 – Answer Sheet | Prepared by: Dr. Aumar G. Daguia, PCpE 1|P a g e
CCS-ALI-F.07
REVISION 4
JUNE 14, 2022
II. SnakeGame.java
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
//Images
Image asaImage;
Tile(int x, int y) {
this.x = x;
this.y = y;
}
}
int boardWidth;
int boardHeight;
int tileSize = 25;
//Snake
Tile snakeHead;
ArrayList<Tile> snakeBody;
//Food
Tile food;
Random random;
//Game Logic
Timer gameLoop;
int velocityX;
int velocityY;
boolean gameOver = false;
velocityX = 0;
velocityY = 0;
CpEHDL | Laboratory Exercise 02 – Answer Sheet | Prepared by: Dr. Aumar G. Daguia, PCpE 2|P a g e
CCS-ALI-F.07
REVISION 4
JUNE 14, 2022
gameLoop = new Timer(100, this);
gameLoop.start();
//Food
g.setColor(Color.red);
//g.fillRect(food.x * tileSize, food.y * tileSize, tileSize, tileSize);
g.fill3DRect(food.x * tileSize, food.y * tileSize, tileSize, tileSize, true);
//Snake Head
g.setColor(Color.green);
//g.fillRect(snakeHead.x * tileSize, snakeHead.y * tileSize, tileSize, tileSize);
g.fill3DRect(snakeHead.x * tileSize, snakeHead.y * tileSize, tileSize, tileSize,
true);
//Snake Body
for (int i = 0; i < snakeBody.size(); i++) {
Tile snakePart = snakeBody.get(i);
//g.fillRect(snakePart.x * tileSize, snakePart.y * tileSize, tileSize, tileSize);
g.fill3DRect(snakePart.x * tileSize, snakePart.y * tileSize, tileSize, tileSize,
true);
}
//Score
g.setFont(new Font("Arial", Font.PLAIN, 16));
if (gameOver) {
g.setColor(Color.red);
g.drawString("Game Over: " + String.valueOf(snakeBody.size()), tileSize - 16,
tileSize);
}
else {
g.drawString("Score: " + String.valueOf(snakeBody.size()), tileSize - 16,
tileSize);
}
}
}
CpEHDL | Laboratory Exercise 02 – Answer Sheet | Prepared by: Dr. Aumar G. Daguia, PCpE 3|P a g e
CCS-ALI-F.07
REVISION 4
JUNE 14, 2022
public boolean collision(Tile tile1, Tile tile2) {
return tile1.x == tile2.x && tile1.y == tile2.y;
}
//Snake Body
for (int i = snakeBody.size()-1; i >= 0; i--) {
Tile snakePart = snakeBody.get(i);
if (i == 0) {
snakePart.x = snakeHead.x;
snakePart.y = snakeHead.y;
}
else {
Tile prevSnakePart = snakeBody.get(i-1);
snakePart.x = prevSnakePart.x;
snakePart.y = prevSnakePart.y;
}
}
//Snake Head
snakeHead.x += velocityX;
snakeHead.y += velocityY;
@Override
public void actionPerformed(ActionEvent e) {
move();
repaint();
if (gameOver) {
gameLoop.stop();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP && velocityY != 1) {
velocityX = 0;
CpEHDL | Laboratory Exercise 02 – Answer Sheet | Prepared by: Dr. Aumar G. Daguia, PCpE 4|P a g e
CCS-ALI-F.07
REVISION 4
JUNE 14, 2022
velocityY = -1;
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN && velocityY != -1) {
velocityX = 0;
velocityY = 1;
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT && velocityX != 1) {
velocityX = -1;
velocityY = 0;
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT && velocityX != -1) {
velocityX = 1;
velocityY = 0;
}
}
@Override
public void keyReleased(KeyEvent e) {}
Output(s)
I. Start of the Game
CpEHDL | Laboratory Exercise 02 – Answer Sheet | Prepared by: Dr. Aumar G. Daguia, PCpE 5|P a g e
CCS-ALI-F.07
REVISION 4
JUNE 14, 2022
II. Playing the Game
CpEHDL | Laboratory Exercise 02 – Answer Sheet | Prepared by: Dr. Aumar G. Daguia, PCpE 6|P a g e
CCS-ALI-F.07
REVISION 4
JUNE 14, 2022