Face
Program Plan:
face.py
- Import the required packages.
- Definition of main “init” method.
- Assign the size value, eye size, eye off, mouth size, mouth off, window value to the corresponding variables.
- Definition of “getCenter” method.
- Return the center value.
- Definition of “move” method.
- Call the method “undrawn()”.
- Call the method getCenter().
- Get the position of “x” and “y”.
- Get the center position.
- Check the condition for the face to smile.
- Call the method “smile()”.
- Check the condition for the face to wink.
- Call the method “wink()”.
- Check the condition for the face to Grim Face.
- Call the method “GrimFace()”.
- Call the method “mediate()”.
- Check the condition for the face to wink.
- Call the method “smile()”.
- Definition “initializeGrimFace” method.
- Create a circle.
- Call the function to open the left eye and right eye.
- Create an array for teeth.
- Call the method to draw the mouth.
- Call the method to draw the face.
- Definition of method “lineMouth()”.
- Call the method “clone()”.
- Condition to set the mouth.
- Call the method “clone()”.
- Call the method “Line()”.
- Return the line.
- Definition of method “rectMouth()”
- Get the position to place the mouth.
- Return the position.
- Definition of method “undrawn()”.
- Remove the head, left eye, right eye, teeth and mouth.
- Definition of method “drawFace()”.
- Draw the circle to place the outline of the face.
- Set the head to the window.
- Set the left eye to the window.
- Set the left eye to the window.
- Set the mouth to the window.
- Definition of “leftEyeOpen()”.
- Set the left eye to the circle.
- Condition to open and close the left eyes.
- Definition of “rightEyeOpen()”.
- Set the right eye to the circle.
- Condition to open and close the right eyes.
- Definition of “leftEyeWink()” method.
- Place the position of the left eye.
- Get the values of “x” and “y”.
- Condition to get the “leftWink”.
- Set the value to “leftWink”.
- Definition of “rightEyeWink()” method.
- Place the position of the right eye.
- Get the values of “x” and “y”.
- Condition to get the “rightWink”.
- Set the value to “rightWink”.
- Definition of “wink()”.
- Call the method “unDraw()”.
- Call the method “lineMouth()”, leftEyeOpen(), “rightEyeWink()”, “drawFace()”.
- Definition of “smile()” method.
- Call the method “unDraw()”.
- Call the method “rectMouth()”.
- Get the “xint” value.
- Condition to append the mouth.
- Traverse the loop till “i” reaches “8”.
- Call the method “Line()” to get the two points.
- Condition to append the teeth.
- Check the condition to place the teeth.
- Draw the tooth for the face.
- Call the method “rectMouth()”., “leftEyeOpen()”, “rightEyeOpen()”, “drawFace()”.
- Definition of “mediate()” method.
- Call the respective method to un draw the face, draw mouth, right and left eye and face
- Definition of “mediate()” method.
Main.py:
- Import the required packages.
- Definition of method “makeButtons()”.
- Create a button “wink()”.
- Create a button “mediate()”.
- Create a button “Smile()”.
- Crate a button “Quit”.
- Activate all the buttons.
- Return the values.
- Definition of method “main()”.
- Creating the interface.
- Call the method “getMouse()”.
- Check whether the “endGame” is clicked or not.
- Check whether the “wink” is clicked.
- Call the method “wink”.
- Call the method “getMouse()”.
- Check whether the “smile” is clicked.
- Call the method “smile()”.
- Call the method “getMouse()”.
- Check whether the “mediate” is clicked.
- Call the method “mediate()”.
- Call the method “getMouse()”.
- Close the window.
- Call the method “main()”.
- Check whether the “wink” is clicked.
Explanation of Solution
Program:
face.py
#Import required packages
from graphics import *
#Definition of class Face
class Face:
#Definition of init method
def __init__(self, window, center, size):
#Assign the size value
self.size = size
#Calculate the eye size
self.eyeSize = 0.15 * size
#Condition to close the eye
self.eyeOff = size / 3.0
#Condition of mouth size
self.mouthSize = 0.8 * size
#Condition to close the mouth
self.mouthOff = size / 2.0
#Set the interface to centre
self.center = center
#Assign the window value
self.window = window
#Call the method initializeGrimFace
self.initializeGrimFace()
#Assign the rightEye
self.rightEye
#Definition of getCenter method
def getCenter(self):
#Return the centre value
return self.center
#Definition of method move
def move(self, dx, dy):
#Call the method undraw
self.unDraw()
#Call the method getCenter
center = self.getCenter()
#Get the position of x and y
x = center.getX()
y = center.getY()
#Get the centre position
self.center = Point(x + dx, y + dy)
#Check the condition for the face to simile
if dx < 0 and dy < 0:
#Call the smile method
self.smile()
#Check the condition for the face to wink
elif dx < 0 and dy > 1:
#Call the method wink
self.wink()
#Check the condition for GrimFace
elif dx > 0 and dy > 0:
#Call the method initializeGrimFace()
self.initializeGrimFace()
#Otherwise, call the method mediate()
else:
self.meditate()
#Definition of method initializeGrimFace
def initializeGrimFace(self):
#Create a circle
self.head = Circle(self.center, self.size)
#Call the function to open the left eye
self.leftEyeOpen()
#Call the function to open the right eye
self.rightEyeOpen()
#Create an array for teeth
self.teeth = []
#Call the method to draw the mouth
self.lineMouth()
#Call the method to draw the face
self.drawFace()
#Definition of method lineMouth
def lineMouth(self):
#Call the method clone()
p1 = self.center.clone()
#Condition to set the mouth
p1.move(-self.mouthSize/2, self.mouthOff)
#Call the method clone
p2 = self.center.clone()
#Condition to set the mouth
p2.move(self.mouthSize/2, self.mouthOff)
#Call the method Line()
self.mouth = Line(p1, p2)
#Return the line
return Line(p1, p2)
#Definition of method rectMouth()
def rectMouth(self):
#Get the position to place the mouth
p1, p2 = self.mouth.getP1(), self.mouth.getP2()
x1, x2, y1, y2 = p1.getX(), p2.getX(), p1.getY(), p2.getY()
offset = self.eyeSize / 2
self.mouth = Rectangle(Point(x1, y1 - offset), Point(x2, y2 + offset))
#Return the position
return x2, x1, y1, y2, offset
#Definition of method unDraw()
def unDraw(self):
#Remove the head, left eye, right eye, teeth and mouth
self.head.undraw()
self.leftEye.undraw()
self.rightEye.undraw()
self.mouth.undraw()
for tooth in self.teeth:
tooth.undraw()
#Definition of method drawFace()
def drawFace(self):
#Draw the circle to place the outline of the face
self.head = Circle(self.center, self.size)
#Set the head to the window
self.head.draw(self.window)
#Set the left eye to the window
self.leftEye.draw(self.window)
#Set the right eye to the window
self.rightEye.draw(self.window)
#Set the mouth to the window
self.mouth.draw(self.window)
#Definition of leftEyeOpen()
def leftEyeOpen(self):
#Set the left eye to the circle
self.leftEye = Circle(self.center, self.eyeSize)
#condition to open and close the left eyes
self.leftEye.move(-self.eyeOff, -self.eyeOff)
#Definition of rightEyeOpen()
def rightEyeOpen(self):
#Set the right eye to the circle
self.rightEye = Circle(self.center, self.eyeSize)
#Condition to open and close the right eyes
self.rightEye.move(self.eyeOff, -self.eyeOff)
#Definition of leftEyeWink()
def leftEyeWink(self):
#Place the position of the left eye
center = self.leftEye.getCenter()
#Get the x and y value
x = center.getX()
y = center.getY()
#Condition to get the leftWink
leftWink = Line(Point(x - self.eyeSize, y), Point(x + self.eyeSize, y))
#Set the value leftWink
self.leftEye = leftWink
#Definition of rightEyeWink()
def rightEyeWink(self):
#Place the position of the right eye
center = self.rightEye.getCenter()
#Get the x and y value
x = center.getX()
y = center.getY()
#Condition to get the rightWink
rightWink = Line(Point(x - self.eyeSize, y), Point(x + self.eyeSize, y))
#Set the value rightEye
self.rightEye = rightWink
#Definition of wink()
def wink(self):
#Call the method unDraw()
self.unDraw()
#Call the method lineMouth(), leftEyeOpen(), rightEyeWink(), drawFace()
self.lineMouth()
self.leftEyeOpen()
self.rightEyeWink()
self.drawFace()
#Definition of method smile()
def smile(self):
#Call the method unDraw()
self.unDraw()
#Call the method rectMouth()
x2, x1, y1, y2, offset = self.rectMouth()
#Get xint value
xint = abs(x2 - x1) / 8
#Condition to append the mouth
self.teeth.append(self.lineMouth())
#Traverse the loop till i reaches 8
for i in range (8):
#Call the method Line() to get the two points
t2 = Line(Point(x1 + i * xint, y1 - offset), Point((x1 + i * xint), y2 + offset))
#Condition to append the teeth
self.teeth.append(t2)
#Check the condition to place the teeth
for tooth in self.teeth:
#Draw the tooth for the face
tooth.draw(self.window)
#Call the method reactMouth(), leftEyeOpen(), rightEyeOpen(), drawFace()
self.rectMouth()
self.leftEyeOpen()
self.rightEyeOpen()
self.drawFace()
#Definition of method mediate()
def meditate(self):
#Call the respective method to undraw the face, draw mouth, right and left eye and face
self.unDraw()
self.lineMouth()
self.leftEyeWink()
self.rightEyeWink()
self.drawFace()
Cbutton.py:
Refer the program “button.py” given in the “Chapter 10” from the text book. Add the method “update()” along with the given code.
#Define the method update
def update(self, win, label):
#Call the method undraw()
self.label.undraw()
#Assign the position to centre
center = self.center
#Assign the label
self.label = Text(center, label)
#Set active to false
self.active = False
#Call the method draw()
self.label.draw(win)
Main.py
#Import the required packages
from face import Face
from graphics import *
from cbutton import CButton
#Definition of method makeButton(0
def makeButtons(win):
#Create a button Wink
wink = CButton(win, Point(16, 17), 1, "Wink")
#Create a button Meditate
meditate = CButton(win, Point(12, 17), 1, "Meditate")
#Create a button Smile
smile = CButton(win, Point(8, 17), 1, "Smile")
#Create a button Quit
endGame = CButton(win, Point(4, 17), 1, "Quit")
#Activate all the buttons
wink.activate()
meditate.activate()
smile.activate()
endGame.activate()
#Return the values
return wink, meditate, smile, endGame
#Definition of method main()
def main():
#Creating the interface
win = GraphWin("Emoji Jawn", 600, 600)
win.setCoords(20, 20, 0, 0)
face = Face(win, Point(10,8), 7)
wink, meditate, smile, endGame = makeButtons(win)
#Call the method getMouse()
pt = win.getMouse()
#Check whether the endGame is clicked or not
while not endGame.clicked(pt):
#Check whther the Wink is clicked
if wink.clicked(pt):
#Call the method wink()
face.wink()
#Call the method getMouse()
pt = win.getMouse()
#Check whether the smile is clicked
elif smile.clicked(pt):
#Call the method smile()
face.smile()
#Call the getMouse() method
pt = win.getMouse()
#Check whether the meditate is clicked
elif meditate.clicked(pt):
#Call the method meditate()
face.meditate()
#Call the getMouse() method
pt = win.getMouse()
#Otherwise
else:
#Call the getMouse() method
pt = win.getMouse()
#close up shop
win.close()
#Call the method main()
main()
Output:
Screenshot of output
Clicking Wink button:
Screenshot of output
Clicking Mediate button:
Screenshot of output
Clicking Smile button:
Screenshot of output
Want to see more full solutions like this?
Chapter 10 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- Use this Bird class - i.e. a blueprint for creating Bird objects and using them - to answer questions #1 and #2. class Bird: animal_class = 'Aves' animal_phyla = "Chordata" lays_eggs = True def __init__(self, name, species, color): self.species = species self.name = name self.color = color def get_species(self): return self.species def getting_older(self, yrs): self.age = self_age + yrs def fly(self): print("Now I'm flying") def camouflage(self): print("Blending in with a " + str(self.color) + " environment") def sing(self): print("Tweet Tweet") Which function call below will define a object of the Bird class with name "Audrey", species "Red Robin", and color "red", assigned to the 'my_bird' variable name? a) my_bird = Red_Robin("Audrey") b) my_bird = Red_Robin(Bird("Audrey")) c) my_bird = Bird("Audrey", "Red Robin", "red") d) my_bird = Bird("Red Robin", "Audrey", "red") e) my_bird = Bird("Audrey") 2) What line of code calls the sing…arrow_forwardWrite a program that uses a bar chart to display the percentagesof the overall market share represented by Apple, HTC, Samsung, andOthers, as shown in Figure . Suppose that Apple takes 20 percent and isdisplayed in red, HTC takes 26 percent and is displayed in blue, Samsung takes28 percent and is displayed in green, and the Others take 26 and are displayedin orange. Use the Rectangle class to display the bars. Interested readers mayexplore the JavaFX BarChart class for further study.arrow_forwardWrite a Java class called HotelRoom that represents a hotel room. The class will contain the following attributes: •RoomNo (int) •PhoneNumber (string) •Capacity (int) – Number of people that room can accommodate. •Grade (int) – Grade of room ranging 1-5, 5 being most valuable. Your class should have a constructor that takes a single parameter: RoomNo. The necessary accessors (i.e. get methods) and mutators (i.e. set methods) for class variables: Room number only accessor Phone number both mutator and accessor Capacity both mutator and accessor Grade both mutator and accessor 2.Write a Java class called Hotel that represents a hotel. The class will contain the following attributes: •Name (string) •ReceptionPhone (string) •RoomList (ArrayList of HotelRoom's) hotel class should have a constructor that takes two parameters: Hotel name and reception phone. The necessary accessors (i.e. get methods) and mutators (i.e. set methods) for class variables: Hotel name only accessor Reception phone…arrow_forward
- In Java, please. Complete the Course class by implementing the findStudentHighestGpa() method, which returns the Student object with the highest GPA in the course. Assume that no two students have the same highest GPA. Given classes: Class LabProgram contains the main method for testing the program. Class Course represents a course, which contains an ArrayList of Student objects as a course roster. (Type your code in here.) Class Student represents a classroom student, which has three fields: first name, last name, and GPA. (Hint: getGPA() returns a student's GPA.) Note: For testing purposes, different student values will be used. Ex. For the following students: Henry Nguyen 3.5 Brenda Stern 2.0 Lynda Robison 3.2 Sonya King 3.9 the output is: Top student: Sonya King (GPA: 3.9) LabProgram.java import java.util.Scanner; public class LabProgram { public static void main(String args[]) { Scanner scnr = new Scanner(System.in); Course course = new Course(); int…arrow_forwardAn arithmetic progression is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15 is an arithmetic progression with a common difference of 2.Given the class IntNumber:1 public class IntNumber2 {3 public final int value;45 public IntNumber(int number) {6 this.value = number;7 }8 }where value is an immutable field, that can be directly accessed (public), implement a class called ArithmeticProgressionthat implements the following methods:•1 public static IntNumber getArithmeticProgressionDifference(ArrayList<IntNumber> list)which returns the difference between terms of the arithmetic progression (as an object of type IntNumber) if thevalues of the IntNumber objects are an arithmetic progression. The method returns null in any other case.•1 public static void printIsArithmeticProgression(ArrayList<IntNumber> list)which prints on the console:The list is an arithmetic progression with difference =…arrow_forwardImplement the ShoppingCart and Product class with necessary properties so that the given output is produced. Do not change any given code.s1 = ShoppingCart()p1 = Product("Razer BlackShark","Headset",99.99,5)p2 = Product.createProduct("Razer Huntsman","Keyboard",249.99,12)print("1.====================================")s1.addGadget(p1,p2)print("2.====================================")s1.setCartName("Amazon")s1.addGadget(p1,p2)s1.addGadget(Product("HyperX Fury","Mousepad",26.99,21))print("3.====================================")print("Total Products created:",Product.totalProducts)print("4.====================================")s1.printCartDetails()print("5.====================================")s1.removeFromCart("Logitech G ProX Superlight")print("6.====================================")s1.removeFromCart("Razer Huntsman")print("7.====================================")s1.printCartDetails()Output:1.====================================Please set the cart name…arrow_forward
- Please add the code after this one. The code has to be in java eclipse. Please add comments in what you are doing. I'll appreciate it public class Computer { private String ModelNumber; private String BrandName; private String manufacturingDate; private int NumberOfCores; //Constructor a public Computer() {} //Construct a computer object with model, BrandName, NumberOfCoresComputer (String ModelNumber, String BrandName, String manufacturingDate, int NumberOfCores){ this.ModelNumber=ModelNumber; this.BrandName=BrandName; this.manufacturingDate=manufacturingDate; this.NumberOfCores=NumberOfCores;}//generate getters/setterspublic String getModel() { return ModelNumber;} public void setModel(String ModelNumber) { ModelNumber = ModelNumber;} public String getBrandName() { return BrandName;} public void setBrandName(String brandName) { BrandName = brandName;} public String getManufacturingDate() { return manufacturingDate;} public void setManufacturingDate(String…arrow_forwardDevelop a class VisualCounter that allows both increment and decrementoperations. Take two arguments N and max in the constructor, where N specifies themaximum number of operations and max specifies the maximum absolute value forthe counter. As a side effect, create a plot showing the value of the counter each time its tally changes.arrow_forwardWrite a class encapsulating the concept of converting integer grades to letter grades (A, B, C, D, or F), assuming grades are composed of a list of integers between 0 and 100 (instance variable). Write the following methods: • A constructor with just one parameter, the number of students; all grades can be randomly generated. • Accessor, mutator, toString, and equals methods. • A method returning an array of chars corresponding to the integer grades (90 or above should be converted to A, 80 or above to B, 70 or above to C, 60 or above to D, and 59 or less to F). • A method returning the number of A’s. • A method returning an array of int counting how many A’s, B’s, C’s, D’s, and F’s were received.arrow_forward
- in Java language : Write a program to print the area of a rectangle by creating a class named “Rectangle” with constructor having the two sides (width and height) as its parameters Test the class by creating a main class which contains the main method -->name the class “TestRectangle” Use the Scanner library to take the two sides as inputs from the console.arrow_forwardImplement the design of the Ironman and DoctorStrange class derived from Avengers class so that the following code generates the output below: class Avengers: numberOfAvengers = 0 def __init__(self, name = None): self.name = name print('Welcome to Multiverse!') if self.name == None: print('Who is your favourite Super Hero?') else: print(f'{self.name} is your favourite Super Hero!') def hasSuperPower(self): print('All Avengers have super powers.') def fightThanos(self): return 'No avenger can fight Thanos' #Write your code here #------------------------------------------------------------------------------------- print('1.===============================') print('Number of Avengers:', Avengers.numberOfAvengers) print('2.===============================') a1 = Avengers() print('3.===============================') a1.hasSuperPower() print('4.===============================') print(a1.fightThanos()) print('5.===============================') im1 = Ironman('Tony Stark')…arrow_forwardWrite a program to test the class LinkedBag. For example create a bag by asking the user for few names then 1.Put those names in the bag. 2. ask the user for a name and delete it from the bag. 3. ask for a name and check whether it is in the bag or not 4. print all names in the bag. Hello! I'll appreciate a simple easy program with comments explaining the steps. Thanks for the help!arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education