Shooting game — Learn to code with Python and Raylib documentation (2024)

In this chapter we will build a shooting game together, step by step.The Python we will use is: conditionals, loops, lists and functions.

10.1. Step 1: Decide what Sprites you will need

Our game will need these Sprites, so we must create images for all ofthem and save them as .png files in the program folder.

variable name

image file name

image size

player

player.png

64x64

background

background.png

600x800

enemies (list)

enemy.png

64x64

bullets (list)

bullet.png

16x16

bombs (list)

bomb.png

16x16

The player and background variables will contain Sprites. Theothers are lists which we initialize to the empty list []. Spriteswill be appended to the lists later.

Program 10.1 Shooter game part 1 of 4

 1from rlzero import * 2import random 3 4WIDTH = 600 5HEIGHT = 800 6MAX_BULLETS = 3 7 8level = 1 9lives = 310score = 01112background = Sprite("background.png")13player = Sprite("player.png", (200, 730))14enemies = []15bullets = []16bombs = []

10.2. Step 2: Draw your Sprites

Every RLZero game needs a draw() function, and it should draw allthe Sprites we created above.

Program 10.2 Shooter game part 2 of 4

 1def draw(): 2 background.draw() 3 player.draw() 4 for enemy in enemies: 5 enemy.draw() 6 for bullet in bullets: 7 bullet.draw() 8 for bomb in bombs: 9 bomb.draw()10 draw_gui()

10.3. Step 3: Move your Sprites

Every RLZero game needs an update() function to move the Sprites,check for collisions, etc.

Program 10.3 Shooter game part 3 of 4

1def update(delta):2 move_player()3 move_bullets()4 move_enemies()5 create_bombs()6 move_bombs()7 check_for_end_of_level()

10.4. Step 4: Define your functions

Python cannot call a function that has not yet been defined. Thereforewe must at least provide empty, dummy versions of our functions thatdon’t do anything so we can fill them in later. However Python cannotdefine a completely empty function - it must contain at least one line.Therefore we use the pass keyword to create a line that doesn’t doanything.

Program 10.4 Shooter game part 4 of 4

 1 2def move_player(): 3 pass 4 5def move_enemies(): 6 pass 7 8def move_bullets(): 9 pass1011def create_bombs():12 pass1314def move_bombs():15 pass1617def check_for_end_of_level():18 pass1920def draw_gui():21 pass2223run()

Exercise

Create the png image files (player.png, background.png, bullet.png, bomb.png, enemy.png). Type in program Program 10.1, Program 10.2 and Program 10.3 and Program 10.4 into a single file. Verify the game now runs and you can see the player at the bottom of the screen. (He can’t move yet.)

10.5. Create enemies

Add this new function to the end of the program, and then call itimmediately. It uses a loop within a loop to create enemy Sprites and putthem in the enemies list. The reason we put this in a function is wewill need to call it again at the start of each level.

 def create_enemies(): for x in range(0, 600, 60): for y in range(0, 200, 60): enemy = Sprite("enemy.png", (x, y)) enemy.vx = level * 2 enemies.append(enemy)create_enemies()

10.6. Move the player

Replace the move_player() dummy function definition with this.Remember there can only be one function with a given name. Therecannot be two ``move_player()`` function definitions.

def move_player(): if keyboard.right: player.x = player.x + 5 if keyboard.left: player.x = player.x - 5 if player.x > WIDTH: player.x = WIDTH if player.x < 0: player.x = 0

10.7. Move the enemies

Replace the move_enemies() dummy function definition with this:

def move_enemies(): global score for enemy in enemies: enemy.x = enemy.x + enemy.vx if enemy.x > WIDTH or enemy.x < 0: enemy.vx = -enemy.vx enemy.y += 60 for bullet in bullets: if bullet.colliderect(enemy): enemies.remove(enemy) bullets.remove(bullet) score = score + 1 if enemy.colliderect(player): exit()

10.8. Draw text on the screen

Replace the draw_gui() dummy function definition with this:

def draw_gui(): draw_text("Level " + str(level), 0, 0, 20, RED) draw_text("Score " + str(score), 100, 0, 20, RED) draw_text("Lives " + str(lives), 200, 0, 20, RED)

10.9. Player bullets

Add this new function to the end of the program. Pygame will call it forus automatically when a key is pressed.

def on_key_pressed(key): if key == keys.space and len(bullets) < MAX_BULLETS: bullet = Sprite("bullet.png", pos=(player.x, player.y)) bullets.append(bullet))

Replace the move_bullets() dummy function definition with this:

def move_bullets(): for bullet in bullets: bullet.y = bullet.y - 6 if bullet.y < 0: bullets.remove(bullet)

10.10. Enemy bombs

Replace the create_bombs() dummy function definition with this:

