Emojicode Documentation 0.5
Guides Language Reference Package Index

The Cookie Monster Game

This chapter introduces you to the basics of writing a simple game with the allegro package, which ships with Emojicode. This guide assumes that you have have a basic knowledge of object-oriented programming and have read Compile and Run Your First Program.

Creating an app

First of all we need to important the allegro package:

๐Ÿ“ฆ allegro ๐Ÿ”ด

This makes all types from the allegro package available in the namespace ๐Ÿ”ด (of our package, which by definition is _), i.e. we can access all classes provided by the allegro package from our application.

Next we need to startup the application, so weโ€™ll create the ๐Ÿ function:

๐Ÿ ๐Ÿ‡
  ๐Ÿฉ๐Ÿ™‹๐Ÿ” ๐Ÿ‡ app ๐Ÿ”

  ๐Ÿ‰
๐Ÿ‰

As youโ€™ve seen, thereโ€™s already some code in the ๐Ÿ function. ๐Ÿฉ๐Ÿ™‹๐Ÿ” is a type method call. This means we call a method (๐Ÿ™‹) directly on a type (๐Ÿ”). Thereโ€™s one more thing to notice: the following code block. This code block creates a closure that takes an argument app of type ๐Ÿ”. Closures are a kind of function that capture the context in which they were created, i.e. the variables and the object on which a method was called.

So what is this good for? The ๐Ÿ™‹ type method of the ๐Ÿ” class, which is provided by the allegro package, is really important as it initiates the application in such a way that it can display windows, receive events and play sounds. This method normally doesnโ€™t return but instead calls the given callable in an appropriate way and passes it an instance of ๐Ÿ” that represents the created application. Weโ€™ll use it to draw in a moment.

To recap: Call this class method, provide it with a callable and setup the application from there.

Weโ€™ll do so by creating a ๐Ÿ“บ instance. A ๐Ÿ“บ instance (say display) is responsible for displaying graphics on the computers screen. On many operating systems a ๐Ÿ“บ will be represented by a window.

๐Ÿ“บ provides a single initializer:

๐Ÿ”“ ๐Ÿˆ ๐Ÿ†• width ๐Ÿš‚ height ๐Ÿš‚ 

Our code to get a display is therefore:

๐Ÿฆ display ๐Ÿ”ท๐Ÿ“บ๐Ÿ†• 1000 1000

Now that weโ€™ve a ๐Ÿ“บ instance we can configure it. Weโ€™ll set its title:

๐Ÿท display ๐Ÿ”คCookie Monster Game๐Ÿ”ค

Thatโ€™s good moment to test if things are working so far. If we, however, compiled and ran this program know we wouldnโ€™t see much due to the fact that program would create a display and immediately terminate. So weโ€™ll tell it to wait for a few seconds after the display was created:

๐Ÿฉโฒ๐Ÿ’ˆ 10_000_000

Donโ€™t worry, โฒ takes the number of microseconds to wait. And 10,000,000 microseconds are only 10 seconds. Save the file, compile it and run it. You should see something similar to this:

Not very impressive, is it? Letโ€™s do something more interesting and draw some cookies onto the screen. Download a cookie image from here, copy it into the directory in which your program is stored.

In order to display the image on the screen, it must be loaded as a bitmap first. Fortunately thereโ€™s a ๐Ÿ–ผ - thatโ€™s the bitmap class โ€“ initializer that can exactly do that. Itโ€™s called ๐Ÿ“„ takes a path to the image and is declared like this:

You certainly noted the ๐Ÿšจ๐ŸŒฉ, which indicates that the initializer might return an error instead of a bitmap object. We must deal with this possibly arising error and do so by simply assuming that there will never be an error:

๐Ÿฆ cookieBmp ๐Ÿš‡๐Ÿ”ท๐Ÿ–ผ๐Ÿ“„ ๐Ÿ”คcookie.png๐Ÿ”ค

The ๐Ÿš‡ tells the compiler that we are sure there will never be an error. (If in reality there is an error the program will simply crash. Not very elegant.) For brevity, let's leave it at that. But make sure that you properly handle errors in a real application.

Now that weโ€™ve the bitmap, weโ€™ll draw it onto the screen:

๐Ÿšฟ app ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 255 255 255 255
๐Ÿ“ผ app cookieBmp 500 500
๐ŸŽฆ app

