Python Programming: An Introduction to Computer Science, 3rd Ed.
Python Programming: An Introduction to Computer Science, 3rd Ed.
3rd Edition
ISBN: 9781590282755
Author: John Zelle
Publisher: Franklin, Beedle & Associates
Question
Book Icon
Chapter 10, Problem 13PE
Program Plan Intro

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()”.
  • 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

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()”.

Expert Solution & Answer
Check Mark

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()

Sample Output

Output:

Screenshot of output

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 10, Problem 13PE , additional homework tip  1

Clicking Wink button:

Screenshot of output

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 10, Problem 13PE , additional homework tip  2

Clicking Mediate button:

Screenshot of output

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 10, Problem 13PE , additional homework tip  3

Clicking Smile button:

Screenshot of output

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 10, Problem 13PE , additional homework tip  4

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Please help me fix this java code which is supposed to be a Pac-Man game Java program is below import java.awt.*; import javax.swing.JFrame.*; public class ManyPacMans extends JFrame { public void paint(Graphics g) { g.drawghostShape(60,30,40); setBackground(Color.BLUE); g.fillghostShape(130, 30,100, 80); g.drawghostShape(60,30,40); setBackground(Color.PINK); g.fillghostShape(130, 30,100, 80); g.drawghostShape(60,30,40); setBackground(Color.RED); g.fillghostShape(130, 30,100, 80); g.drawOvalpacman(30,130,50, 60); setForeground(Color.yellow); g.fillOval(130,130,50, 60); } public Ghost(String imgPath, int x, int y, int speed, boolean ip) { super(imgPath, new int[] {0,0}, GHOST_WIDTH, GHOST_HEIGHT, GHOST_FRAMERATE, x, y); super.addFramesForAnimation("scatter", "ghost_scatter.png", 0, 0, 22,0); super.speed = speed; startX = x; startY = y; scoreValue = 200; } public ManyPacMans() { initUI(); } private void initUI() { add(new…
Implement a class that simulates a traffic light. The next method advances the color in the usual way, from green to yellow to red, then again to green. Provide two constructors, as described in the documentation of the public interface. Also supply a method that yields the number of times that this traffic light has been red. /**   A simulated traffic light.*/public class TrafficLight{   private String color;   private int reds;   /**      Constructs a green traffic light.   */   public TrafficLight()   {      /* Your code goes here */   }      /**      Constructs a traffic light.      @param initialColor the initial color "green", "yellow", or "red"   */   public TrafficLight(String initialColor)   {      /* Your code goes here */   }    /**      Moves this traffic light to the next color.   */   public void next()   {      /* Your code goes here */   }    /**      Returns the current color of this traffic light.      @return the current color   */   public String getColor()   {…
Please solve this using java and attach output screenshot and implementation screenshot.   Using this clock class please: public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec = seconds; else sec = 0; } public int getHours() { return hr; } public int getMinutes() { return min; } public int getSeconds() { return sec; } public void printTime() { if (hr < 10) System.out.print("0"); System.out.print(hr + ":"); if (min < 10) System.out.print("0"); System.out.print(min + ":"); if (sec < 10) System.out.print("0"); System.out.print(sec); } public void incrementSeconds() { sec++; if (sec >…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education