Build your first game with Arcade Library in Python
Games are always a great way of fun, entertainment and refreshment. Most of you might have grown up playing computer games or maybe playing even now. Now imagine building your own game, that too just with a few simple lines of code in Python! In this tutorial, you will learn to build your first game with Arcade Library in Python.
Arcade library in Python
Arcade is a Python library that provides options for building creative games in 2D, along with graphics and sound. It is easy to learn and use the library, hence you can learn by building simple games and gradually scale up.
Note: The Arcade library was developed as an improvement of the PyGame library and works only on Python versions 3.6 and above.
First of all, make sure that you have the arcade library installed by running the following command on your terminal:
pip install arcade
Firstly, make sure to import the arcade library to your program before performing operations using the same.
To open a window, set the window title, dimensions:
Syntax: arcade.open_window(Width, Height, Title)
Setting Background colour:
The colour_name can be specified either in RGB format or directly by using the color name.
You can always refer to Arcade Library Colors and Color Codes for the same.
To clear the window and start drawing(To switch to drawing mode):
Once you are done with the drawing part, you can indicate competition by using:
On completion of the drawing, designing and all other required code, you can use run() to execute the code:
Designing a game – arcade library
You can then start designing your game by using the functions provided by the arcade library.
Below, you can see a program to open a window, set the background color and draw a circle of a specified radius at a specified position;
import arcade width = 600 height = 800 title = "Welcome to CodeSpeedy!" radius = 150 arcade.open_window(width, height, radius) arcade.set_background_color(arcade.color.BLACK) arcade.start_render() arcade.draw_circle_filled( width / 2, height / 2, radius, arcade.color.RED ) arcade.finish_render() arcade.run()
Similarly, the arcade library provides functions that allow you to draw many different shapes such as ellipses, rectangles, triangles, parabolas, arcs, lines etc.
You can use your creativity and mix up these shapes to bring out fun designs!
Sprites to get larger scene for our 2d game using arcade in Python
Sprites are two-dimensional bitmaps that you can join together to get a larger scene that you can use in your 2D games. Let us now learn how to create sprites.
Consider the below example;
sprite_coin_scale = 0.3 coin = arcade.Sprite("coin.jpg", sprite_coin_scale)
The above code creates a 2D bitmap/ sprite of the image specified (coin.jpg) and reduces its size by scaling down to 30% of the original dimensions.
You can create and use such sprites as players, enemies, or even collectables like coins.
Check out below sample program to understand the basic usage of sprites:
import arcade width = 1000 height = 650 title = "Sample" char_scale = 1 tile_scale = 0.5 class MyGame(arcade.Window): def __init__(self): super().__init__(width, height, title) self.wall_list = None self.player_list = None self.player_sprite = None arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE) def setup(self): self.player_list = arcade.SpriteList() self.wall_list = arcade.SpriteList(use_spatial_hash=True) image_source = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png" self.player_sprite = arcade.Sprite(image_source, char_scale) self.player_sprite.center_x = 64 self.player_sprite.center_y = 128 self.player_list.append(self.player_sprite) for x in range(0, 1250, 64): wall = arcade.Sprite(":resources:images/tiles/grassMid.png", tile_scale) wall.center_x = x wall.center_y = 32 self.wall_list.append(wall) coordinate_list = [[512, 96], [256, 96], [768, 96]] for coordinate in coordinate_list: wall = arcade.Sprite( ":resources:images/tiles/boxCrate_double.png", tile_scale ) wall.position = coordinate self.wall_list.append(wall) def on_draw(self): self.clear() self.wall_list.draw() self.player_list.draw() def main(): window = MyGame() window.setup() arcade.run() if __name__ == "__main__": main()
- Here, firstly we have initialized the scaling values.
- Further, we have created sprite lists to make it easier to manage the sprites.
- In the setup method, we have initialized and set up player sprites.
We have created coins for the game and also defined the actions of the mouse along with sprite movement. We then define the logic of the game and code for detecting collision and adding scores.
One of the most effective ways to understand all this is by looking into examples. What’s nice is the fact that the arcade library itself has a number of example programs that you can access, run and understand. Further, you can modify these programs and build your own games on them.
Click here to look into the examples provided by the Arcade library!
You can also refer to the official documentation for the arcade library by clicking here!
You may also like to read, Stimulate bouncing game using Pygame!