Use this file to discover Exa products, pricing, evaluations, comparisons, use cases, agent interfaces, and technical documentation before exploring further.
Python game development libraries open a window, read keyboard and mouse input, draw sprites or scenes, and play sound from Python code. A game framework runs a loop that handles input, updates the game state, and draws a new frame many times a second. These libraries let developers build prototypes, teaching projects, and small games without a full commercial engine.
Popularity
pygame has the most monthly downloads in this comparison, at 2.7M.
def apply_colour_to_surface(
colour: pygame.Color,
shape_surface: pygame.surface.Surface,
rect: Union[pygame.Rect, None] = None,
):
"""
Apply a colour to a shape surface by multiplication blend. This works best when the shape
surface is predominantly white.
:param colour: The colour to apply.
:param shape_surface: The shape surface to apply the colour to.
:param rect: A rectangle to apply the colour inside.
"""
if rect is not None:
colour_surface = pygame.surface.Surface(
rect.size, flags=pygame.SRCALPHA, depth=32
)
colour_surface.fill(colour)
shape_surface.blit(colour_surface, rect, special_flags=pygame.BLEND_RGBA_MULT)
else:
colour_surface = pygame.surface.Surface(
shape_surface.get_size(), flags=pygame.SRCALPHA, depth=32
)
colour_surface.fill(colour)
shape_surface.blit(colour_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
Panda3D
Provides a three-dimensional scene engine with Python bindings, collisions, and audio systems.
Downloads / mo
210.5K
Stars
5.3K
Last release
1.10.16
License
BSD-3-Clause
Strengths
Includes a complete scene engine.Use Python to control a scene-oriented game and rendering engine.
Integrates Bullet physics.Use Bullet bodies for rigid-body dynamics and collision detection.
Limitations
Collision systems stay separate.Bullet bodies do not collide with ODE bodies or native CollisionNodes.
def generate(self): # Must be called after a mesh's values has been updated in order to update visually
self._generated_vertices = None
if hasattr(self, 'geomNode'):
self.geomNode.removeAllGeoms()
else:
self.geomNode = p3d.GeomNode('mesh_geom')
self.attachNewNode(self.geomNode)
if len(self.vertices) == 0 and self.vertex_buffer is None:
return
static_mode = p3d.Geom.UHStatic if self.static else p3d.Geom.UHDynamic
vertex_format = p3d.GeomVertexFormat()
color_attribute_index = -1
uv_attribute_index = -1
normal_attribute_index = -1
if self.vertex_buffer is not None:
vertex_array_format = p3d.GeomVertexArrayFormat()
attributes = self.vertex_buffer_format.split(",")
for attribute in attributes:
attribute_type = attribute[0]
attribute_type_name = attribute[0]
if attribute_type == "p":
attribute_type = p3d.Geom.C_point
attribute_type_name = "vertex"
elif attribute_type == "c":
attribute_type = p3d.Geom.C_color
attribute_type_name = "color"
elif attribute_type == "t":
attribute_type = p3d.Geom.C_texcoord
attribute_type_name = "texcoord"
elif attribute_type == "n":
attribute_type = p3d.Geom.C_normal
attribute_type_name = "normal"
else:
raise Exception("Invalid vertex buffer format attribute type: {}.".format(attribute_type))
attribute_count = int(attribute[1:-1])
attribute_dtype = attribute[-1]
if attribute_dtype == "f":
attribute_dtype = p3d.Geom.NT_float32
elif attribute_dtype == "I":
attribute_dtype = p3d.Geom.NT_uint32
else:
raise Exception("Invalid vertex buffer format attribute data type: {}.".format(attribute_dtype))
vertex_array_format.addColumn(attribute_type_name, attribute_count, attribute_dtype, attribute_type)
vertex_format.addArray(vertex_array_format)
else:
vertex_format = p3d.GeomVertexFormat()
vertex_format.add_array(p3d.GeomVertexFormat.getV3().arrays[0])
attribute_count = 1
if self.colors is not None and len(self.colors) > 0:
vertex_format.add_array(p3d.GeomVertexArrayFormat('color', 4, p3d.Geom.NT_float32, p3d.Geom.C_color))
color_attribute_index = attribute_count
attribute_count += 1
if self.uvs is not None and len(self.uvs) > 0 and self.mode not in ['line', 'point']:
vertex_format.add_array(p3d.GeomVertexArrayFormat('texcoord', 2, p3d.Geom.NT_float32, p3d.Geom.C_texcoord))
uv_attribute_index = attribute_count
attribute_count += 1
if self.normals is not None and len(self.normals) > 0 and self.mode not in ['line', 'point']:
vertex_format.add_array(p3d.GeomVertexArrayFormat('normal', 3, p3d.Geom.NT_float32, p3d.Geom.C_normal))
normal_attribute_index = attribute_count
attribute_count += 1
vertex_format = p3d.GeomVertexFormat.register_format(vertex_format)
vdata = p3d.GeomVertexData('vertex_data', vertex_format, static_mode)
if self.vertex_buffer is not None:
m = memoryview(self.vertex_buffer).cast('B')
vdata.unclean_set_num_rows(self.vertex_buffer_length)
array_handle = vdata.modify_array(0)
vmem = memoryview(array_handle).cast('B')
vmem[:] = m
else:
if isinstance(self.vertices[0], numbers.Real):
vdata.unclean_set_num_rows(len(self.vertices) // 3)
else:
vdata.unclean_set_num_rows(len(self.vertices))
self._set_array_data(vdata.modify_array(0), self._ravel(self.vertices), 'f')
if self.colors is not None and len(self.colors) > 0:
self._set_array_data(vdata.modify_array(color_attribute_index), self._ravel(self.colors), 'f')
if self.uvs is not None and len(self.uvs) > 0 and self.mode not in ['line', 'point']:
self._set_array_data(vdata.modify_array(uv_attribute_index), self._ravel(self.uvs), 'f')
if self.normals is not None and len(self.normals) > 0 and self.mode not in ['line', 'point']:
self._set_array_data(vdata.modify_array(normal_attribute_index), self._ravel(self.normals), 'f')
geom = p3d.Geom(vdata)
self.geom = geom
if len(self.triangles) == 0: # no triangles provided, so just add them in order
prim = Mesh._modes[self.mode](static_mode)
prim.set_index_type(p3d.GeomEnums.NT_uint32)
parray = prim.modify_vertices()
n = len(self.vertices)
if isinstance(self.vertices[0], numbers.Real):
n = n // 3
triangles = [i for i in range(n)]
parray.unclean_set_num_rows(n)
self._set_array_data(parray, triangles, 'I')
prim.close_primitive()
geom.addPrimitive(prim)
else:
if not isinstance(self.triangles[0], numbers.Real): # triangles provided as [(0,1,2), (3,4,5,6), ...] etc., so unpack them
line_segments = []
indices = self.indices
for tup in self.triangles:
if len(tup) == 2:
line_segments.append(tup)
for line_segment in line_segments:
prim = Mesh._modes['line'](static_mode)
prim.set_index_type(p3d.GeomEnums.NT_uint32)
parray = prim.modify_vertices()
parray.unclean_set_num_rows(len(line_segment))
self._set_array_data(parray, line_segment, 'I')
prim.close_primitive()
geom.addPrimitive(prim)
if len(indices) > 0:
prim = Mesh._modes[self.mode](static_mode)
prim.set_index_type(p3d.GeomEnums.NT_uint32)
parray = prim.modify_vertices()
parray.unclean_set_num_rows(len(indices))
self._set_array_data(parray, indices, 'I')
prim.close_primitive()
geom.addPrimitive(prim)
else: # got triangles as [0,1,2,3,4,5], ie. flat
prim = Mesh._modes[self.mode](static_mode)
prim.set_index_type(p3d.GeomEnums.NT_uint32)
parray = prim.modify_vertices()
parray.unclean_set_num_rows(len(self.triangles))
self._set_array_data(parray, self._ravel(self.triangles), 'I')
prim.close_primitive()
geom.addPrimitive(prim)
self.geomNode.addGeom(geom)
if self.mode == 'point':
self.setTexGen(p3d.TextureStage.getDefault(), p3d.TexGenAttrib.MPointSprite)
def on_update(self, delta_time: float):
"""Update all the game objects
Arguments:
delta_time {float} -- How many seconds since the last frame?
"""
# Check if you've picked up a coin coins_hit = arcade.check_for_collision_with_list(
sprite=self.player, sprite_list=self.coins
)
for coin in coins_hit:
# Add the coin score to your score self.score += COIN_VALUE
# Play the coin sound arcade.play_sound(self.coin_pickup_sound)
# Remove the coin coin.remove_from_sprite_lists()
# Are there more coins than allowed on the screen? if len(self.coins) > COIN_COUNT:
# Stop adding coins arcade.unschedule(function_pointer=self.add_coin)
# Show the mouse cursor self.set_mouse_visible(True)
# Print the final score and exit the game print(f"Game over! Final score: {self.score}")
exit()
pygame suits two-dimensional games with a hand-written event and draw loop. Arcade suits two-dimensional games built around sprite lists, views, and callbacks. Panda3D and Ursina suit three-dimensional games with a scene graph.
Panda3D is a full game engine with a live scene graph, model loading, collisions, and audio. Ursina builds an entity and prefab framework on top of it for faster setup. Pyxel is a retro engine with its own pixel graphics, sound, and asset editor.
pygame-ce is a separately published community fork that exposes the same pygame import and game APIs. A project installs one of the two distributions, and its dependency manifest names which one.
pyglet provides native windows, input, OpenGL graphics, and media playback for Python applications. It also loads sprites and models.
For coding agents
Found your game development library? Help your agent use it.
Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.