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