Showing posts with label penguin. Show all posts
Showing posts with label penguin. Show all posts

Wednesday, May 21, 2014

Quick Update

Taking a brake from the penguin Java porting project for the moment to work on Wizard's Scroll.  I realized I didn't want to port the code until it was a lot more polished.  That said, the entire process of ripping chunks of code out of games to use in different games has taught me a lot.  Specifically, I'm rewriting the code in Wizard's Scroll so that it's more like a library that could be used for any game, rather then code hobbled together to only do one thing.  Who knows, maybe it will evolve into something I could release someday.

Wednesday, May 14, 2014

JavaScript and Java: Side-by-Side Comparison

[UPDATED 5/15/14: Tweaked a couple things in the Java code]

Currently translating my maze-generating code from JavaScript to Java.  Here's the original code in JavaScript:

var room=new Array();
var size=100

for (x=2;x<size;x+=2)
{for (y=2;y<size;y+=2)

 {room[x+(y*size)]=" ";
   var r=Math.floor(Math.random()*4);
   if (r==0 && x<size-2) {room[x+1+(y*size)]=  " ";}
   if (r==1 && x>02)     {room[x-1+(y*size)]=  " ";}
   if (r==2 && y<size-2) {room[x+((y+1)*size)]=" ";}
   if (r==3 && y>02)     {room[x+((y-1)*size)]=" ";}
   if (r==0 && x==size-2){room[x-1+(y*size)]=  " ";}
   if (r==1 && x==02)    {room[x+1+(y*size)]=  " ";}
   if (r==2 && y==size-2){room[x+((y-1)*size)]=" ";}
   if (r==3 && y==02)    {room[x+((y+1)*size)]=" ";}
  }
}


And here's the same code in Java:

int size = 100;
int[][] room = new int[size][size];
for (int x=2;x<size;x+=2)
{for (int y=2;y<size;y+=2)
  {room[x][y]=1;
    int r = 1; //haven't looked up the code for random numbers yet!
    if (r==0 && x<size-2) {room[x+1][y]=1;}
    if (r==1 && x>02)     {room[x-1][y]=1;}
    if (r==2 && y<size-2) {room[x][y+1]=1;}
    if (r==3 && y>02)     {room[x][y-1]=1;}
    if (r==0 && x==size-2){room[x-1][y]=1;}
    if (r==1 && x==02)    {room[x+1][y]=1;}
    if (r==2 && y==size-2){room[x][y-1]=1;}
    if (r==3 && y==02)    {room[x][y+1]=1;}
  }
}


The maze code works by 1) filling a grid with walls, 2) making every other space of every other row an open space (or, if you divided the maze into 2x2 squares, it would be the fourth quadrant of every square), and then 3) using random numbers to connect those spaces.

As you'll notice, the code hasn't actually changed all that much.  The biggest and most obvious change is declaring variables. In JavaScript, you need only use var, and you don't need to declare the variables in for statements.  In Java, variables are typed, meaning that you must declare what a variable will be used for at the time you declare it.

The other two changes may not have been strictly necessary.  The first is the change to double arrays (not supported in JavaScript).  As you can see, the double array makes reading code much easier then the multiplying required in the JavaScript code.  The second change was from strings to integers.  This has mostly to do with the fact that in the game the code was originally from, the map code could contain many different characters besides just walls and spaces.  In the penguin game, we will only be checking for walls or spaces, so it was more efficient to use numbers. Come to think of it, I may actually change them to Booleans - true on/off numbers.

Next time: Python to Java!

Tuesday, May 13, 2014

A Little Tougher Then It Has to Be

Finally finished moving on Friday; spent the last couple of days settling in. But you guys didn't come here to talk about that! You came here to talk about code.  So let's talk.

A few days ago I decided to write a game by taking code from a couple other games (in different languages, mind you), put them together, and then shore up the edges, so to speak.  So then I decided, since they were in different languages anyways, and I've been meaning to learn Java for ages, I could teach myself by porting the code over to a language I've never used before!  While moving.  Yeah, apparently I just like making things hard on myself.

At any rate, since this is suppose to be a blog about coding and syntax and I haven't talked much about either lately, I thought it might be interesting to include you guys in the process; side by side comparisons of code, that sort of thing.

In the meantime, if anybody has any suggestions or tips about learning Java, I'm all ears.  I'm familiar with C++ and JavaScript, so the syntax is nothing new, but all these different class types are confounding me.

Sunday, May 4, 2014

Repurposing Code

Before going back to school for software engineering, I spent almost fifteen years in the restaurant industry.  One of the terms that chefs like to throw around is "repurposing" food; this is where you take the components left over from previous dishes and put them together to make something entirely new.

My family is currently in the midst of moving.  I'm not getting much done on my current projects, because they're all at points that require a lot of focusing, and I don't have time fore that.  What I do have is a lot of short sporadic bursts of time.  So I decided to issue myself a challenge: could I repurpose code from some of my other projects to create an entirely new game?

I'm keeping it simple for this challenge, so the goal is to make something similar to Sega's classic arcade game Pengo.  To work with, I've got the game engine from Wizard's Scroll, which already includes code for pushing blocks, and the random maze generating algorithm I wrote for Lore.  (Of course, one's in Python and one's in JavaScript, but hey, let's not make it too easy, right?)  So, "all" I have to do is take the parts I need, rewrite them to work together, and then add in enemies.

 
I just started it this afternoon.  Not very far yet, but who knows?  Maybe I'll finish it up and throw it on Kongregate.....