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-orientated 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:

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

  ๐Ÿ‰
๐Ÿ‰

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. Closures are kind of functions that capture the context in which they were created. Closures are a special kind of callables. Callables is what the type of values that can be executed are called in Emojicode.

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.

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 ๐Ÿš‚

You may have noticed the ๐Ÿฌ in front of the ๐Ÿˆ. ๐Ÿฌ indicates that this initializer may return Nothingness. Our code to get a display is therefore:

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

Please note that we skipped error checking for the moment and just unwrapped the optional with ๐Ÿบ here. Thatโ€™s not a really good idea but weโ€™ll leave it like that because it keeps the sample code shorter (and itโ€™s long enough).

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

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 same directory as the program.

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 also marked with ๐Ÿฌ โ€“ it will return Nothingness if the path is invalid or the given file is not an image. Still believing we live in a fail-safe, we skip error checking once again:

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

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

๐Ÿฉ๐Ÿšฟ๐Ÿ” ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 255 255 255 255
๐Ÿฉ๐Ÿ“ผ๐Ÿ” cookieBmp 500 500
๐Ÿฉ๐ŸŽฆ๐Ÿ”๏ธ

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

Wondering whatโ€™s going on โ€™cause thatโ€™s a bunch of strange type method calls? Well, these are a strange bunch of type method calls but these are all very important drawing functions. And they all have one thing in common: They are type methods 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 a additional cookie:

๐Ÿ“ฆ allegro ๐Ÿ”ด

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

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

    ๐Ÿฆ monster ๐Ÿบ๐Ÿ”ท๐Ÿ–ผ๐Ÿ“„ ๐Ÿ”คcookie_monster.png๐Ÿ”ค
    ๐Ÿฉ๐Ÿ“ผ๐Ÿ” monster 200 200

    ๐Ÿฉ๐ŸŽฆ๐Ÿ”๏ธ

    ๐Ÿฉโณ๐Ÿ’ˆ 10
  ๐Ÿ‰
๐Ÿ‰

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๐Ÿ”ค
  ๐Ÿ‰

  ๐Ÿ– ๐Ÿ–Œ ๐Ÿ‡
    ๐Ÿฉ๐Ÿ“ผ๐Ÿ” 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 ๐Ÿ–ผ  ๐Ÿ‡
    ๐Ÿฎ x _x
    ๐Ÿฎ y _y
    ๐Ÿฎ cookie _cookie
  ๐Ÿ‰

  ๐Ÿ– ๐Ÿ–Œ ๐Ÿ‡
    ๐Ÿฉ๐Ÿ“ผ๐Ÿ” 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:

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

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

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

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

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

    ๐Ÿฉ๐ŸŽฆ๐Ÿ”๏ธ

    ๐Ÿฉโณ๐Ÿ’ˆ 10
  ๐Ÿ‰
๐Ÿ‰

As you can see we didnโ€™t just replace the drawing calls but also introduced a ๐Ÿจ cookies which stores ten randomly placed cookie objects. ๐Ÿ”ท๐Ÿš‚๐ŸŽฐ 50 900 generates a random integer between 50 and 900 which is then converted into a ๐Ÿš€ with ๐Ÿš‚โ€™s ๐Ÿš€ method. Later on this ๐Ÿจ is iterated and the ๐Ÿ–Œ 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:

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

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

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

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

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

      ๐Ÿฆ 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:

๐Ÿ– ๐Ÿ–Š monsterX ๐Ÿš€ monsterY ๐Ÿš€ โžก๏ธ ๐Ÿ‘Œ ๐Ÿ‡
  ๐ŸŠ ๐ŸŽŠ๐ŸŽŠโžก๏ธ monsterX x โฌ…๏ธ monsterX โž• x 50 ๐ŸŽŠโžก๏ธ monsterY y โฌ…๏ธ monsterY โž• y 50 ๐Ÿ‡
    ๐ŸŽ ๐Ÿ‘
  ๐Ÿ‰
  ๐Ÿฉ๐Ÿ“ผ๐Ÿ” 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 ๐Ÿ‘‰๏ธ monster ๐Ÿ‘‡ monster ๐Ÿ‡
    ๐Ÿšฏ iterator
  ๐Ÿ‰
๐Ÿ‰

The code above is really straightforward. cookies is asked for an ๐Ÿก (iterator) 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 the reason why we explicitly asked for an ๐Ÿก.)

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:

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

Place this code before ๐Ÿฉ๐ŸŽฆ๐Ÿ”๏ธ. ๐Ÿฉ๐Ÿ”ก๐Ÿ” draws a text using the given font. The signature of this method is:

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

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 ๐Ÿ‡ ๐Ÿ‘ด No cookies left!
  ๐Ÿฉ๐Ÿšฟ๐Ÿ” ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 4 115 187 255
  ๐Ÿฉ๐Ÿ”ก๐Ÿ” font ๐Ÿ”ท๐ŸŽจ๐Ÿ†• 255 255 255 255 500 500 ๐Ÿ”คWell done!๐Ÿ”ค ๐Ÿ”ทโš–โ†”
  ๐Ÿฉ๐ŸŽฆ๐Ÿ”๏ธ

  ๐Ÿ” ๐Ÿ‘ ๐Ÿ‡ ๐Ÿ‘ด Custom run loop to wait for the exit command "q"
    ๐Ÿฆ event โณ queue
    ๐ŸŠ๐Ÿฆ keyboardEvent ๐Ÿ”ฒ event ๐Ÿ“ฉ ๐Ÿ‡
      ๐ŸŠ ๐Ÿฆ key ๐Ÿ”ฃ keyboardEvent ๐Ÿ‡
        ๐ŸŠ ๐Ÿ˜› key ๐Ÿ”Ÿq ๐Ÿ‡
          ๐ŸŽ โšก๏ธ
        ๐Ÿ‰
      ๐Ÿ‰
    ๐Ÿ‰
  ๐Ÿ‰
  ๐ŸŽ โšก๏ธ ๐Ÿ‘ด Same here
๐Ÿ‰

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 ๐Ÿ‘‰๏ธ monster ๐Ÿ‘‡ monster ๐Ÿ‡
  ๐Ÿšฏ iterator
  ๐ŸŠ โ–ถ๏ธ ๐Ÿ” cookies 0 ๐Ÿ‡ ๐Ÿ‘ด This is new!
    ๐Ÿ 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๐Ÿ”ค
  ๐Ÿ‰

  ๐Ÿ– ๐Ÿ–Œ ๐Ÿ‡
    ๐Ÿฉ๐Ÿ“ผ๐Ÿ” 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 ๐Ÿ–ผ  ๐Ÿ‡
    ๐Ÿฎ x _x
    ๐Ÿฎ y _y
    ๐Ÿฎ cookie _cookie
  ๐Ÿ‰

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

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

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

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

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

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

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

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

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

        ๐Ÿฆ 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!