Make sure you place this code before ๐Ÿฉโฒ๐Ÿ’ˆ 10_000_000 or you wonโ€™t see anything. If everything goes well, you should see a tasty cookie in front of a white background, neatly centered in the display.

Wondering whatโ€™s going on โ€™cause thatโ€™s a bunch of method calls? Well, these are all important drawing functions. And they all have one thing in common: They are methods called on our app and draw to the current drawing target. By creating a display weโ€™ve created a drawing target which was then also set as the current drawing target.

The ๐Ÿšฟ type method fills the entire drawing target with the given color which was here created by providing RGBA values. The ๐Ÿ“ผ method then draws a bitmap at the specified coordinates. Last but not least the ๐ŸŽฆ makes all changes visible: Everything you draw is drawn in a backbuffer first and gets visible once you call the ๐ŸŽฆ method.

Letโ€™s extend our code to also show the cookie monster (download here) and an additional cookie:

๐Ÿ“ฆ allegro ๐Ÿ”ด

๐Ÿ ๐Ÿ‡
  ๐Ÿฉ๐Ÿ™‹๐Ÿ” ๐Ÿ‡ app ๐Ÿ”
    ๐Ÿฆ display ๐Ÿ”ท๐Ÿ“บ๐Ÿ†• 1000 1000
    ๐Ÿท display ๐Ÿ”คCookie Monster Game๐Ÿ”ค

    ๐Ÿฆ cookieBmp ๐Ÿš‡๐Ÿ”ท๐Ÿ–ผ๐Ÿ“„ ๐Ÿ”คcookie.png๐Ÿ”ค
    ๐Ÿšฟ app ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 255 255 255 255
    ๐Ÿ“ผ app cookieBmp 500 500
    ๐Ÿ“ผ app cookieBmp 400 400 ๐Ÿ‘ด The Cookie Monster can never get enough cookies

    ๐Ÿฆ monster ๐Ÿš‡๐Ÿ”ท๐Ÿ–ผ๐Ÿ“„ ๐Ÿ”คcookie_monster.png๐Ÿ”ค
    ๐Ÿ“ผ app monster 200 200

    ๐ŸŽฆ app

    ๐Ÿฉโฒ๐Ÿ’ˆ 10_000_000
  ๐Ÿ‰
๐Ÿ‰

Running this should result in something similar to this:

Power to the classes

Everything works, but frankly, itโ€™s not really future-proof code. Emojicode is an object-oriented language, so everything would be much better with classes.

First of all, let us create a ๐Ÿฆ class that represents the Cookie Monster:

๐Ÿ‡ ๐Ÿฆ ๐Ÿ‡
  ๐Ÿฐ x ๐Ÿš€
  ๐Ÿฐ y ๐Ÿš€
  ๐Ÿฐ monster ๐Ÿ–ผ

  ๐Ÿˆ ๐Ÿ†• ๐Ÿ‡
    ๐Ÿฎ x 500
    ๐Ÿฎ y 500
    ๐Ÿฎ monster ๐Ÿš‡๐Ÿ”ท๐Ÿ–ผ๐Ÿ“„ ๐Ÿ”คcookie_monster.png๐Ÿ”ค
  ๐Ÿ‰

  ๐Ÿ– ๐Ÿ–Œ app ๐Ÿ” ๐Ÿ‡
    ๐Ÿ“ผ app monster x y
  ๐Ÿ‰
๐Ÿ‰

This is a very basic class. It has three instance variables: x and y that hold the position of the Cookie Monster and bitmap that holds the bitmap. The initializer sets the initial position of the monster to (500, 500) and loads the bitmap. Also, the cookie monster has a method draw itself: ๐Ÿ–Œ.

Next, hereโ€™s a class for cookies:

๐Ÿ‡ ๐Ÿช ๐Ÿ‡
  ๐Ÿฐ cookie ๐Ÿ–ผ
  ๐Ÿฐ x ๐Ÿš€
  ๐Ÿฐ y ๐Ÿš€

  ๐Ÿˆ ๐Ÿ†• ๐Ÿผ x ๐Ÿš€ ๐Ÿผ y ๐Ÿš€ ๐Ÿผ cookie ๐Ÿ–ผ  ๐Ÿ‡๐Ÿ‰

  ๐Ÿ– ๐Ÿ–Œ app ๐Ÿ” ๐Ÿ‡
    ๐Ÿ“ผ app cookie x y
  ๐Ÿ‰
