精灵弹跳币#

sprite_bouncing_coins.py#
1"""
2Sprite Simple Bouncing
3
4Simple program to show how to bounce items.
5This only works for straight vertical and horizontal angles.
6
7Artwork from https://kenney.nl
8
9If Python and Arcade are installed, this example can be run from the command line with:
10python -m arcade.examples.sprite_bouncing_coins
11"""
12
13import arcade
14import random
15
16SPRITE_SCALING = 0.5
17
18SCREEN_WIDTH = 832
19SCREEN_HEIGHT = 632
20SCREEN_TITLE = "Sprite Bouncing Coins"
21
22MOVEMENT_SPEED = 5
23
24
25class MyGame(arcade.Window):
26 """ Main application class. """
27
28 def __init__(self, width, height, title):
29 """
30 Initializer
31 """
32 super().__init__(width, height, title)
33
34 # Sprite lists
35 self.coin_list = None
36 self.wall_list = None
37
38 def setup(self):
39 """ Set up the game and initialize the variables. """
40
41 # Sprite lists
42 self.wall_list = arcade.SpriteList()
43 self.coin_list = arcade.SpriteList()
44
45 # -- Set up the walls
46
47 # Create horizontal rows of boxes
48 for x in range(32, SCREEN_WIDTH, 64):
49 # Bottom edge
50 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
51 wall.center_x = x
52 wall.center_y = 32
53 self.wall_list.append(wall)
54
55 # Top edge
56 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
57 wall.center_x = x
58 wall.center_y = SCREEN_HEIGHT - 32
59 self.wall_list.append(wall)
60
61 # Create vertical columns of boxes
62 for y in range(96, SCREEN_HEIGHT, 64):
63 # Left
64 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
65 wall.center_x = 32
66 wall.center_y = y
67 self.wall_list.append(wall)
68
69 # Right
70 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
71 wall.center_x = SCREEN_WIDTH - 32
72 wall.center_y = y
73 self.wall_list.append(wall)
74
75 # Create boxes in the middle
76 for x in range(128, SCREEN_WIDTH, 196):
77 for y in range(128, SCREEN_HEIGHT, 196):
78 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
79 wall.center_x = x
80 wall.center_y = y
81 # wall.angle = 45
82 self.wall_list.append(wall)
83
84 # Create coins
85 for i in range(10):
86 coin = arcade.Sprite(":resources:images/items/coinGold.png", scale=0.25)
87 coin.center_x = random.randrange(100, 700)
88 coin.center_y = random.randrange(100, 500)
89 while coin.change_x == 0 and coin.change_y == 0:
90 coin.change_x = random.randrange(-4, 5)
91 coin.change_y = random.randrange(-4, 5)
92
93 self.coin_list.append(coin)
94
95 # Set the background color
96 self.background_color = arcade.color.AMAZON
97
98 def on_draw(self):
99 """
100 Render the screen.
101 """
102
103 # This command has to happen before we start drawing
104 self.clear()
105
106 # Draw all the sprites.
107 self.wall_list.draw()
108 self.coin_list.draw()
109
110 def on_update(self, delta_time):
111 """ Movement and game logic """
112
113 for coin in self.coin_list:
114
115 coin.center_x += coin.change_x
116 walls_hit = arcade.check_for_collision_with_list(coin, self.wall_list)
117 for wall in walls_hit:
118 if coin.change_x > 0:
119 coin.right = wall.left
120 elif coin.change_x < 0:
121 coin.left = wall.right
122 if len(walls_hit) > 0:
123 coin.change_x *= -1
124
125 coin.center_y += coin.change_y
126 walls_hit = arcade.check_for_collision_with_list(coin, self.wall_list)
127 for wall in walls_hit:
128 if coin.change_y > 0:
129 coin.top = wall.bottom
130 elif coin.change_y < 0:
131 coin.bottom = wall.top
132 if len(walls_hit) > 0:
133 coin.change_y *= -1
134
135
136def main():
137 """ Main function """
138 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
139 window.setup()
140 arcade.run()
141
142
143if __name__ == "__main__":
144 main()