def create_bombs(): if random.randint(0, 100 - level * 6) == 0: enemy = random.choice(enemies) bomb = Sprint("bomb.png", enemy.pos) bombs.append(bomb)

Replace the move_bombs() dummy function definition with this:

def move_bombs(): global lives for bomb in bombs: bomb.y = bomb.y + 10 if bomb.colliderect(player): bombs.remove(bomb) lives = lives - 1 if lives == 0: exit()

10.11. Check for end of level

Replace the check_for_end_of_level() dummy function definition withthis:

def check_for_end_of_level(): global level if len(enemies) == 0: level = level + 1 create_enemies()

10.12. Ideas for extension

  • Draw an explosion image and create an explosion Sprite every time analien dies.

  • Make the explosion Sprite disappear after a short time.

  • Draw several explosion images, put them in a list and make the Spriteanimate displaying each of them in order.

  • The player can only fire 3 bullets at a time. Create a powerup thatallows him to fire additional bullets.

  • Add music and sound effects.

Shooting game — Learn to code with Python and Raylib  documentation (2024)

FAQs

Is raylib easier than Sdl2? ›

Raylib is a new all around game framework, with a focus on graphics. It's designed to be easy to use and to get beginner programmers into game/graphics programming. It rolls it's own window handling, so SDL is more mature in that regard. For your use case (pixelart game) both would work.

Is raylib C or C++? ›

raylib-cpp is a C++ wrapper library for raylib, a simple and easy-to-use library to enjoy videogames programming.

What graphics API does raylib use? ›

No, raylib is built on top of OpenGL API, and there are currently no plans to support any other graphics APIs. In any case, raylib uses rlgl as an abstraction layer to support different OpenGL versions.

Is raylib a game engine? ›

raylib is by no means a full-featured game engine, and so has no in-built physics engine; it is great for putting a game together quickly though, without having to worry about setting up Vulkan or platform specific graphics libraries.

Is SDL2 faster than SFML? ›

SFML/OpenGL relies on the hardware being there. SDL does not. Therefore if you have the hardware, SFML is probably going to be faster. If you don't, SDL will probably be faster.

Is learning SDL worth it? ›

It can be a great learning experience that equips you with a deeper understanding of how game mechanics are implemented at a low level.

Is C C++ outdated? ›

There's nothing outwardly wrong with C++, – that's why it's still so widely used today.” In 2022, C++ is a useful, up-to-date, and vital programming language, especially as many of the world's major operating systems such as Microsoft Windows were built from the program.

What is the difference between Pygame and raylib? ›

Raylib is written in C, while Pygame uses Python, C, Cython, and Assembly. Raylib offers extensive 3D support, shaders, VR rendering, whereas Pygame primarily focuses on 2D. While both are cross-platform, Raylib supports more platforms including Raspberry Pi and HTML5 which Pygame lacks.

Can raylib do 3d? ›

raylib features

Full 3d support for 3d Shapes, Models, Billboards, Heightmaps and more!

Is raylib hard? ›

raylib is a simple and easy-to-use library to enjoy videogames programming.

What is raylib written in? ›

raylib is a cross-platform easy-to-use graphics library, built around OpenGL 1.1, 2.1, 3.3 and OpenGL ES 2.0. Even though it is written in C it has bindings to over 50 different languages. This tutorial will use C, more specifically C99.

Is raylib safe? ›

raylib is a safe Rust binding to Raylib, a C library for enjoying games programming.

Which game engine is built in Python? ›

Top 5 Python Game Engines Compared
EngineLanguagesPlatforms
Ren'Py 2004 // 2D //Pythonmobile, desktop, browser
Cocos2d 2010 // 2DC++, Python, Javascriptmobile, desktop, browser
Panda3D 2002 // 2D + 3DPython, C++desktop, browser, mobile
UPBGE 2D + 3D //Pythondesktop
1 more row

How old is raylib? ›

raylib development was started in August 2013 by Ramon Santamaria to support a game development course, focused on students with no previous coding experience and artistic profile.

Is raylib easy? ›

raylib is a simple and easy-to-use library to enjoy videogames programming.

Is raylib good for game dev? ›

Raylib is a lightweight C/C++ game development framework, ideal for prototyping. We see some resources for getting up-to-speed with Raylib, using C++ in this post.

Is raylib based on SDL? ›

SDL and raylib depend on several external libraries for some functionality (usually file-formats loading). SDL usually relies on the official file-format loading libraries while raylib mostly relies on self-contained single-file header-only libraries that are integrated with the raylib code-base.

Which is better OpenGL or SDL2? ›

While SDL can be used for simple 2D graphics, OpenGL is more suitable for complex 2D and 3D rendering. Supported Platforms: OpenGL is supported on multiple platforms, such as Windows, macOS, Linux, and various mobile platforms. It provides a cross-platform solution for graphics programming.

Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 6247

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.