Advice on code optimization and bug issue

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
CagedWalrus
Prole
Posts: 11
Joined: Thu Feb 21, 2013 3:03 am

Advice on code optimization and bug issue

Post by CagedWalrus »

Background
I've taken a couple of computer science classes back in high school (several years ago), and when Udacity first appeared I took their introductory Python course.
I'm by no means an expert programmer, or even an intermediate-level programmer for that matter, but I've taken it upon myself to learn Lua/Love2d for the past couple of days. I've put together a small game (if you can call it that), combining many of the things I've come across since starting, with a few of my own addtions.

It's essentially a sprite character that can move by pressing left and right, sprint by pressing space, and is on top of a background with a moving cloud.

Issue
I'm having one issue in particular that I haven't been able to solve. Most of my solutions tend to breed more problems than solutions, so I wanted to get some advice on how to tackle the issue.

Basically, the issue is, when you are moving the character in one direction (holding down the right arrow, for instance), and then simultaneously press another direction (left arrow, for instance), the character will do a sort of moon-walk. I understand, logically, why it's doing this and I can see in the code where it is having the issue, but I'm just not sure on how to fix it without it creating other problems.

Advice/Misc
Additionally, if anyone could offer any advice on cleaning, optimizing, or simplifying my code I'd greatly appreciate it. I'd like to begin my learning experience with a solid foundation of good habits to follow.

On another unrelated note, how are modular sprites implemented (akin to something like Terraria), assuming that is a real thing? I'm currently using Quads to integrate my sprites, and I could image you could just use quad sections or something of that nature, but I'm sure there is a more elegant solution.

Anyways, thank you in advance for your suggestions. I appreciate anything anyone can offer. I also apologize in advance for my custom music, though some might just find it humorous.

TL;DR: Having an issue with character "moon-walking". Looking for suggestions and general coding advice.
Attachments
Game.love
(5.06 MiB) Downloaded 186 times
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Advice on code optimization and bug issue

Post by Taehl »

Issue
You've got code checking the keyboard in two different places. In one you check "if left then ... elseif right", and in the other you "if right then ... elseif left", which is producing your weird behavior. Since you set the variables "moving" and "direction" in the keyboard callbacks, I'd recommend using those instead of more keyboard checks on lines 88 and 95. Just change lines 88-104 to this:

Code: Select all

  if direction == "right" then
    Hero.x = Hero.x + (Hero.speed * dt)
    if love.keyboard.isDown(" ") then
      p:start()
      Hero.x = Hero.x+((Hero.speed + 100) * dt)
      p:setPosition(Hero.x, Hero.y + 10)
    end
  elseif direction == "left" then
    Hero.x = Hero.x - (Hero.speed * dt)
    if love.keyboard.isDown(" ") then
      p:start()
      Hero.x = Hero.x -((Hero.speed + 100) * dt)
      p:setPosition(Hero.x, Hero.y + 10)
    end
  else
    p:stop()
  end
You could simplify it even further, but how exactly you want to do that is up to you. Personally, I eschew keyboard checks entirely and use TLbind, which not only makes things easier, but also lets you use joystick controls at the same time.

Other advice
- MainTheme.wav is huge! You should convert music to .ogg so it will take up far less space.
- Variables iterator, max, timer, moving, and direction, in my opinion, should be in the Hero table because they're related to the Hero, instead of being global variables.
- Since Lua is case-sensitive, I'd recommend choosing an uppercase convention for your variables and stick with it. As is, some of your variables start with caps, some don't.
- You've put quite a few comments in your code, which is good! Even if you don't plan on other people seeing it, comments can be very helpful to yourself, too. Just remember: Don't explain what's happening (you can just read the code), explain /why/ you do it.
- For modular sprites, you need sprites which are made of different pieces. For instance, a bunch of different animations of just legs in various outfits, and a bunch of torsos in different outfits, and a bunch of heads/faces, a bunch of different hairs, and a bunch of different helmets. Then you draw them on top of each other based on what the character is wearing.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Advice on code optimization and bug issue

Post by Inny »

The Moonwalking is as you probably guessed, because you check one direction before the other. However the solution requires a design decision. Specifically this: "What should happen when the player holds one direction and then holds another?" Should he continue in the original direction, should he go the other, or should he stop dead. What if he's pressing left and then holds down, should he move in the diagonal between the two directions? That kind of stuff you need to ask before you can program a solution.

As for the animation not matching the direction you're moving, it's because the keypressed handler sets the direction, but the update handler does the movement. Pick one or the other to do this for you, rather than both. For instance, the love.update handler should probably only move the player according to a delta_x and not care about if its negative or positive, and the keypressed handler is responsible for setting it.

As for optimization, the immortal words amongst all computer scientists is "don't do it yet." I.E. get things working first. Don't worry about optimization until you run into performance issues. I would however highly recommend that you optimize for readability: format everything properly and use nice indenting, put things into neat structures, make lots of small functions, use descriptive variable and function names, and use commenting to describe the why of something, rather than the what. It turns out that you'll read your code 10 times more than you write it, so make it pleasant and easy to read. This wiki page will be very helpful toward that end: http://lua-users.org/wiki/LuaStyleGuide
CagedWalrus
Prole
Posts: 11
Joined: Thu Feb 21, 2013 3:03 am

Re: Advice on code optimization and bug issue

Post by CagedWalrus »

Awesome! Thank you both for the advice. I made the modifications to the update function, and also created a delta_x variable in the keypressed function. Saved myself a lot of code, and I learned something useful!

Taehl, I see that you suggested using TLbind. I've also seen many people suggest using AnAL or anim8 for animation. These suggestions seem to be pretty common place, but would it be in my best interest to first learn how to do the code without the help of these libraries?

Again, I really appreciate the advice!
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Advice on code optimization and bug issue

Post by Taehl »

Personally, I'd recommend using libraries. They save you from having to re-invent the wheel, solving boring problems when you could be learning by solving important, game-making problems. I suppose it would be a good exercise to do it your own way once or twice, but you could probably also learn a lot just by reading the library's code and seeing how they did it. While learning is important, it'll happen automatically as you work; the really important part is actually making your game.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
CagedWalrus
Prole
Posts: 11
Joined: Thu Feb 21, 2013 3:03 am

Re: Advice on code optimization and bug issue

Post by CagedWalrus »

Thanks for the cogent reply. I think you've sold me on the idea. I'll give some of those libraries mentioned above a whirl; looking forward to it, actually!

Also, just a quick side question, seeing as how you mentioned the .ogg files earlier. Can you recommend a good converter?

Thanks!
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Advice on code optimization and bug issue

Post by Taehl »

I turn to Audacity for most of my sound-manipulation needs, since it's free, open-source, and small. There may be a better/faster/more-convenient one out there, but Audacity can do more than convert, which comes in handy. If you use Linux, chances are you already have it installed.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
CagedWalrus
Prole
Posts: 11
Joined: Thu Feb 21, 2013 3:03 am

Re: Advice on code optimization and bug issue

Post by CagedWalrus »

I just got it installed and running; works great. Thanks for the suggestion!
Post Reply

Who is online

Users browsing this forum: No registered users and 262 guests