Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

College of Engineering

CPEOOP 122 – Object Oriented Programming


Game Project

Last Name, First Name M.I. Section Score


Panesa, Janedy G. ________________________________________
BSCPE-1B
Asistin, Joan Jade A._______________________________________
Date Submitted
_______________________________________________________

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.

Goals of the Game:


Control a snake to navigate a map.
Collect as much food as possible.
Grow longer by eating food.
Avoid collisions with the walls and the snake's own body.

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.*;

public class SnakeGame extends JPanel implements ActionListener, KeyListener {


private class Tile {
int x;
int y;

//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;

SnakeGame(int boardWidth, int boardHeight) {


this.boardWidth = boardWidth;
this.boardHeight = boardHeight;
setPreferredSize(new Dimension(this.boardWidth, this.boardHeight));
setBackground(Color.black);
addKeyListener(this);
setFocusable(true);

snakeHead = new Tile(5, 5);


snakeBody = new ArrayList<Tile>();

food = new Tile(10, 10);


random = new Random();
placeFood();

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();

public void paintComponent(Graphics g) {


super.paintComponent(g);
draw(g);
}

public void draw(Graphics g) {


//Grid
//for(int i = 0; i < boardWidth/tileSize; i++) {
// //(x1, y1, x2, y2)
// g.drawLine(i*tileSize, 0, i*tileSize, boardHeight);
// g.drawLine(0, i*tileSize, boardWidth, i*tileSize);
// }

//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);
}
}

public void placeFood () {


food.x = random.nextInt(boardWidth/tileSize); //600/25 = 24
food.y = random.nextInt(boardHeight/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;
}

public void move() {


//eat food
if (collision(snakeHead, food)) {
snakeBody.add(new Tile(food.x, food.y));
placeFood();
}

//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;

//game over conditions


for (int i = 0; i < snakeBody.size(); i++) {
Tile snakePart = snakeBody.get(i);
//collide with the snake head
if (collision(snakeHead, snakePart)) {
gameOver = true;
}
}

if (snakeHead.x*tileSize < 0 || snakeHead.x*tileSize > boardWidth ||


snakeHead.y*tileSize < 0 || snakeHead.y*tileSize > boardHeight) {
gameOver = true;
}
}

@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;
}
}

//do not need


@Override
public void keyTyped(KeyEvent e) {}

@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

III. Game Over

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

You might also like