HomeLanguagesAdd Background Image in Python Arcade

Add Background Image in Python Arcade

In this article, we will learn how to add background images to arcade games in Python.

Adding Background Image

We are going to use the below image as our background image.

So to add this image as our background image we are going to use load_texture() and draw_texture_rectangle() function.

load_texture( ): 

load_texture function is used to import texture from file in arcade.

Syntax: arcade.load_texture(name, x, y, width, height)

Parameters:

  • name: Name of the file to that holds the texture.
  • x: X position of the crop area of the texture
  • y: Y position of the crop area of the texture
  • width: width of the texture
  • height: height of the texture

draw_texture_rectangle( ):

draw_texture_rectangle function used to import texture with specific coordinated.

Syntax: arcade.draw_texture_rectangle(x, y, width, height, texture, angle, alpha)

Parameters:

  • x:  x coordinate of rectangle center.
  • y:  y coordinate of rectangle center.
  • width:  width of texture
  • height: height of the texture
  • texture: identifier of texture returned from load_texture() call
  • angle:  rotation of the rectangle
  • alpha: Transparency of image

Below is the implementation:

Python3




# Importing arcade module
import arcade
 
# Creating MainGame class
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600, title = "Background Image")
 
        # Loading the background image
        self.background = arcade.load_texture("BACKGROUND.png")
 
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
         
        # Drawing the background image
        arcade.draw_texture_rectangle(300, 300, 600,
                                      600, self.background)
 
# Calling MainGame class
MainGame()
arcade.run()


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32685 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7052 POSTS0 COMMENTS
Nicole Veronica
12191 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12291 POSTS0 COMMENTS
Shaida Kate Naidoo
7177 POSTS0 COMMENTS
Ted Musemwa
7444 POSTS0 COMMENTS
Thapelo Manthata
7156 POSTS0 COMMENTS
Umr Jansen
7145 POSTS0 COMMENTS