Python Arcade
Python Arcade
Main menu
ArticlesResourcesAboutCommunityThe Open Org
Sign Up
Image by :
Paul Vincent Craven (https://opensource.com/users/paul-vincent-craven). CC BY-SA 4.0
(https://creativecommons.org/licenses/by-sa/4.0/)
1 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
I wanted a library that was easier to use, more powerful, and used some of the new features
of Python 3, like decorators and type-hinting. Arcade is it. And this is how to get started.
Installation
Arcade, like many other packages, is available via PyPi (https://pypi.python.org/pypi), which
2 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
means you can install Arcade using the pip command (or the pipenv (https://opensource.com
/article/18/2/why-python-devs-should-use-pipenv) command). If you already have Python
installed, you can likely just open up a command prompt on Windows and type:
Get the newsletter x
Join the 85,000 open
source advocates who
receive our giveaway
MacOS alertstype:
and Linux
and article roundups.
For more detailed installation instructions, you can refer to the Arcade installation
documentation (http://arcade.academy/installation.html).
Simple drawing
You can open a window and create simple drawings with just a few lines of code. Let's create
an example that draws a smiley face like the figure below:
3 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
The script below shows how you can use Arcade's drawing commands
(http://arcade.academy/quick_index.html#drawing-module) to do this. Note that you don't
need to know how to use classes or even define functions. Programming with quick visual
feedback is great for anyone who wants to start learning to program.
import arcade
4 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
# Open the window. Set the window title and dimensions (width and height)
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")
# Keep the window open until the user hits the 'close' button
arcade.run()
Using functions
Of course, writing code in the global context isn't good form. Thankfully improving your
program by using functions is easy. Here we can see an example of a drawing a pine tree at
5 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
6 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
The more experienced programmer will know that modern graphics programs first load
drawing information onto the graphics card, and then ask the graphics card to draw it later as
a batch. Arcade supports this (http://arcade.academy/examples/shape_list_demo.html) as
well. Drawing 10,000 rectangles individually takes about 0.800 seconds. Drawing them as a
batch takes less that 0.001 seconds.
7 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
class MyGame(arcade.Window):
""" Main application class. """
arcade.set_background_color(arcade.color.AMAZON)
def setup(self):
# Set up your game here
pass
def on_draw(self):
""" Render the screen. """
arcade.start_render()
# Your drawing code goes here
def main():
game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
game.setup()
arcade.run()
if __name__ == "__main__":
main()
The Window class has several methods that your programs can override to provide
functionality to the program. Here are some of the most commonly used ones:
8 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
on_key_release: Handle when a key is released, here you might stop a player from moving.
on_mouse_motion: This is called every time the mouse moves.
on_mouse_press: Called when a mouse button is pressed.
Get the newsletter x
: This function is used in scrolling games, when you have a world much larger
Join thewhat can
85,000 be seen on one screen. Calling set_viewport allows a programmer to set
open
sourcet advocates who
part of that world is currently visible.
receive our giveaway alerts
and article roundups.
Sprites
Creating a sprite
SPRITE_SCALING_COIN = 0.2
This code will create a sprite using the image stored in coin_01.png. The image will be scaled
down to 20% of its original height and width.
9 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
Sprite lists
Sprites are normally organized into lists. These lists make it easier to manage the sprites.
Sprites in a list will use OpenGL to batch-draw the sprites as a group. The code below sets up
a game with a player, and a bunch of coins for the player to collect. We use two lists, one for
the player and one for the coins.
def setup(self):
""" Set up the game and initialize the variables. """
# Score
self.score = 0
10 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
def on_draw(self):
""" Draw everything """
arcade.start_render()
self.coin_list.draw()
self.player_list.draw()
# Loop through each colliding sprite, remove it, and add to the score.
for coin in coins_hit_list:
coin.kill()
self.score += 1
Game physics
11 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
Many games include some kind of physics. The simplest are top-down programs that prevent
the player from walking through walls. Platformers add more complexity with gravity and
platforms that move. Some games use a full 2D physics engine with mass, friction, springs,
Get the newsletter x
Join the 85,000 open
source advocates who
receive our giveaway alerts
and article roundups.
For simple top-down based games, an Arcade program needs a list of walls that the player
(or anything else) can't move through. I usually call this wall_list. Then a physics engine is
created in the Window class's setup code with:
The player_sprite is given a movement vector with its two attributes change_x and change_y. A
simple example of doing this would be to have the player move with the keyboard. For
example, this might be in the custom child of the Window class:
12 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
MOVEMENT_SPEED = 5
Although that code sets the player's speed, it doesn't move the player. In the update method
of the Window class, calling physics_engine.update() will move the player, but not through
walls.
self.physics_engine.update()
Platformers
13 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
Moving to a side view platformer is rather easy. A programmer just needs to switch the
physics engine to PhysicsEnginePlatformer and add in the gravity constant.
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
self.wall_list,
gravity_constant=GRAVITY)
You can use a program like Tiled (http://www.mapeditor.org/) to lay the tiles/blocks that make
up your level.
Learn by example
14 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
One of the best ways to learn is by example. The Arcade library has a long list of example
programs (http://arcade.academy/examples/index.html) that a person can draw on to create
games. These examples each show a game concept that students have asked for in my
s or
Get the x
online over the years.
newsletter
Join the 85,000 open
source gadvocates
any of these
who demos is easy once Arcade has been installed. Each of the samples
comment
receive at the
our giveaway beginning of the program with a command you can type on the
alerts
and-line
and article to run the sample, for example:
roundups.
python -m arcade.examples.sprite_moving_platforms
Summary
Arcade lets you start programming graphics and games with easy-to-understand code. Many
new programmers have created great games (http://arcade.academy/sample_games.html)
not long after getting started. Give it a try!
To learn more, attend Paul Vincent Craven's talk, Easy 2D Game Creation With Arcade
(https://us.pycon.org/2018/schedule/presentation/100/), at PyCon Cleveland 2018
(https://us.pycon.org/2018/).
15 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
Recommended reading
7 Python libraries for more How to analyze your Build an interactive CLI
maintainable code (/article system with perf and with Node.js (/article
/18/7/7-python-libraries- Python (/article/18/7/fun- /18/7/node-js-interactive-
more-maintainable- perf-and- cli?utm_campaign=intrel)
code?utm_campaign=intrel) python?utm_campaign=intrel)
5 Comments
First: Python 3.6 is a pain because it's so new, most distributions haven't even included 3.anything but at least
getting 3.5.x up and running is fairly easy on most distributions as far as I know, but 3.6.? Compiling from
source and wrestling with getting added bits and bobs and whatnot actually working? Who hasn't wasted far
too many hours on stuff like that?
You're not helping yourself. This is the prefect mirror image of PyGame not supporting whatever Python
16 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
people already have. In PyGame it's because PyGame is old (and broken) and in Arcade it's because the
Python is too new. Same difference. Okay if you run in dedicated environments etc. but not for normal people
at home wanting to take a peek and get right into it at minimal cost.
x a completely open and non-reviewed security issue as per the year-old report from
nd issue: pip, is it still
Get the newsletter
zech intelligence agency? Should anyone be using pip at all?
Join the 85,000 open
on lost the plot with pip. It's not your fault
source advocates who
receive our giveaway alerts issues in what is nothing but a drive-by comment using a mail address that is never
for such negative
and article roundups.
read (because Google is evil) but someone should point these things out and people usually don't, so for
once I nudged my temporary JavaScript settings for opensource.com and commented.
Despite all that your actual library looks interesting, too bad I'm tired of hoops.
If you don't like pip, install Arcade using setup.py. I'm not quite sure what you are looking for here.
I just installed it on Linux and will try it out very soon. It looks great. I like PyGame, but it *is* showing its age,
so this is a hugely welcome development.
(http://creativecommons.org/licenses/by-sa/4.0/)
17 of 18 7/31/18, 10:17 PM
How to create a 2D game with Python and the Arcade li... https://opensource.com/article/18/4/easy-2d-game-crea...
Learn more
Get the newsletter x
Join the 85,000 open
source advocates who
receive our giveaway alerts
Find us:
and article roundups.
Privacy Policy | Terms of Use | Contact | Meet the Team | Visit opensource.org
18 of 18 7/31/18, 10:17 PM