Capture

Snowman Jumper

Intro

My final programming project is a platformer game where you play as a snowman that is trying to avoid rain. The idea came from another project I had previously done that created a rain simulator with user interface. I enjoyed the idea of making a platformer game with an element not used in most games.

You can try the game here before you read the rest of the reflection. There are some bugs regarding jumping movements, but other than that the game should perform well.

Rain-maker

As I had already created a program to simulate rain, the rain code in my project was all taken and modified from there. I did remove the section that allowed user control of the rain, because it was intended to be a constant enemy in the game.

The rain created in the game uses an array list to spawn more raindrops based on a random x-value by using this code:

for (int i = 0; i < 10; i++)
  {
    rainfall.add(new Rain()); 
    rainfall.add(new Rain());
    rainfall.add(new Rain());
    rainfall.add(new Rain());
    rainfall.add(new Rain());
  }

if (random(1) < a)
  {
    rainfall.add(new Rain());
    rainfall.add(new Rain());
  }
  for (int i = 10; i < rainfall.size(); i++)
  {
    Rain r = rainfall.get(i);
    r.drawRain();

The code also enables the rain to have a variety of sizes. After being spawned, the rain has a rather simple movement code that increases the y-value of the rain by the y velocity of .05. There is still some vestigial x-movement code from when I originally planned to incorporate wind into the rain code as well, but ended up not implementing the idea.

Incorporating Gravity

Initially, when deciding which program to use for my game, I struggled between Greenfoot and Processing. Whereas Processing had the visual elements I required to use the rain program I had already made, Greenfoot was better for creating games, as it has an element of gravity to it already. I ended up choosing Processing in order to reuse my rain code, and was left with the problem of how to make my character jump and fall at a reasonable speed.

If you have tried the game, there is a very obvious bug once you attempt to make the character jump. Regrettably, I never found out what causes said glitch in the programming to make it warp, as initially the jumping animation worked fine.

Essentially, the gravity code is as follows:

 speed = speed + gravity;
 chary = chary + speed;

With speed starting at 0, and gravity increasing at .03, the code will accelerate the character until hitting a surface. Initially I had planned on having the jump code also use a function involving gravity, but due to bug issues, it became easier to use a while loop and a timer to launch the character into the air.

Making Platforms

The platforms were both one of the hardest and easiest parts of my code. While spawning platforms is easy, defining character and rain interaction with said platforms is not. As platforms are their own class, I found it very hard to consolidate them with the actions of the Snowman class and the ArrayList of the rain at the same time. As attempting to have both classes sense a platform ended in several moments of crashing my computer, I took another route.

if ((r.ypos >= 275) && (r.xpos > 100) && (r.xpos < 150) || (r.ypos >= 375) && (r.xpos > 300) && (r.xpos < 350) || ((r.ypos >= 325)&& (r.xpos > 200 && r.xpos < 250)))
    {      
      rainfall.remove(i);/*rain disappears when touching platforms*/
    }

While it may involve more typing, this code essentially tells the rain to remove itself once it hits a certain point, that being the platform in question. The code below follows a similar pattern of  using booleans to check if the character is underneath or touching a platform.

void touching()//code tests to see if the character is currently touching the ground or a platform
  {
    if (chary >= 475 || ((chary >= 275 && chary <= 300)&& (charx > 100 && charx < 150)) || ((chary >= 375 && chary <= 400)&& (charx > 300 && charx < 350)) || ((chary >= 325 && chary <= 330)&& (charx > 200 && charx < 250)))
    {
      ground = true;/*registers that when touching platforms or ground*/
  
    } 
    else
    {
      ground = false;
    }
  }

void inRain() { if (((chary >= 300)&& (charx > 100 && charx < 150)) || ((chary >= 400)&& (charx > 300 && charx < 350)) || ((chary >= 350)&& (charx > 200 && charx < 250))) 
    {
      melting = false; /*registers that when under platforms, character is not in rain*/
    } 
    else
    {
      melting = true;
    }
  }

Difficulties

Aside from the aforementioned difficulties with the platformer sensing, the bugs with the gravity gave me the most trouble. there were several minute bugs, such as incorrect values for gravity and not properly measuring when the character was touching the ground and telling the gravity to stop that took a few days to solve. However, one glitch that had me stumped for a while was how to make the character continue falling after a key is released. Initially, if you let go of the up key while jumping, the character would simply float in space until you hit the key again. I solved this by creating a void called noKey() that was activated when the key is released.

 void noKey()
  {
    touching();
    while (ground == false)
    {      
      p1.fall(charx, chary, 0, 0);
    }
  }

This code registers if the character is touching the ground first, and then if it is not, enables the fall code so long as the key is not pressed again.

One other issue I had was with the jumping code. Originally there was no way to check if the character was touching the ground, or a platform, and would continue to jump into space forever. I solved this by implementing a sensor code to detect if it is touching a surface (code is above, labeled void touching()), and then only letting the code run while the ground boolean is true.

if (ground == true)
    {
      while (t<50)
      {
        chary -= 2;       
        charx = charx + xmov;    
        t += 1;
        p1.fallUpdate(charx, chary);
      }   

Health Meter

The health meter was one of the last things I implemented into the code. I borrowed part of the code from here and altered it to work with my code. after creating the bar itself, I was faced with creating a health system for the character. In the original idea for the game, the snowman would melt in the rain and be able to rebuild itself in dry places. After creating the senor code (once again, shown above as void inRain()) I made a rather simplistic code to remove health only when in the rain.

 void damage()
  {
    inRain();
    /*registers if character is in rain, causes damage*/
    if (melting == true)
    {
      health -= 1;
      if (health <= 0) { melting = false; health = 0; } } /*registers if character is under platform, restores health*/ if (melting == false) { if (health >= 400)
      {
        health += 0;
      }    
      if (health < 400)
      {
        health += .2;
      }
    }

Conclusion

I found this program fun to develop and despite getting stumped a few times, felt that it went pretty smoothly overall. If I had more time I would definitely attempt to go further into my code to attempt to solve the jumping glitch, and to actually create an end goal for the level. Overall, i’m still proud of what I managed to do with this code in the timespan I could work on it.

Leave a Reply

Your email address will not be published. Required fields are marked *