Learning Python via CodeCombat

in programming •  6 years ago 

Hi guys,

I have recently being using a website called CodeCombat. This is a website that teaches you to code via gaming. You can find it here: https://codecombat.com/play/dungeon


What is CodeCombat?

CodeCombat is an open-source community project and is a game that teaches programming.

Screenshot (65).png

The current options for programming languages to learn via CodeCombat are Python, Javascript and Coffescript. Coffeescript is in the earlier stages I believe as it says "experimental" next to that choice of language.

I am currently learning Python with it as Python is what I use to program my Anki Vector. Before playing this game I already had intentions to learn Python outside of just the context of using the Anki Vector SDK, and I had done a small amount of Python via Lynda Library (free version of Lynda.com for people who are members of a library).
The course I was doing through Lynda Library was okay, but I didn't tend to do much of it, preferring to work on my creative projects, such as my Anki Vector videos.

I enjoy learning through making things as opposed to following courses, and courses in programming still typically involve making things, but at the start of learning a programming language, it's all pretty much the same projects and it isn't as interesting as doing a bigger project or using a gamified platform like CodeCombat.

CodeCombat has an RPG style to it and it is a proper game. It involves writing code in order to beat levels.


Problem Solving in CodeCombat

Programming typically involves problem solving. It's part of the parcel of programming.

However, there are two sources of problems to solve in CodeCombat:

  • The programming itself
  • Aspects of the game

Some of the levels are straightforward, but teach you how to use a particular programming concept.
On the other hand, some require the use of strategy.

The level pictured below is one that requires strategy as well as programming.

Screenshot (58).png

In this level, you need to free the prisoner, defeat the guard and grab the gem down the bottom right. The problem is that the guard ("Two") moves towards you as you head towards him and when he attacks he does a lot of damage and can kill you easily.
If it wasn't for that it would be easy to program a solution, but you need the right solution to

  1. have the programming work (ie no errors for what you are trying to do)
  2. to defeat the guard without him killing you or the prisoner
    It appears to depend on what gear you have (armour etc) as I saw someone in a video do it one way and I died doing the same thing, but this level can be harder than it looks.

Some of the levels are just the basics of teaching new concepts but some, like the one discussed above (and others) involve the use of problem solving, creativity and interacting with the game elements within CodeCombat. In some ways it is similar to the problem solving in some puzzle games.

Some levels also have challenges where the code to complete the level should be below a certain number of lines of code. This is another problem solving challenge.


The Programming

The programming that I've been playing with is in the programming language Python, but the wording logically matches the concepts of the game.

There are various abilities.

The character you play as is called hero and you refer to them in the programming as hero. The hero is what is known as an object and you can apply methods to it, and arguments to those methods.

To make the hero walk right you write:

hero.moveRight()

The hero is the object (and your character) and moveRight() is the method.

You could make the character move right twice, without having to write it twice by adding an argument that says to do it twice.

The way you write that is:
hero.moveRight(2)

The bit in the brackets is the argument.

Attack is another method we have, which makes sense with our character having swords and the like.

To do this we type:
hero.attack("arg")

arg is the argument. You wouldn't actually type arg. You would type the name of the object you are attacking.
If there was an enemy called "steem" you would type:

hero.attack("steem")

There's other abilities too, but I won't spoil too much.


Items

There's various items within CodeCombat. Some are unlocked throughout the process of completing levels and are needed to complete certain levels, and some are not automatically unlocked but you can unlock it with in-game currency. You gain the in-game currency through completing levels, but you can also buy the in-game currency directly if you want more.

This picture shows the item menu which shows items I own as well as items that I don't currently own and could buy with in-game currency. It doesn't show all items I don't own though, since there is only one book in "books" (which was the one teaching me the while-true loop) so I"m guessing it doesn't show items that are only relevant later in the game.
Screenshot (59).png

The items do various things. There are weapons such as swords which do various amounts of damage, just like how weapons in a normal RPG game would have various damage stats. There is also armour which is again similar to a normal RPG game. The accessories tend to have skills related to them though.

Just look at these crude glasses:
Screenshot (60).png

These grant a skill (findNearestEnemy). This skill lets the player target enemies that don't display names provided they are within range and in line-of-sight. It returns null if there are no enemies that meet these criteria (range, line-of-sight).


while True (something I learned)

One interesting thing I learned about because of CodeCombat though was a while-true loop.
The description of the level that introduced this said what the aim of the level was followed by
Use a while-true loop to repeat forever like this:
while True:
hero.moveLeft() #This will happen over and over

In this picture the character is moving to the right, then to the left over and over via a while True loop:
Screenshot (61).png

This is my first encounter with a while-true loop.

I have done programming in various languages. I made my Birchmark website myself using various web languages and frameworks. My HTML canvas PugBee game I've mentioned in previous posts was programmed using javascript, jquery and HTML. You might remember this scene from my HTML game which was the result of me leaving the game running with enemies spawning but with no way for the enemies nor the player character to die:

I've encounter While loops before. While loops execute code as long as a specified condition is true.

Their typical form is:

while (condition)
{
code to be executed
}

where for as long as the bit in brackets (the condition) is true, the while loop will run the code to be executed, over and over again until the condition is no longer true.

