Using a Background Image

sprite_collect_coins_background.py
1"""
2Sprite Collect Coins with Background
3
4Simple program to show basic sprite usage.
5
6Artwork from https://kenney.nl
7
8If Python and Arcade are installed, this example can be run from the command line with:
9python -m arcade.examples.sprite_collect_coins_background
10"""
11import random
12import arcade
13
14PLAYER_SCALING = 0.75
15COIN_SCALING = 0.4
16
17WINDOW_WIDTH = 1280
18WINDOW_HEIGHT = 720
19WINDOW_TITLE = "Sprite Collect Coins with Background Example"
20
21
22class GameView(arcade.View):
23 """
24 Main application class.
25 """
26
27 def __init__(self):
28 """ Initializer """
29
30 # Call the parent class initializer
31 super().__init__()
32
33 # Background image will be stored in this variable
34 self.background = arcade.load_texture(":resources:images/backgrounds/abstract_1.jpg")
35
36 # Variables that will hold sprite lists
37 self.player_sprite = arcade.Sprite(
38 ":resources:images/animated_characters/female_person/femalePerson_idle.png",
39 scale=PLAYER_SCALING,
40 )
41 self.player_list = arcade.SpriteList()
42 self.player_list.append(self.player_sprite)
43 self.coin_list = arcade.SpriteList()
44
45 # Set up the player info
46 self.score = 0
47 self.score_text = arcade.Text("Score: 0", 10, 20, arcade.color.WHITE, 14)
48
49 # Don't show the mouse cursor
50 self.window.set_mouse_visible(False)
51
52 # Set the background color
53 self.background_color = arcade.color.AMAZON
54
55 def reset(self):
56 """Restart the game."""
57 # Sprite lists
58 self.coin_list.clear()
59
60 # Set up the player
61 self.score = 0
62 self.player_sprite.center_x = 50
63 self.player_sprite.center_y = 50
64
65 for i in range(50):
66 # Create the coin instance
67 coin = arcade.Sprite(":resources:images/items/coinGold.png", scale=COIN_SCALING)
68
69 # Position the coin
70 coin.center_x = random.randrange(WINDOW_WIDTH)
71 coin.center_y = random.randrange(WINDOW_HEIGHT)
72
73 # Add the coin to the lists
74 self.coin_list.append(coin)
75
76 def on_draw(self):
77 """
78 Render the screen.
79 """
80
81 # This command has to happen before we start drawing
82 self.clear()
83
84 # Draw the background texture
85 arcade.draw_texture_rect(
86 self.background,
87 arcade.LBWH(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT),
88 )
89
90 # Draw all the sprites.
91 self.coin_list.draw()
92 self.player_list.draw()
93
94 # Update the score text and draw it
95 self.score_text.text = f"Score: {self.score}"
96 self.score_text.draw()
97
98 def on_mouse_motion(self, x, y, dx, dy):
99 """
100 Called whenever the mouse moves.
101 """
102 self.player_sprite.center_x = x
103 self.player_sprite.center_y = y
104
105 def on_update(self, delta_time):
106 """ Movement and game logic """
107
108 # Call update on the coin sprites (The sprites don't do much in this
109 # example though.)
110 self.coin_list.update()
111
112 # Generate a list of all sprites that collided with the player.
113 hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
114
115 # Loop through each colliding sprite, remove it, and add to the score.
116 for coin in hit_list:
117 coin.remove_from_sprite_lists()
118 self.score += 1
119
120 def on_key_press(self, symbol: int, modifiers: int):
121 if symbol == arcade.key.R:
122 self.reset()
123 elif symbol == arcade.key.ESCAPE:
124 self.window.close()
125
126
127def main():
128 """ Main function """
129 # Create a window class. This is what actually shows up on screen
130 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
131
132 # Create and setup the GameView
133 game = GameView()
134 game.reset()
135
136 # Show GameView on screen
137 window.show_view(game)
138
139 # Start the arcade game loop
140 arcade.run()
141
142
143if __name__ == "__main__":
144 main()