Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package RedHazard;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.Random;
- import javax.swing.JFrame;
- import javax.swing.Timer;
- public class Redhazard implements ActionListener, KeyListener
- {
- public static Redhazard rh;
- public int width = 700, height = 700;
- public int minscore = -5;
- public int maxscore = 10;
- public Renderer renderer;
- public Ship ship;
- public Obstacle obstacle[];
- public boolean z, left, right;
- public JFrame jframe;
- public Random random;
- public int gameStatus = 0;
- public Goal goal;
- public Redhazard()
- {
- Timer timer = new Timer(20, this);
- random = new Random();
- jframe = new JFrame("RedHazard");
- renderer = new Renderer();
- jframe.setSize(width + 15, height + 15);
- jframe.setVisible(true);
- jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jframe.add(renderer);
- jframe.addKeyListener(this);
- timer.start();
- }
- public void start()
- {
- gameStatus = 1;
- ship = new Ship(this);
- obstacle = new Obstacle[8];
- obstacle[1] = new Obstacle(this, 1);
- obstacle[2] = new Obstacle(this, 1);
- obstacle[3] = new Obstacle(this, 1);
- obstacle[4] = new Obstacle(this, 1);
- obstacle[0] = new Obstacle(this, 0);
- obstacle[5] = new Obstacle(this, 0);
- obstacle[6] = new Obstacle(this, 0);
- obstacle[7] = new Obstacle(this, 0);
- goal = new Goal(this);
- }
- public void update()
- {
- if(ship.score <= minscore)
- {
- gameStatus = 2;
- }
- if(ship.score >= maxscore)
- {
- gameStatus = 2;
- }
- if(left)
- {
- ship.move(true);
- }
- if(right)
- {
- ship.move(false);
- }
- obstacle[0].update(ship);
- obstacle[1].update(ship);
- obstacle[2].update(ship);
- obstacle[3].update(ship);
- obstacle[4].update(ship);
- obstacle[5].update(ship);
- obstacle[6].update(ship);
- obstacle[7].update(ship);
- goal.update(ship);
- }
- public void render(Graphics2D g)
- {
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, width, height);
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- if(gameStatus == 0)
- {
- g.setColor(Color.WHITE);
- g.setFont(new Font("Arial", 1, 50));
- g.drawString("Red Hazard", width / 2 - 75, 50);
- g.setFont(new Font("Arial", 1, 30));
- g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);
- }
- if(gameStatus == 1)
- {
- g.setColor(Color.WHITE);
- g.setFont(new Font("Arial", 1, 16));
- g.drawString(String.valueOf(ship.score), width / 2 - 90, 50);
- ship.render(g);
- obstacle[0].render(g);
- obstacle[1].render(g);
- obstacle[2].render(g);
- obstacle[3].render(g);
- obstacle[4].render(g);
- obstacle[5].render(g);
- obstacle[6].render(g);
- obstacle[7].render(g);
- goal.render(g);
- }
- if(gameStatus == 2)
- {
- if(ship.score <= minscore)
- {
- g.setColor(Color.RED);
- g.setFont(new Font("Arial", 1, 50));
- g.drawString("You Lose", width / 2 - 170, 200);
- }
- else if(ship.score >= maxscore)
- {
- g.setColor(Color.GREEN);
- g.setFont(new Font("Arial", 1, 50));
- g.drawString("You Win", width / 2 - 170, 200);
- }
- g.setColor(Color.GREEN);
- g.setFont(new Font("Arial", 1, 30));
- g.drawString("Press Space to Play Again", width / 2 - 170, height / 2 - 25);
- }
- }
- @Override
- public void actionPerformed(ActionEvent e)
- {
- if(gameStatus == 1)
- {
- update();
- }
- renderer.repaint();
- }
- public static void main(String[] args)
- {
- rh = new Redhazard();
- }
- @Override
- public void keyPressed(KeyEvent e)
- {
- int id = e.getKeyCode();
- if(id == KeyEvent.VK_LEFT)
- {
- left = true;
- }
- else if(id == KeyEvent.VK_RIGHT)
- {
- right = true;
- }
- else if(id == KeyEvent.VK_SPACE)
- {
- if(gameStatus == 2 || gameStatus == 0)
- {
- gameStatus = 1;
- start();
- }
- }
- }
- @Override
- public void keyReleased(KeyEvent e)
- {
- int id = e.getKeyCode();
- if(id == KeyEvent.VK_LEFT)
- {
- left = false;
- }
- else if(id == KeyEvent.VK_RIGHT)
- {
- right = false;
- }
- }
- @Override
- public void keyTyped(KeyEvent e)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement