步骤6-重置#
您可能已经注意到,在本教程中,有一个 setup
函数在我们的窗口类中。到目前为止,我们根本没有使用这个函数,那么它是做什么的呢?
让我们设想一下,我们想要一种将游戏“重置”到初始状态的方法。这可能是因为玩家输了,我们想重新开始比赛,或者我们只是想给玩家重新开始的选项。
使用我们目前的架构,在我们的 __init__
函数,我们将不得不在另一个函数中复制所有逻辑以实现这一点,或者完全重新创建我们的窗口,这对玩家来说将是一种不愉快的体验。
在本章中,我们将对现有代码进行少量重新组织,以便以一种允许简单地调用 setup
功能,只要我们想让我们的游戏恢复到它原来的状态。
首先,我们将改变我们的 __init__
函数如下所示。我们正在将价值设置为类似于 None
、0或类似。此步骤的目的是确保在类上创建属性。在Python中,我们不能将新属性添加到 __init__
功能。
def __init__(self):
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
self.player_texture = None
self.player_sprite = None
self.player_list = None
self.wall_list = None
接下来,我们将把这些对象的实际创建移到我们的设置函数中。这个看起来和我们原来的几乎一模一样 __init__
功能。尝试自己移动这些代码段,如果卡住了,可以看到 setup
函数,请参见下面的完整源代码清单。
我们需要做的最后一件事是创造一种重置游戏的方法。现在,我们将添加一个简单的按键来完成此操作。将以下内容添加到您的 on_key_press
当按下Esc键时重置游戏的功能。
if key == arcade.key.ESCAPE:
self.setup()
源代码#
正在重置#
1"""
2Platformer Game
3
4python -m arcade.examples.platform_tutorial.06_reset
5"""
6import arcade
7
8# Constants
9SCREEN_WIDTH = 800
10SCREEN_HEIGHT = 600
11SCREEN_TITLE = "Platformer"
12
13# Constants used to scale our sprites from their original size
14TILE_SCALING = 0.5
15
16# Movement speed of player, in pixels per frame
17PLAYER_MOVEMENT_SPEED = 5
18GRAVITY = 1
19PLAYER_JUMP_SPEED = 20
20
21
22class MyGame(arcade.Window):
23 """
24 Main application class.
25 """
26
27 def __init__(self):
28
29 # Call the parent class and set up the window
30 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
31
32 # Variable to hold our texture for our player
33 self.player_texture = None
34
35 # Separate variable that holds the player sprite
36 self.player_sprite = None
37
38 # SpriteList for our player
39 self.player_list = None
40
41 # SpriteList for our boxes and ground
42 # Putting our ground and box Sprites in the same SpriteList
43 # will make it easier to perform collision detection against
44 # them later on. Setting the spatial hash to True will make
45 # collision detection much faster if the objects in this
46 # SpriteList do not move.
47 self.wall_list = None
48
49 def setup(self):
50 """Set up the game here. Call this function to restart the game."""
51 self.player_texture = arcade.load_texture(":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png")
52
53 self.player_sprite = arcade.Sprite(self.player_texture)
54 self.player_sprite.center_x = 64
55 self.player_sprite.center_y = 128
56
57 self.player_list = arcade.SpriteList()
58 self.player_list.append(self.player_sprite)
59
60 self.wall_list = arcade.SpriteList(use_spatial_hash=True)
61
62 # Create the ground
63 # This shows using a loop to place multiple sprites horizontally
64 for x in range(0, 1250, 64):
65 wall = arcade.Sprite(":resources:images/tiles/grassMid.png", scale=TILE_SCALING)
66 wall.center_x = x
67 wall.center_y = 32
68 self.wall_list.append(wall)
69
70 # Put some crates on the ground
71 # This shows using a coordinate list to place sprites
72 coordinate_list = [[512, 96], [256, 96], [768, 96]]
73
74 for coordinate in coordinate_list:
75 # Add a crate on the ground
76 wall = arcade.Sprite(
77 ":resources:images/tiles/boxCrate_double.png", scale=TILE_SCALING
78 )
79 wall.position = coordinate
80 self.wall_list.append(wall)
81
82 # Create a Platformer Physics Engine, this will handle moving our
83 # player as well as collisions between the player sprite and
84 # whatever SpriteList we specify for the walls.
85 # It is important to supply static to the walls parameter. There is a
86 # platforms parameter that is intended for moving platforms.
87 # If a platform is supposed to move, and is added to the walls list,
88 # it will not be moved.
89 self.physics_engine = arcade.PhysicsEnginePlatformer(
90 self.player_sprite, walls=self.wall_list, gravity_constant=GRAVITY
91 )
92
93 self.background_color = arcade.csscolor.CORNFLOWER_BLUE
94
95 def on_draw(self):
96 """Render the screen."""
97
98 # Clear the screen to the background color
99 self.clear()
100
101 # Draw our sprites
102 self.player_list.draw()
103 self.wall_list.draw()
104
105 def on_update(self, delta_time):
106 """Movement and Game Logic"""
107
108 # Move the player using our physics engine
109 self.physics_engine.update()
110
111 def on_key_press(self, key, modifiers):
112 """Called whenever a key is pressed."""
113
114 if key == arcade.key.ESCAPE:
115 self.setup()
116
117 if key == arcade.key.UP or key == arcade.key.W:
118 if self.physics_engine.can_jump():
119 self.player_sprite.change_y = PLAYER_JUMP_SPEED
120
121 if key == arcade.key.LEFT or key == arcade.key.A:
122 self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
123 elif key == arcade.key.RIGHT or key == arcade.key.D:
124 self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
125
126 def on_key_release(self, key, modifiers):
127 """Called whenever a key is released."""
128
129 if key == arcade.key.LEFT or key == arcade.key.A:
130 self.player_sprite.change_x = 0
131 elif key == arcade.key.RIGHT or key == arcade.key.D:
132 self.player_sprite.change_x = 0
133
134
135def main():
136 """Main function"""
137 window = MyGame()
138 window.setup()
139 arcade.run()
140
141
142if __name__ == "__main__":
143 main()
运行本章#
python -m arcade.examples.platform_tutorial.06_reset