๐Ÿ‰

Thereโ€™s a difference to the ๐Ÿฆ class: ๐Ÿช๐Ÿ†• takes a bitmap argument and coordinates. The reason for this is that we want to create multiple cookies later on and they should, of course, share the same bitmap and have different positions. For this reason weโ€™ll instantiate the bitmap once and pass it to all ๐Ÿช instances.

So letโ€™s update the ๐Ÿ function to use the new classes:

๐Ÿ ๐Ÿ‡
  ๐Ÿฉ๐Ÿ™‹๐Ÿ” ๐Ÿ‡ app ๐Ÿ”
    ๐Ÿฆ display ๐Ÿ”ท๐Ÿ“บ๐Ÿ†• 1000 1000
    ๐Ÿท display ๐Ÿ”คCookie Monster Game๐Ÿ”ค

    ๐Ÿฆ cookieBmp ๐Ÿš‡๐Ÿ”ท๐Ÿ–ผ๐Ÿ“„ ๐Ÿ”คcookie.png๐Ÿ”ค  ๐Ÿ‘ด Load the cookie bitmap

    ๐Ÿฆ cookies ๐Ÿ”ท๐Ÿจ๐Ÿš๐Ÿช๐Ÿธ
    ๐Ÿฆ prng ๐Ÿ”ท๐ŸŽฐ๐Ÿ†•
    ๐Ÿ”‚ i โฉ 0 10 ๐Ÿ‡  ๐Ÿ‘ด Create 10 randomly placed cookies
      ๐Ÿป cookies ๐Ÿ”ท๐Ÿช๐Ÿ†• ๐Ÿš€๐Ÿš‚ prng 50 900 ๐Ÿš€๐Ÿš‚ prng 50 900 cookieBmp
    ๐Ÿ‰

    ๐Ÿฆ monster ๐Ÿ”ท๐Ÿฆ๐Ÿ†• ๐Ÿ‘ด Instantiate a monster

    ๐Ÿšฟ app ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 255 255 255 255
    ๐Ÿ–Œ monster app ๐Ÿ‘ด Draw the monster
    ๐Ÿ”‚ cookie cookies ๐Ÿ‡ ๐Ÿ‘ด Draw all cookies
      ๐Ÿ–Œ cookie app
    ๐Ÿ‰

    ๐ŸŽฆ app

    ๐Ÿฉโฒ๐Ÿ’ˆ 10_000_000
  ๐Ÿ‰
๐Ÿ‰

As you can see we didnโ€™t just replace the drawing calls but also introduced a ๐Ÿจ cookies which stores ten randomly placed cookie objects. ๐Ÿ”ท๐ŸŽฐ๐Ÿ†• creates a pseudo-random number generator, an object we can ask for random numbers. We use it to get integers between 50 and 900, which are then converted into ๐Ÿš€s with ๐Ÿš‚โ€™s ๐Ÿš€ method. Later on this ๐Ÿจ is iterated and ๐Ÿ–Œ is called on each cookie.

If you run the program youโ€™ll see something similar to this:

Check out that event

Everything is working fine so far but itโ€™s not a game yet. We need to grant the user a bit of control over the game. In order to achieve this we need to instantiate an event queue which is constantly queried for events.

First, create the event queue:

๐Ÿฆ queue ๐Ÿ”ท๐Ÿ—ƒ๐Ÿ†•
โŒจ๏ธ queue

The code above creates an event queue and registers the keyboard as an event source for that queue. Next, a loop which constantly queries the event queue for an event. Such a loop is often referred to as the run loop:

๐Ÿ” ๐Ÿ‘ ๐Ÿ‡
  ๐Ÿฆ event โณ queue

๐Ÿ‰

The โณ method of โณ waits for an event to occur and returns it. So the next thing is to classify the event and handle it appropriately. Since we want the Cookie Monster to be movable with the arrow keys we need to check for a key-down event and then check in which direction that Cookie Monster should move.

