Move By Turning
Use the left/right keys to turn.
Up and down keys to move forward and backwards.

sprite_move_angle.py
1"""
2Move Sprite by Angle
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_move_angle
10"""
11import arcade
12import math
13
14SPRITE_SCALING = 0.5
15
16WINDOW_WIDTH = 1280
17WINDOW_HEIGHT = 720
18WINDOW_TITLE = "Move Sprite by Angle Example"
19
20MOVEMENT_SPEED = 5
21ANGLE_SPEED = 5
22
23
24class Player(arcade.Sprite):
25 """ Player class """
26
27 def __init__(self, image, scale):
28 """ Set up the player """
29
30 # Call the parent init
31 super().__init__(image, scale=scale)
32
33 # Create a variable to hold our speed. 'angle' is created by the parent
34 self.speed = 0
35
36 def update(self, delta_time: float = 1/60):
37 # Rotate the ship
38 self.angle += self.change_angle
39
40 # Convert angle in degrees to radians.
41 angle_rad = math.radians(self.angle)
42
43 # Use math to find our change based on our speed and angle
44 self.center_x += self.speed * math.sin(angle_rad)
45 self.center_y += self.speed * math.cos(angle_rad)
46
47
48class GameView(arcade.View):
49 """
50 Main application class.
51 """
52
53 def __init__(self):
54 """
55 Initializer
56 """
57
58 # Call the parent class initializer
59 super().__init__()
60
61 # Variables that will hold sprite lists
62 self.player_list = None
63
64 # Set up the player info
65 self.player_sprite = None
66
67 # Set the background color
68 self.background_color = arcade.color.BLACK
69
70 def setup(self):
71 """ Set up the game and initialize the variables. """
72
73 # Sprite lists
74 self.player_list = arcade.SpriteList()
75
76 # Set up the player
77 self.player_sprite = Player(":resources:images/space_shooter/playerShip1_orange.png",
78 SPRITE_SCALING)
79 self.player_sprite.center_x = WINDOW_WIDTH / 2
80 self.player_sprite.center_y = WINDOW_HEIGHT / 2
81 self.player_list.append(self.player_sprite)
82
83 def on_draw(self):
84 """
85 Render the screen.
86 """
87
88 # This command has to happen before we start drawing
89 self.clear()
90
91 # Draw all the sprites.
92 self.player_list.draw()
93
94 def on_update(self, delta_time):
95 """ Movement and game logic """
96
97 # Call update on all sprites (The sprites don't do much in this
98 # example though.)
99 self.player_list.update(delta_time)
100
101 def on_key_press(self, key, modifiers):
102 """Called whenever a key is pressed. """
103
104 # Forward/back
105 if key == arcade.key.UP:
106 self.player_sprite.speed = MOVEMENT_SPEED
107 elif key == arcade.key.DOWN:
108 self.player_sprite.speed = -MOVEMENT_SPEED
109
110 # Rotate left/right
111 elif key == arcade.key.LEFT:
112 self.player_sprite.change_angle = -ANGLE_SPEED
113 elif key == arcade.key.RIGHT:
114 self.player_sprite.change_angle = ANGLE_SPEED
115
116 def on_key_release(self, key, modifiers):
117 """Called when the user releases a key. """
118
119 if key == arcade.key.UP or key == arcade.key.DOWN:
120 self.player_sprite.speed = 0
121 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
122 self.player_sprite.change_angle = 0
123
124
125def main():
126 """ Main function """
127 # Create a window class. This is what actually shows up on screen
128 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
129
130 # Create and setup the GameView
131 game = GameView()
132 game.setup()
133
134 # Show GameView on screen
135 window.show_view(game)
136
137 # Start the arcade game loop
138 arcade.run()
139
140
141if __name__ == "__main__":
142 main()