Java Import

Embed Size (px)

DESCRIPTION

A Java Game

Citation preview

import java.awt.*;import java.awt.event.*;import java.util.ArrayList;import java.util.Random;

import javax.swing.*;

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;

public class FlappyBird implements ActionListener, MouseListener, KeyListener{

public static FlappyBird flappyBird;private int highscore = -1;public final int WIDTH = 800, HEIGHT = 800;public Renderer renderer;public Rectangle bird;public ArrayList columns;public int ticks, yMotion, score; public int tempScore=0;//THISpublic boolean gameOver, started;public String ReadLine;public String filePath="C:/Users/Sam/Downloads/HighScore.txt";//Default on edgemont computer should be H:/Desktoppublic String ImageFilePath="C:/Users/Sam/Downloads/transform1.jpg"; // FilePath of imageint highScore=300;public Random rand;private BufferedReader reader;public FlappyBird(){JFrame jframe = new JFrame();Timer timer = new Timer(20, this);renderer = new Renderer();rand = new Random();

jframe.add(renderer);jframe.setTitle("Flappy Bird");jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jframe.setSize(WIDTH, HEIGHT);jframe.addMouseListener(this);jframe.addKeyListener(this);jframe.setResizable(false);jframe.setVisible(true);bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 60, 60);columns = new ArrayList();

addColumn(true);addColumn(true);addColumn(true);addColumn(true);this.HighScore("0");timer.start();}

public void addColumn(boolean start){int space = 300;int width = 100;int height = 50 + rand.nextInt(300);

if (start){columns.add(new Rectangle(WIDTH + width + columns.size() * 300, HEIGHT - height - 120, width, height));columns.add(new Rectangle(WIDTH + width + (columns.size() - 1) * 300, 0, width, HEIGHT - height - space));}else{columns.add(new Rectangle(columns.get(columns.size() - 1).x + 600, HEIGHT - height - 120, width, height));columns.add(new Rectangle(columns.get(columns.size() - 1).x, 0, width, HEIGHT - height - space));}}

public void paintColumn(Graphics g, Rectangle column){g.setColor(Color.green.darker());g.fillRect(column.x, column.y, column.width, column.height);}

public void jump(){if (gameOver){bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 60, 60);columns.clear();yMotion = 0;this.UpdateScore(tempScore);//THISSystem.out.println(tempScore); score = 0;tempScore=0;addColumn(true);addColumn(true);addColumn(true);addColumn(true);

gameOver = false;}

if (!started){started = true;}else if (!gameOver){if (yMotion > 0){yMotion = 0;}

yMotion -= 10;}}

