Skip to content

Commit

Permalink
Bunch of work improving the examples section.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvcraven committed Mar 22, 2016
1 parent 1998d12 commit d38d04e
Show file tree
Hide file tree
Showing 28 changed files with 772 additions and 6 deletions.
11 changes: 8 additions & 3 deletions arcade/application.py
Expand Up @@ -25,8 +25,7 @@ def animate(self, dt):
Move everything.
Args:
:dt (float): Time interval since the last time the function
was called.
:dt (float): Time interval since the last time the function was called.
"""
pass
Expand All @@ -48,4 +47,10 @@ def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):

def on_mouse_release(self, x, y, button, modifiers):
""" Override this function to add mouse button functionality. """
pass
pass

def on_key_press(self, symbol, modifiers):
pass

def on_key_release(self, symbol, modifiers):
pass
2 changes: 1 addition & 1 deletion arcade/version.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python

BUILD = 15
BUILD = 16
VERSION = "0.0.6"
RELEASE = VERSION + "a" + str(BUILD)
2 changes: 1 addition & 1 deletion doc/source/conf.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python

BUILD = 15
BUILD = 16
VERSION = "0.0.6"
RELEASE = VERSION + "a" + str(BUILD)

Expand Down
74 changes: 74 additions & 0 deletions examples/bouncing_ball.py
@@ -0,0 +1,74 @@
""" Bounce a ball on the screen, using gravity. """

import arcade

# Set up the constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

# Size of the circle.
CIRCLE_RADIUS = 20

# How strong the gravity is.
GRAVITY_CONSTANT = 0.3

# Percent of velocity maintained on a bounce.
BOUNCINESS = 0.9


def draw(dt):
""" Use this function to draw everything to the screen. """

# Start the render. This must happen before any drawing
# commands. We do NOT need an stop render command.
arcade.start_render()

# Draw our rectangle
arcade.draw_circle_filled(draw.x, draw.y, CIRCLE_RADIUS,
arcade.color.BLACK)

# Modify rectangles position based on the delta
# vector. (Delta means change. You can also think
# of this as our speed and direction.)
draw.x += draw.delta_x
draw.y += draw.delta_y

draw.delta_y -= GRAVITY_CONSTANT

# Figure out if we hit the left or right edge and need to reverse.
if draw.x < CIRCLE_RADIUS and draw.delta_x < 0:
draw.delta_x *= -BOUNCINESS
elif draw.x > SCREEN_WIDTH - CIRCLE_RADIUS and draw.delta_x > 0:
draw.delta_x *= -BOUNCINESS

# See if we hit the bottom
if draw.y < CIRCLE_RADIUS and draw.delta_y < 0:
# If we bounce with a decent velocity, do a normal bounce.
# Otherwise we won't have enough time resolution to accurate represent
# the bounce and it will bounce forever. So we'll divide the bounciness
# by half to let it settle out.
if draw.delta_y * -1 > GRAVITY_CONSTANT * 15:
draw.delta_y *= -BOUNCINESS
else:
draw.delta_y *= -BOUNCINESS / 2

# These are function-specific variables. Before we
# use them in our function, we need to give them initial
# values.
draw.x = CIRCLE_RADIUS
draw.y = SCREEN_HEIGHT - CIRCLE_RADIUS
draw.delta_x = 2
draw.delta_y = 0

# Open up our window
arcade.open_window("Bouncing Ball", SCREEN_WIDTH, SCREEN_HEIGHT)
arcade.set_background_color(arcade.color.WHITE)

# Tell the computer to call the draw command at the specified interval.
arcade.schedule(draw, 1 / 80)

# Run the program
arcade.run()

# When done running the program, close the window.
arcade.close_window()
7 changes: 7 additions & 0 deletions examples/bouncing_ball.rst
@@ -0,0 +1,7 @@
.. _example-bouncing-rectangle:

Animation: Bouncing ball
========================


.. literalinclude:: bouncing_ball.py
7 changes: 7 additions & 0 deletions examples/bouncing_rectangle.rst
@@ -0,0 +1,7 @@
.. _example-bouncing-rectangle:

Animation: Bouncing rectangle
=============================


.. literalinclude:: bouncing_rectangle.py
Binary file added examples/drawing_primitives.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions examples/drawing_primitives.rst
@@ -0,0 +1,12 @@
.. _example-drawing-primitives:

Drawing: Drawing primitives
===========================

.. image:: drawing_primitives.png
:width: 600px
:height: 600px
:align: center
:alt: Screenshot of drawing primitives example program

.. literalinclude:: drawing_primitives.py
Binary file added examples/drawing_with_functions.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 79 additions & 0 deletions examples/drawing_with_functions.py
@@ -0,0 +1,79 @@
"""
Example "Arcade" library code.
This example shows how to use functions to draw a scene.
"""

# Library imports
import arcade

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600


def draw_background():
"""
This function draws the background. Specifically, the sky and ground.
"""
# Draw the sky in the top two-thirds
arcade.draw_rect_filled(0, SCREEN_HEIGHT - 1,
SCREEN_WIDTH - 1, SCREEN_HEIGHT * 2 / 3,
arcade.color.SKY_BLUE)

# Draw the ground in the bottom third
arcade.draw_rect_filled(0, SCREEN_HEIGHT / 3,
SCREEN_WIDTH - 1, SCREEN_HEIGHT / 3,
arcade.color.DARK_SPRING_GREEN)


def draw_bird(x, y):
"""
Draw a bird using a couple arcs.
"""
arcade.draw_arc_outline(x, y, 20, 20, arcade.color.BLACK, 0, 90)
arcade.draw_arc_outline(x + 40, y, 20, 20, arcade.color.BLACK, 90, 180)


def draw_pine_tree(x, y):
"""
This function draws a pine tree at the specified location.
"""
# Draw the trunk
arcade.draw_rect_filled(x + 30, y - 100, 20, 40, arcade.color.DARK_BROWN)

# Draw the triangle on top of the trunk
point_list = ((x + 40, y),
(x, y - 100),
(x + 80, y - 100))

arcade.draw_polygon_filled(point_list, arcade.color.DARK_GREEN)


def main():
"""
This is the main program.
"""

# Open the window
arcade.open_window("Drawing With Functions", 600, 600)

# Start the render process. This must be done before any drawing commands.
arcade.start_render()

# Call our drawing functions.
draw_background()
draw_pine_tree(50, 250)
draw_pine_tree(350, 320)
draw_bird(70, 500)
draw_bird(470, 550)

# Finish the render.
# Nothing will be drawn without this.
# Must happen after all draw commands
arcade.finish_render()

# Keep the window up until someone closes it.
arcade.run()

if __name__ == "__main__":
main()
12 changes: 12 additions & 0 deletions examples/drawing_with_functions.rst
@@ -0,0 +1,12 @@
.. _example-drawing-with-functions:

Functions: Drawing with functions
=================================

.. image:: drawing_with_functions.png
:width: 600px
:height: 600px
:align: center
:alt: Screenshot of drawing with functions example program

.. literalinclude:: drawing_with_functions.py
Binary file added examples/drawing_with_loops.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions examples/drawing_with_loops.py
@@ -0,0 +1,96 @@
"""
Example "Arcade" library code.
This example shows how to use functions to draw a scene.
"""

# Library imports
import arcade
import random

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600


def draw_background():
"""
This function draws the background. Specifically, the sky and ground.
"""
# Draw the sky in the top two-thirds
arcade.draw_rect_filled(0, SCREEN_HEIGHT - 1,
SCREEN_WIDTH - 1, SCREEN_HEIGHT * 2 / 3,
arcade.color.SKY_BLUE)

# Draw the ground in the bottom third
arcade.draw_rect_filled(0, SCREEN_HEIGHT / 3,
SCREEN_WIDTH - 1, SCREEN_HEIGHT / 3,
arcade.color.DARK_SPRING_GREEN)


def draw_bird(x, y):
"""
Draw a bird using a couple arcs.
"""
arcade.draw_arc_outline(x, y, 20, 20, arcade.color.BLACK, 0, 90)
arcade.draw_arc_outline(x + 40, y, 20, 20, arcade.color.BLACK, 90, 180)


def draw_pine_tree(x, y):
"""
This function draws a pine tree at the specified location.
"""
# Draw the trunk
arcade.draw_rect_filled(x + 30, y - 100, 20, 40, arcade.color.DARK_BROWN)

# Draw the triangle on top of the trunk
point_list = ((x + 40, y),
(x, y - 100),
(x + 80, y - 100))

arcade.draw_polygon_filled(point_list, arcade.color.DARK_GREEN)


def main():
"""
This is the main program.
"""

# Open the window
arcade.open_window("Drawing With Loops", 600, 600)

# Start the render process. This must be done before any drawing commands.
arcade.start_render()

# Call our drawing functions.
draw_background()

# Loop to draw ten birds in random locations.
for bird_count in range(10):
# Any random x from 0 to the width of the screen
x = random.randrange(0, SCREEN_WIDTH)

# Any random y from in the top 2/3 of the screen.
# No birds on the ground.
y = random.randrange(SCREEN_HEIGHT / 3, SCREEN_HEIGHT)

# Draw the bird.
draw_bird(x, y)

# Draw the top row of trees
for x in range(5, SCREEN_WIDTH, 90):
draw_pine_tree(x, 320)

# Draw the bottom row of trees
for x in range(25, SCREEN_WIDTH, 90):
draw_pine_tree(x, 200)

# Finish the render.
# Nothing will be drawn without this.
# Must happen after all draw commands
arcade.finish_render()

# Keep the window up until someone closes it.
arcade.run()

if __name__ == "__main__":
main()
12 changes: 12 additions & 0 deletions examples/drawing_with_loops.rst
@@ -0,0 +1,12 @@
.. _example-drawing-with-loops:

Loops: Drawing with loops
=========================

.. image:: drawing_with_loops.png
:width: 600px
:height: 600px
:align: center
:alt: Screenshot of drawing with loops example program

.. literalinclude:: drawing_with_loops.py
Binary file added examples/images/character.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/coin_01.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions examples/index.rst
@@ -0,0 +1,14 @@
Example Code
============

.. toctree::
:maxdepth: 3

drawing_primitives
drawing_with_functions
bouncing_rectangle
bouncing_ball
drawing_with_loops
shapes
move_mouse
move_keyboard
Binary file added examples/move_keyboard.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d38d04e

Please sign in to comment.