forked from pythonarcade/arcade
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bunch of work improving the examples section.
- Loading branch information
Showing
28 changed files
with
772 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/usr/bin/env python | ||
|
||
BUILD = 15 | ||
BUILD = 16 | ||
VERSION = "0.0.6" | ||
RELEASE = VERSION + "a" + str(BUILD) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#!/usr/bin/env python | ||
|
||
BUILD = 15 | ||
BUILD = 16 | ||
VERSION = "0.0.6" | ||
RELEASE = VERSION + "a" + str(BUILD) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.. _example-bouncing-rectangle: | ||
|
||
Animation: Bouncing ball | ||
======================== | ||
|
||
|
||
.. literalinclude:: bouncing_ball.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.. _example-bouncing-rectangle: | ||
|
||
Animation: Bouncing rectangle | ||
============================= | ||
|
||
|
||
.. literalinclude:: bouncing_rectangle.py |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.