๐ŸŠ๐Ÿฆ keyboardEvent ๐Ÿ”ฒ event ๐Ÿ“ฉ ๐Ÿ‡
  ๐ŸŠ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 84 ๐Ÿ‡
    โฌ†๏ธ monster
  ๐Ÿ‰
  ๐Ÿ‹ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 85 ๐Ÿ‡
    โฌ‡๏ธ monster
  ๐Ÿ‰
  ๐Ÿ‹ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 82 ๐Ÿ‡
    โฌ…๏ธ monster
  ๐Ÿ‰
  ๐Ÿ‹ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 83 ๐Ÿ‡
    โžก๏ธ monster
  ๐Ÿ‰
๐Ÿ‰

Letโ€™s walk through this code: The first line tries to cast event to ๐Ÿ“ฉ, a key press event. Then we determine which keycode was associated with this event. 84 stands for arrow up, 85 for arrow down, 82 for arrow left and 83 for arrow right. According to these direction, methods get called on monster. And yes, we have yet to implement them in ๐Ÿฆ:

๐Ÿ– โฌ‡๏ธ ๐Ÿ‡
  ๐Ÿฎ y โž• y 10
๐Ÿ‰

๐Ÿ– โฌ†๏ธ ๐Ÿ‡
  ๐Ÿฎ y โž– y 10
๐Ÿ‰

๐Ÿ– โฌ…๏ธ ๐Ÿ‡
  ๐Ÿฎ x โž– x 10
๐Ÿ‰

๐Ÿ– โžก๏ธ ๐Ÿ‡
  ๐Ÿฎ x โž• x 10
๐Ÿ‰

This methods obviously just change the coordinates to move the monster accordingly. So thereโ€™s just one step left, before you can control the Cookie Monster with the arrow keys. We need to redraw the game in the run loop, i.e. we need to draw everything all over again to make the changes to the Cookie Monster visible:

๐Ÿ ๐Ÿ‡
  ๐Ÿฉ๐Ÿ™‹๐Ÿ” ๐Ÿ‡ app ๐Ÿ”
    ๐Ÿฆ display ๐Ÿ”ท๐Ÿ“บ๐Ÿ†• 1000 1000
    ๐Ÿท display ๐Ÿ”คCookie Monster Game๐Ÿ”ค

    ๐Ÿฆ cookieBmp ๐Ÿš‡๐Ÿ”ท๐Ÿ–ผ๐Ÿ“„ ๐Ÿ”คcookie.png๐Ÿ”ค

    ๐Ÿฆ cookies ๐Ÿ”ท๐Ÿจ๐Ÿš๐Ÿช๐Ÿธ
    ๐Ÿฆ prng ๐Ÿ”ท๐ŸŽฐ ๐Ÿ†•
    ๐Ÿ”‚ i โฉ 0 10 ๐Ÿ‡
      ๐Ÿป cookies ๐Ÿ”ท๐Ÿช๐Ÿ†• ๐Ÿš€๐Ÿš‚ prng 50 900 ๐Ÿš€๐Ÿš‚ prng 50 900 cookieBmp
    ๐Ÿ‰

    ๐Ÿฆ monster ๐Ÿ”ท๐Ÿฆ๐Ÿ†•

    ๐Ÿฆ queue ๐Ÿ”ท๐Ÿ—ƒ๐Ÿ†•
    โŒจ๏ธ queue
    ๐Ÿ” ๐Ÿ‘ ๐Ÿ‡
      ๐Ÿšฟ app ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 255 255 255 255
      ๐Ÿ–Œ monster app

      ๐Ÿ”‚ cookie cookies ๐Ÿ‡
        ๐Ÿ–Œ cookie app
      ๐Ÿ‰
      ๐ŸŽฆ app

      ๐Ÿฆ event โณ queue

      ๐ŸŠ๐Ÿฆ keyboardEvent ๐Ÿ”ฒ event ๐Ÿ“ฉ ๐Ÿ‡
        ๐ŸŠ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 84 ๐Ÿ‡
          โฌ†๏ธ monster
        ๐Ÿ‰
        ๐Ÿ‹ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 85 ๐Ÿ‡
          โฌ‡๏ธ monster
        ๐Ÿ‰
        ๐Ÿ‹ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 82 ๐Ÿ‡
          โฌ…๏ธ monster
        ๐Ÿ‰
        ๐Ÿ‹ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 83 ๐Ÿ‡
          โžก๏ธ monster
        ๐Ÿ‰
      ๐Ÿ‰
    ๐Ÿ‰
  ๐Ÿ‰
