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

Add files via upload #20

Open
wants to merge 3 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
Binary file added DVD1/DVDBouncingAnimation.class
Binary file not shown.
92 changes: 92 additions & 0 deletions DVD1/DVDBouncingAnimation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@


import javax.swing.*;
import java.awt.*;

public class DVDBouncingAnimation extends JPanel {



// Panel dimensions
private final int PANEL_WIDTH = 800;
private final int PANEL_HEIGHT = 600;

// DVD logo dimensions
private final int LOGO_WIDTH = 100;
private final int LOGO_HEIGHT = 50;

// DVD logo position
private int logoX = 100; // Initial X position
private int logoY = 100; // Initial Y position

// DVD logo movement speed
private int xSpeed = 5; // Horizontal speed
private int ySpeed = 5; // Vertical speed

// DVD logo color
private Color logoColor = Color.RED;

// Constructor to set up the animation panel
public DVDBouncingAnimation() {
setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
setBackground(Color.BLACK);

// Timer for animation loop
Timer timer = new Timer(20, e -> moveLogo());
timer.start();
}

// Method to move the DVD logo and handle bouncing
private void moveLogo() {
// Update logo position
logoX += xSpeed;
logoY += ySpeed;

// Bounce off left and right edges
if (logoX <= 0 || logoX + LOGO_WIDTH >= PANEL_WIDTH) {
xSpeed = -xSpeed;
changeColor(); // Change color on bounce
}

// Bounce off top and bottom edges
if (logoY <= 0 || logoY + LOGO_HEIGHT >= PANEL_HEIGHT) {
ySpeed = -ySpeed;
changeColor(); // Change color on bounce
}

// Repaint the panel to show the updated position
repaint();
}

// Method to change the logo's color randomly
private void changeColor() {
logoColor = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
}

// Custom painting of the DVD logo
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// Draw the DVD logo
g.setColor(logoColor);
g.fillRect(logoX, logoY, LOGO_WIDTH, LOGO_HEIGHT);

// Draw "DVD" text on the logo
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("DVD", logoX + 25, logoY + 30);
}

// Main method to run the animation
public static void main(String[] args) {
JFrame frame = new JFrame("DVD Bouncing Animation");
DVDBouncingAnimation animation = new DVDBouncingAnimation();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(animation);
frame.pack();
frame.setLocationRelativeTo(null); // Center the window
frame.setVisible(true);
}
}
Binary file added Game/CarGamePanel$Obstacle.class
Binary file not shown.
Binary file added Game/CarGamePanel.class
Binary file not shown.
177 changes: 177 additions & 0 deletions Game/CarGamePanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class CarGamePanel extends JPanel implements ActionListener, KeyListener {
private Image roadImage;
private Image carImage;
private Image[] obstacleImages;
private int carX = 250;
private int carY = 500;
private int carSpeed = 10;
private Timer timer;
private ArrayList<Obstacle> obstacles;
private int obstacleSpeed = 5;
private Random random;
private boolean gameOver;
private int score;

private static class Obstacle {
Rectangle rect;
Image image;

Obstacle(Rectangle rect, Image image) {
this.rect = rect;
this.image = image;
}
}

public CarGamePanel() {
try {
roadImage = ImageIO.read(getClass().getResource("road.png"));
carImage = ImageIO.read(getClass().getResource("car.png"));
obstacleImages = new Image[3];
obstacleImages[0] = ImageIO.read(getClass().getResource("car.png"));
obstacleImages[1] = ImageIO.read(getClass().getResource("car.png"));
obstacleImages[2] = ImageIO.read(getClass().getResource("car.png"));
} catch (IOException e) {
System.err.println("Error loading images:");
e.printStackTrace();
}

obstacles = new ArrayList<>();
random = new Random();
timer = new Timer(40, this);
timer.start();

addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
gameOver = false;
score = 0;
}

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

if (roadImage != null) {
g.drawImage(roadImage, 0, 0, getWidth(), getHeight(), this);
}
if (carImage != null) {
g.drawImage(carImage, carX, carY, 60, 90, this);
}

for (Obstacle obstacle : obstacles) {
g.drawImage(obstacle.image, obstacle.rect.x, obstacle.rect.y, obstacle.rect.width, obstacle.rect.height,
this);
}

g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 18));
g.drawString("Score: " + score, 10, 20);

if (gameOver) {
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString("Game Over!", getWidth() / 2 - 100, getHeight() / 2);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Press R to Restart", getWidth() / 2 - 90, getHeight() / 2 + 40);
}
}

@Override
public void actionPerformed(ActionEvent e) {
if (!gameOver) {
for (int i = 0; i < obstacles.size(); i++) {
Obstacle obstacle = obstacles.get(i);
obstacle.rect.y += obstacleSpeed;

if (obstacle.rect.y > getHeight()) {
obstacles.remove(i);
i--;
score++;
}

if (obstacle.rect.intersects(new Rectangle(carX, carY, 60, 90))) {
gameOver = true;
timer.stop();
}
}
if (random.nextInt(60) == 0) {
int obstacleX = random.nextInt(280) + 120;
int obstacleY = 0;
int obstacleImageIndex = random.nextInt(obstacleImages.length);
Image obstacleImage = obstacleImages[obstacleImageIndex];

boolean overlap = false;
for (Obstacle existingObstacle : obstacles) {
if (existingObstacle.rect.intersects(new Rectangle(obstacleX, obstacleY, 60, 90))) {
overlap = true;
break;
}
}

if (!overlap) {
obstacles.add(new Obstacle(new Rectangle(obstacleX, obstacleY, 60, 90), obstacleImage));
}
}

repaint();
}
}

@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (gameOver && key == KeyEvent.VK_R) {
carX = 250;
carY = 500;
obstacles.clear();
score = 0;
gameOver = false;
timer.start();
repaint();
} else {
if (key == KeyEvent.VK_LEFT && carX > 120) {
carX -= carSpeed;
}
if (key == KeyEvent.VK_RIGHT && carX < 400) {
carX += carSpeed;
}
if (key == KeyEvent.VK_UP && carY > 0) {
carY -= carSpeed;
}
if (key == KeyEvent.VK_DOWN && carY < getHeight() - 90) {
carY += carSpeed;
}
}
}

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent e) {
}

public static void main(String[] args) {
JFrame frame = new JFrame("Car Game");
CarGamePanel gamePanel = new CarGamePanel();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(gamePanel);
frame.setSize(600, 700);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}
}
Binary file added Game/DVD.class
Binary file not shown.
Binary file added Game/car.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Game/road.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions Saini.java/FlappyBird.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import javax.swing.*;

public class FlappyBird {
public static void main(String[] args) {
JFrame frame = new JFrame("Flappy Bird");
GamePanel gamePanel = new GamePanel();
frame.add(gamePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
}
}
Loading