Never before this had I seen a while-true loop. Basically if you put true next to while like in the statement:

while True:
hero.moveLeft()

it will happen forever because here the condition is "true" and true will always evaluate to true and when things are true in a while loop, the while loop executes code.

I did have to leave the game and look it up to learn more about how this works as the game only said what I quoted earlier that you can use a while-true loop to repeat forever without explaining its place within the concept of while loops in general, but it was an interesting concept I have learned that I didn't know about before.


CodeCombat Premium

I do not have CodeCombat Premium, the paid version, but I have some information on what it involves.

It costs $9.99 per month or $99 for LIFETIME access.

Buying premium unlocks extra levels, apparently more than 300 more levels, so it unlocks a decent amount of extra content and it unlocks aspects of the game that are only for subscribers.

There are quite a few heroes to choose from when you start with varying difficulty levels. Without premium you choice is a lot more limited and none of the heroes you can choose are any harder than beginner level.

Screenshot (63).png

Premium also lets you use pets. The pets aren't just cosmetic. You can use programming to control the pets.
I do have a slight issue with this one, as it let me unlock pets, just to put them in a subscriber only spot in my inventory when going into levels. That I think is a slightly mean way of doing it as I got excited when it notified me about my Wolf Pup that I can't use.

Screenshot (64).png

I think pets should just be out of the game for free people rather than giving us a notification that we got a pet just to not be able to use it. It's a cool addition though and it would be fun to program them when using premium.

Premium also extends the learning to building websites, games and interactive apps and allows you to build new CodeCombat levels to share with other players. I can see that being of benefit because I think making your own levels would be fun and I think extending the functionality further to stuff like building websites and apps is a good thing.

I can see the benefits of subscribing to CodeCombat. I'd consider getting the lifetime subscription if I find that I keep using CodeCombat for a while and don't move on from it too quickly. If I find I'm using it a lot it seems like good value. I wouldn't do the monthly subscription though as 10 months would add up to the cost of a lifetime subscription. It's just a case of seeing if it sticks or if I move on to the next thing really. I'm currently enjoying it a lot though.

I do wonder if the game would be better with more options though, such as microtransactions so that people can buy an individual pet etc without subscribing. That would certainly make me pay earlier depending on the price as I'm not ready to drop $99 on something like this, but I'd pay a little bit for a pet. I'm not sure what's the best strategy for this game in terms of monetisation.


Similarities to Anki Vector SDK

vector moustache.jpg

Of course there are going to be similarities between what I'm learning from CodeCombat and the Anki Vector SDK as it is programmed using the same language, but I'm now seeing how it fits together and understand more of what I'm programming in Python when working with Vector rather than just knowing it as "That's how you program that thing for Vector" without knowing the basic Python knowledge related to it.

Playing one of Vector's animations is done using this code:

robot.anim.play_animation('args')

To do one of his tantrums, you would type:

robot.anim.play_animation('anim_rt_pickup_loop_10')

Looking at the code used in CodeCombat where hero is an object, moveRight is a method and (2) for twice is an argument, I can see the parallels between the SDK and basic python programming and see that robot must be an object, anim must be some sort of sub-object, play_animation must be a method and the argument for the method is 'anim_rt_pickup_loop_10').
I knew enough to know what I was doing, but not what the individual components of that line of code was and now I know. I had learned a bit about object oriented programming in the past (python is an object oriented language) so I was aware of these concepts, but I didn't remember the full breakdown of what each bit was and didn't translate it to the lines written in the Vector SDK. I want to know more about Python though.

It turns out that it takes an object, a sub-object, a method and an argument to make Vector be a poor sport in a game of Overwatch and do this:


Conclusion

In conclusion, I've had a lot of fun so far with CodeCombat, and I've learned some new things. I've also come to understand both Python and the Anki Vector SDK better as a result of play CodeCombat.

A lot of the principles involved in the levels so far have been principles I have already encountered in programming, but through playing this I have still:

  • Learned python specific information
  • Learned more about how object oriented programming works in practice
  • Learned how to program certain things using Python - even if I know how to do it in Javascript etc, it is till different to make the same program in Python
  • Learned about while-true loops
  • Practiced my programming skills
  • Practiced problem solving
  • Had fun

I am having a lot of fun with this and it is a fun way to learn some of the basics. I have gone looking on Google to further understand some things I encountered in the game so it isn't quite explaining everything I start to question as I'm going along, but it's a fun way to learn it, and with the bit of problem solving involved it is a fun game in its own right too, so even if you look at it as just a game, it is fun and has an added bonus of teaching you programming.

I'm quite impressed with it so far.

Thank you for reading. I hope you enjoyed this post.


LINKS

CodeCombat: https://codecombat.com/play/dungeon

Birchmark Website / Portfolio: http://birchmark.com.au/

YouTube: https://www.youtube.com/c/BirchmarkAu

Twitter: https://twitter.com/Birchmark_

Facebook: https://www.facebook.com/birchmark/

Redbubble: https://www.redbubble.com/people/birchmark?asc=u

Threadless: https://birchmark.threadless.com/

Discord: https://discord.gg/3ZZbbBs


Thank you for reading!
Please consider commenting, upvoting or resteeming this post if you enjoyed it.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!