![]() ![]() We will also apply the bouncing algorithm to check if it needs to bounce against any of the four walls. (See lines 33 to 36) We will add this object to the all_sprites_list group of sprites. (See line 5) We will then create an object called ball using the Ball class. In the main.py file, we will first import the Ball class. # Fetch the rectangle object that has the dimensions of the image. # Set the background color and set it to be transparent # Pass in the color of the ball, its width and height. ![]() # Call the parent class (Sprite) constructor It derives from the "Sprite" class in Pygame.ĭef _init_(self, color, width, height): It moves (changes the (x,y) coordinates of) the ball using its velocity vector. The update() method of this class will be called for each frame of the main program loop. You will need to copy this code in a new Python file called ball.py. Let’s investigate how the velocity vector is affected when the sprite bounces against vertical and horizontal walls/edges.īelow is the code for the Ball class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |