本文主要是介绍Arcade官方教程解析 2 Scene Object,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
介绍
接下来我们将向游戏中添加一个场景。场景是一种管理多个不同的精灵列表的工具,通过为每个精灵列表分配一个名称,以及维护绘制顺序。
"""
Platformer Game
"""
import arcade# Constants
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_TITLE = "Platformer"# Constants used to scale our sprites from their original size
CHARACTER_SCALING = 1
TILE_SCALING = 0.5class MyGame(arcade.Window):"""Main application class."""def __init__(self):# Call the parent class and set up the windowsuper().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)# Our Scene Objectself.scene = None# Separate variable that holds the player spriteself.player_sprite = Nonearcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)def setup(self):"""Set up the game here. Call this function to restart the game."""# 初始化一个场景self.scene = arcade.Scene()# 向场景中添加一个精灵列表self.scene.add_sprite_list("Player")self.scene.add_sprite_list("Walls", use_spatial_hash=True)# 实例化一个精灵对象,并添加到场景中image_source = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)self.player_sprite.center_x = 64self.player_sprite.center_y = 128self.scene.add_sprite("Player", self.player_sprite)# Create the ground# 实例化一个精灵对象,并添加到场景中for x in range(0, 1250, 64):wall = arcade.Sprite(":resources:images/tiles/grassMid.png", TILE_SCALING)wall.center_x = xwall.center_y = 32self.scene.add_sprite("Walls", wall)# Put some crates on the ground# 实例化一个精灵对象,并添加到场景中coordinate_list = [[512, 96], [256, 96], [768, 96]]for coordinate in coordinate_list:# Add a crate on the groundwall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", TILE_SCALING)wall.position = coordinateself.scene.add_sprite("Walls", wall)def on_draw(self):"""Render the screen."""# Clear the screen to the background colorself.clear()# Draw our Sceneself.scene.draw()def main():"""Main function"""window = MyGame()window.setup()arcade.run()if __name__ == "__main__":main()
解析
这个程序是一个平台游戏。它使用Arcade库来创建游戏窗口和精灵对象。窗口的宽度和高度分别为1000和650像素,窗口的标题为"Platformer"。
在MyGame类的构造函数中,我们调用父类的构造函数来设置窗口的宽度、高度和标题,并设置了背景颜色。
在setup()方法中,我们实例化了一个场景对象,并向场景中添加了一个玩家精灵列表和一个墙精灵列表。然后,我们实例化了一个玩家精灵对象,并将其添加到玩家精灵列表中。我们还创建了一些地面和箱子的精灵对象,并将它们添加到墙精灵列表中。
在on_draw()方法中,我们调用clear()方法来清除屏幕,并调用场景的draw()方法来绘制场景中的精灵。
最后,在main()函数中,我们创建了一个MyGame对象,并调用其setup()方法来设置游戏。然后,我们调用arcade.run()函数来运行游戏循环。
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_32759777/article/details/136563612
这篇关于Arcade官方教程解析 2 Scene Object的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!