Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Palak #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 215 additions & 0 deletions FlappyBird .java
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;

public class FlappyBird extends JPanel implements ActionListener, KeyListener {
int boardWidth = 360;
int boardHeight = 640;

//images
Image backgroundImg;
Image birdImg;
Image topPipeImg;
Image bottomPipeImg;

//bird class
int birdX = boardWidth/8;
int birdY = boardWidth/2;
int birdWidth = 34;
int birdHeight = 24;

class Bird {
int x = birdX;
int y = birdY;
int width = birdWidth;
int height = birdHeight;
Image img;

Bird(Image img) {
this.img = img;
}
}

//pipe class
int pipeX = boardWidth;
int pipeY = 0;
int pipeWidth = 64; //scaled by 1/6
int pipeHeight = 512;

class Pipe {
int x = pipeX;
int y = pipeY;
int width = pipeWidth;
int height = pipeHeight;
Image img;
boolean passed = false;

Pipe(Image img) {
this.img = img;
}
}

//game logic
Bird bird;
int velocityX = -4; //move pipes to the left speed (simulates bird moving right)
int velocityY = 0; //move bird up/down speed.
int gravity = 1;

ArrayList<Pipe> pipes;
Random random = new Random();

Timer gameLoop;
Timer placePipeTimer;
boolean gameOver = false;
double score = 0;

FlappyBird() {
setPreferredSize(new Dimension(boardWidth, boardHeight));
// setBackground(Color.blue);
setFocusable(true);
addKeyListener(this);

//load images
backgroundImg = new ImageIcon(getClass().getResource("./flappybirdbg.png")).getImage();
birdImg = new ImageIcon(getClass().getResource("./flappybird.png")).getImage();
topPipeImg = new ImageIcon(getClass().getResource("./toppipe.png")).getImage();
bottomPipeImg = new ImageIcon(getClass().getResource("./bottompipe.png")).getImage();

//bird
bird = new Bird(birdImg);
pipes = new ArrayList<Pipe>();

//place pipes timer
placePipeTimer = new Timer(1500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Code to be executed
placePipes();
}
});
placePipeTimer.start();

//game timer
gameLoop = new Timer(1000/60, this); //how long it takes to start timer, milliseconds gone between frames
gameLoop.start();
}

void placePipes() {
//(0-1) * pipeHeight/2.
// 0 -> -128 (pipeHeight/4)
// 1 -> -128 - 256 (pipeHeight/4 - pipeHeight/2) = -3/4 pipeHeight
int randomPipeY = (int) (pipeY - pipeHeight/4 - Math.random()*(pipeHeight/2));
int openingSpace = boardHeight/4;

Pipe topPipe = new Pipe(topPipeImg);
topPipe.y = randomPipeY;
pipes.add(topPipe);

Pipe bottomPipe = new Pipe(bottomPipeImg);
bottomPipe.y = topPipe.y + pipeHeight + openingSpace;
pipes.add(bottomPipe);
}


public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}

public void draw(Graphics g) {
//background
g.drawImage(backgroundImg, 0, 0, this.boardWidth, this.boardHeight, null);

//bird
g.drawImage(birdImg, bird.x, bird.y, bird.width, bird.height, null);

//pipes
for (int i = 0; i < pipes.size(); i++) {
Pipe pipe = pipes.get(i);
g.drawImage(pipe.img, pipe.x, pipe.y, pipe.width, pipe.height, null);
}

//score
g.setColor(Color.white);

g.setFont(new Font("Arial", Font.PLAIN, 32));
if (gameOver) {
g.drawString("Game Over: " + String.valueOf((int) score), 10, 35);
}
else {
g.drawString(String.valueOf((int) score), 10, 35);
}

}

public void move() {
//bird
velocityY += gravity;
bird.y += velocityY;
bird.y = Math.max(bird.y, 0); //apply gravity to current bird.y, limit the bird.y to top of the canvas

//pipes
for (int i = 0; i < pipes.size(); i++) {
Pipe pipe = pipes.get(i);
pipe.x += velocityX;

if (!pipe.passed && bird.x > pipe.x + pipe.width) {
score += 0.5; //0.5 because there are 2 pipes! so 0.5*2 = 1, 1 for each set of pipes
pipe.passed = true;
}

if (collision(bird, pipe)) {
gameOver = true;
}
}

if (bird.y > boardHeight) {
gameOver = true;
}
}

boolean collision(Bird a, Pipe b) {
return a.x < b.x + b.width && //a's top left corner doesn't reach b's top right corner
a.x + a.width > b.x && //a's top right corner passes b's top left corner
a.y < b.y + b.height && //a's top left corner doesn't reach b's bottom left corner
a.y + a.height > b.y; //a's bottom left corner passes b's top left corner
}

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

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
// System.out.println("JUMP!");
velocityY = -9;

if (gameOver) {
//restart game by resetting conditions
bird.y = birdY;
velocityY = 0;
pipes.clear();
gameOver = false;
score = 0;
gameLoop.start();
placePipeTimer.start();
}
}
}

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

@Override
public void keyReleased(KeyEvent e) {}
}
62 changes: 62 additions & 0 deletions dvdanimation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class dvdanimation extends JPanel implements ActionListener {

private int x = 50, y = 50; // Initial position
private int xVelocity = 2, yVelocity = 2; // Movement speed
private final int WIDTH = 600, HEIGHT = 400; // Screen size
private final int DVD_WIDTH = 100, DVD_HEIGHT = 50; // DVD logo size
private final Timer timer;

public dvdanimation() {
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
this.setBackground(Color.BLACK);
timer = new Timer(10, this); // Timer to control animation speed
timer.start();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// Draw the "DVD" rectangle
g.setColor(Color.CYAN);
g.fillRect(x, y, DVD_WIDTH, DVD_HEIGHT);

// Draw "DVD" text
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.BOLD, 24));
g.drawString("DVD", x + 20, y + 40);
}

@Override
public void actionPerformed(ActionEvent e) {
// Update position
x += xVelocity;
y += yVelocity;

// Check for collisions with edges and reverse direction if needed
if (x < 0 || x + DVD_WIDTH > WIDTH) {
xVelocity = -xVelocity; // Reverse horizontal direction
}
if (y < 0 || y + DVD_HEIGHT > HEIGHT) {
yVelocity = -yVelocity; // Reverse vertical direction
}

repaint(); // Redraw with new position
}

public static void main(String[] args) {
JFrame frame = new JFrame("DVD Animation");
dvdanimation animation = new dvdanimation();

frame.add(animation);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}