/ Published in: Java
OO Software Development Project
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * */ package burgertime; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.geom.RectangularShape; import java.awt.geom.Rectangle2D.Double; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; import javax.imageio.ImageIO; import javax.swing.JComponent; /** * DrawableComponent class draws everything in the game, handles pausing / * resuming * * @author xiaox. trowbrct. obrienm. */ private static final int FRAMES_PER_SECOND = 30; private static final long REPAINT_INTERVAL_MS = 1000 / FRAMES_PER_SECOND; public boolean displayPowerup; public long timePassed; private double powerupTime; public boolean[] isLevelThreading; private boolean leveledUp; @SuppressWarnings("javadoc") public Hero hero; public Burger[] burger; private ArrayList<Drawable> drawableParts; private ArrayList<Rectangle2D> pathList; private Score score; private boolean isPaused; @SuppressWarnings("javadoc") public Level level; public Game game; /** * loads the level, starts the threads, initializes all of the variables * @throws IOException * */ this.isLevelThreading = new boolean[4]; this.isLevelThreading[0] = true; this.isLevelThreading[1] = false; this.isLevelThreading[2] = false; this.isLevelThreading[3] = false; this.score=score; this.game = game; this.level = new Level(this,this.score); this.level.loadLevel(); this.initialize(); this.startThread(); } @SuppressWarnings("deprecation") private void startThread() { // Creates a separate "thread of execution" to trigger periodic // repainting of this component. @Override public void run() { // Periodically asks Java to repaint this component try { while (true) { repaint(); timePassed(); } // Stop when interrupted } } }; this.guardThread1.resume(); this.guardThread2.resume(); this.guardThread3.resume(); this.guardThread4.resume(); for (int i = 0; i < 4; i++) { this.burger[i].fallThread.resume(); } } /** * Initialize all of the variables from the level class * */ public void initialize() { //random number between 10 and 20 for powerup spawn time this.displayPowerup = false; if (this.level.currentLevel == 1){ } else if (this.level.currentLevel == 2){ } else if (this.level.currentLevel == 3){ } else { } this.leveledUp = false; this.isLevelThreading[this.level.currentLevel - 1] = true; this.pathList = new ArrayList<Rectangle2D>(); this.pathList = this.level.getPath(); this.pathList.addAll(this.level.getPlates()); this.guard[0] = this.level.getGuard(0); this.guard[1] = this.level.getGuard(1); this.guard[2] = this.level.getGuard(2); this.guard[3] = this.level.getGuard(3); this.burger = new Burger[4]; this.burger[0] = this.level.getBurger(0); this.burger[1] = this.level.getBurger(1); this.burger[2] = this.level.getBurger(2); this.burger[3] = this.level.getBurger(3); for (int i=0;i<this.burger.length;i++){ this.burger[i].readImage(); } this.heroRect = this.level.getHero(); this.heroRect.getY()), this.heroRect.getWidth(), this.heroRect.getHeight(), this.pathList, this.burger, this.hero.lives = 3; this.hero.burgers = this.burger; this.drawableParts = new ArrayList<Drawable>(); for (int i = 0; i < this.guard.length; i++) { } this.drawableParts.add(this.hero); this.guardThread1.start(); this.guardThread2.start(); this.guardThread3.start(); this.guardThread4.start(); } @Override super.paintComponent(g); for (int sprays = 1; sprays <= this.hero.pepperSprays; sprays++) { 505, 30, 30); g2.fill(spray); } for (int lives = 1; lives <= this.hero.lives; lives++) { try { g2.drawImage(image,null,30*lives+10,500); e.printStackTrace(); } } for (int i = 0; i < this.pathList.size(); i++) { this.drawPathway(g2, this.pathList.get(i)); } for (Drawable d : this.drawableParts) { this.drawDrawable(g2, d); } for (int i = 0; i < this.burger.length; i++) { for (Burger d : this.burger) { this.drawBurger(g2, d); } } if (this.hero.pepperDeployed) { g2.fill(this.hero.deployPepperSpray()); } if (this.displayPowerup) { if (this.isPaused) { this.displayPowerup = false; } else { g2.draw(this.powerUp); int x = (int) this.powerUp.getX(); int y = (int) this.powerUp.getY(); g2.drawImage(this.powerup, null, x, y); @Override public void run() { DrawableComponent.this.displayPowerup = false; .currentTimeMillis(); } }, 4500); } } } /** * This helper method draws the given drawable object on the given graphics * area. * * @param g2 * the graphics area on which to draw * @param d * the drawable object */ g2.drawImage(d.getImage(), null, } if (pathSegment.getHeight() > pathSegment.getWidth()) { for (int i = 0; i < pathSegment.getHeight(); i += 4) { .getY() + i, pathSegment.getWidth(), 2)); } } else { g2.fill(pathSegment); } } for (int i = 0; i < shape.length; i++) { for (int j = 0; j < shape[i].length; j++) { g2.drawImage(image[i][j], null, (int) shape[i][j].getX(), (int) shape[i][j].getY()); } } } /** * When the hero dies, reset position of the characters and burgers * */ public void resetCharacters() { this.hero.resetPosition(); for (int i = 0; i < 4; i++) { this.burger[i].resetPosition(); } } @Override public void timePassed() { this.displayPowerup = true; } if (this.displayPowerup){ if (this.powerUp.intersects(this.hero.character)){ this.score.addScore(500); this.hero.pepperSprays++; this.displayPowerup = false; } } if (!this.hero.pepperDeployed) { if (this.hero.isDead()) { this.resetCharacters(); this.hero.heroState = false; } if (this.hero.contactWithGuard(this.hero.getHeroCorners(0, 0))) { this.hero.die(); } } if (this.hero.lives == 0) { this.isLevelThreading[this.level.currentLevel - 1] = false; this.game.gameOver(true); this.hero.lives = -1; } int plateCount = 0; if (!this.leveledUp) { for (int i = 0; i < 4; i++) { if (this.burger[i].isPlated()) { plateCount++; } } } if (plateCount == 4) { this.leveledUp = true; @Override public void run() { int level = DrawableComponent.this.level.currentLevel; DrawableComponent.this.level.upLevel(); if (level != DrawableComponent.this.level.currentLevel) { DrawableComponent.this.leveledUp = false; } } }, 1500); } } /* * (non-Javadoc) * * @see burgertime.Temporal#setIsPaused(boolean) */ @SuppressWarnings("deprecation") @Override public void setIsPaused(boolean isPaused) { this.isPaused = isPaused; for (int i = 0; i < 4; i++) { this.burger[i].setIsPaused(this.isPaused); } if (!this.isPaused) { startThread(); } else { this.guardThread1.suspend(); this.guardThread2.suspend(); this.guardThread3.suspend(); this.guardThread4.suspend(); for (int i = 0; i < 4; i++) { this.burger[i].fallThread.suspend(); } } } /* * (non-Javadoc) * * @see burgertime.Temporal#getIsPaused() */ @Override public boolean getIsPaused() { return this.isPaused; } }