6. Improving your games
Here are some tips that will enable you to enhance your games.
6.1. Game controllers
You may call them joysticks or controllers, but Raylib calls them gamepads.
Some controllers have different inputs and some are not compatible at all so don’t be surprised if this doesnt work properly! PS4 and Xbox One controllers connected by USB cable seems to work best.
1from rlzero import *
2
3alan = Sprite('alien.png')
4alan.pos = (0, 50)
5
6def draw():
7 alan.draw()
8
9def update():
10 if keyboard.right or gamepad.right:
11 alan.x = alan.x + 2
12 elif keyboard.left or gamepad.left:
13 alan.x = alan.x - 2
14
15run()
16
Optional, if you have a controller
Make the alien move up and down as well as left and right using the controller. Do the same for any other examples that use the keyboard!
6.2. Colours
Note
The word colour is incorrectly spelt color in American programs such as Raylib. We will try to use the correct spelling wherever we can but sometimes you will have to use color.
Instead of using ready made colours like RED
, YELLOW
, etc. you can
create your own colour with a tuple of three numbers. The numbers must
be between 0 - 255 and represent how much red, green and blue to
mix together.
1from rlzero import *
2
3# my_colour = Color(0,0,0,255) # makes black
4# my_colour = Color(255,255,255,255) # makes white
5
6red_amount = 0
7green_amount = 0
8blue_amount = 0
9alpha_amount = 255
10
11def draw():
12 my_colour = Color(red_amount, green_amount, blue_amount, alpha_amount)
13 clear_background(my_colour)
14
15# This function makes the colour change every frame
16# Remove it if you just want to see a static colour.
17def update():
18 global red_amount
19 red_amount += 1
20 red_amount = red_amount % 255
21
22run()
Advanced
Change the green and blue amounts to make different colours.
Exercise
Make a gray colour.
Advanced
Make random colours. (see Program 3.4).
6.3. Using loops
The loops from Program 3.6 are useful for graphical games too! Here we draw red circles using a for loop.
We draw green circles using a loop within another loop.
1from rlzero import *
2
3WIDTH = 500
4HEIGHT = 500
5
6def draw():
7 for x in range(0, WIDTH, 40):
8 draw_circle(x, 20, 20, RED)
9
10 for x in range(0, WIDTH, 40):
11 for y in range(60, HEIGHT, 40):
12 draw_circle(x, y, 10, GREEN)
13
14run()
Exercise
import random at the top of the program and then make the positions random, e.g:
x = random.randint(0, 100)
Advanced
Learn about RGB colour and make random colours (see Program 6.2).
Advanced
Create a timer variable and change colours based on time (see Program 7.6)
6.4. Fullscreen mode
Sometimes it is nice to play your game using the entire screen rather than in a window. This program show how to enter fullscreen and and how to quit.
1from rlzero import *
2
3WIDTH = 500
4HEIGHT = 500
5
6alien = Sprite("alien")
7
8def draw():
9 clear()
10 alien.draw()
11
12def update():
13 if keyboard.f1:
14 toggle_fullscreen()
15 if keyboard.enter:
16 exit()
17
18run()
Note
RLZero automatically uses the F key for fullscreen and Escape key to quit, so you don’t actually need this code in your programs unless you want to use a different key.
6.5. Displaying the score
This game shows how you can keep the score in a variable and print it on to the game screen. You can display any other messages to the player the same way.
1from rlzero import *
2
3WIDTH = 500
4HEIGHT = 500
5
6score = 0
7
8def draw():
9 clear()
10 draw_text(f"Player 1 score: {score}", 0, 0, 20, RED)
11
12# This is another special function that is called by RLzero automatically
13# each time a key is pressed. That way player cannot just hold down the key!
14
15def on_key_pressed(key):
16 global score
17 if key == KEY_SPACE:
18 score += 1
19
20run()
Exercise
Make the score text larger.
Advanced
Add a second player who presses a different key and show their score too.
Exercise
Add text to one of your other games.
6.6. Animation
We did some animation ourselves in Program 5.5.
Animation is a RLZero class that makes it easier to show a sequence of different images on the same Sprite. You just need to give it the image file names and the number of frames-per-second you want and then call its update method.
1from rlzero import *
2
3WIDTH = 500
4HEIGHT = 500
5alien = Sprite("alien.png")
6animation = Animation(["alien.png","alien_hurt.png"], 5)
7alien.pos = (0, 50)
8
9
10def draw():
11 clear()
12 alien.draw()
13
14def update():
15 if keyboard.right:
16 alien.x = alien.x + 2
17 elif keyboard.left:
18 alien.x = alien.x - 2
19 animation.update(alien)
20
21run()
Exercise
Draw three images of a man walking and save them as man1.png, man2.png, man3.png. Edit the program so it animates these images.