Last active
December 25, 2020 02:56
-
-
Save dfsklar/27d529dbfaf76434b4a5570a629f8802 to your computer and use it in GitHub Desktop.
This is the starter code for the exercise described at https://docs.google.com/document/d/1g0jIbT89hR7PfRSo9rZhXb2y5PLY-p6px4sOl7U3BTY
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 is the starter code for the exercise described at | |
https://docs.google.com/document/d/1g0jIbT89hR7PfRSo9rZhXb2y5PLY-p6px4sOl7U3BTY | |
I M P O R T A N T : | |
The best way to copy this code into your own Python code editor is to hit the "Raw" button | |
and then copy the code from the text-only version that will appear. | |
""" | |
import arcade | |
# WE START BY DECLARING OUR "CONSTANTS". | |
# "Constants" are storage boxes that contains values that never change. | |
# They allow you to avoid typing "hardwired" numbers into the logic | |
# of the actual computation parts of the program. | |
# Size of the screen | |
SCREEN_WIDTH = 200 | |
SCREEN_HEIGHT = 200 | |
SCREEN_TITLE = "Bouncing Ball Example" | |
TABLE_THICKNESS = 8 | |
TABLE_WIDTH = 80 | |
TABLE_TOP_Y = 70 | |
TABLE_LEFT_X = SCREEN_WIDTH - TABLE_WIDTH | |
TABLE_CENTER_X = SCREEN_WIDTH - (TABLE_WIDTH / 2) | |
TABLE_CENTER_Y = TABLE_TOP_Y - (TABLE_THICKNESS / 2) | |
# Size of the circle. | |
CIRCLE_RADIUS = 12 | |
# How strong the gravity is. | |
GRAVITY_CONSTANT = 0.2 | |
# Percent of velocity maintained on a bounce. | |
# E.g. 0.9 means "90% of the speed is maintained when the bounce reverses direction". | |
BOUNCINESS = 0.9 | |
def draw(_delta_time): | |
# This function is automatically called by the arcade system based on an interval schedule | |
# (e.g. 30 times a second, 60 times a second, etc). | |
# The schedule is setup in the code found near the very bottom of this program. | |
# This is how arcade achieves "animation", by letting you redraw the entire screen every time this function is invoked. | |
# This function draws the "current" version of the "scene", and then it "plans" the "next" version of the scene. | |
# First, we need to move the ball to its "current" location by updating the ball's X and Y values. | |
# We update the ball's X and Y location values by adding the current "X speed" and current "Y speed". | |
# Note that we use the term "delta" to represent "speed". | |
# Delta is a Greek symbol that represents "change". | |
# And speed is indeed nothing more than a value that represents the amount of "change in location". | |
# The delta X is quite simple: negative means "going towards left", positive means "going towards right". | |
draw.x = draw.x + draw.delta_x | |
# The delta Y is tricky -- a negative number for delta Y means "moving towards the floor". | |
# Ball is Falling == negative delta_y | |
# Ball is Rising == postive delta_y | |
draw.y = draw.y + draw.delta_y | |
# OK so the ball is now at its updated location. | |
# So let's draw the "scene": | |
# 1. Background color | |
# 2. Table | |
# 3. Ball | |
arcade.start_render() | |
arcade.set_background_color(arcade.color.WHITE) | |
arcade.draw_rectangle_filled(TABLE_CENTER_X, TABLE_CENTER_Y, TABLE_WIDTH, TABLE_THICKNESS, arcade.color.BLUE) | |
arcade.draw_circle_filled(draw.x, draw.y, CIRCLE_RADIUS, | |
arcade.color.BLACK) | |
# Figure out if we hit the left or right edge of the screen. | |
# If we've hit either side of the screen, we need to "reverse" the X speed. | |
if draw.x < CIRCLE_RADIUS and draw.delta_x < 0: # if we hit the left wall | |
draw.delta_x = draw.delta_x * -BOUNCINESS | |
if draw.x > SCREEN_WIDTH - CIRCLE_RADIUS and draw.delta_x > 0: #if we hit the right wall | |
draw.delta_x = draw.delta_x * -BOUNCINESS | |
# IF | |
# The center of the ball (draw.y) is "underground" (less than zero) | |
# AND | |
# the ball is "falling" (i.e. its speed is negative) | |
# THEN we want to force the ball to "bounce" by a reversal of its vertical speed (draw.delta_y). | |
if (draw.y < 0) and (draw.delta_y < 0): | |
# OK SO LET'S BOUNCE! | |
if (draw.delta_y * -1) > (GRAVITY_CONSTANT * 15): | |
# The downward speed of the ball is quite large, so it should bounce with full power | |
draw.delta_y = draw.delta_y * -BOUNCINESS | |
else: | |
# The downward speed of the ball is quite small, so it should bounce with half power | |
draw.delta_y = draw.delta_y * (-BOUNCINESS / 2) | |
# Now we need to "plan" the animation frame that should come next. | |
# We do that by updating the ball's speed since gravity always wants to change the vertical speed of an object. | |
# This next line simulates gravity by changing the Y speed to increase it towards the negative direction. | |
draw.delta_y = draw.delta_y - GRAVITY_CONSTANT | |
###################################### END OF FUNCTION DECLARATION | |
# Initial values for the variables that *change* during the program execution: | |
draw.x = CIRCLE_RADIUS # Initial x: left side of ball at left edge of screen. | |
draw.y = SCREEN_HEIGHT - CIRCLE_RADIUS # Initial y: top of ball at top edge of screen. | |
draw.delta_x = 1.4 # Initial x speed | |
draw.delta_y = 0 # Initial y speed | |
# Open up our window | |
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) | |
arcade.set_background_color(arcade.color.WHITE) | |
# Tell the computer to call the draw command at the specified interval. | |
arcade.schedule(draw, 1 / 30) | |
# Run the program | |
arcade.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment