一个J2ME地牢游戏的源码 |
| 作者:J2MEDEV 来源:J2MEDEV 发布时间:2006-7-25 9:06:59 |
|
e calculate the distance to fall by raising two // to the power of the absolute value of myIsJumping. vertical = (2 << (myIsJumping)); } // now we temporarily move the princess the desired // vertical distance (with the corresponding horizontal // distance also thrown in), and see if she hits anything: myPrincess.move(horizontal, vertical); if (checkCollision()) { // here we're in the case where she did hit something. // we move her back into position and then see what // to do about it. myPrincess.move(-horizontal, -vertical); if (vertical > 0) { // in this case the player is falling. // so we need to determine precisely how // far she can fall before she hit the bottom vertical = 0; // we temporarily move her the desired horizontal // distance while calculating the corresponding // vertical distance. myPrincess.move(horizontal, 0); while (!checkCollision()) { vertical++; myPrincess.move(0, 1); } // now that we've calculated how far she can fall, // we move her back to her earlier position myPrincess.move(-horizontal, -vertical); // we subtract 1 pixel from the distance calculated // because once she has actually collided with the // floor, she's gone one pixel too far... vertical--; // now that she's hit the floor, she's not jumping // anymore. myIsJumping = NO_JUMP; } else { // in this case we're going up, so she // must have hit her head. // This next if is checking for a special // case where there's room to jump up exactly // one square. In that case we increase the // value of myIsJumping in order to make the // princess not rise as high. The details // of the calculation in this case were found // through trial and error: if (myIsJumping == NO_JUMP + 2) { myIsJumping++; vertical = -(2 << (-myIsJumping)); // now we see if the special shortened jump // still makes her hit her head: // (as usual, temporarily move her to test // for collisions) myPrincess.move(horizontal, vertical); if (checkCollision()) { // if she still hits her head even // with this special shortened jump, // then she was not meant to jump... myPrincess.move(-horizontal, -vertical); vertical = 0; myIsJumping = NO_JUMP; } else { // now that we've chhecked for collisions, // we move the player back to her earlier // position: myPrincess.move(-horizontal, -vertical); } } else { // if she hit her head, then she should not // jump up. vertical = 0; myIsJumping = NO_JUMP; } } } else { // since she didn't hit anything when we moved // her, then all we have to do is move her back. myPrincess.move(-horizontal, -vertical); } return (vertical); } /** * Internal to requestMove. Once the moves have been determined, actually * perform the move. */ private void move(int horizontal, int vertical) { // repaint only if we actually change something: if ((horizontal != 0) || (vertical != 0)) { myModifiedSinceLastPaint = true; } // if the princess is moving left or right, we set // her image to be facing the right direction: if (horizontal > 0) { myPrincess.setTransform(Sprite.TRANS_NONE); } else if (horizontal < 0) { myPrincess.setTransform(Sprite.TRANS_MIRROR); } // if she's jumping or falling, we set the image to // the frame where the skirt is inflated: if (vertical != 0) { myPrincess.setFrame(0); // if she's just running, we alternate between the // two frames: } else if (horizontal != 0) { if (myPrincess.getFrame() == 1) { myPrincess.setFrame(0); } else { myPrincess.setFrame(1); } } // move the position of the view window so that // the player stays in the center: myViewWindowX += horizontal; myViewWindowY += vertical; // after all that work, we finally move the // princess for real!!! myPrincess.move(horizontal, vertical); } //------------------------------------------------------- // sprite interactions /** * Drops the currently held key and picks up another. */ void putDownPickUp() { // we do not want to allow the player to put // down the key in the air, so we verify that // we're not jumping or falling first: if ((myIsJumping == NO_JUMP) && (myPrincess.getY() % SQUARE_WIDTH == 0)) { // since we're picking something up or putting // something down, the display changes and needs // to be repainted: setNeedsRepaint(); // if the thing we're picking up is the crown, // we're done, the player has won: if (myPrincess.collidesWith(myCrown, true)) { myCanvas.setGameOver(); return; } // keep track of the key we're putting down in // order to place it correctly: DoorKey oldHeld = myHeldKey; myHeldKey = null; // if the princess is on top of another key, // that one becomes the held key and is hence // made invisible: for (int i = 0; i < myKeys.length; i++) { // we check myHeldKey for null because we don't // want to accidentally pick up two keys. if ((myPrincess.collidesWith(myKeys[i], true)) && (myHeldKey == null)) { myHeldKey = myKeys[i]; myHeldKey.setVisible(false); } } if (oldHeld != null) { // place the key we're putting down in the Princess's // current position and make it visible: oldHeld.setPosition(myPrincess.getX(), myPrincess.getY()); oldHeld.setVisible(true); } } } /** * Checks of the player hits a stone wall or a door. */ boolean checkCollision() { boolean retVal = false; // the "true" arg meand to check for a pixel-level // collision (so merely an overlap in image // squares does not register as a collision) if (myPrincess.collidesWith(myBackground, true)) { retVal = true; } else { // Note: it is not necessary to synchronize // this block because the thread that calls this // method is the same as the one that puts down the // keys, so there's no danger of the key being put down // between the moment we check for the key and // the moment we open the door: for (int i = 0; i < myDoors.length; i++) { // if she's holding the right key, |
| [] [返回上一页] [打 印] |
|
文章评论 |