๐Ÿ‰

The ๐Ÿ function should now look like above. Note, that weโ€™ve also dropped the delay at the end of the initializer โ€“ itโ€™s no longer needed.

You can now control the monster with your keyboard!

Me want eat cookies

So far, so good. But actually the Cookie Monster wants to the eat the cookies and not just to walk underneath them. In order to achieve this, we need to add two methods to the ๐Ÿฆ class that return the current coordinates of the mouth of Cookie Monster:

๐Ÿ– ๐Ÿ‘‰๏ธ โžก๏ธ ๐Ÿš€ ๐Ÿ‡
  ๐ŸŽ โž• x 60
๐Ÿ‰

๐Ÿ– ๐Ÿ‘‡ โžก๏ธ ๐Ÿš€ ๐Ÿ‡
  ๐ŸŽ โž• y 70
๐Ÿ‰

Thatโ€™s really nothing special. Since the coordinates stored in the x and y variables point to the top left of the cookie monster, 60 and 70 get added so that the returned coordinates point to the center of the mouth.

Weโ€™ll now replace ๐Ÿชโ€™s ๐Ÿ–Œ method by a much more advanced method:

๐Ÿ– ๐Ÿ–Š app ๐Ÿ” monsterX ๐Ÿš€ monsterY ๐Ÿš€ โžก๏ธ ๐Ÿ‘Œ ๐Ÿ‡
  ๐ŸŠ ๐ŸŽŠ๐ŸŽŠโžก๏ธ monsterX x โฌ…๏ธ monsterX โž• x 50 ๐ŸŽŠโžก๏ธ monsterY y โฌ…๏ธ monsterY โž• y 50 ๐Ÿ‡
    ๐ŸŽ ๐Ÿ‘
  ๐Ÿ‰
  ๐Ÿ“ผ app cookie x y
  ๐ŸŽ ๐Ÿ‘Ž
๐Ÿ‰

Evidently, this method takes two arguments which are the location of the Cookie Monster, or more specifically, its mouth. The next line might be a bit more difficult to understand. It compares whether the coordinate of the Cookie Monsterโ€˜s mouth are within the cookie. If thatโ€˜s the case the method immediately returns ๐Ÿ‘, otherwise the cookie is drawn as usual and ๐Ÿ‘Ž is returned.

Clearly, we now also need to update our loop to draw cookies:

๐Ÿฆ iterator ๐Ÿ”ท๐ŸŒฑ๐Ÿš๐Ÿช๐Ÿ†• cookies
๐Ÿ”‚ cookie iterator ๐Ÿ‡
๐ŸŠ ๐Ÿ–Š ๐Ÿบcookie app ๐Ÿ‘‰๏ธ monster ๐Ÿ‘‡ monster ๐Ÿ‡
  ๐Ÿšฏ iterator
๐Ÿ‰

The code above is really straightforward. We create an ๐ŸŒฑ iterator instance for cookies, to which a reference is then stored in iterator. Then this iterator is used with the ๐Ÿ”‚ loop to get each cookie. ๐Ÿ–Š is called on each cookie and the coordinates of the monster, which we can get from the new ๐Ÿ‘‰๏ธ๐Ÿ‘‡ methods, are passed. If the ๐Ÿ–Š method returns true, this means the Cookie Monsters mouth touched the cookie, the ๐Ÿšฏ method is called on the iterator, which removes the current element from the array. (Thatโ€™s a special capability of ๐ŸŒฑ and the reason why we created it explicitly.)

Try running the game now! The Cookie Monster will be happy to eat the cookies.

Scores

Cool game, isnโ€™t it? But it would be nice if there was a counter showing how many cookies there are still left, wouldnโ€™t it? And probably a special screen when the game is finished?

To recap: We want to display some text. And therefore we need a font. Download a cool font here and put it into the same directory as cookie.emojic.

Now we can load that font (and you should do this before the run loop):

๐Ÿฆ font ๐Ÿš‡๐Ÿ”ท๐Ÿ•‰๐Ÿ“„ ๐Ÿ”คMonoton-Regular.ttf๐Ÿ”ค 63

The same disclaimer applies again: Donโ€™t skip error checking although we do it here for shortness. ๐Ÿ”ท๐Ÿ•‰๐Ÿ“„ instantiates a new instance of ๐Ÿ•‰, which represents the font. The two arguments are the path to the font and the size of the font.

