Answered step by step
Verified Expert Solution
Question
1 Approved Answer
need help with this code in Python, please ADD the DROP class only as you see in the pictures. thanks!!! When you run the starting
need help with this code in Python, please ADD the DROP class only as you see in the pictures. thanks!!! When you run the starting code, there will be three balls moving. Change the program so that it: Starts by not showing any balls Every time the user types the letter b the program should create a new ball. Create a Drop.py file containing a Drop class. I suggest starting with a copy of the Ball.py file. The Drop class should be similar to but simpler than the Ball class. The user should be able to create as many drops Drop objects as they want. Each drop should start start at some randomized X location, and at or above the top of the window. Every drop that is created should fall straight down at some random speed eg between and pixels per frame When a drop falls off the bottom it should be recycled, and relocated to above the window, so that it falls again. You can rerandomize the X position if you wish when the drop recycles The art is available in the "images" folder. Every time the user types the letter d the program should create a new drop. Code files: Main HWpy # pygame demo using Ball class, bounce many balls # Import packages import pygame from pygame.locals import import sys import random from Ball import # bring in the Ball class code # Define constants BLACK WHITE WINDOWWIDTH WINDOWHEIGHT FRAMESPERSECOND NBALLS # Initialize the world pygame.init window pygame.display.setmodeWINDOWWIDTH, WINDOWHEIGHT clock pygame.time.Clock # set the speed frames per second # Load assets: images sounds, etc. oInstructions pygame.image.loadimagesinstructionsjpg # Initialize variables ballList for oBall in range NBALLS: # create a ball object for each ball oBall Ballwindow WINDOWWIDTH, WINDOWHEIGHT ballList.appendoBall # append the new ball to the list of balls # Loop forever while True: # Check for and handle events for event in pygame.event.get: if event.type pygame.QUIT: pygame.quit sysexit # Do any "per frame" actions for oBall in ballList: oBall.update # tell each ball to update itself # Clear the screen before drawing it again window.fillBLACK # Draw the screen elements window.blitoInstructions for oBall in ballList: oBall.draw # tell each ball to draw itself # Update the screen pygame.display.update # Slow things down a bit clock.tickFRAMESPERSECOND # make PyGame wait the correct amount #:::::: Ball.py import pygame from pygame.locals import import random # BALL CLASS class Ball: def initself window, windowWidth, windowHeight: self.window window # remember the window, so we can draw later self.windowWidth windowWidth self.windowHeight windowHeight self.ballImage pygame.image.loadimagesballpng # A rect is made up of x y width, height ballRect self.ballImage.getrect self.width ballRect self.height ballRect self.maxWidth windowWidth self.width self.maxHeight windowHeight self.height # Pick a random starting position self.x random.randrange self.maxWidth self.y random.randrange self.maxHeight # Choose a random speed in both the x and y directions self.xSpeed random.randrange self.ySpeed random.randrange def updateself: # check for hitting a wall. If so change that direction if selfx or selfx self.maxWidth: self.xSpeed self.xSpeed if selfy or selfy self.maxHeight: self.ySpeed self.ySpeed # update the balls x and y based on the speed in two directions self.x self.x self.xSpeed self.y self.y self.ySpeed def drawself: self.window.blitselfballImage, selfx self.y Drop.py # make the drop class here. the final should look like this, multiple balls and drops, according to how many times the user press press b for a new ball or d for a new drop. i a Diopey cleck prosese. tiese,clech set the speed freses per second a a Loed assetst langets sconds, etc. os Initistise wariables tertusiat in if for ebell in rasgeto, nusmushi create a betl ebject for each bell ceep tereverD Mah HWApy is Leep ferever allie Truet Check for and handle events for event in crgase, erentget: If event, type pyeme,uts: Byereequit B Do any "per frane" octions for obblt in batusists etathaspate o tell each balt to wedate itself Clear the screen before drawing it agath indec, usutelsox viedextutefinstructicns, E tor atell in bethiots O Usdete the serees prenedisplay Preteredisplay upiote check for hatting a wall, If so change that difectienec or a ballipy & ball Choose a randan speed in both the x and y directions setf.xpeed randos, randrange def yodoteself: If check for sakting a wath. If so change that direction If setf, x or setf x sotf. saxatidth t wadate the batls x and y based on the speed in twe directions
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started