Introduction
Pyglet is a library for the Python programming language that gives an item situated application programming connection point for making games and other media applications. Therefore if we wish to create any such application, Pyglet is the best alternative. We will learn how to make the famous Asteroids game with the help of Pyglet.
Asteroids game using Pyglet
To begin with the game, first, we need to make sure that we have pip and pyglet installed in our device in working condition with the latest updates.
We would also require an editor to code the program here; we will take Visual Studio Code with Python extension as an example. First, we need to create folders for the game project that will contain the effects in the game like images, sounds, SRC.
These folders have their purpose, like the image folder is for the image files used in the game, the sound folder is for the game's sound files, and the SRC folder is for python programming files. We can add image files of game characters and objects in the image folder and the same for the sound file.
Starting with Visual Studio Code
Start by creating a new folder in the SRC file in Visual Studio Code itself. The code will include importing pyglet libraries and making a game window. The code for all this is given below. Now add global variants for the game score and other help box information like FPS. Label creations for Current score, Best score, Information, FPS, Help, and Pause. These are shown in the game window. Label Class of Pyglet provides a simple interface for the typical case where an application simply needs to display some text in a window. Add a global variant for whether the game is paused or not, then add extra processing to the Pyglet function that already exists using Decorator of Python and other commands for the program game screen and labels. Now add Programming showing and Moving the player's character, shooting three guns on asteroids, and then programming showing the Barrier for the player's battle plane. At last, add Programming for Implementation of asteroids, Collision Detection Processing, Crash processing for enemies and the player, and so on.
Here are the top functions that are required for the game
- Update the game score.
- Returns a set of objects collided with other_object.
- Finds the collisions between two groups.
- Create an asteroid object.
- Destroy the movement.
- Process a sprite group.
- Squared distance between two points.
- Generate random positions.
- Handle mouse press events.
Different code snippets that help in creating the game
To convert LiveData Entity to LiveData Domain model
override suspend fun getAsteroids(): LiveData<List<Asteroid>> {
return Transformations.map(appLocalDb.asteroidDao.getAsteroidsFromDb()) { it.map{ item -> Asteroid(item.name,....)} }
}For returning a list of data to ViewModel using Repository patterns from different data sources
override suspend fun getAsteroid(): List<Asteroid> {
var result : List<Asteroid>
try {
var isDataAvailable = getAnyAsteroidFromDb()
if (isDataAvailable == null) {
result = getAsteroidApi().asDomainModel()
} else {
result = getAsteroidFromDb().value //<==List<Asteroid>
}
} catch (e : Exception) {
print(e)
}
return resultType mismatch in dao insert - how to convert a list of objects to an entity
var result = repository.getAsteroid()
_asteroidList.value = result.toMutableList()
appLocalDb.asteroidDao.insertFeed(*result.map{ repoAsteroid ->
com.asteroidradar.Asteroid(
//Set full properties here
)
}.toTypedArray())Raylib DrawTriangle() usage
// draw triangle
DrawTriangle(
{100, 10}, // point a
{10, 100}, // point b
{100, 100}, // point c
BLACK // triangle color
);When the Camera is not following the spaceship
window.scene = this;
//this.cameras.main.setBounds(0, 0, 3200, 3200);
scene.cameras.main.setBounds(0, 0, 3200, 3200);
scene.cameras.main.startFollow(self.ship);
implement gravity to a free floating space object and some sort of friction when thrusting in opposite direction
self.acc += vec(PLAYER_ACC, 0).rotate(-self.rot)
self.vel += self.acc
max_vel = 2
self.vel[0] = max(-max_vel, min(max_vel, self.vel[0]))
self.vel[1] = max(-max_vel, min(max_vel, self.vel[1]))
class Player(pg.sprite.Sprite):
# [...]
def get_keys(self):
self.rot_speed = 0
self.acc = vec(0, PLAYER_GRAV)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.rot_speed = PLAYER_ROT_SPEED
if keys[pg.K_RIGHT]:
self.rot_speed = -PLAYER_ROT_SPEED
if keys[pg.K_UP]:
self.acc += vec(PLAYER_ACC, 0).rotate(-self.rot)
if keys[pg.K_SPACE]:
self.shoot()
self.vel += self.acc + self.vel * PLAYER_FRICTION
max_vel = 2
self.vel[0] = max(-max_vel, min(max_vel, self.vel[0]))
self.vel[1] = max(-max_vel, min(max_vel, self.vel[1]))
self.pos += self.vel