In the run loop weโ€™ll draw the number of cookies left:

๐Ÿ”ก app font ๐Ÿ”ก ๐Ÿ” cookies 10 990 10 ๐Ÿ”ทโš–โžก๏ธ ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 0 0 0 255

Place this code before ๐ŸŽฆ app. ๐Ÿ”ก draws a text using the given font. It is define like this:

๐Ÿ”“ ๐Ÿ– ๐Ÿ”ก font ๐Ÿ•‰ text ๐Ÿ”ก x ๐Ÿš€ y ๐Ÿš€ align โš– color ๐ŸŽจ โžก๏ธ โœจ

Quite a lot of arguments. Most things should be pretty clear. align is a โš–, an enum, which specifies how the text should be aligned. In our example we used ๐Ÿ”ทโš–โžก๏ธ to align the text to the right, i.e. the top right corner of the text will always be at the coordinates passed to x and y.

If you try the game now youโ€™ll see a counter in the top right showing how many cookies are left.

As mentioned before, a screen at the end of the game would be nice:

๐ŸŠ ๐Ÿ˜› ๐Ÿ” cookies 0 ๐Ÿ‡
  ๐Ÿšฟ app ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 4 115 187 255
  ๐Ÿ”ก app font ๐Ÿ”คWell done!๐Ÿ”ค 500 500 ๐Ÿ”ทโš–โ†” ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 255 255 255 255
  ๐ŸŽฆ app

  ๐Ÿ” ๐Ÿ‘ ๐Ÿ‡
    ๐Ÿฆ event โณ queue
    ๐ŸŠ๐Ÿฆ keyboardEvent ๐Ÿ”ฒ event ๐Ÿ“ฉ ๐Ÿ‡
      ๐ŸŠ ๐Ÿฆ key ๐Ÿ”ฃ keyboardEvent ๐Ÿ‡
        ๐ŸŠ ๐Ÿ˜› key ๐Ÿ”Ÿq ๐Ÿ‡
          ๐ŸŽ โšก๏ธ
        ๐Ÿ‰
      ๐Ÿ‰
    ๐Ÿ‰
  ๐Ÿ‰
  ๐ŸŽ โšก๏ธ
๐Ÿ‰

Obviously, this code should be placed after the ๐Ÿ”‚ loop for cookies and before querying for new events. Once all cookies have been collected, youโ€™ll see the following screen:

The acoustic experience

Our game is missing something really essential: Sound. So go, and grab this nice sound and this sound and put them aside cookie.emojic.

The sound.wav should be played whenever the Cookie Monster is eating a cookie. Weโ€™ll of course load the file only once and that of course before the run loop.

๐Ÿฆ sample ๐Ÿš‡๐Ÿ”ท๐ŸŽถ๐Ÿ“„ ๐Ÿ”คsound.wav๐Ÿ”ค

Then letโ€™s add some code to play the sound after a cookie was eaten:

๐Ÿ”‚ cookie iterator ๐Ÿ‡
  ๐ŸŠ ๐Ÿ–Š ๐Ÿบcookie app ๐Ÿ‘‰๏ธ monster ๐Ÿ‘‡ monster ๐Ÿ‡
    ๐Ÿšฏ iterator
    ๐ŸŠ โ–ถ๏ธ ๐Ÿ” cookies 0 ๐Ÿ‡
      ๐Ÿ sample 1 0 1
    ๐Ÿ‰
  ๐Ÿ‰
๐Ÿ‰

This code might need some explanation. We added a ๐ŸŠ to check ensure we donโ€™t play the sound for the last cookie. ๐Ÿ sample 1 0 1 plays the loaded sample at full volume, no shift towards the left or right and normal speed.

When presenting the โ€œWell done!โ€ screen sound_end.wav should be played so weโ€™ll add the following code:

๐Ÿฆ endSample ๐Ÿš‡๐Ÿ”ท๐ŸŽถ๐Ÿ“„ ๐Ÿ”คsound_end.wav๐Ÿ”ค
๐Ÿ endSample 1 0 1

The whole program should now look like this:

๐Ÿ“ฆ allegro ๐Ÿ”ด

