Programming the game:
Our class Game follows the pattern above. The core of its initialization section is reproduced below:
// get a reference to the graphics
g = getGraphics();
// the game will count down from 125 gameTicks = 125;
try
{
// get the robot and starry portal images
// an IO exception may occur, hence the try-catch
robotImage = Image.createImage("/res/robot.PNG");
starPortalImage = Image.createImage("/res/portal.PNG");
}
catch (Exception e)
{
e.printStackTrace();
}
// create the sprites
robot = new RobotSprite(robotImage, ROBOT_WIDTH, ROBOT_HEIGHT);
starPortal = new Sprite(starPortalImage);
// make the robot's collision rectangle one pixel smaller
// all round, otherwise it can't negotiate the obstacles robot.defineCollisionRectangle
(1, 1, robot.getWidth() - 1, robot.getHeight() - 1);
// move the star portal to the right square starPortal.setPosition(100,140);
// create the layer manager layerManager = new LayerManager();
// add the sprites to the layer manager layerManager.append(robot); layerManager.append(starPortal);
// create the background using a helper method background = createBackground();
// add the background to the layer manager
// it will be behind the sprites because it was appended later layerManager.append(background);
// the first tile of the animated water animatedWaterTileIndex = 2;
This uses the helper method createBackground()mentioned previously. The method returns the background as a TiledLayer of 8 6 8 cells, each 20 6 20 pixels. Placing the code that does this in the helper method helps to streamline the initialization code.
The game loop is as follows:
while (gameTicks >= 0) // loop until time runs out
{
// display the graphics draw(g);
// decrement the game count gameTicks--;
// wait a little before the next frame otherwise
// the animation will be too rapid try
{
Thread.sleep(FRAME_DELAY);
}
catch (Exception e)
{
e.printStackTrace();
}
// animate the robot robot.nextFrame();
// cycle water through tiles 2, 3, 4, 5 animatedWaterTileIndex ++;
if (animatedWaterTileIndex > 5)
{
animatedWaterTileIndex = 2;
}
background.setAnimatedTile(-1, animatedWaterTileIndex);
// check the key states checkKeyStates();
// check for collisions checkCollisions();
// check robot is within bounds checkRobotInBounds();
}
This uses several helper methods:
- draw(g) is responsible for painting everything visible on screen. The various steps involved have been gathered together into a single method so the main game loop logic will be easier to follow.
- checkKeyStates() checks whether the user has pressed a key; if so, an appropriate message is sent to the robot, telling it to move and in what direction.
- checkCollisions() checks whether the robot has bumped into the background; if it has, its last move is undone, putting it back where it was before. This guarantees that the robot cannot move over the background.
- checkBounds() checks whether the robot has moved outside the boundary of the micro-world. If it is out of bounds, its last move is undone. This guarantees that the robot is confined to the micro-world.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Programming the game homework and assignments? Live Programming the game experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Programming the game homework help, java assignment help and Programming the game projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours