Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the attached in-class code to achieve the following: 1. Have the user dra

ID: 3827045 • Letter: M

Question

Modify the attached in-class code to achieve the following:

1. Have the user drag the paddle up and down using the mouse pointer when holding down the left-mouse button.

2. Bounce the disk off the paddle when it hits it from the left or from the right.

3. Provide a "target slot" on the left edge of the canvas. When the disk hits the target "slot", it does not bounce back, but instead passes through. (End of game. You won!)

4. Use well-chosen identifiers. Most of the numbers which give the sizes or locations of objects should be defined at the very start of init. This way, when you need to modify a parameter, you can easily find it.

5. Remove unused code from your program so it is easier to read.

Explanation / Answer

from tkinter import * import random class ControlAnimation: def __init__(self): my_window = Tk() # create a window my_window.title("Control Animation Demo") self.width = 400 self.height = 200 self.line_x = 350 self.line_top = 75 self.line_bot = 125 self.paddle_width = 10 self.dy = 5 self.sleep_time = 50 self.is_stopped = False self.my_canvas = Canvas(my_window, bg = 'white', width = self.width, height = self.height) self.my_canvas.pack() frm_control = Frame(my_window) # for comand buttons below canvas frm_control.pack() btn_stop = Button(frm_control, text = 'Stop', command = self.stop) btn_stop.pack(side = LEFT) btn_resume = Button(frm_control, text = 'Resume', command = self.resume) btn_resume.pack(side = LEFT) btn_faster = Button(frm_control, text = 'Faster', command = self.faster) btn_faster.pack(side = LEFT) btn_slower = Button(frm_control, text = 'Slower', command = self.slower) btn_slower.pack(side = LEFT) self.radius = 20 self.x = self.radius # just to start; y is at canvas center self.y = self.height/2 # (x, y) is center of disk for this program, but ... # recall: x1,y1 and x2,y2 form a bounding box for disk self.my_canvas.create_oval( self.x - self.radius, self.height/2 + self.radius, self.x + self.radius, self.height/2 - self.radius, fill = "red", tags = "disk") self.my_canvas.create_line(self.line_x, self.line_top, self.line_x, self.line_bot, width = self.paddle_width, fill = "blue", tags = "paddle") self.my_canvas.bind("", self.move_paddle) self.my_canvas.bind("", self.move_paddle) self.my_canvas.bind("", self.left_click_paddle) self.my_canvas.bind("", self.right_click_paddle) self.animate() self.my_canvas.focus_set() my_window.mainloop() def stop(self): self.is_stopped = True def resume(self): self.is_stopped = False self.animate() def faster(self): if self.sleep_time > 5: self.sleep_time -= 15 def slower(self): self.sleep_time += 15 def animate(self): dx = 3 dy = 2 while not self.is_stopped : self.my_canvas.move("disk", dx, dy) # move right self.my_canvas.after(self.sleep_time) # sleep for a few ms # redraw/update the canvas w/ new oval position self.my_canvas.update() # increment x to set up for next re-draw r = random.randint(-1, 1) self.x += dx # moves the disk if self.x + self.radius > self.width: # hit right boundary dx = -dx + r # add randomness elif self.x - self.radius self.line_x and self.x + self.radius
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote