Thursday, November 29, 2012
Mario Game
Lately I have been working on a basic 2D game. In my game I've been using the jumping and falling I learned. You control Mario from the popular Nintendo game. So far I've gotten Mario to jump but to also come down. When I was learning how to jump at first it wasn't really jumping. It was just going up in the world and falling when I released the space bar. I used to be able to just hold the space bar and I could pretty much my my actor fly but I changed that. I wrote the code so that the actor can only jump when he is on the ground and not in the air.
Thursday, November 15, 2012
Jumping
On Greenfoot I learned to make my actor jump. Making him jump is very similar to making an actor fall. What you do is write public void jump(). Then in the parameters you write vspeed = -(whatever number you want);. The negative makes it go up. then you write fall() in the parameters. In my last post I believed I defined fall(). Then in the checkkeys I made the spacebar make the actor jump. Now that I know how to make an actor run,jump and fall I think I have all the things I need to make a very basic 2D game which I think I'm going to try to do.
Thursday, November 8, 2012
Running, Jumping and falling
Instead of working on chapter 6 I
have been working on another project where I am learning how to switch images
when moving a picture and make it able to stay on a ground and when it is
not on the ground it falls. I will continue watching the tutorial so that I can
make it my actor jump. Here are the links for the videos I have been
watching Part 1 Part 2. So in my project I have a pig. The pig
can move left and right. I found the picture of the pig on google images. When
I got the picture I put it in word and flipped it so that it could face to the
left. Then when I typed in the code I told it that it would move to the right
but also have the picture face the right and when I am moving to the left the
pig face the left. This what the code looks like.
private void checkkeys()
{
if
(Greenfoot.isKeyDown("A") )
{
setImage("Pig Left.png");
moveLeft();
}
if
(Greenfoot.isKeyDown("D") )
{
setImage("Pig Right.png");
moveRight();
}
This is also the first time I used a
moveRight(); method. Usually I use move(4) and Move (-4). I have to say the
moveRight() method is more complex and I don't see me using it that much.
You can't tell it just to move right. You have to say the speed and then write
this public void moveRight()
{
setLocation (
getX() + speed, getY() );
}
public void moveLeft()
{
setLocation (
getX() - speed, getY() );
}
So ya that was new. Then I learned how to make the pig fall. I first needed to make a command called
public void fall(). Then I set the vertical speed to 0 and made it accelerate
by 2. Here is the code.
public void fall()
{
setLocation ( getX(),
getY() + vspeed);
vspeed = vspeed +
acceleration;
}
Then I wanted to make the Pig stand onground and
not fall. This was pretty complex and hard for me but the video helped a lot.
public boolean onGround()
{
Actor under =
getOneObjectAtOffset ( 0, getHeight() / 2, Ground.class);
return under != null;
}
I am going to finish watching the videos and learn
how to make the pig jump. There are some flaws in the project through. For
example, its seems whenever I compile it everything works fine for once but
when I set it up manually without compiling it doesn’t work right. I might change into a game once I fix everything. It probably be having to get to a certain point to another while avoiding certain obstacles.
![]() |
Here's the project so far |
Subscribe to:
Posts (Atom)