๐Ÿ‡ ๐Ÿฆ ๐Ÿ‡
  ๐Ÿฐ x ๐Ÿš€
  ๐Ÿฐ y ๐Ÿš€
  ๐Ÿฐ monster ๐Ÿ–ผ

  ๐Ÿˆ ๐Ÿ†• ๐Ÿ‡
    ๐Ÿฎ x 500
    ๐Ÿฎ y 500
    ๐Ÿฎ monster ๐Ÿš‡๐Ÿ”ท๐Ÿ–ผ๐Ÿ“„ ๐Ÿ”คcookie_monster.png๐Ÿ”ค
  ๐Ÿ‰

  ๐Ÿ– ๐Ÿ–Œ app ๐Ÿ” ๐Ÿ‡
    ๐Ÿ“ผ app monster x y
  ๐Ÿ‰

  ๐Ÿ– โฌ‡๏ธ ๐Ÿ‡
    ๐Ÿฎ y โž• y 10
  ๐Ÿ‰

  ๐Ÿ– โฌ†๏ธ ๐Ÿ‡
    ๐Ÿฎ y โž– y 10
  ๐Ÿ‰

  ๐Ÿ– โฌ…๏ธ ๐Ÿ‡
    ๐Ÿฎ x โž– x 10
  ๐Ÿ‰

  ๐Ÿ– โžก๏ธ ๐Ÿ‡
    ๐Ÿฎ x โž• x 10
  ๐Ÿ‰

  ๐Ÿ– ๐Ÿ‘‰๏ธ โžก๏ธ ๐Ÿš€ ๐Ÿ‡
    ๐ŸŽ โž• x 60
  ๐Ÿ‰

  ๐Ÿ– ๐Ÿ‘‡ โžก๏ธ ๐Ÿš€ ๐Ÿ‡
    ๐ŸŽ โž• y 70
  ๐Ÿ‰
๐Ÿ‰


๐Ÿ‡ ๐Ÿช ๐Ÿ‡
  ๐Ÿฐ cookie ๐Ÿ–ผ
  ๐Ÿฐ x ๐Ÿš€
  ๐Ÿฐ y ๐Ÿš€

  ๐Ÿˆ ๐Ÿ†• ๐Ÿผ x ๐Ÿš€ ๐Ÿผ y ๐Ÿš€ ๐Ÿผ cookie ๐Ÿ–ผ  ๐Ÿ‡๐Ÿ‰

  ๐Ÿ– ๐Ÿ–Š app ๐Ÿ” monsterX ๐Ÿš€ monsterY ๐Ÿš€ โžก๏ธ ๐Ÿ‘Œ ๐Ÿ‡
    ๐ŸŠ ๐ŸŽŠ๐ŸŽŠโžก๏ธ monsterX x โฌ…๏ธ monsterX โž• x 50 ๐ŸŽŠโžก๏ธ monsterY y โฌ…๏ธ monsterY โž• y 50 ๐Ÿ‡
      ๐ŸŽ ๐Ÿ‘
    ๐Ÿ‰
    ๐Ÿ“ผ app cookie x y
    ๐ŸŽ ๐Ÿ‘Ž
  ๐Ÿ‰
๐Ÿ‰

