Game Geng
Game Geng
import java.awt.Graphics;
import java.awt.Rectangle;
return isOnTile;
setFocusable(true);
addKeyListener(this);
addMouseListener(this);
private int enemyX = 0, enemy1X = 0, enemy2X = 0;
Timer timer = new Timer(1000 / 60, this); // MERONG
private int enemyY = 440,enemy1Y = 280, enemy2Y = /60 PARA TUMAAS YUNG FRAMES PER SECOND
55;
timer.start();
showInventory = false;
apple1 = new ImageIcon("apple2.png");
try{
drawimage = g.drawImage(cactus, 270, 620, 50, 50, g.drawImage(tile1, 60, 600, this);
this);
drawimage = g.drawImage(cactus, 300, 620, 50, 50, g.drawImage(tile1, 120, 520, this);
this);
g.drawImage(tile1, 150, 520, this);
drawimage = g.drawImage(cactus, 330, 620, 50, 50,
this);
drawimage = g.drawImage(cactus, 360, 620, 50, 50, g.drawImage(tile1, 230, 470, this);
this); g.drawImage(tile1, 260, 470, this);
drawimage = g.drawImage(cactus, 390, 620, 50, 50,
this);
g.drawImage(tile1, 430, 390, this);
drawimage = g.drawImage(cactus, 420, 620, 50, 50,
this);
drawimage = g.drawImage(cactus, 450, 620, 50, 50, g.drawImage(tile1, 150, 420, this);
this);
g.drawImage(tile1, 120, 420, this);
//design
g.drawImage(tile1, 100, 360, this);
drawimage = g.drawImage(design, 230, 470,50, 180, drawimage = g.drawImage(chest, 200, 300, 50, 50,
this); this);
}
repaint();
g.drawImage(characterSprite, destX, destY, destX +
}
scaledWidth, destY + scaledHeight,
if (showInventory) {
g.setColor(Color.ORANGE);
playerBounds = new Rectangle(characterX, characterY, g.fillRect(20, 50, 150, 100);
scaledWidth, scaledHeight);
g.setColor(Color.BLACK);
enemyBounds = new Rectangle(enemyX, enemyY, 40,
40); g.drawRect(20, 50, 150, 100);
if (enemy != null) {
}
Rectangle cactusBounds1= new Rectangle(30, 620, 480,
50);
if (characterY < 0) {
characterY = 0;
}
playerLife--;
if (playerLife <= 0) {
private void updateAnimation() {
playerLife = 3;
}
}
}
private void checkTileCollision() {
Rectangle enemyBounds = new Rectangle(260, 470, 40, // Collided with the first tile
40); characterY = 600 - scaledHeight;
Rectangle enemy1Bounds = new Rectangle(enemy1X, velocityY = 0; // Reset vertical velocity
enemy1Y, 40, 40);
isJumping = false;
Rectangle enemy2Bounds = new Rectangle(enemy2X,
enemy2Y, 40, 40); isOnTile = true; // Set the flag to true when on a tile
isOnTile = true; // Set the flag to true when on a tile isOnTile = true; // Set the flag to true when on a tile
} }
if (playerBounds.intersects(tile6Bounds)) { if (playerBounds.intersects(tile20Bounds)) {
// Collided with the first tile // Collided with the first tile
isOnTile = true; // Set the flag to true when on a tile isOnTile = true; // Set the flag to true when on a tile
} }
if (playerBounds.intersects(tile8Bounds)) { if (playerBounds.intersects(tile22Bounds)) {
// Collided with the first tile // Collided with the first tile
isOnTile = true; // Set the flag to true when on a tile isOnTile = true; // Set the flag to true when on a tile
} }
if (playerBounds.intersects(tile13Bounds)) { if (playerBounds.intersects(tile23Bounds)) {
// Collided with the first tile // Collided with the first tile
isOnTile = true; // Set the flag to true when on a tile isOnTile = true; // Set the flag to true when on a tile
} }
if (playerBounds.intersects(tile15Bounds)) { if (playerBounds.intersects(cactusBounds1)) {
// Collided with the first tile playerLife--; // Deduct life when the player touches
a cactus
characterY = 310- scaledHeight;
if (playerBounds.intersects(tile19Bounds)) {
repaint();
velocityY += gravity;
enemy1X += enemySpeed1;
repaint();
if (playerBounds.intersects(enemyBounds) ||
}
playerBounds.intersects(enemy1Bounds) ||
playerBounds.intersects(enemy2Bounds)) {
playerLife--;
if (playerLife <= 0) {
// Handle the logic when the player runs out of public static void main (String[]args){
life (you can add game over logic here)
JFrame frame= new JFrame("Sprite World");
playerLife = 3;
tryy spriteWorld = new tryy();
characterX = 0;
frame.add(spriteWorld);
characterY = 0;
frame.setSize(500, 700);
// Add any other necessary game state resets
} frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS
E);
}
frame.setVisible(true);
}
@Override
}
int spriteX = 0;
currentDirection = Direction.LEFT;
showInventory = !showInventory;
velocityX = -characterSpeed; // Move character
repaint();
left
}
} else if (keyCode == KeyEvent.VK_D) {
}
currentDirection = Direction.RIGHT;
isJumping = true;
showInventory = !showInventory;
@Override repaint();
@Override }