Answered step by step
Verified Expert Solution
Link Copied!

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: 1) Starts by not showing any balls 2) Every time the user types the letter b, the program should create a new ball. 3) 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 (e.g., between 3 and 6 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 re-randomize the X position if you wish when the drop recycles). The art is available in the "images" folder. 4) Every time the user types the letter d, the program should create a new drop. Code files: Main HW4.py # pygame demo using Ball class, bounce many balls # 1- Import packages import pygame from pygame.locals import * import sys import random from Ball import * # bring in the Ball class code # 2- Define constants BLACK =(0,0,0) WHITE =(255,255,255) WINDOW_WIDTH =640 WINDOW_HEIGHT =480 FRAMES_PER_SECOND =30 N_BALLS =3 # 3- Initialize the world pygame.init() window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) clock = pygame.time.Clock() # set the speed (frames per second) # 4- Load assets: image(s), sounds, etc. oInstructions = pygame.image.load('images/instructions.jpg') # 5- Initialize variables ballList =[] for oBall in range(0, N_BALLS): # create a ball object for each ball oBall = Ball(window, WINDOW_WIDTH, WINDOW_HEIGHT) ballList.append(oBall) # append the new ball to the list of balls # 6- Loop forever while True: # 7- Check for and handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 8- Do any "per frame" actions for oBall in ballList: oBall.update() # tell each ball to update itself # 9- Clear the screen before drawing it again window.fill(BLACK) # 10- Draw the screen elements window.blit(oInstructions,(85,430)) for oBall in ballList: oBall.draw() # tell each ball to draw itself # 11- Update the screen pygame.display.update() # 12- Slow things down a bit clock.tick(FRAMES_PER_SECOND) # make PyGame wait the correct amount #/////::://////////////:::///// Ball.py import pygame from pygame.locals import * import random # BALL CLASS class Ball(): def __init__(self, window, windowWidth, windowHeight): self.window = window # remember the window, so we can draw later self.windowWidth = windowWidth self.windowHeight = windowHeight self.ballImage = pygame.image.load("images/ball.png") # A rect is made up of [x, y, width, height] ballRect = self.ballImage.get_rect() self.width = ballRect[2] self.height = ballRect[3] self.maxWidth = windowWidth - self.width self.maxHeight = windowHeight - self.height # Pick a random starting position self.x = random.randrange(0, self.maxWidth) self.y = random.randrange(0, self.maxHeight) # Choose a random speed in both the x and y directions self.xSpeed = random.randrange(1,4) self.ySpeed = random.randrange(1,4) def update(self): # check for hitting a wall. If so, change that direction if (self.x <0) or (self.x > self.maxWidth): self.xSpeed =-self.xSpeed if (self.y <0) or (self.y > 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 draw(self): self.window.blit(self.ballImage, (self.x, 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 3 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 ?-33 is 6- Leep ferever 34 allie Truet -7- Check for and handle events for event in crgase, erent-get(): If event, type \(=\) pyeme,0uts: Byeree-quit()- B - Do any "per frane" octions for obblt in batusists etathaspate(1 o tell each balt to wedate itself -3- Clear the screen before drawing it agath -indec, 1usutelsox) viedextutefinstructicns, (E8,430)) tor atell in bethiots O 13- Usdete the serees prene-display 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.x5peed = randos, randrange \((1,4)\) def yodote(self): If check for sakting a wath. If so, change that direction If \((\) setf, \( x<0)\) 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design A Practical Approach

Authors: Marilyn Campbell

1st Edition

1587193175, 978-1587193170

More Books

Students also viewed these Databases questions

Question

5. Understand how cultural values influence conflict behavior.

Answered: 1 week ago