๐Ÿ ๐Ÿ‡
  ๐Ÿฉ๐Ÿ™‹๐Ÿ” ๐Ÿ‡ app ๐Ÿ”
    ๐Ÿฆ display ๐Ÿ”ท๐Ÿ“บ๐Ÿ†• 1000 1000
    ๐Ÿท display ๐Ÿ”คCookie Monster Game๐Ÿ”ค

    ๐Ÿฆ cookieBmp ๐Ÿš‡๐Ÿ”ท๐Ÿ–ผ๐Ÿ“„ ๐Ÿ”คcookie.png๐Ÿ”ค

    ๐Ÿฆ cookies ๐Ÿ”ท๐Ÿจ๐Ÿš๐Ÿช๐Ÿธ
    ๐Ÿฆ prng ๐Ÿ”ท๐ŸŽฐ ๐Ÿ†•
    ๐Ÿ”‚ i โฉ 0 10 ๐Ÿ‡
      ๐Ÿป cookies ๐Ÿ”ท๐Ÿช๐Ÿ†• ๐Ÿš€๐Ÿš‚ prng 50 900 ๐Ÿš€๐Ÿš‚ prng 50 900 cookieBmp
    ๐Ÿ‰

    ๐Ÿฆ sample ๐Ÿš‡๐Ÿ”ท๐ŸŽถ๐Ÿ“„ ๐Ÿ”คsound.wav๐Ÿ”ค

    ๐Ÿฆ font ๐Ÿš‡๐Ÿ”ท๐Ÿ•‰๐Ÿ“„ ๐Ÿ”คMonoton-Regular.ttf๐Ÿ”ค 63
    ๐Ÿฆ monster ๐Ÿ”ท๐Ÿฆ๐Ÿ†•

    ๐Ÿฆ queue ๐Ÿ”ท๐Ÿ—ƒ๐Ÿ†•
    โŒจ๏ธ queue
    ๐Ÿ” ๐Ÿ‘ ๐Ÿ‡
      ๐Ÿšฟ app ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 255 255 255 255
      ๐Ÿ–Œ monster app

      ๐Ÿฆ iterator ๐Ÿ”ท๐ŸŒฑ๐Ÿš๐Ÿช๐Ÿ†• cookies
      ๐Ÿ”‚ cookie iterator ๐Ÿ‡
        ๐ŸŠ ๐Ÿ–Š ๐Ÿบcookie app ๐Ÿ‘‰๏ธ monster ๐Ÿ‘‡ monster ๐Ÿ‡
          ๐Ÿšฏ iterator
          ๐ŸŠ โ–ถ๏ธ ๐Ÿ” cookies 0 ๐Ÿ‡
            ๐Ÿ sample 1 0 1
          ๐Ÿ‰
        ๐Ÿ‰
      ๐Ÿ‰
      ๐Ÿ”ก app font ๐Ÿ”ก ๐Ÿ” cookies 10 990 10 ๐Ÿ”ทโš–โžก๏ธ ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 0 0 0 255
      ๐ŸŽฆ app

      ๐ŸŠ ๐Ÿ˜› ๐Ÿ” cookies 0 ๐Ÿ‡
        ๐Ÿšฟ app ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 4 115 187 255
        ๐Ÿ”ก app font ๐Ÿ”คWell done!๐Ÿ”ค 500 500 ๐Ÿ”ทโš–โ†” ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 255 255 255 255
        ๐ŸŽฆ app

        ๐Ÿฆ endSample ๐Ÿš‡๐Ÿ”ท๐ŸŽถ๐Ÿ“„ ๐Ÿ”คsound_end.wav๐Ÿ”ค
        ๐Ÿ endSample 1 0 1

        ๐Ÿ” ๐Ÿ‘ ๐Ÿ‡
          ๐Ÿฆ event โณ queue
          ๐ŸŠ๐Ÿฆ keyboardEvent ๐Ÿ”ฒ event ๐Ÿ“ฉ ๐Ÿ‡
            ๐ŸŠ ๐Ÿฆ key ๐Ÿ”ฃ keyboardEvent ๐Ÿ‡
              ๐ŸŠ ๐Ÿ˜› key ๐Ÿ”Ÿq ๐Ÿ‡
                ๐ŸŽ โšก๏ธ
              ๐Ÿ‰
            ๐Ÿ‰
          ๐Ÿ‰
        ๐Ÿ‰
        ๐ŸŽ โšก๏ธ
      ๐Ÿ‰

      ๐Ÿฆ event โณ queue

      ๐ŸŠ๐Ÿฆ keyboardEvent ๐Ÿ”ฒ event ๐Ÿ“ฉ ๐Ÿ‡
        ๐ŸŠ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 84 ๐Ÿ‡
          โฌ†๏ธ monster
        ๐Ÿ‰
        ๐Ÿ‹ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 85 ๐Ÿ‡
          โฌ‡๏ธ monster
        ๐Ÿ‰
        ๐Ÿ‹ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 82 ๐Ÿ‡
          โฌ…๏ธ monster
        ๐Ÿ‰
        ๐Ÿ‹ ๐Ÿ˜›๐Ÿ“Ÿ keyboardEvent 83 ๐Ÿ‡
          โžก๏ธ monster
        ๐Ÿ‰
      ๐Ÿ‰
    ๐Ÿ‰
  ๐Ÿ‰
๐Ÿ‰

Well done! We have got a fully-fledge game!

โ† Previous Next โ†’
Want to improve this page? You can edit the source on GitHub and send us a pull request!