diff --git a/arcade/examples/array_backed_grid_sprites.py b/arcade/examples/array_backed_grid_sprites_1.py similarity index 98% rename from arcade/examples/array_backed_grid_sprites.py rename to arcade/examples/array_backed_grid_sprites_1.py index deab610c2..6c6def775 100644 --- a/arcade/examples/array_backed_grid_sprites.py +++ b/arcade/examples/array_backed_grid_sprites_1.py @@ -7,7 +7,7 @@ This version syncs the grid to the sprite list in one go using resync_grid_with_sprites. If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.array_backed_grid_sprites +python -m arcade.examples.array_backed_grid_sprites_1 """ import arcade diff --git a/arcade/experimental/bloom_defender.py b/arcade/examples/bloom_defender.py similarity index 100% rename from arcade/experimental/bloom_defender.py rename to arcade/examples/bloom_defender.py diff --git a/arcade/examples/joystick.py b/arcade/examples/joystick.py deleted file mode 100644 index 5ea881aa5..000000000 --- a/arcade/examples/joystick.py +++ /dev/null @@ -1,151 +0,0 @@ -""" -This simple animation example shows how to move an item with the joystick. - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.joystick -""" - -import arcade - -# Set up the constants -SCREEN_WIDTH = 800 -SCREEN_HEIGHT = 600 -SCREEN_TITLE = "Joystick Control Example" - -RECT_WIDTH = 50 -RECT_HEIGHT = 50 - -MOVEMENT_MULTIPLIER = 5 -DEAD_ZONE = 0.05 - - -class MyGame(arcade.Window): - """ - Main application class. - """ - def __init__(self, width, height, title): - super().__init__(width, height, title) - self.player = None - self.left_down = False - - joysticks = arcade.get_joysticks() - if joysticks: - self.joystick = joysticks[0] - self.joystick.open() - self.joystick.on_joybutton_press = self.on_joybutton_press - self.joystick.on_joybutton_release = self.on_joybutton_release - self.joystick.on_joyhat_motion = self.on_joyhat_motion - else: - print("There are no Joysticks") - self.joystick = None - - # noinspection PyMethodMayBeStatic - def on_joybutton_press(self, _joystick, button): - print("Button {} down".format(button)) - - # noinspection PyMethodMayBeStatic - def on_joybutton_release(self, _joystick, button): - print("Button {} up".format(button)) - - # noinspection PyMethodMayBeStatic - def on_joyhat_motion(self, _joystick, hat_x, hat_y): - print("Hat ({}, {})".format(hat_x, hat_y)) - - def setup(self): - """ Set up the game and initialize the variables. """ - width = RECT_WIDTH - height = RECT_HEIGHT - x = SCREEN_WIDTH // 2 - y = SCREEN_HEIGHT // 2 - angle = 0 - color = arcade.color.WHITE - self.player = Rectangle(x, y, width, height, angle, color) - self.left_down = False - - def on_update(self, dt): - # Grab the position of the joystick - # This will be between -1.0 and +1.0 - - if self.joystick: - self.player.delta_x = self.joystick.x * MOVEMENT_MULTIPLIER - # Set a "dead zone" to prevent drive from a centered joystick - if abs(self.player.delta_x) < DEAD_ZONE: - self.player.delta_x = 0 - - self.player.delta_y = -self.joystick.y * MOVEMENT_MULTIPLIER - # Set a "dead zone" to prevent drive from a centered joystick - if abs(self.player.delta_y) < DEAD_ZONE: - self.player.delta_y = 0 - - """ Move everything """ - self.player.move() - - def on_draw(self): - """ - Render the screen. - """ - arcade.start_render() - - self.player.draw() - - -class Rectangle: - - """ Class to represent a rectangle on the screen """ - - def __init__(self, x, y, width, height, angle, color): - """ Initialize our rectangle variables """ - - # Position - self.x = x - self.y = y - - # Vector - self.delta_x = 0 - self.delta_y = 0 - - # Size and rotation - self.width = width - self.height = height - self.angle = angle - - # Color - self.color = color - - def draw(self): - """ Draw our rectangle """ - arcade.draw_rectangle_filled(self.x, self.y, self.width, self.height, - self.color, self.angle) - - def move(self): - - """ Move our rectangle """ - - # Move left/right - self.x += self.delta_x - - # See if we've gone beyond the border. If so, reset our position - # back to the border. - if self.x < RECT_WIDTH // 2: - self.x = RECT_WIDTH // 2 - if self.x > SCREEN_WIDTH - (RECT_WIDTH // 2): - self.x = SCREEN_WIDTH - (RECT_WIDTH // 2) - - # Move up/down - self.y += self.delta_y - - # Check top and bottom boundaries - if self.y < RECT_HEIGHT // 2: - self.y = RECT_HEIGHT // 2 - if self.y > SCREEN_HEIGHT - (RECT_HEIGHT // 2): - self.y = SCREEN_HEIGHT - (RECT_HEIGHT // 2) - - -def main(): - window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - window.setup() - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/experimental/mini_map_defender.py b/arcade/examples/mini_map_defender.py similarity index 100% rename from arcade/experimental/mini_map_defender.py rename to arcade/examples/mini_map_defender.py diff --git a/arcade/examples/move_joystick.py b/arcade/examples/move_joystick.py deleted file mode 100644 index 13a4cb333..000000000 --- a/arcade/examples/move_joystick.py +++ /dev/null @@ -1,112 +0,0 @@ -""" -This simple animation example shows how to move an item with the joystick -and game-pad. - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.move_joystick -""" - -import arcade - -SCREEN_WIDTH = 640 -SCREEN_HEIGHT = 480 -SCREEN_TITLE = "Move Joystick Example" -MOVEMENT_SPEED = 5 -DEAD_ZONE = 0.02 - - -class Ball: - def __init__(self, position_x, position_y, change_x, change_y, radius, color): - - # Take the parameters of the init function above, and create instance variables out of them. - self.position_x = position_x - self.position_y = position_y - self.change_x = change_x - self.change_y = change_y - self.radius = radius - self.color = color - - def draw(self): - """ Draw the balls with the instance variables we have. """ - arcade.draw_circle_filled(self.position_x, self.position_y, self.radius, self.color) - - def update(self): - # Move the ball - self.position_y += self.change_y - self.position_x += self.change_x - - # See if the ball hit the edge of the screen. If so, change direction - if self.position_x < self.radius: - self.position_x = self.radius - - if self.position_x > SCREEN_WIDTH - self.radius: - self.position_x = SCREEN_WIDTH - self.radius - - if self.position_y < self.radius: - self.position_y = self.radius - - if self.position_y > SCREEN_HEIGHT - self.radius: - self.position_y = SCREEN_HEIGHT - self.radius - - -class MyGame(arcade.Window): - - def __init__(self, width, height, title): - - # Call the parent class's init function - super().__init__(width, height, title) - - # Make the mouse disappear when it is over the window. - # So we just see our object, not the pointer. - self.set_mouse_visible(False) - - arcade.set_background_color(arcade.color.ASH_GREY) - - # Create our ball - self.ball = Ball(50, 50, 0, 0, 15, arcade.color.AUBURN) - - # Get a list of all the game controllers that are plugged in - joysticks = arcade.get_joysticks() - - # If we have a game controller plugged in, grab it and - # make an instance variable out of it. - if joysticks: - self.joystick = joysticks[0] - self.joystick.open() - else: - print("There are no joysticks.") - self.joystick = None - - def on_draw(self): - - """ Called whenever we need to draw the window. """ - arcade.start_render() - self.ball.draw() - - def on_update(self, delta_time): - - # Update the position according to the game controller - if self.joystick: - - # Set a "dead zone" to prevent drive from a centered joystick - if abs(self.joystick.x) < DEAD_ZONE: - self.ball.change_x = 0 - else: - self.ball.change_x = self.joystick.x * MOVEMENT_SPEED - - # Set a "dead zone" to prevent drive from a centered joystick - if abs(self.joystick.y) < DEAD_ZONE: - self.ball.change_y = 0 - else: - self.ball.change_y = -self.joystick.y * MOVEMENT_SPEED - - self.ball.update() - - -def main(): - MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/move_keyboard.py b/arcade/examples/move_keyboard.py deleted file mode 100644 index d49217daa..000000000 --- a/arcade/examples/move_keyboard.py +++ /dev/null @@ -1,99 +0,0 @@ -""" -This simple animation example shows how to move an item with the keyboard. - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.move_keyboard -""" - -import arcade - -SCREEN_WIDTH = 640 -SCREEN_HEIGHT = 480 -SCREEN_TITLE = "Move Keyboard Example" -MOVEMENT_SPEED = 3 - - -class Ball: - def __init__(self, position_x, position_y, change_x, change_y, radius, color): - - # Take the parameters of the init function above, and create instance variables out of them. - self.position_x = position_x - self.position_y = position_y - self.change_x = change_x - self.change_y = change_y - self.radius = radius - self.color = color - - def draw(self): - """ Draw the balls with the instance variables we have. """ - arcade.draw_circle_filled(self.position_x, self.position_y, self.radius, self.color) - - def update(self): - # Move the ball - self.position_y += self.change_y - self.position_x += self.change_x - - # See if the ball hit the edge of the screen. If so, change direction - if self.position_x < self.radius: - self.position_x = self.radius - - if self.position_x > SCREEN_WIDTH - self.radius: - self.position_x = SCREEN_WIDTH - self.radius - - if self.position_y < self.radius: - self.position_y = self.radius - - if self.position_y > SCREEN_HEIGHT - self.radius: - self.position_y = SCREEN_HEIGHT - self.radius - - -class MyGame(arcade.Window): - - def __init__(self, width, height, title): - - # Call the parent class's init function - super().__init__(width, height, title) - - # Make the mouse disappear when it is over the window. - # So we just see our object, not the pointer. - self.set_mouse_visible(False) - - arcade.set_background_color(arcade.color.ASH_GREY) - - # Create our ball - self.ball = Ball(50, 50, 0, 0, 15, arcade.color.AUBURN) - - def on_draw(self): - """ Called whenever we need to draw the window. """ - arcade.start_render() - self.ball.draw() - - def on_update(self, delta_time): - self.ball.update() - - def on_key_press(self, key, modifiers): - """ Called whenever the user presses a key. """ - if key == arcade.key.LEFT: - self.ball.change_x = -MOVEMENT_SPEED - elif key == arcade.key.RIGHT: - self.ball.change_x = MOVEMENT_SPEED - elif key == arcade.key.UP: - self.ball.change_y = MOVEMENT_SPEED - elif key == arcade.key.DOWN: - self.ball.change_y = -MOVEMENT_SPEED - - def on_key_release(self, key, modifiers): - """ Called whenever a user releases a key. """ - if key == arcade.key.LEFT or key == arcade.key.RIGHT: - self.ball.change_x = 0 - elif key == arcade.key.UP or key == arcade.key.DOWN: - self.ball.change_y = 0 - - -def main(): - MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/move_mouse.py b/arcade/examples/move_mouse.py deleted file mode 100644 index 032f3be49..000000000 --- a/arcade/examples/move_mouse.py +++ /dev/null @@ -1,78 +0,0 @@ -""" -This simple animation example shows how to move an item with the mouse, and -handle mouse clicks. - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.move_mouse -""" - -import arcade - -SCREEN_WIDTH = 640 -SCREEN_HEIGHT = 480 -SCREEN_TITLE = "Move Mouse Example" - - -class Ball: - def __init__(self, position_x, position_y, radius, color): - - # Take the parameters of the init function above, and create instance variables out of them. - self.position_x = position_x - self.position_y = position_y - self.radius = radius - self.color = color - - def draw(self): - """ Draw the balls with the instance variables we have. """ - arcade.draw_circle_filled(self.position_x, self.position_y, self.radius, self.color) - - -class MyGame(arcade.Window): - - def __init__(self, width, height, title): - - # Call the parent class's init function - super().__init__(width, height, title) - - # Make the mouse disappear when it is over the window. - # So we just see our object, not the pointer. - self.set_mouse_visible(False) - - arcade.set_background_color(arcade.color.ASH_GREY) - - # Create our ball - self.ball = Ball(50, 50, 15, arcade.color.AUBURN) - - def on_draw(self): - """ Called whenever we need to draw the window. """ - arcade.start_render() - self.ball.draw() - - def on_mouse_motion(self, x, y, dx, dy): - """ Called to update our objects. Happens approximately 60 times per second.""" - self.ball.position_x = x - self.ball.position_y = y - - def on_mouse_press(self, x, y, button, modifiers): - """ - Called when the user presses a mouse button. - """ - print(f"You clicked button number: {button}") - if button == arcade.MOUSE_BUTTON_LEFT: - self.ball.color = arcade.color.BLACK - - def on_mouse_release(self, x, y, button, modifiers): - """ - Called when a user releases a mouse button. - """ - if button == arcade.MOUSE_BUTTON_LEFT: - self.ball.color = arcade.color.AUBURN - - -def main(): - MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/particle_stress.py b/arcade/examples/particle_stress.py deleted file mode 100644 index 58c40f49e..000000000 --- a/arcade/examples/particle_stress.py +++ /dev/null @@ -1,62 +0,0 @@ -""" -Particle system stress test - -Run a particle system that spawns, updates, draws and reaps many particles every frame for performance testing. - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.particle_stress -""" -import os -import arcade -from arcade.examples.frametime_plotter import FrametimePlotter - -SCREEN_WIDTH = 800 -SCREEN_HEIGHT = 600 -SCREEN_TITLE = "Particle stress test" -TEXTURE = ":resources:images/pinball/pool_cue_ball.png" - - -def make_emitter(): - return arcade.Emitter( - center_xy=(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2), - emit_controller=arcade.EmitterIntervalWithTime(0.0004, 15.0), - particle_factory=lambda emitter: arcade.LifetimeParticle( - filename_or_texture=TEXTURE, - change_xy=arcade.rand_in_circle((0.0, 0.0), 5.0), - lifetime=1.0, - scale=0.5, - alpha=128 - ) - ) - - -class MyGame(arcade.Window): - def __init__(self): - super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - - # Set the working directory (where we expect to find files) to the same - # directory this .py file is in. You can leave this out of your own - # code, but it is needed to easily run the examples using "python -m" - # as mentioned at the top of this program. - file_path = os.path.dirname(os.path.abspath(__file__)) - os.chdir(file_path) - - self.emitter = make_emitter() - arcade.set_background_color(arcade.color.BLACK) - self.frametime_plotter = FrametimePlotter() - - def on_update(self, delta_time): - self.emitter.update() - if self.emitter.can_reap(): - arcade.close_window() - self.frametime_plotter.end_frame(delta_time) - - def on_draw(self): - arcade.start_render() - self.emitter.draw() - - -if __name__ == "__main__": - app = MyGame() - arcade.run() - app.frametime_plotter.show() diff --git a/arcade/examples/perlin_noise_1.py b/arcade/examples/perlin_noise_1.py deleted file mode 100644 index 57b0cbfb1..000000000 --- a/arcade/examples/perlin_noise_1.py +++ /dev/null @@ -1,162 +0,0 @@ -""" -Perlin Noise 1 - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.perlin_noise_1 - -TODO: This code doesn't work properly, and isn't currently listed in the examples. -""" -import arcade -import numpy as np -from PIL import Image - -# Set how many rows and columns we will have -ROW_COUNT = 30 -COLUMN_COUNT = 30 - -# This sets the WIDTH and HEIGHT of each grid location -WIDTH = 10 -HEIGHT = 10 - -# This sets the margin between each cell -# and on the edges of the screen. -MARGIN = 2 - -# Do the math to figure out our screen dimensions -SCREEN_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN -SCREEN_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN -SCREEN_TITLE = "Perlin Noise 1 Example" - - -# Perlin noise generator from: -# https://stackoverflow.com/questions/42147776/producing-2d-perlin-noise-with-numpy - -def perlin(x, y, seed=0): - # permutation table - np.random.seed(seed) - p = np.arange(256, dtype=int) - np.random.shuffle(p) - p = np.stack([p, p]).flatten() - # coordinates of the top-left - xi = x.astype(int) - yi = y.astype(int) - # internal coordinates - xf = x - xi - yf = y - yi - # fade factors - u = fade(xf) - v = fade(yf) - # noise components - n00 = gradient(p[p[xi] + yi], xf, yf) - n01 = gradient(p[p[xi] + yi + 1], xf, yf - 1) - n11 = gradient(p[p[xi + 1] + yi + 1], xf - 1, yf - 1) - n10 = gradient(p[p[xi + 1] + yi], xf - 1, yf) - # combine noises - x1 = lerp(n00, n10, u) - x2 = lerp(n01, n11, u) # FIX1: I was using n10 instead of n01 - return lerp(x1, x2, v) # FIX2: I also had to reverse x1 and x2 here - - -def lerp(a, b, x): - """linear interpolation""" - return a + x * (b - a) - - -def fade(t): - """6t^5 - 15t^4 + 10t^3""" - return 6 * t ** 5 - 15 * t ** 4 + 10 * t ** 3 - - -def gradient(h, x, y): - """grad converts h to the right gradient vector and return the dot product with (x,y)""" - vectors = np.array([[0, 1], [0, -1], [1, 0], [-1, 0]]) - g = vectors[h % 4] - return g[:, :, 0] * x + g[:, :, 1] * y - - -class MyGame(arcade.Window): - """ - Main application class. - """ - - def __init__(self, width, height, title): - """ - Set up the application. - """ - super().__init__(width, height, title) - - self.shape_list = None - - arcade.set_background_color(arcade.color.BLACK) - - self.grid = None - self.recreate_grid() - - def recreate_grid(self): - lin = np.linspace(0, 5, ROW_COUNT, endpoint=False) - y, x = np.meshgrid(lin, lin) - self.grid = (perlin(x, y, seed=0)) - - self.grid *= 255 - self.grid += 128 - - # for row in range(ROW_COUNT): - # for column in range(COLUMN_COUNT): - # print(f"{self.grid[row][column]:5.2f} ", end="") - # print() - - self.shape_list = arcade.ShapeElementList() - for row in range(ROW_COUNT): - for column in range(COLUMN_COUNT): - color = self.grid[row][column], 0, 0 - - x = (MARGIN + WIDTH) * column + MARGIN + WIDTH // 2 - y = (MARGIN + HEIGHT) * row + MARGIN + HEIGHT // 2 - - current_rect = arcade.create_rectangle_filled(x, y, WIDTH, HEIGHT, color) - self.shape_list.append(current_rect) - - im = Image.fromarray(np.uint8(self.grid), "L") - im.save("test.png") - - def on_draw(self): - """ - Render the screen. - """ - - # This command has to happen before we start drawing - arcade.start_render() - - self.shape_list.draw() - - def on_mouse_press(self, x, y, button, modifiers): - """ - Called when the user presses a mouse button. - """ - - # Change the x/y screen coordinates to grid coordinates - column = x // (WIDTH + MARGIN) - row = y // (HEIGHT + MARGIN) - - print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})") - - # Make sure we are on-grid. It is possible to click in the upper right - # corner in the margin and go to a grid location that doesn't exist - if row < ROW_COUNT and column < COLUMN_COUNT: - - # Flip the location between 1 and 0. - if self.grid[row][column] == 0: - self.grid[row][column] = 1 - else: - self.grid[row][column] = 0 - - self.recreate_grid() - - -def main(): - MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/perlin_noise_2.py b/arcade/examples/perlin_noise_2.py deleted file mode 100644 index 7f9d6aaa0..000000000 --- a/arcade/examples/perlin_noise_2.py +++ /dev/null @@ -1,154 +0,0 @@ -""" -Perlin Noise 2 - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.perlin_noise_2 - -TODO: This code doesn't work properly, and isn't currently listed in the examples. -""" -import arcade -import numpy as np -from PIL import Image - -# Set how many rows and columns we will have -ROW_COUNT = 30 -COLUMN_COUNT = 30 - -# This sets the WIDTH and HEIGHT of each grid location -WIDTH = 10 -HEIGHT = 10 - -# This sets the margin between each cell -# and on the edges of the screen. -MARGIN = 2 - -# Do the math to figure out our screen dimensions -SCREEN_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN -SCREEN_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN -SCREEN_TITLE = "Perlin Noise 2 Example" - - -# Perlin noise generator from: -# https://stackoverflow.com/questions/42147776/producing-2d-perlin-noise-with-numpy - -def perlin(x, y, seed=0): - # permutation table - np.random.seed(seed) - p = np.arange(256, dtype=int) - np.random.shuffle(p) - p = np.stack([p, p]).flatten() - # coordinates of the top-left - xi = x.astype(int) - yi = y.astype(int) - # internal coordinates - xf = x - xi - yf = y - yi - # fade factors - u = fade(xf) - v = fade(yf) - # noise components - n00 = gradient(p[p[xi] + yi], xf, yf) - n01 = gradient(p[p[xi] + yi + 1], xf, yf - 1) - n11 = gradient(p[p[xi + 1] + yi + 1], xf - 1, yf - 1) - n10 = gradient(p[p[xi + 1] + yi], xf - 1, yf) - # combine noises - x1 = lerp(n00, n10, u) - x2 = lerp(n01, n11, u) # FIX1: I was using n10 instead of n01 - return lerp(x1, x2, v) # FIX2: I also had to reverse x1 and x2 here - - -def lerp(a, b, x): - """linear interpolation""" - return a + x * (b - a) - - -def fade(t): - """6t^5 - 15t^4 + 10t^3""" - return 6 * t ** 5 - 15 * t ** 4 + 10 * t ** 3 - - -def gradient(h, x, y): - """grad converts h to the right gradient vector and return the dot product with (x,y)""" - vectors = np.array([[0, 1], [0, -1], [1, 0], [-1, 0]]) - g = vectors[h % 4] - return g[:, :, 0] * x + g[:, :, 1] * y - - -class MyGame(arcade.Window): - """ - Main application class. - """ - - def __init__(self, width, height, title): - """ - Set up the application. - """ - super().__init__(width, height, title) - self.background_list = None - arcade.set_background_color(arcade.color.BLACK) - - self.grid = None - self.recreate_grid() - - def recreate_grid(self): - lin = np.linspace(0, 5, ROW_COUNT, endpoint=False) - y, x = np.meshgrid(lin, lin) - self.grid = (perlin(x, y, seed=0)) - self.grid *= 255 - self.grid += 128 - - # for row in range(ROW_COUNT): - # for column in range(COLUMN_COUNT): - # print(f"{self.grid[row][column]:7.1f} ", end="") - # print() - - im = Image.fromarray(np.uint8(self.grid), "L") - background_sprite = arcade.Sprite() - background_sprite.center_x = SCREEN_WIDTH / 2 - background_sprite.center_y = SCREEN_HEIGHT / 2 - background_sprite.append_texture(arcade.Texture("dynamic noise image", im)) - background_sprite.set_texture(0) - - self.background_list = arcade.SpriteList() - self.background_list.append(background_sprite) - - def on_draw(self): - """ - Render the screen. - """ - - # This command has to happen before we start drawing - arcade.start_render() - self.background_list.draw() - - def on_mouse_press(self, x, y, button, modifiers): - """ - Called when the user presses a mouse button. - """ - - # Change the x/y screen coordinates to grid coordinates - column = x // (WIDTH + MARGIN) - row = y // (HEIGHT + MARGIN) - - print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})") - - # Make sure we are on-grid. It is possible to click in the upper right - # corner in the margin and go to a grid location that doesn't exist - if row < ROW_COUNT and column < COLUMN_COUNT: - - # Flip the location between 1 and 0. - if self.grid[row][column] == 0: - self.grid[row][column] = 1 - else: - self.grid[row][column] = 0 - - self.recreate_grid() - - -def main(): - MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/pymunk_apples.py b/arcade/examples/pymunk_apples.py deleted file mode 100644 index 1f229b043..000000000 --- a/arcade/examples/pymunk_apples.py +++ /dev/null @@ -1,196 +0,0 @@ -""" -Use Pymunk physics engine. - -For more info on Pymunk see: -http://www.pymunk.org/en/latest/ - -To install pymunk: -pip install pymunk - -Artwork from http://kenney.nl - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.pymunk_box_stacks - -Click and drag with the mouse to move the boxes. -""" - -import arcade -import pymunk -import timeit -import math -import os - -SCREEN_WIDTH = 1800 -SCREEN_HEIGHT = 800 -SCREEN_TITLE = "Pymunk test" - - -class PhysicsSprite(arcade.Sprite): - def __init__(self, pymunk_shape, filename): - super().__init__(filename, center_x=pymunk_shape.body.position.x, center_y=pymunk_shape.body.position.y) - self.pymunk_shape = pymunk_shape - - -class CircleSprite(PhysicsSprite): - def __init__(self, pymunk_shape, filename): - super().__init__(pymunk_shape, filename) - self.width = pymunk_shape.radius * 2 - self.height = pymunk_shape.radius * 2 - - -class BoxSprite(PhysicsSprite): - def __init__(self, pymunk_shape, filename, width, height): - super().__init__(pymunk_shape, filename) - self.width = width - self.height = height - - -class MyGame(arcade.Window): - """ Main application class. """ - - def __init__(self, width, height, title): - super().__init__(width, height, title) - - # Set the working directory (where we expect to find files) to the same - # directory this .py file is in. You can leave this out of your own - # code, but it is needed to easily run the examples using "python -m" - # as mentioned at the top of this program. - file_path = os.path.dirname(os.path.abspath(__file__)) - os.chdir(file_path) - - arcade.set_background_color(arcade.color.DARK_SLATE_GRAY) - - # -- Pymunk - self.space = pymunk.Space() - self.space.iterations = 35 - self.space.gravity = (0.0, -900.0) - - # Lists of sprites or lines - self.sprite_list = arcade.SpriteList() - self.static_lines = [] - - # Used for dragging shapes around with the mouse - self.shape_being_dragged = None - self.last_mouse_position = 0, 0 - - self.draw_time = 0 - self.processing_time = 0 - - # Create the floor - floor_height = 80 - body = pymunk.Body(body_type=pymunk.Body.STATIC) - shape = pymunk.Segment(body, [0, floor_height], [SCREEN_WIDTH, floor_height], 0.0) - shape.friction = 10 - self.space.add(shape) - self.static_lines.append(shape) - - def on_draw(self): - """ - Render the screen. - """ - - # This command has to happen before we start drawing - arcade.start_render() - - # Start timing how long this takes - draw_start_time = timeit.default_timer() - - # Draw all the sprites - self.sprite_list.draw() - - # Draw the lines that aren't sprites - for line in self.static_lines: - body = line.body - - pv1 = body.position + line.a.rotated(body.angle) - pv2 = body.position + line.b.rotated(body.angle) - arcade.draw_line(pv1.x, pv1.y, pv2.x, pv2.y, arcade.color.WHITE, 2) - - # Display timings - output = f"Processing time: {self.processing_time:.3f}" - arcade.draw_text(output, 20, SCREEN_HEIGHT - 20, arcade.color.WHITE, 12) - - output = f"Drawing time: {self.draw_time:.3f}" - arcade.draw_text(output, 20, SCREEN_HEIGHT - 40, arcade.color.WHITE, 12) - - self.draw_time = timeit.default_timer() - draw_start_time - - def on_mouse_press(self, x, y, button, modifiers): - if button == arcade.MOUSE_BUTTON_LEFT: - self.last_mouse_position = x, y - # See if we clicked on anything - shape_list = self.space.point_query((x, y), 1, pymunk.ShapeFilter()) - - # If we did, remember what we clicked on - if len(shape_list) > 0: - self.shape_being_dragged = shape_list[0] - - elif button == arcade.MOUSE_BUTTON_RIGHT: - # With right mouse button, shoot a heavy coin fast. - mass = 60 - radius = 10 - inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0)) - body = pymunk.Body(mass, inertia) - body.position = x, y - body.velocity = 0, 0 - shape = pymunk.Circle(body, radius, pymunk.Vec2d(0, 0)) - shape.friction = 0.3 - self.space.add(body, shape) - - sprite = CircleSprite(shape, "images/coin_01.png") - self.sprite_list.append(sprite) - - def on_mouse_release(self, x, y, button, modifiers): - if button == arcade.MOUSE_BUTTON_LEFT: - # Release the item we are holding (if any) - self.shape_being_dragged = None - - def on_mouse_motion(self, x, y, dx, dy): - if self.shape_being_dragged is not None: - # If we are holding an object, move it with the mouse - self.last_mouse_position = x, y - self.shape_being_dragged.shape.body.position = self.last_mouse_position - self.shape_being_dragged.shape.body.velocity = dx * 20, dy * 20 - - def on_update(self, delta_time): - start_time = timeit.default_timer() - - # Check for balls that fall off the screen - for sprite in self.sprite_list: - if sprite.pymunk_shape.body.position.y < 0: - # Remove balls from physics space - self.space.remove(sprite.pymunk_shape, sprite.pymunk_shape.body) - # Remove balls from physics list - sprite.kill() - - # Update physics - # Use a constant time step, don't use delta_time - # See "Game loop / moving time forward" - # http://www.pymunk.org/en/latest/overview.html#game-loop-moving-time-forward - self.space.step(1 / 60.0) - - # If we are dragging an object, make sure it stays with the mouse. Otherwise - # gravity will drag it down. - if self.shape_being_dragged is not None: - self.shape_being_dragged.shape.body.position = self.last_mouse_position - self.shape_being_dragged.shape.body.velocity = 0, 0 - - # Move sprites to where physics objects are - for sprite in self.sprite_list: - sprite.center_x = sprite.pymunk_shape.body.position.x - sprite.center_y = sprite.pymunk_shape.body.position.y - sprite.angle = math.degrees(sprite.pymunk_shape.body.angle) - - # Save the time it took to do this. - self.processing_time = timeit.default_timer() - start_time - - -def main(): - MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/resources.py b/arcade/examples/resources.py deleted file mode 100644 index 0d7cea31a..000000000 --- a/arcade/examples/resources.py +++ /dev/null @@ -1,31 +0,0 @@ -import arcade - -SCREEN_WIDTH = 800 -SCREEN_HEIGHT = 600 -SCREEN_TITLE = "Resources" - - -class MyGame(arcade.Window): - def __init__(self, width, height, title): - super().__init__(width, height, title) - arcade.set_background_color(arcade.color.AMAZON) - - self.sprite = arcade.Sprite(arcade.resources.image_male_adventurer_idle, center_x=50, center_y=50) - self.sprite.velocity = 1, 1 - - def on_draw(self): - arcade.start_render() - self.sprite.draw() - - def update(self, delta_time): - self.sprite.update() - - -def main(): - """ Main method """ - game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/shape_list_demo_1.py b/arcade/examples/shape_list_demo_1.py index 90dab6d0e..d812ad8aa 100644 --- a/arcade/examples/shape_list_demo_1.py +++ b/arcade/examples/shape_list_demo_1.py @@ -1,7 +1,7 @@ """ This demo shows the speed of drawing a full grid of squares using no buffering. -For me this takes about 0.850 seconds per frame. +For me this takes about 1.3 seconds per frame. It is slow because we load all the points and all the colors to the card every time. @@ -19,7 +19,7 @@ SQUARE_WIDTH = 5 SQUARE_HEIGHT = 5 -SQUARE_SPACING = 40 +SQUARE_SPACING = 10 class MyGame(arcade.Window): diff --git a/arcade/examples/shape_list_non_center_rotate.py b/arcade/examples/shape_list_non_center_rotate.py deleted file mode 100644 index 4653aec63..000000000 --- a/arcade/examples/shape_list_non_center_rotate.py +++ /dev/null @@ -1,67 +0,0 @@ -""" -Shape List Non-center Rotation Demo - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.shape_list_non_center_rotate -""" -import arcade - -SCREEN_WIDTH = 800 -SCREEN_HEIGHT = 600 -SCREEN_TITLE = "Shape List Non-center Rotation Demo" - - -def make_shape(): - - shape_list = arcade.ShapeElementList() - - # Shape center around which we will rotate - center_x = 20 - center_y = 30 - - width = 30 - height = 40 - - shape = arcade.create_ellipse_filled(center_x, center_y, width, height, arcade.color.WHITE) - shape_list.append(shape) - - return shape_list - - -class MyGame(arcade.Window): - """ Main application class. """ - - def __init__(self): - # Call the parent class initializer - super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - - self.shape_list = make_shape() - - # This specifies where on the screen the center of the shape will go - self.shape_list.center_x = SCREEN_WIDTH / 2 - self.shape_list.center_y = SCREEN_HEIGHT / 2 - - arcade.set_background_color(arcade.color.AMAZON) - - def on_draw(self): - """ - Render the screen. - """ - - # This command has to happen before we start drawing - arcade.start_render() - - self.shape_list.draw() - - def on_update(self, delta_time): - """ Movement and game logic """ - self.shape_list.angle += 1 - - -def main(): - MyGame() - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/sound_example.py b/arcade/examples/sound_example.py deleted file mode 100644 index db0b6f698..000000000 --- a/arcade/examples/sound_example.py +++ /dev/null @@ -1,20 +0,0 @@ -""" -Sound Demo - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.sound -""" -import arcade -import os - -# Set the working directory (where we expect to find files) to the same -# directory this .py file is in. You can leave this out of your own -# code, but it is needed to easily run the examples using "python -m" -# as mentioned at the top of this program. -file_path = os.path.dirname(os.path.abspath(__file__)) -os.chdir(file_path) - -arcade.open_window(300, 300, "Sound Demo") -laser_sound = arcade.load_sound(":resources:sounds/laser1.wav") -arcade.play_sound(laser_sound) -arcade.run() diff --git a/arcade/examples/sound_test.py b/arcade/examples/sound_test.py deleted file mode 100644 index d67ae838f..000000000 --- a/arcade/examples/sound_test.py +++ /dev/null @@ -1,67 +0,0 @@ -""" Test for sound in Arcade. -(May only work for windows at current time) - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.sound_test -""" - -import arcade -import os - - -SCREEN_WIDTH = 800 -SCREEN_HEIGHT = 600 -SCREEN_TITLE = "Sound Test Example" - -window = None - - -class MyGame(arcade.Window): - """ Main sound test class """ - - def __init__(self): - """ Initializer """ - # Call the parent class initializer - super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - - # Set the working directory (where we expect to find files) to the same - # directory this .py file is in. You can leave this out of your own - # code, but it is needed to easily run the examples using "python -m" - # as mentioned at the top of this program. - file_path = os.path.dirname(os.path.abspath(__file__)) - os.chdir(file_path) - - # Set background color to black - arcade.set_background_color(arcade.color.BLACK) - - def on_draw(self): - """Render the screen""" - - arcade.start_render() - - # Text on screen - text = "Press left mouse to make noise" - - # Render text - arcade.draw_text(text, 150, 300, arcade.color.WHITE, 30) - - def on_mouse_press(self, x, y, button, modifiers): - """Plays sound on key press""" - - # Load sound - loaded_sound = arcade.sound.load_sound(":resources:sounds/laser1.wav") - - # Play Sound - arcade.sound.play_sound(loaded_sound) - - def on_update(self, delta_time): - """animations""" - - -def main(): - MyGame() - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/sprite_collect_solid.py b/arcade/examples/sprite_collect_solid.py deleted file mode 100644 index 8df89e458..000000000 --- a/arcade/examples/sprite_collect_solid.py +++ /dev/null @@ -1,126 +0,0 @@ -""" -Sprite Collect Coins - -Simple program to show basic sprite usage. - -Artwork from http://kenney.nl - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.sprite_collect_coins -""" - -import random -import arcade -import os - -# --- Constants --- -SPRITE_SCALING_PLAYER = 1 -SPRITE_SCALING_COIN = 1 -COIN_COUNT = 50 - -SCREEN_WIDTH = 800 -SCREEN_HEIGHT = 600 -SCREEN_TITLE = "Sprite Collect Coins Example" - - -class MyGame(arcade.Window): - """ Our custom Window Class""" - - def __init__(self): - """ Initializer """ - # Call the parent class initializer - super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - - # Set the working directory (where we expect to find files) to the same - # directory this .py file is in. You can leave this out of your own - # code, but it is needed to easily run the examples using "python -m" - # as mentioned at the top of this program. - file_path = os.path.dirname(os.path.abspath(__file__)) - os.chdir(file_path) - - # Variables that will hold sprite lists - self.player_list = None - self.coin_list = None - - # Set up the player info - self.player_sprite = None - self.score = 0 - - # Don't show the mouse cursor - self.set_mouse_visible(False) - - arcade.set_background_color(arcade.color.AMAZON) - - def setup(self): - """ Set up the game and initialize the variables. """ - - # Sprite lists - self.player_list = arcade.SpriteList() - self.coin_list = arcade.SpriteList() - - # Score - self.score = 0 - - # Set up the player - # Character image from kenney.nl - self.player_sprite = arcade.SpriteSolidColor(64, 64, arcade.color.RED) - self.player_sprite.center_x = 50 - self.player_sprite.center_y = 50 - self.player_list.append(self.player_sprite) - - # Create the coins - for i in range(COIN_COUNT): - - # Create the coin instance - # Coin image from kenney.nl - coin = arcade.SpriteSolidColor(32, 32, arcade.color.BLUE) - - # Position the coin - coin.center_x = random.randrange(SCREEN_WIDTH) - coin.center_y = random.randrange(SCREEN_HEIGHT) - - # Add the coin to the lists - self.coin_list.append(coin) - - def on_draw(self): - """ Draw everything """ - arcade.start_render() - self.coin_list.draw() - self.player_list.draw() - - # Put the text on the screen. - output = f"Score: {self.score}" - arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14) - - def on_mouse_motion(self, x, y, dx, dy): - """ Handle Mouse Motion """ - - # Move the center of the player sprite to match the mouse x, y - self.player_sprite.center_x = x - self.player_sprite.center_y = y - - def on_update(self, delta_time): - """ Movement and game logic """ - - # Call update on all sprites (The sprites don't do much in this - # example though.) - self.coin_list.update() - - # Generate a list of all sprites that collided with the player. - coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list) - - # Loop through each colliding sprite, remove it, and add to the score. - for coin in coins_hit_list: - coin.remove_from_sprite_lists() - self.score += 1 - - -def main(): - """ Main method """ - window = MyGame() - window.setup() - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/sprite_ramps.py b/arcade/examples/sprite_ramps.py deleted file mode 100644 index 93bdced81..000000000 --- a/arcade/examples/sprite_ramps.py +++ /dev/null @@ -1,249 +0,0 @@ -""" -Load a map stored in csv format, as exported by the program 'Tiled.' - -Artwork from http://kenney.nl - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.sprite_ramps -""" -import arcade -import os - -from typing import List, Union - -SPRITE_SCALING = 0.5 - -SCREEN_WIDTH = 800 -SCREEN_HEIGHT = 600 -SCREEN_TITLE = "Sprite with Ramps Example" -SPRITE_PIXEL_SIZE = 128 -GRID_PIXEL_SIZE = (SPRITE_PIXEL_SIZE * SPRITE_SCALING) - -# How many pixels to keep as a minimum margin between the character -# and the edge of the screen. -VIEWPORT_MARGIN = 40 -RIGHT_MARGIN = 150 - -# Physics -MOVEMENT_SPEED = 5 -JUMP_SPEED = 14 -GRAVITY = 0.5 - - -def get_map(): - map_file = open("map_with_ramps_2.csv") - map_array = [] - for line in map_file: - line = line.strip() - map_row: List[Union[int, str]] = line.split(",") - for index, item in enumerate(map_row): - map_row[index] = int(item) - map_array.append(map_row) - return map_array - - -class MyGame(arcade.Window): - """ Main application class. """ - - def __init__(self, width, height, title): - """ - Initializer - """ - - super().__init__(width, height, title) - - # Set the working directory (where we expect to find files) to the same - # directory this .py file is in. You can leave this out of your own - # code, but it is needed to easily run the examples using "python -m" - # as mentioned at the top of this program. - file_path = os.path.dirname(os.path.abspath(__file__)) - os.chdir(file_path) - - # Sprite lists - self.all_sprites_list = None - self.coin_list = None - self.player_list = None - - # Set up the player - self.player_sprite = None - self.wall_list = None - self.physics_engine = None - self.view_left = 0 - self.view_bottom = 0 - self.end_of_map = 0 - self.game_over = False - - def start_new_game(self): - """ Set up the game and initialize the variables. """ - - # Sprite lists - self.all_sprites_list = arcade.SpriteList() - self.wall_list = arcade.SpriteList() - self.player_list = arcade.SpriteList() - - # Set up the player - self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", - SPRITE_SCALING) - self.player_sprite.center_x = 64 - self.player_sprite.center_y = 270 - self.player_list.append(self.player_sprite) - self.all_sprites_list.append(self.player_sprite) - - map_array = get_map() - - # Right edge of the map in pixels - self.end_of_map = len(map_array[0]) * GRID_PIXEL_SIZE - - map_items = [":resources:images/tiles/boxCrate_double.png", - ":resources:images/tiles/grassCenter.png", - ":resources:images/tiles/grassCorner_left.png", - ":resources:images/tiles/grassCorner_right.png", - ":resources:images/tiles/grassHill_left.png", - ":resources:images/tiles/grassHill_right.png", - ":resources:images/tiles/grassLeft.png", - ":resources:images/tiles/grassMid.png", - ":resources:images/tiles/grassRight.png", - ":resources:images/tiles/stoneHalf.png" - ] - for row_index, row in enumerate(map_array): - for column_index, item in enumerate(row): - - if item == -1: - continue - else: - wall = arcade.Sprite(map_items[item], - SPRITE_SCALING) - - # Change the collision polygon to be a ramp instead of - # a rectangle - if item == 4: - wall.points = ((-wall.width // 2, wall.height // 2), - (wall.width // 2, -wall.height // 2), - (-wall.width // 2, -wall.height // 2)) - elif item == 5: - wall.points = ((-wall.width // 2, -wall.height // 2), - (wall.width // 2, -wall.height // 2), - (wall.width // 2, wall.height // 2)) - - wall.right = column_index * 64 - wall.top = (7 - row_index) * 64 - self.all_sprites_list.append(wall) - self.wall_list.append(wall) - - self.physics_engine = \ - arcade.PhysicsEnginePlatformer(self.player_sprite, - self.wall_list, - gravity_constant=GRAVITY) - - # Set the background color - arcade.set_background_color(arcade.color.AMAZON) - - # Set the viewport boundaries - # These numbers set where we have 'scrolled' to. - self.view_left = 0 - self.view_bottom = 0 - - self.game_over = False - - def on_draw(self): - """ - Render the screen. - """ - - # This command has to happen before we start drawing - arcade.start_render() - - # Draw all the sprites. - self.wall_list.draw() - self.player_list.draw() - - # Put the text on the screen. - # Adjust the text position based on the viewport so that we don't - # scroll the text too. - distance = self.player_sprite.right - output = "Distance: {}".format(distance) - arcade.draw_text(output, self.view_left + 10, self.view_bottom + 20, - arcade.color.WHITE, 14) - - if self.game_over: - output = "Game Over" - arcade.draw_text(output, self.view_left + 200, - self.view_bottom + 200, - arcade.color.WHITE, 30) - - def on_key_press(self, key, modifiers): - """ - Called whenever a key is pressed down. - """ - if key == arcade.key.UP: - if self.physics_engine.can_jump(): - self.player_sprite.change_y = JUMP_SPEED - elif key == arcade.key.LEFT: - self.player_sprite.change_x = -MOVEMENT_SPEED - elif key == arcade.key.RIGHT: - self.player_sprite.change_x = MOVEMENT_SPEED - - def on_key_release(self, key, modifiers): - """ - Called when the user releases a key. - """ - if key == arcade.key.LEFT or key == arcade.key.RIGHT: - self.player_sprite.change_x = 0 - - def on_update(self, delta_time): - """ Movement and game logic """ - - if self.player_sprite.right >= self.end_of_map: - self.game_over = True - - # Call update on all sprites (The sprites don't do much in this - # example though.) - if not self.game_over: - self.physics_engine.update() - - # --- Manage Scrolling --- - - # Track if we need to change the viewport - - changed = False - - # Scroll left - left_bndry = self.view_left + VIEWPORT_MARGIN - if self.player_sprite.left < left_bndry: - self.view_left -= int(left_bndry - self.player_sprite.left) - changed = True - - # Scroll right - right_bndry = self.view_left + SCREEN_WIDTH - RIGHT_MARGIN - if self.player_sprite.right > right_bndry: - self.view_left += int(self.player_sprite.right - right_bndry) - changed = True - - # Scroll up - top_bndry = self.view_bottom + SCREEN_HEIGHT - VIEWPORT_MARGIN - if self.player_sprite.top > top_bndry: - self.view_bottom += int(self.player_sprite.top - top_bndry) - changed = True - - # Scroll down - bottom_bndry = self.view_bottom + VIEWPORT_MARGIN - if self.player_sprite.bottom < bottom_bndry: - self.view_bottom -= int(bottom_bndry - self.player_sprite.bottom) - changed = True - - # If we need to scroll, go ahead and do it. - if changed: - arcade.set_viewport(self.view_left, - SCREEN_WIDTH + self.view_left, - self.view_bottom, - SCREEN_HEIGHT + self.view_bottom) - - -def main(): - window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - window.start_new_game() - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/arcade/examples/starting_template.py b/arcade/examples/starting_template.py index 048d0f6d3..508e368f3 100644 --- a/arcade/examples/starting_template.py +++ b/arcade/examples/starting_template.py @@ -32,6 +32,7 @@ def __init__(self, width, height, title): # and set them to None def setup(self): + """ Set up the game variables. Call to re-start the game. """ # Create your sprites and sprite lists here pass diff --git a/arcade/examples/starting_template_simple.py b/arcade/examples/starting_template_simple.py index 716283d69..5888353b6 100644 --- a/arcade/examples/starting_template_simple.py +++ b/arcade/examples/starting_template_simple.py @@ -1,23 +1,18 @@ """ Starting Template Simple -Once you have learned how to use classes, you can begin your program with this -template. - If Python and Arcade are installed, this example can be run from the command line with: python -m arcade.examples.starting_template_simple """ import arcade -SCREEN_WIDTH = 500 +SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 SCREEN_TITLE = "Starting Template Simple" class MyGame(arcade.Window): - """ - Main application class. - """ + """ Main application class. """ def __init__(self, width, height, title): super().__init__(width, height, title) @@ -29,18 +24,9 @@ def setup(self): pass def on_draw(self): - """ - Render the screen. - """ - + """ Render the screen. """ arcade.start_render() - def on_mouse_press(self, x, y, button, key_modifiers): - """ - Called when the user presses a mouse button. - """ - pass - def main(): """ Main method """ diff --git a/arcade/examples/tmx_object_demo.py b/arcade/examples/tmx_object_demo.py deleted file mode 100644 index 80ea4278e..000000000 --- a/arcade/examples/tmx_object_demo.py +++ /dev/null @@ -1,112 +0,0 @@ -import arcade - -""" -Starting Template - -Once you have learned how to use classes, you can begin your program with this -template. - -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.starting_template -""" -import arcade - -SCREEN_WIDTH = 800 -SCREEN_HEIGHT = 600 -SCREEN_TITLE = "Starting Template" - -TILE_SCALING = .25 - -class MyGame(arcade.Window): - """ - Main application class. - - NOTE: Go ahead and delete the methods you don't need. - If you do need a method, delete the 'pass' and replace it - with your own code. Don't leave 'pass' in this program. - """ - - def __init__(self, width, height, title): - super().__init__(width, height, title) - - arcade.set_background_color(arcade.color.AMAZON) - - # If you have sprite lists, you should create them here, - # and set them to None - self.tile_list = None - self.tile_list_2 = None - - def setup(self): - # Create your sprites and sprite lists here - map_name = ":resources:/tmx_maps/test_objects.tmx" - - # Read in the tiled map - my_map = arcade.tilemap.read_tmx(map_name) - - self.tile_list = arcade.tilemap.process_layer(my_map, 'Tiles', TILE_SCALING) - self.tile_list_2 = arcade.tilemap.process_layer(my_map, 'Group/Tiles', TILE_SCALING) - - def on_draw(self): - """ - Render the screen. - """ - - # This command should happen before we start drawing. It will clear - # the screen to the background color, and erase what we drew last frame. - arcade.start_render() - - # Call draw() on all your sprite lists below - self.tile_list_2.draw() - self.tile_list.draw() - - def on_update(self, delta_time): - """ - All the logic to move, and the game logic goes here. - Normally, you'll call update() on the sprite lists that - need it. - """ - pass - - def on_key_press(self, key, key_modifiers): - """ - Called whenever a key on the keyboard is pressed. - - For a full list of keys, see: - http://arcade.academy/arcade.key.html - """ - pass - - def on_key_release(self, key, key_modifiers): - """ - Called whenever the user lets off a previously pressed key. - """ - pass - - def on_mouse_motion(self, x, y, delta_x, delta_y): - """ - Called whenever the mouse moves. - """ - pass - - def on_mouse_press(self, x, y, button, key_modifiers): - """ - Called when the user presses a mouse button. - """ - pass - - def on_mouse_release(self, x, y, button, key_modifiers): - """ - Called when a user releases a mouse button. - """ - pass - - -def main(): - """ Main method """ - game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) - game.setup() - arcade.run() - - -if __name__ == "__main__": - main() diff --git a/doc/examples/array_backed_grid_sprites_1.rst b/doc/examples/array_backed_grid_sprites_1.rst index 8f5436ec8..4a2a65cce 100644 --- a/doc/examples/array_backed_grid_sprites_1.rst +++ b/doc/examples/array_backed_grid_sprites_1.rst @@ -18,6 +18,6 @@ You may also want to look at: * :ref:`array_backed_grid_sprites_1` - (This program) super-fast and uses sprites. Resyncs to number grid in one function call * :ref:`array_backed_grid_sprites_2` - super-fast and uses sprites. Keeps a second 2D grid of sprites to match 2D grid of numbers -.. literalinclude:: ../../arcade/examples/array_backed_grid_sprites.py - :caption: array_backed_grid_sprites.py +.. literalinclude:: ../../arcade/examples/array_backed_grid_sprites_1.py + :caption: array_backed_grid_sprites_1.py :linenos: diff --git a/doc/examples/array_backed_grid_sprites_2.rst b/doc/examples/array_backed_grid_sprites_2.rst index f0991eafd..f525a747e 100644 --- a/doc/examples/array_backed_grid_sprites_2.rst +++ b/doc/examples/array_backed_grid_sprites_2.rst @@ -18,6 +18,6 @@ You may also want to look at: * :ref:`array_backed_grid_sprites_1` - super-fast and uses sprites. Resyncs to number grid in one function call * :ref:`array_backed_grid_sprites_2` - (This program) super-fast and uses sprites. Keeps a second 2D grid of sprites to match 2D grid of numbers -.. literalinclude:: ../../arcade/examples/array_backed_grid_sprites.py +.. literalinclude:: ../../arcade/examples/array_backed_grid_sprites_2.py :caption: array_backed_grid_sprites.py :linenos: diff --git a/doc/examples/bloom_defender.rst b/doc/examples/bloom_defender.rst index 7f805ad14..f8769eecf 100644 --- a/doc/examples/bloom_defender.rst +++ b/doc/examples/bloom_defender.rst @@ -48,7 +48,7 @@ Lines 264-265 Finally we render that buffer to the screen. -.. literalinclude:: ../../arcade/experimental/bloom_defender.py +.. literalinclude:: ../../arcade/examples/bloom_defender.py :caption: mini_map_defender.py :linenos: :emphasize-lines: 176-202, 239-252, 264-265 \ No newline at end of file diff --git a/doc/examples/decorator_drawing_example.png b/doc/examples/decorator_drawing_example.png deleted file mode 100644 index 374419d68..000000000 Binary files a/doc/examples/decorator_drawing_example.png and /dev/null differ diff --git a/doc/examples/decorator_drawing_example.rst b/doc/examples/decorator_drawing_example.rst deleted file mode 100644 index 7a8dc2dd0..000000000 --- a/doc/examples/decorator_drawing_example.rst +++ /dev/null @@ -1,15 +0,0 @@ -:orphan: - -.. _decorator_drawing_example: - -Drawing With Decoractors -======================== - -.. image:: decorator_drawing_example.png - :width: 600px - :align: center - :alt: Screenshot of a drawing example - -.. literalinclude:: ../../arcade/examples/decorator_drawing_example.py - :caption: decorator_drawing_example.py - :linenos: diff --git a/doc/examples/index.rst b/doc/examples/index.rst index 02ee908a0..bf46ef058 100644 --- a/doc/examples/index.rst +++ b/doc/examples/index.rst @@ -92,12 +92,12 @@ Drawing with Loops .. figure:: thumbs/nested_loops_top_left_triangle.png :figwidth: 170px - :ref:`nested_loops_bottom_left_triangle` + :ref:`nested_loops_top_left_triangle` -.. figure:: thumbs/nested_loops_top_right_triangle.png +.. figure:: thumbs/nested_loops_bottom_right_triangle.png :figwidth: 170px - :ref:`nested_loops_top_right_triangle` + :ref:`nested_loops_bottom_right_triangle` .. figure:: thumbs/snow.png :figwidth: 170px diff --git a/doc/examples/joystick.rst b/doc/examples/joystick.rst deleted file mode 100644 index bd0399413..000000000 --- a/doc/examples/joystick.rst +++ /dev/null @@ -1,11 +0,0 @@ -:orphan: - -.. _joystick: - -Game controller buttons -======================= - - -.. literalinclude:: ../../arcade/examples/joystick.py - :caption: joystick.py - :linenos: diff --git a/doc/examples/mini_map_defender.rst b/doc/examples/mini_map_defender.rst index f42390608..5bb47b3e7 100644 --- a/doc/examples/mini_map_defender.rst +++ b/doc/examples/mini_map_defender.rst @@ -78,7 +78,7 @@ In this case, we render it to the pre-defined location with: The rest of the code in this section calculates a rectangle on the mini-map that outlines what the user can see on the mini-map. -.. literalinclude:: ../../arcade/experimental/mini_map_defender.py +.. literalinclude:: ../../arcade/examples/mini_map_defender.py :caption: mini_map_defender.py :linenos: :emphasize-lines: 26-32, 172-189, 226-238, 263-280 \ No newline at end of file diff --git a/doc/examples/mountains_midpoint_displacement.png b/doc/examples/mountains_midpoint_displacement.png deleted file mode 100644 index af57cbd8e..000000000 Binary files a/doc/examples/mountains_midpoint_displacement.png and /dev/null differ diff --git a/doc/examples/mountains_midpoint_displacement.rst b/doc/examples/mountains_midpoint_displacement.rst deleted file mode 100644 index c8ab99427..000000000 --- a/doc/examples/mountains_midpoint_displacement.rst +++ /dev/null @@ -1,15 +0,0 @@ -:orphan: - -.. _mountains_midpoint_displacement: - -Mountains Midpoint Displacement -=============================== - -.. image:: mountains_midpoint_displacement.png - :width: 600px - :align: center - :alt: Screen shot of a mountains created by midpoint displacement - -.. literalinclude:: ../../arcade/examples/mountains_midpoint_displacement.py - :caption: mountains_midpoint_displacement.py - :linenos: diff --git a/doc/examples/mountains_random_walk.png b/doc/examples/mountains_random_walk.png deleted file mode 100644 index c302e9614..000000000 Binary files a/doc/examples/mountains_random_walk.png and /dev/null differ diff --git a/doc/examples/mountains_random_walk.rst b/doc/examples/mountains_random_walk.rst deleted file mode 100644 index f8010a555..000000000 --- a/doc/examples/mountains_random_walk.rst +++ /dev/null @@ -1,15 +0,0 @@ -:orphan: - -.. _mountains_random_walk: - -Mountains Random Walk -===================== - -.. image:: mountains_random_walk.png - :width: 600px - :align: center - :alt: Screen shot of a mountains created by a random walk - -.. literalinclude:: ../../arcade/examples/mountains_random_walk.py - :caption: mountains_random_walk.py - :linenos: diff --git a/doc/examples/platformer.rst b/doc/examples/platformer.rst deleted file mode 100644 index 2adafd710..000000000 --- a/doc/examples/platformer.rst +++ /dev/null @@ -1,14 +0,0 @@ -:orphan: - -.. _11_animate_character: - -Platformer with Character Animations -==================================== - -.. raw:: html - - - -.. literalinclude:: ../../arcade/examples/platform_tutorial/11_animate_character.py - :caption: sprite_tiled_map_with_levels.py - :linenos: diff --git a/doc/get_started.rst b/doc/get_started.rst index 9c14b006a..a402c41fa 100644 --- a/doc/get_started.rst +++ b/doc/get_started.rst @@ -58,7 +58,7 @@ Arcade Skill Tree * Keyboard, rotate and move forward/back like a space ship: :ref:`sprite_move_angle` * Game Controller - :ref:`sprite_move_joystick` - * Game controller buttons - :ref:`joystick` + * Game controller buttons - *Supported, but documentation needed.* * Sprite collision detection diff --git a/doc/tutorials/lights/index.rst b/doc/tutorials/lights/index.rst index ec8f017de..d9ce9dfee 100644 --- a/doc/tutorials/lights/index.rst +++ b/doc/tutorials/lights/index.rst @@ -1,5 +1,7 @@ -Lights -====== +Lights Tutorial +=============== + +.. image:: lights.png (To be done.) diff --git a/doc/tutorials/lights/light_demo.py b/doc/tutorials/lights/light_demo.py index 98a7ec6d8..ba24b2466 100644 --- a/doc/tutorials/lights/light_demo.py +++ b/doc/tutorials/lights/light_demo.py @@ -7,8 +7,6 @@ Artwork from http://kenney.nl -If Python and Arcade are installed, this example can be run from the command line with: -python -m arcade.examples.light_demo """ import arcade from arcade.experimental.lights import Light, LightLayer diff --git a/tests/test_examples/check_examples_2.py b/tests/test_examples/check_examples_2.py new file mode 100644 index 000000000..0d453313d --- /dev/null +++ b/tests/test_examples/check_examples_2.py @@ -0,0 +1,37 @@ +import re +import os +from pathlib import Path + + +def get_references_in_index(): + txt = Path('../../doc/examples/index.rst').read_text() + references_in_index = re.findall(":ref:`(.*)`", txt) + return references_in_index + +def get_references_in_rsts(): + mypath = Path("../../doc/examples/") + + # Get list of python files + python_example_filename_list = [] + references = [] + filenames = os.listdir(mypath) + for filename in filenames: + if filename.endswith(".rst") and filename != "index.rst": + python_example_filename_list.append(filename) + txt = Path(mypath / filename).read_text() + reference = re.findall("\.\. _(.*):", txt) + references.extend(reference) + + return references + +def main(): + references_in_index = get_references_in_index() + files_to_reference = get_references_in_rsts() + + for reference in files_to_reference: + if not reference in references_in_index: + print(f"index.rst is missing any mention of '{reference}'") + + print("Done with checking to make sure references in doc/examples/*.rst are in doc/examples/index.rst") + +main() \ No newline at end of file diff --git a/tests/test_examples/check_samples.py b/tests/test_examples/check_samples.py index 0fd555e3c..a0b23bd57 100644 --- a/tests/test_examples/check_samples.py +++ b/tests/test_examples/check_samples.py @@ -10,12 +10,14 @@ def main(): mypath = "../../arcade/examples" + # Get list of python files python_example_filename_list = [] filenames = os.listdir(mypath) for filename in filenames: if filename.endswith(".py"): python_example_filename_list.append(filename) + # Get list of rst files mypath = "../../doc/examples" python_rst_filename_list = [] filenames = os.listdir(mypath) @@ -23,19 +25,19 @@ def main(): if filename.endswith(".rst"): python_rst_filename_list.append(filename) + # See if there are rst files for all the py files for py_file in python_example_filename_list: base_name = py_file[:len(py_file) - 3] rst_name = base_name + ".rst" if rst_name not in python_rst_filename_list: print("Missing " + rst_name) + # See if there are py files for all the rst files print() - for rst_file in python_rst_filename_list: base_name = rst_file[:len(rst_file) - 4] py_name = base_name + ".py" if py_name not in python_example_filename_list: print("Missing " + py_name) - main()