JoBase Home Examples Reference

## Coin Collector

If you are new to JoBase, please visit the previous tutorials to understand the the basic setup. You can run this game by typing the following command into the terminal.

`python -m JoBase.examples.coin_collector`

To build this game ourselves, we need to start by drawing our character. To do this, we need to load an image. There are a set of default images that come when you install JoBase. For our character, we are going to use the MAN image.

```from JoBase import *

man = Image(MAN)

def loop():
man.draw()

run()
```

You should see our character in the middle of the screen. Now we can start moving him.

```def loop():
man.draw()

man.y = man.y + 1

run()
```

Each frame, the character's vertical position is increased, this makes him move up. In the actual game, we want him to move up when we press the arrow key, let's sort that out now.

```def loop():
man.draw()

if key.up:
man.y = man.y + 1

run()
```

When you run the program, the player will only move when you press the up arrow key. We can make the code slightly smaller by using a new coding feature.

```    man.draw()

if key.up:
man.y += 1

run()
```

Now we can do the same for the other arrow keys.

```    man.draw()

if key.up:
man.y += 1

if key.down:
man.y -= 1

if key.left:
man.x -= 1

if key.right:
man.x += 1

run()
```

To prevent the player from moving diagonally, we should stop him from moving in one direction if he is already moving in another direction. To do this, we can use the Python elif (else if) feature. We can also make our player faster by changing the numbers.

```from JoBase import *

man = Image(MAN)

def loop():
man.draw()

if key.up:
man.y += 3

elif key.down:
man.y -= 3

elif key.left:
man.x -= 3

elif key.right:
man.x += 3

run()
```

Now we can add the coin.

```from JoBase import *

man = Image(MAN)
coin = Image(COIN)

def loop():
man.draw()
coin.draw()

if key.up:
man.y += 3
```

Whenever the player collides with the coin, we want the coin to jump to a new random position. This makes it look like the coin has been collected.

```from JoBase import *

man = Image(MAN)
coin = Image(COIN)

def loop():
man.draw()
coin.draw()

if key.up:
man.y += 3

elif key.down:
man.y -= 3

elif key.left:
man.x -= 3

elif key.right:
man.x += 3

if man.collides_with(coin):
coin.x = random(camera.left, camera.right)
coin.y = random(camera.bottom, camera.top)

run()
```

In the code above, the random function is used to position the coin randomly on the screen (or camera).

Let's add the enemy.

```man = Image(MAN)
coin = Image(COIN)
enemy = Enemy(ENEMY)

def loop():
man.draw()
coin.draw()
enemy.draw()

if key.up:
man.y += 3
```

Now we want the emeny to eat the coins.

```from JoBase import *

man = Image(MAN)
coin = Image(COIN)
enemy = Image(ENEMY)

def loop():
man.draw()
coin.draw()
enemy.draw()

if key.up:
man.y += 3

elif key.down:
man.y -= 3

elif key.left:
man.x -= 3

elif key.right:
man.x += 3

enemy.look_at(coin)
enemy.move_toward(coin, 1.5)

if man.collides_with(coin):
coin.x = random(camera.left, camera.right)
coin.y = random(camera.bottom, camera.top)

if enemy.collides_with(coin):
coin.x = random(camera.left, camera.right)
coin.y = random(camera.bottom, camera.top)

run()
```

The look_at function makes the enemy face the coin. Then we use the move_toward function to make the enemy move towards the coin. We have set the speed of the enemy to 1.5 to give the player a chance (though it is quite fun to set the speed to 10).

To add the score, we need to draw some text. Let's do that now.

```coin = Image(COIN)
enemy = Image(ENEMY)

text = Text("Score: 0", font_size = 30)

def loop():
man.draw()
coin.draw()
enemy.draw()
text.draw()

if key.up:
man.y += 3
```
```    if enemy.collides_with(coin):
coin.x = random(camera.left, camera.right)
coin.y = random(camera.bottom, camera.top)

text.bottom = camera.bottom
text.left = camera.left + 5

run()
```

At the end of the game loop, we position the text in the bottom left corner of the camera. To make the text change, we need store it as a variable.

```enemy = Image(ENEMY)

text = Text("Score: 0", font_size = 30)
score = 0

def loop():
global score

man.draw()
coin.draw()
```
```    if man.collides_with(coin):
coin.x = random(camera.left, camera.right)
coin.y = random(camera.bottom, camera.top)

score += 1
print(score)

if enemy.collides_with(coin):
coin.x = random(camera.left, camera.right)
coin.y = random(camera.bottom, camera.top)

score -= 1
print(score)

text.bottom = camera.bottom
text.left = camera.left + 5
```

In the code above, we initialise the score variable at 0, then we update it whenever the player or enemy collides with the coin. We wrote global score at the top of the game loop to tell Python that the score variable can be accessed anywhere in the program. If this doesn't really make sense, don't worry; just make sure you include it at the top of the loop.

When you run the code, the score is printed whenever the coin is collected. Instead of printing it, let's update the text.

```from JoBase import *

man = Image(MAN)
coin = Image(COIN)
enemy = Image(ENEMY)

text = Text("Score: 0", font_size = 30)
score = 0

def loop():
global score

man.draw()
coin.draw()
enemy.draw()
text.draw()

if key.up:
man.y += 3

elif key.down:
man.y -= 3

elif key.left:
man.x -= 3

elif key.right:
man.x += 3

enemy.look_at(coin)
enemy.move_toward(coin, 1.5)

if man.collides_with(coin):
coin.x = random(camera.left, camera.right)
coin.y = random(camera.bottom, camera.top)

score += 1
text.content = "Score: " + str(score)

if enemy.collides_with(coin):
coin.x = random(camera.left, camera.right)
coin.y = random(camera.bottom, camera.top)

score -= 1
text.content = "Score: " + str(score)

text.bottom = camera.bottom
text.left = camera.left + 5

run()
```

You may notice that we added the score variable to the end of the "Score: " string, this is called concatenation (joining two strings together). The str function turns the score into a string before joining it.

Well done, that's it for the game. You can make it more interesting by adding multiple enimies, a countdown timer, or even another player (controlled with different keys). Please email any of your final projects to hello@jobase.org.