views:

419

answers:

6

I am developing a simple platform game in Java using BlueJ.

Over the next few weeks I will be posting a few questions asking for help with this project as am not very proficient at programming. So I hope I don't become annoying and I would be very grateful if you can kindly help me.

In my game I have a gameOver method which ends the game and prints a statement to the console. What I would like is to call upon this method when my player sprite "falls off" the screen.

Right now I have a rectangle shape placed at the bottom of the screen to represent some kind of hazard (like water) and when the player collides with it, it calls the gameOver method and the game ends. However, I would still prefer if the game ended if the player fell below a certain height of the screen.

This is my gameOver method (from the 'Game' class)-

/** End the game. */
public void gameOver()
{
    world.terminate();
    isOver = true;
}

This is my player sprite (currently drawn using a polygon)-

super(Utils.createPolygon(170, 102,
    155, 118, 137, 118, 151, 134, 146,
    140, 167, 140, 170, 135, 174, 140,
    193, 140, 188, 134, 202, 118, 186,
    118, 170, 102)); 
setColor(new Color(254, 167, 184));

This is the code (from the 'Enemy' class, similar code is used in a Hazard class also) that ends the game when player collides with an enemy-

if (e.contact.involves(player) && (player.getLives() < 1)) {
    System.out.println("YOU ARE DEAD!");
    game.gameOver();
}

I would like a similar, or at least simple solution that will call on my gameOver method when my player sprite falls or goes beyond a certain coordinate of the screen.

I would appreciate it if you could kindly provide a sample/example code, with comments to help me understand it, in order to give an idea of what I should do to achieve this.

Thank you very much and I look forward to your answers.

+2  A: 

To help you do your homework:

   // if the player goes out of bounds, the game is over
   if (player.getBounds().outside(game.getBounds()) 
   {
        System.out.println("YOU WENT OUT!");
        game.gameOver();
    }

assuming that the player has some kind of bounding area and the game itself has some kind of area...

John Gardner
The player does not have a bounding area. Should I do this using a setBounds method?
Dew
+2  A: 

A good question to ask yourself is, "Where is my sprite?".

The next question is, "What are the boundaries of my world?".

If you can answer those two questions, then you should be able to answer your own question.

Assuming that you have a two-dimensional world, as long as the location of the sprite is within the horizontal boundaries as well as within the vertical boundaries, then your sprite is still on the map, otherwise, he's not.

Jason Lepack
I am trying to use the getPosition method for my sprite. But I need some direction. Also my world is only limited using the height and width I have set in the constructor of my game class. I am new to all this so I would appreciate some examples of what I should be looking for.
Dew
Since you have control over the height and width, and you can get the position of the sprite you have all you need. IF the sprite is at any space surrounded by the rectangle (0,0), (maxwidth,0), (0, maxHeight), (maxwidth,maxheight), he's on the map. If he's not on the map, then he's fallen.
Jason Lepack
+3  A: 

To get you started:

  1. Think about how you might get the player's current position.
  2. Think about what makes the player fall (there should be some method/class that implements gravity and falling?)
  3. Check the player's position as they fall against the boundries of your world. If they're outside the boundries, it's time to call your GameOver method.

To check the boundries:

  1. Your world should have a minimum height that the player has to keep above to stay alive. Remember this value.
  2. If you get your player's vertical position and compare to this minimum height, you should get a boolean result (safe vs. dead).
  3. Note that screen position starts at the top left and increases as you go right and down, so you're player position probably reflects this. Thus, you're most likely looking for the test (player.getPosition().Y > world.getMaxAllowedHeight());


For reference: Please post what you've tried already and where you're having problems. Asking for a spoonfed solution isn't going to help anyone - you're going to have to think about this a bit yourself. If you have no idea where to start, tell us that instead; we can try to steer you in the right direction.

Update: Sounds like you've done a bit of thinking already, which is great! Sorry if I was harsh to jump down your throat, but your original post suggested otherwise. Next time, though, do try to ask a more help-me-help-myself question than a solve-it-for-me one. A simple "Can someone give me an idea of how to start tackling this problem?" will do.

lc
I am using a games engine provided to me by my instructor which handles the player's physics. For my other sprites, like the enemy, I am using a setFloating and setGhostly method. It would be very long to post my entire code here. Is there any specific part you'd like to see to help you get an idea?
Dew
Hmm...sounds like you probably aren't going to get notification from the physics engine when the player's position is changed then, so you'll have to test it before you draw the sprite. What does your main game loop look like? Has your instructor given you any other hints as to where to do the test?
lc
Well I use player.putOn(ground); to place the player on the 'ground' when the game starts, which is an actual shape in the game world. Other than that and KeyEvents used to move the player, the engine does the rest, which unfortunately I don't have any control over.
Dew
You do have control over drawing, though, right?
lc
...and if not, what exactly DO you have control over? You have to find a place in the game logic that allows you to test the player's position.
lc
Yes I have control over drawing and that's really about it other than setting movements, velocity, etc for the shapes I draw. I will have to speak to my instructor for more details on what I'm able and unable to do, so I will try to find out tomorrow.
Dew
But thank you for pointing out the getPosition method, I think I'm on the right track now. And no problem. As I said I'm not good at this and usually even when I identify what I need to do I can't do it without at least seeing an example. But I will try a more DIY approach to seeking help next time.
Dew
If you're setting movements for shapes, you're probably the one updating the object's position without really knowing it. Somewhere in the course of things between setting the velocities and drawing the shapes, you'll have to check the player's position. If it's below the threshold, call GameOver.
lc
As for the DIY approach, please don't get me wrong - it's perfectly ok to seek help and even to ask for an example. If you can, just try to word your question more like you're seeking an example and not just a quick answer to your assignment.
lc
That is, instead of "Please provide a _code that I may use_ with comments to elaborate on what they do.", "Please provide a simple _code example_ of how I might do this, with comments so I can understand what's happening." might garner a more favorable initial response.
lc
I suppose I worded it wrong, however I thought it would have obvious what I asked for is essentially a sample/example since no one can give me the exact code that specifically works with my program as I only provided snippets of my own code to give an idea of what it currently does.
Dew
But thanks anyway for the help.
Dew
You do have a valid point. I've just seen a number of questions simply asking for an answer in order to plug in the result, get the assignment done, and not have to understand anything. And yes, it is indeed unfortunate to put the burden on you to separate yourself from these folk.
lc
Did you manage to figure out the answer?
lc
+1  A: 

Do you have a gameloop?

Usually in a game there is somekind of tightloop that controls the logic and rendering process, something like.

while(stillRunning)
{

   if(isPlayerDead()) 
   { 
       showGameOverScreen(); 
   }
   else
   {
       PerformLogic();
       RenderToScreen();
   }

   sleep(0);
}

You will need to expand on the isPlayerDead() method to check for collisions and if the location of your sprite is greater than the height of the screen.

JSmyth
A: 

you will be checked against plagiarims, thanks.

A: 

Thou shalt not cheat