0% found this document useful (0 votes)
18 views2 pages

Notes 20240825230338

Uploaded by

santhoshmjv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Notes 20240825230338

Uploaded by

santhoshmjv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

08.

25 11:03 PM

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

public void move() {


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

//move snake body


for (int i = snakeBody.size()-1; i >= 0; i--) {
Tile snakePart = snakeBody.get(i);
if (i == 0) { //right before the head
snakePart.x = snakeHead.x;
snakePart.y = snakeHead.y;
}
else {
Tile prevSnakePart = snakeBody.get(i-1);
snakePart.x = prevSnakePart.x;
snakePart.y = prevSnakePart.y;
}
}
//move 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 snake head


if (collision(snakeHead, snakePart)) {
gameOver = true;
}
}

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


//passed left border or right border
snakeHead.y*tileSize < 0 || snakeHead.y*tileSize > boardHeight ) {
//passed top border or bottom border
gameOver = true;
}
}

public boolean collision(Tile tile1, Tile tile2) {


return tile1.x == tile2.x && tile1.y == tile2.y;
}

@Override
public void actionPerformed(ActionEvent e) { //called every x milliseconds by
gameLoop timer
move();
repaint();
if (gameOver) {
gameLoop.stop();
}
}

@Override
public void keyPressed(KeyEvent e) {
// System.out.println("KeyEvent: " + e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_UP && velocityY != 1) {
velocityX = 0;
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;
}
}

//not needed
@Override
public void keyTyped(KeyEvent e) {}

@Override
public void keyReleased(KeyEvent e) {}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy