在Python Arcade 中添加背景图像
在本文中,我们将学习如何在Python为街机游戏添加背景图像。
添加背景图像
我们将使用下面的图片作为我们的背景图片。
因此,要将此图像添加为我们的背景图像,我们将使用 load_texture() 和 draw_texture_rectangle()函数。
加载纹理():
load_texture函数用于从 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函数用于导入具有特定坐标的纹理。
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
下面是实现:
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()
输出: