Assignment 4 -- Simple Bot (one pose)

Assignment 4 – Simple Bot (One-Pose)
Dean Zeller
CG120/CS101
Spring 2017
Objective
20 points
Early Submission: Friday, February 3rd
Due: Friday, February 10th
The student will create the initial pose for a bot object, able to display, move, resize, and change
colors.
Background: Bots
This assignment introduces a new flavor of programming that incorporates objects. For lack of a better term,
the next two assignments will cover how to program a bot. The term “bot” has many connotations, so this
assignment will create an operational definition of bot.
Background: OOP
This assignment also introduces the most important concepts of Object Oriented Programming (OOP),
including objects, attributes, and methods. Until now, all assignments were completed using procedural
programming techniques.
Background: Drawing Window
Finally, this assignment introduces the concept of an abstract drawing window, a necessity in any graphic
programing venture. A drawing window allows programmers to define a visual using abstract terms, and then
draw it anywhere at any size with specified colors.
Terms (OOP)
Object A piece of code to program an entity that can be created (instantiated) by a
calling program. Defined objects are called “instances.”
Attribute A piece of information stored by an object.
Method A section of code connected to the object that can be called by either the
object or the calling program.
Parameter Information passed from the calling program to a function or method.
Terms (Assignment)
“bot” A graphic object, programmed to easily move, resize, change shape, and
change color.
Pose Similar but different views of the same object.
Posable Object A graphic object capable of multiple poses, discussed in next assignment.
Animation Methods A set of methods within the graphic object template that allow for various
animations, including moving, resizing, and changing colors.
Instructions
Follow the design standards below to create a bot similar to the Face, but representing a different picture. Do
not make a face – change the overall structure of the bot visual.
1. Download the assignment files from the course website. ZellerDeanBotFace.py contains the object
definition for an animated smiley face. ZellerDeanBotFaceTester.py contains a working animation
that uses the face bot. In both files, change Zeller to your last name and Dean to your first name. As
such, change Face to a one-word description of the bot, such as stickfigure, spider, gorilla, pacman, d20,
etc…
2. Design a new object based on the template provided. The bulk of the changes will be in the __init__,
draw, and setProperties methods. As long as the code is written properly, there should be no
modifications necessary to the move, moveTo, scale, pause, and delete methods. If it suits your
project, you may make changes to them as necessary. In the handout, the code segments to modify for
this project are shaded in yellow.
3. Modify the documentation to indicate you as the programmer, and that the program was based off a
template. Make sure all block documentation describes your object.
4. Create an animation based on ZellerDeanBotFaceTester.py that briefly demonstrates all of the
methods of your object. Create at least two instances of the object and show each of the methods in
action. Use text annotations to explain what is going on. The objects should move, resize, delete, and
change color.
5. Create a videocapture demo of your finished animation. It should be a minute or two in length, and only
show the program execution, not the code. Name your screencapture ZellerDeanBotFaceDemo, where
Zeller is replaced with your last name, Dean with your first name, and Face is replaced with a oneword description of the bot.
6. Create a videocapture demo of your program. Show the “important” parts of your code, specifically the
__init__ constructor, the guides and drawing commands in the draw method, and the
setProperties method. Name your screencapture ZellerDeanBotFaceTutorial, where Zeller is
replaced with your last name, Dean with your first name, and Face is replaced with a one-word
description of the bot.
7. Submit your programs (2) and video screencaptures (2) to the appropriate folder on Canvas.
Grading
You will be graded on the following criteria:
Accuracy
Correctly using the template to create a new bot
Reusability
Correctly modifying the documentation to match implementation details
Creativity
Imagination and aesthetic quality of the bot appearance and abilities
Example
After the guides are set up, the is drawn with the
following code. Note the placement of the
points and the coordinates.
c.create_oval(x[0],y[0], x[10],y[10])
c.create_oval(x[2],y[2], x[4],y[4])
c.create_oval(x[6],y[2], x[8],y[4])
c.create_polygon(x[2],y[6],
x[4],y[8],
x[6],y[8],
x[8],y[2], smooth=1)
Methods
A method is a function designed within an object. It is called by the user program to perform a specific task.
Methods are called using the “dot” notation. The following methods are defined in the Face object:
dean = Face(parameters)
Defines a new Face object with the specified parameters. Parameters available:
Canvas parameter:
canvas
The canvas to draw the picture on
Position parameters: left, top
Coordinates of the left-top corner of the picture
Size parameters:
width, height
Width and height of the picture
Color parameters:
color1='blue'
Optional parameters to change colors
Name parameters:
name='blank'
Given name of the object
printName=1
1 = print the name, 0 = do not print the name
dean.draw()
Draws the bot at the location, size, and colors stored in the attributes.
dean.move(deltaX, deltaY, delay)
Moves the bot relative to its current location, after an optional delay.
dean.moveTo(x, y, steps, delay)
Moves the bot to a specific location, taking a number of steps, with an optional delay between each step.
for i in range(10):
dean.move(10,10, delay=0)
dave.move(25,25, delay=50)
Uses a for-loop to move two bots at the same time. Note the delay values for parallel movement.
dean.setProperties(faceColor='blue',eyeColor='green', pupilColor='black')
Allows the user to change the colors of the bot. If a color is not specified, then it will not change.
dean.scale(xscale,yscale)
Changes the size of the face, according to the xscale and yscale parameters supplied. No origin
point is needed, as it is a calculated attribute.
dean.delete()
Deletes the bot visual from the canvas. Note that the bot is still defined, and can be drawn again in the
future. While deleted, it can be moved or altered as necessary.
ZellerDeanBotFace.py
###########################################################################
#
Assignment 4 -- Simple Bot (One-Pose)
#
#
Face
#
#
#
# Programmed by <YourName> (<TodaysDate>)
#
# Instructor: Dean Zeller
#
#
#
# Description: The file contains an smiley face bot object with a
#
#
single pose.
#
#
#
# Objects:
#
#
Face
A traditional smiley face
#
#
#
# This program is copyright (c) 2016 <YourName> and Dean Zeller.
#
# All rights reserved. Permission granted to use and modify for
#
# educational purposes only. Any commercial use of this code must
#
# receive permission from the author(s).
#
###########################################################################
from tkinter import *
###########################################################################
#
Face
#
#
#
#
Description: This uses the tkinter graphics library to create a
#
#
smiley face bot, based on the user's parameters and
#
#
instructions.
#
#
#
#
Parameters:
#
#
canvas
Canvas to draw the picture
#
#
left, top
Left and top of the entire picture
#
#
width, height Width and height of the entire picture
#
#
faceColor
Color of the face
#
#
eyeColor
Color of the eyes
#
#
pupilColor
Color of the pupils
#
#
name
Name of the face
#
#
displayName
Boolean to display the character name
#
#
0 -> does not display name
#
#
1 -> displays name
#
#
#
###########################################################################
class Face:
# class attributes
objectName = "ZellerDeanBotFace"
objectNum = 0
# Title for this bot
# Number of instances created
#######################################################################
# __init__ constructor
#
#
#
# Initializes all attributes to given parameters.
#
#######################################################################
def __init__ (self, canvas, left=0,top=0,width=100,height=100,
faceColor="yellow", eyeColor="white", pupilColor="black",
name="blank", displayName=0):
# attributes directly from parameters
self.c = canvas
self.left = left
self.top = top
self.width = width
self.height = height
self.faceColor = faceColor
self.eyeColor = eyeColor
self.pupilColor = pupilColor
self.name = name
self.displayName = displayName
# calculated attributes
self.tag = Face.objectName + str(Face.objectNum)
Face.objectNum += 1
self.right = self.left + self.width
self.bottom = self.top + self.height
self.center = (self.left + self.right) / 2.0
self.middle = (self.top + self.bottom) / 2.0
#######################################################################
# draw method
#
#
#
# Draws the bot according to the attributes, including size,
#
# position, and colors portrayed.
#
#######################################################################
def draw(self):
# create X-coordinate guide list
xspaces = 20 # number of guides in the guide list
xgridwidth = 1.0 * self.width / xspaces
x = []
for i in range(xspaces+1):
x.append(self.left + i*xgridwidth)
# create Y-coordinate guide list
yspaces = 20 # number of guides in the guide list
ygridwidth = 1.0 * self.height / yspaces
y = []
for i in range(yspaces+1):
y.append(self.top + i*ygridwidth)
###################################################################
#
Coordinates
#
###################################################################
# face guides (two points)
face = (
(x[1],y[0]) , (x[19],y[20])
# eye guides (two points each)
eye1 = ( ( x[5],y[5]) , ( x[9],y[9])
eye2 = ( (x[11],y[5]) , (x[15],y[9])
)
)
)
# pupil guides (two points each)
pupil1 = ( ( x[6],y[6]) , (x[8],y[8]) )
pupil2 = ( (x[12],y[6]) , (x[14],y[8]) )
# smile guides (four points)
smile = ( ( x[4],y[12]) , ( x[6],y[16]) , (x[14],y[16]) , (x[16],y[12]) )
# eyebrow guides (four points each)
brow1 = ( ( x[5], y[4]) , ( x[6], y[2]) , ( x[8], y[2]) , ( x[9], y[4]) )
brow2 = ( (x[11] ,y[4]) , (x[12] ,y[2]) , (x[14], y[2]) , (x[15], y[4]) )
###################################################################
#
Drawing
#
###################################################################
# draw face
self.faceID = self.c.create_oval(face, fill=self.faceColor, tag=self.tag)
# draw eyes
self.leftEyeID = self.c.create_oval(eye1, fill=self.eyeColor, tag=self.tag)
self.rightEyeID = self.c.create_oval(eye2, fill=self.eyeColor, tag=self.tag)
# draw pupils
self.leftPupilID = self.c.create_oval(pupil1, fill=self.pupilColor,
tag=self.tag)
self.rightPupilID = self.c.create_oval(pupil2, fill=self.pupilColor,
tag=self.tag)
# draw brows
self.c.create_line(brow1, width=2, fill="black", smooth=1, tag=self.tag)
self.c.create_line(brow2, width=2, fill="black", smooth=1, tag=self.tag)
# draw smile
self.c.create_line(smile, width=2, fill="black", smooth=1, tag=self.tag)
# draw name
if self.displayName: # only display if displayName flag is set
self.c.create_text(self.center, self.bottom, anchor=N,
text=self.name, tag=self.tag)
self.c.update()
#######################################################################
# move method
#
#
#
# Moves the bot on the canvas, relative to its current position,
#
# after the specified delay. Attributes are adjusted accordingly.
#
#######################################################################
def move (self, deltaX=0, deltaY=0, delay=0):
# adjust attributes
self.left += deltaX
self.top += deltaY
self.right += deltaX
self.bottom += deltaY
self.center += deltaX
self.middle += deltaY
# move face
self.c.after(delay)
self.c.move(self.tag,deltaX,deltaY)
self.c.update()
#######################################################################
# moveTo method
#
#
#
# Moves the bot on the canvas to a specific location, broken
#
# into a series of steps, each after the specified delay.
#
#######################################################################
def moveTo (self, x=0, y=0, steps=10, delay=0):
# calculate deltaX and deltaY
distX = x - self.left
deltaX = distX * 1.0 / steps
distY = y - self.top
deltaY = distY * 1.0 / steps
# move in steps
for i in range(steps):
self.move(deltaX=deltaX, deltaY=deltaY, delay=delay)
#######################################################################
# setProperties method
#
#
#
# Changes the parameters of the bot. If a parameter is not
#
# specified, then it remains unchanged.
#
#######################################################################
def setProperties (self, faceColor="default", eyeColor="default",
pupilColor="default"):
if faceColor != "default":
self.faceColor=faceColor
self.c.itemconfig(self.faceID, fill=self.faceColor)
self.c.update()
if eyeColor != "default":
self.eyeColor=eyeColor
self.c.itemconfig(self.leftEyeID, fill=self.eyeColor)
self.c.itemconfig(self.rightEyeID, fill=self.eyeColor)
self.c.update()
if pupilColor != "default":
self.pupilColor=pupilColor
self.c.itemconfig(self.leftPupilID, fill=self.pupilColor)
self.c.itemconfig(self.rightPupilID, fill=self.pupilColor)
self.c.update()
#######################################################################
# scale method
#
#
#
# Changes the size of the object, according to the xscale and
#
# yscale parameters, after a given delay.
#
#
#
# Note: There is a bug in this method. The left, right, top, and
#
# bottom are not recalculated. For most cases, this will not affect #
# animation playback. However, the potential for problems exist.
#
#######################################################################
def scale(self, xscale=1.0, yscale=1.0, delay=0):
self.c.after(delay)
self.c.scale(self.tag, self.center, self.middle, xscale, yscale)
self.width = self.width * xscale
self.height = self.height * yscale
self.c.update()
#######################################################################
# pause method
#
#
#
# Delays the animation, according to the after parameter specified. #
#######################################################################
def pause(self, delay):
self.c.after(delay)
self.c.update()
#######################################################################
# delete method
#
#
#
# Deletes the bot from the canvas. Note that the object still
#
# still exists after it is deleted; it is just not displayed.
#
#######################################################################
def delete(self, delay=0):
self.c.after(delay)
self.c.delete(self.tag)
self.c.update()
ZellerDeanBotFaceTester.py
###########################################################################
#
Dean's Animation
#
#
#
# Programmed by Dean Zeller (02-14-2016)
#
#
#
# The following is a animated story of Dean and his two minions, Megan
#
# and Cody. The purpose of this animation is to demonstrate to CG120
#
# students how to complete Assignment 4.
#
#
#
# EXTERNAL FILES
#
# The following external files are used in the process of running the
#
# animation.
#
#
ZellerDeanBotFace, written by Dean Zeller
#
#
Face -- An animated smiley face
#
#
#
###########################################################################
from ZellerDeanBotFace import Face
from tkinter import *
c = Canvas(width=500, height=500, bg='white')
c.pack(expand=YES, fill=BOTH)
# delay value between actions, in milliseconds (1000 = 1 second)
longDelay = 2000
mediumDelay = 1000
shortDelay = 50
###########################################################################
# Delete all lines of code beneath this line
#
###########################################################################
# define objects used in animation, along with initial position and size
title = c.create_text(250,30, text="It's another animation!",
font=("Helvetica",36))
dean = Face(c, left=200, top=100, width=100, height=100,
faceColor="purple", eyeColor="white")
megan = Face(c, left=300, top=100, width=100, height=100,
faceColor="red", eyeColor="white")
cody = Face(c, left=100, top=100, width=100, height=100,
faceColor="green", eyeColor="red")
# draw bots
c.itemconfig(title, text="Draw the Faces")
c.update()
dean.draw()
c.after(longDelay)
c.update()
megan.draw()
c.after(longDelay)
c.update()
cody.draw()
c.after(longDelay)
c.update()
# Move bots with the move method
c.itemconfig(title, text="The move Method")
c.update()
c.after(longDelay)
cody.move(-100,0)
c.after(longDelay)
megan.move(100,0)
c.after(longDelay)
c.update()
# move bots with moveTo method
c.itemconfig(title, text="The moveTo Method")
c.update()
dean.moveTo(50,100, steps=50, delay=10)
megan.moveTo(350,100, steps=50, delay=10)
cody.moveTo(200,100, steps=50, delay=10)
# delete and draw
c.itemconfig(title, text="Delete and Draw")
c.update()
dean.delete(delay=longDelay)
megan.delete(delay=longDelay)
cody.delete(delay=longDelay)
dean.pause(longDelay)
dean.moveTo(350,200, steps=1, delay=0)
megan.moveTo(50,200, steps=1, delay=0)
cody.moveTo(200,200, steps=1, delay=0)
dean.draw()
dean.pause(longDelay)
megan.draw()
megan.pause(longDelay)
cody.draw()
cody.pause(longDelay)
# set parameters
c.itemconfig(title, text="Setting Colors")
c.update()
dean.setProperties(faceColor="blue", eyeColor="pink", pupilColor="white")
megan.setProperties(faceColor="orange", eyeColor="blue")
cody.setProperties(eyeColor="black")
c.after(longDelay)
c.update()
# use for loops to move in parallel
c.itemconfig(title, text="Movement in Parallel")
c.update()
for i in range(10):
dean.move(0,15,delay=shortDelay)
megan.move(0,-15,delay=shortDelay)
cody.move(-15,15,delay=shortDelay)
# resize
c.itemconfig(title, text="Changing Size")
c.update()
dean.scale(.5, .5, delay=longDelay)
megan.scale(1.5, 1.5, delay=longDelay)
cody.scale(2.5, 2.5, delay=longDelay)
# the end
c.itemconfig(title, text="THE END")
c.update()
Assignment 4 Grading Rubric
CS101/CG120
Name: ____________________________
General Requirements
______ Program runs correctly
No credit if code does not run, for any reason
______ Block documentation
-1 to -2 for incorrect description or entries
-3 if omitted altogether
______ Inline documentation
-1 to -2 for incomplete or missing inline documentation
______ Coding style
Descriptive variable names
Proper indentation and line spacing for readability
______ Files named correctly
(Not important for this assignment, but may become important later)
Assignment Requirements
______ Animated object implemented, following template example
Use of windowing system, guides, drawing commands
setProperties implemented for changing colors
______ Screencapture demo shows requirements
Movement
Color change
Size change
Deletion and reappearing
______ Screencapture tutorial shows all aspects of programming
-2 for missing voice annotation
Extra Credit
______ Early submission
+2 if submitted before the early submission date
______ Extra efforts in object quality and aesthetics