1"""
2Move Sprite by Angle
3
4Simple program to show basic sprite usage.
5
6Artwork from http://kenney.nl
7
8If Python and Arcade are installed, this example can be run from the command line with:
9python -m arcade.examples.sprite_move_angle
10"""
11import arcade
12import os
13import math
14
15SPRITE_SCALING = 0.5
16
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19SCREEN_TITLE = "Move Sprite by Angle Example"
20
21MOVEMENT_SPEED = 5
22ANGLE_SPEED = 5
23
24
25class Player(arcade.Sprite):
26 """ Player class """
27
28 def __init__(self, image, scale):
29 """ Set up the player """
30
31 # Call the parent init
32 super().__init__(image, scale)
33
34 # Create a variable to hold our speed. 'angle' is created by the parent
35 self.speed = 0
36
37 def update(self):
38 # Convert angle in degrees to radians.
39 angle_rad = math.radians(self.angle)
40
41 # Rotate the ship
42 self.angle += self.change_angle
43
44 # Use math to find our change based on our speed and angle
45 self.center_x += -self.speed * math.sin(angle_rad)
46 self.center_y += self.speed * math.cos(angle_rad)
47
48
49class MyGame(arcade.Window):
50 """
51 Main application class.
52 """
53
54 def __init__(self, width, height, title):
55 """
56 Initializer
57 """
58
59 # Call the parent class initializer
60 super().__init__(width, height, title)
61
62 # Set the working directory (where we expect to find files) to the same
63 # directory this .py file is in. You can leave this out of your own
64 # code, but it is needed to easily run the examples using "python -m"
65 # as mentioned at the top of this program.
66 file_path = os.path.dirname(os.path.abspath(__file__))
67 os.chdir(file_path)
68
69 # Variables that will hold sprite lists
70 self.player_list = None
71
72 # Set up the player info
73 self.player_sprite = None
74
75 # Set the background color
76 arcade.set_background_color(arcade.color.BLACK)
77
78 def setup(self):
79 """ Set up the game and initialize the variables. """
80
81 # Sprite lists
82 self.player_list = arcade.SpriteList()
83
84 # Set up the player
85 self.player_sprite = Player(":resources:images/space_shooter/playerShip1_orange.png", SPRITE_SCALING)
86 self.player_sprite.center_x = SCREEN_WIDTH / 2
87 self.player_sprite.center_y = SCREEN_HEIGHT / 2
88 self.player_list.append(self.player_sprite)
89
90 def on_draw(self):
91 """
92 Render the screen.
93 """
94
95 # This command has to happen before we start drawing
96 arcade.start_render()
97
98 # Draw all the sprites.
99 self.player_list.draw()
100
101 def on_update(self, delta_time):
102 """ Movement and game logic """
103
104 # Call update on all sprites (The sprites don't do much in this
105 # example though.)
106 self.player_list.update()
107
108 def on_key_press(self, key, modifiers):
109 """Called whenever a key is pressed. """
110
111 # Forward/back
112 if key == arcade.key.UP:
113 self.player_sprite.speed = MOVEMENT_SPEED
114 elif key == arcade.key.DOWN:
115 self.player_sprite.speed = -MOVEMENT_SPEED
116
117 # Rotate left/right
118 elif key == arcade.key.LEFT:
119 self.player_sprite.change_angle = ANGLE_SPEED
120 elif key == arcade.key.RIGHT:
121 self.player_sprite.change_angle = -ANGLE_SPEED
122
123 def on_key_release(self, key, modifiers):
124 """Called when the user releases a key. """
125
126 if key == arcade.key.UP or key == arcade.key.DOWN:
127 self.player_sprite.speed = 0
128 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
129 self.player_sprite.change_angle = 0
130
131
132def main():
133 """ Main method """
134 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
135 window.setup()
136 arcade.run()
137
138
139if __name__ == "__main__":
140 main()