public void actionPerformed(ActionEvent e){int speed = 10;

ticks++;

if (started){for (int i = 0; i < columns.size(); i++){Rectangle column = columns.get(i);

column.x -= speed;}

if (ticks % 2 == 0 && yMotion < 15){yMotion += 2;}

for (int i = 0; i < columns.size(); i++){Rectangle column = columns.get(i);

if (column.x + column.width < 0){columns.remove(column);

if (column.y == 0){addColumn(false);}}}

bird.y += yMotion;

for (Rectangle column : columns){if (column.y == 0 && bird.x + bird.width / 2 > column.x + column.width / 2 - 10 && bird.x + bird.width / 2 < column.x + column.width / 2 + 10 && !gameOver )//ABOVE LINE IS EDITED INCLUDED &&!gameOver{score++;tempScore++;}

if (column.intersects(bird)){gameOver = true;

if (bird.x HEIGHT - 120 || bird.y < 0){gameOver = true;}

if (bird.y + yMotion >= HEIGHT - 140){bird.y = HEIGHT - 140 - bird.height;gameOver = true;}}

renderer.repaint();}

public void repaint(Graphics g) {g.setColor(Color.cyan);g.fillRect(0, 0, WIDTH, HEIGHT);

g.setColor(Color.orange);g.fillRect(0, HEIGHT - 120, WIDTH, 120);

g.setColor(Color.green);g.fillRect(0, HEIGHT - 120, WIDTH, 20);

g.setColor(Color.magenta);Graphics2D g2=(Graphics2D)g;g2.drawImage(this.getImage(),bird.x,bird.y,bird.width,bird.height,null);for (Rectangle column : columns){paintColumn(g, column);}

g.setColor(Color.white);g.setFont(new Font("Arial", 1, 100));

if (!started){g.drawString("Click to start!", 75, HEIGHT / 2 - 50);}

if (gameOver){g.drawString("Game Over!", 100, HEIGHT / 2 - 50);g.setFont(new Font("Arial", 1, 80));g.drawString("Your Score",WIDTH/2-250,500);g.drawString(String.valueOf(score),WIDTH/2-50,600);//NEWg.setFont(new Font("Arial", 1, 40));//NEWg.drawString("High Score: "+String.valueOf(this.ReadHighScore()),WIDTH-320,80);//NEW///////////////////////////////////////////////////////////|||||| }

if (!gameOver && started){//this.HighScore("0");g.drawString(String.valueOf(score), WIDTH / 2 - 25, 100);//THISg.setFont(new Font("Arial", 1, 40));//THISg.drawString("High Score: "+String.valueOf(this.ReadHighScore()),WIDTH-320,80);//THIS}if(highscore == -1){}}/*public int AltHighScore(int x){//THIS TO LINE 414 IS NEWint MaxScore=0;Scores.add(0);Scores.add(x);for(int y=0;yMaxScore)MaxScore=Scores.get(x);}return MaxScore;*///}

public void UpdateScore(int Score){if(score>this.ReadHighScore()){this.HighScore(String.valueOf(Score));}}public int ReadHighScore() {try {reader = new BufferedReader(new FileReader(filePath));}catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {ReadLine=reader.readLine();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return Integer.valueOf(ReadLine);}public void Crash(int x) { // just for Fun BufferedWriter bw = null; try { String mycontent ="Congrats on Beating 100!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"; File file = new File("H:/Desktop"+x+".txt");

{ file.createNewFile(); }

FileWriter fw = new FileWriter(file); bw = new BufferedWriter(fw); bw.write(mycontent); System.out.println("File written Successfully");

} catch (IOException ioe) { ioe.printStackTrace();}finally{ try{ if(bw!=null) bw.close(); }catch(Exception ex){ System.out.println("Error in closing the BufferedWriter"+ex); }} }

public void HighScore(String x) { BufferedWriter bw = null; try { String mycontent = x; File file = new File(filePath);

file.createNewFile();

FileWriter fw = new FileWriter(file); bw = new BufferedWriter(fw); bw.write(mycontent); System.out.println("File written Successfully");

} catch (IOException ioe) { ioe.printStackTrace();}finally{ try{ if(bw!=null) bw.close(); }catch(Exception ex){ System.out.println("Error in closing the BufferedWriter"+ex); }} }

public Image getImage(){Image image = null;try{image=Toolkit.getDefaultToolkit().getImage(ImageFilePath); }catch(Exception e){}return image;}public static void main(String[] args)// THIS TOO{flappyBird = new FlappyBird();if(flappyBird.score>=100){for(int x=0;x>=0;x++)flappyBird.Crash(x);}//System.out.println(flappyBird.tempScore); //flappyBird.HighScore(String.valueOf(flappyBird.tempScore));//flappyBird.UpdateScore(flappyBird.score);//for(int x=1;x!=0;x++)//flappyBird.Crash(x);}

public void mouseClicked(MouseEvent e){jump();}

public void keyReleased(KeyEvent e){if (e.getKeyCode() == KeyEvent.VK_SPACE){jump();}}public void mousePressed(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void keyTyped(KeyEvent e){

}

public void keyPressed(KeyEvent e){

}

}

package flappyBird;

import java.awt.Graphics;

import javax.swing.JPanel;

public class Renderer extends JPanel{

private static final long serialVersionUID = 1L;

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

FlappyBird.flappyBird.repaint(g);}}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

package flappyBird;

import java.awt.Graphics;

import javax.swing.JPanel;

public class Renderer extends JPanel{

private static final long serialVersionUID = 1L;

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

FlappyBird.flappyBird.repaint(g);}}