Stap 5: Speler en belemmering klassen
Sinds we zijn begonnen met het skelet van klassen, vooraf we nu aan het schrijven van de speler en belemmering klassen. Standaard, moeten we weten dat deze klassen een fundamentele doel dienen: het laden van de afbeelding van de resource die wordt gebruikt door het object, schaal van de afbeelding de gewenste afmetingen krijgen en instellen van de X- en Y-coördinaten (de linkerbovenhoek van de beelden, de xLoc en de yLoc) en krijgen de breedte en de hoogte van de afbeelding van het object.
Want ik heb dit project al voltooid, I 'm gonna drop een spoiler op u allen: voor de botsingdetectie, uitgelegd in een latere stap, u moet ook een methode die geeft als resultaat een rechthoek die het object omvat, evenals een methode die retourneert van het object BufferedImage. Nu dat je alles wat nodig is weet, zal ik u de volledige code voor de vogel, BottomPipe en TopPipe klassen. Alle drie de klassen zijn identiek gestructureerd, die alleen verschillen in naamgeving.
Wanneer de klassen zijn eerste geïnstantieerd, worden de gewenste breedte en hoogte van de beelden doorgegeven aan de klassen in de constructor, die zal automatisch de schaal van de afbeelding door het aanroepen van de methode scale. Tot slot, u zult opmerken er getWidth en getHeight methoden die een try/catch blok bevat. Dit is om te helpen met het voorkomen van logica kwesties in TopClass.
Zult u merken wanneer u de BufferedImage maakt bezwaar, het wordt gemaakt met behulp van het ARGB-type. Dit is gebaseerd op het verlangen naar een alfa-kanaal moet worden opgenomen. U zult zien waarom in de botsing detectie stap.
import java.awt.Graphics; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.Image; import java.awt.image.BufferedImage; public class Bird { //global variables private Image flappyBird; private int xLoc = 0, yLoc = 0; /** * Default constructor */ public Bird(int initialWidth, int initialHeight) { flappyBird = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("resources/blue_bird.png")); scaleBird(initialWidth, initialHeight); } /** * Method to scale the bird sprite into the desired dimensions * width The desired width of the flappy bird * height The desired height of the flappy bird */ public void scaleBird(int width, int height) { flappyBird = flappyBird.getScaledInstance(width, height, Image.SCALE_SMOOTH); } /** * Getter method for the flappyBird object. * Image */ public Image getBird() { return flappyBird; } /** * Method to obtain the width of the Bird object * int */ public int getWidth() { try { return flappyBird.getWidth(null); } catch(Exception e) { return -1; } } /** * Method to obtain the height of the Bird object * int */ public int getHeight() { try { return flappyBird.getHeight(null); } catch(Exception e) { return -1; } } /** * Method to set the x location of the Bird object * x */ public void setX(int x) { xLoc = x; } /** * Method to get the x location of the Bird object * int */ public int getX() { return xLoc; } /** * Method to set the y location of the Bird object * y */ public void setY(int y) { yLoc = y; } /** * Method to get the y location of the Bird object * int */ public int getY() { return yLoc; } /** * Method used to acquire a Rectangle that outlines the Bird's image * Rectangle outlining the bird's position on screen */ public Rectangle getRectangle() { return (new Rectangle(xLoc, yLoc, flappyBird.getWidth(null), flappyBird.getHeight(null))); } /** * Method to acquire a BufferedImage that represents the Bird's image object * Bird's BufferedImage object */ public BufferedImage getBI() { BufferedImage bi = new BufferedImage(flappyBird.getWidth(null), flappyBird.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = bi.getGraphics(); g.drawImage(flappyBird, 0, 0, null); g.dispose(); return bi; } }
import java.awt.Graphics; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.Image; import java.awt.image.BufferedImage; public class BottomPipe { //global variables private Image bottomPipe; private int xLoc = 0, yLoc = 0; /** * Default constructor */ public BottomPipe(int initialWidth, int initialHeight) { bottomPipe = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("resources/tube_bottom.png")); scaleBottomPipe(initialWidth, initialHeight); } /** * Method to scale the BottomPipe sprite into the desired dimensions * width The desired width of the BottomPipe * height The desired height of the BottomPipe */ public void scaleBottomPipe(int width, int height) { bottomPipe = bottomPipe.getScaledInstance(width, height, Image.SCALE_SMOOTH); } /** * Getter method for the BottomPipe object. * Image */ public Image getPipe() { return bottomPipe; } /** * Method to obtain the width of the BottomPipe object * int */ public int getWidth() { return bottomPipe.getWidth(null); } /** * Method to obtain the height of the BottomPipe object * int */ public int getHeight() { return bottomPipe.getHeight(null); } /** * Method to set the x location of the BottomPipe object * x */ public void setX(int x) { xLoc = x; } /** * Method to get the x location of the BottomPipe object * int */ public int getX() { return xLoc; } /** * Method to set the y location of the BottomPipe object * y */ public void setY(int y) { yLoc = y; } /** * Method to get the y location of the BottomPipe object * int */ public int getY() { return yLoc; } /** * Method used to acquire a Rectangle that outlines the BottomPipe's image * Rectangle outlining the BottomPipe's position on screen */ public Rectangle getRectangle() { return (new Rectangle(xLoc, yLoc, bottomPipe.getWidth(null), bottomPipe.getHeight(null))); } /** * Method to acquire a BufferedImage that represents the TopPipe's image object * TopPipe's BufferedImage object */ public BufferedImage getBI() { BufferedImage bi = new BufferedImage(bottomPipe.getWidth(null), bottomPipe.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = bi.getGraphics(); g.drawImage(bottomPipe, 0, 0, null); g.dispose(); return bi; } }
import java.awt.Graphics; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.Image; import java.awt.image.BufferedImage; public class TopPipe { //global variables private Image topPipe; private int xLoc = 0, yLoc = 0; /** * Default constructor */ public TopPipe(int initialWidth, int initialHeight) { topPipe = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("resources/tube_top.png")); scaleTopPipe(initialWidth, initialHeight); } /** * Method to scale the topPipe sprite into the desired dimensions * width The desired width of the topPipe * height The desired height of the topPipe */ public void scaleTopPipe(int width, int height) { topPipe = topPipe.getScaledInstance(width, height, Image.SCALE_SMOOTH); } /** * Getter method for the TopPipe object. * Image */ public Image getPipe() { return topPipe; } /** * Method to obtain the width of the TopPipe object * int */ public int getWidth() { return topPipe.getWidth(null); } /** * Method to obtain the height of the TopPipe object * int */ public int getHeight() { return topPipe.getHeight(null); } /** * Method to set the x location of the TopPipe object * x */ public void setX(int x) { xLoc = x; } /** * Method to get the x location of the TopPipe object * int */ public int getX() { return xLoc; } /** * Method to set the y location of the TopPipe object * y */ public void setY(int y) { yLoc = y; } /** * Method to get the y location of the TopPipe object * int */ public int getY() { return yLoc; } /** * Method used to acquire a Rectangle that outlines the TopPipe's image * Rectangle outlining the TopPipe's position on screen */ public Rectangle getRectangle() { return (new Rectangle(xLoc, yLoc, topPipe.getWidth(null), topPipe.getHeight(null))); } /** * Method to acquire a BufferedImage that represents the TopPipe's image object * TopPipe's BufferedImage object */ public BufferedImage getBI() { BufferedImage bi = new BufferedImage(topPipe.getWidth(null), topPipe.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = bi.getGraphics(); g.drawImage(topPipe, 0, 0, null); g.dispose(); return bi; } }