This is the next challenge, we shall use these cannons as something the rockets cannot collide against. We already had a moving rocket turning 90 degrees on mouse clicks in Part 12, now we shall build on it and create cannons (circles). Advertisements
pygame has something called sprites, actors in the game. It is to facilitate easy drawing, creation, and destruction of the actors. The actors are objects(instances of the class) technically. And the classes inherit (inherit all features from the parent and have some more of their own) from pygame.sprite.Sprite Now, to build upon the game, let… Continue reading Part 12: Objects as pygame Sprites
Now to leverage the power of Object oriented programming. Suppose we have multiple walls(rectangles), where shall we store the Pygame.Rect’s that we need? Will we write 100 such lines for 100 walls? circlerect.colliderect(wallrect) The answer is we create a “Wall” class which is akin to a cookie cutter mould for all the walls (it is… Continue reading Part 11: OOP Introduced – Detect collision for multiple objects
In this exercise, we collide the moving circle that we made in Part 8 with a wall (a rectangle). The moment, the circle touches the wall(rectangle), we shall print “Yay! Collision” on the interpreter shell as seen in the picture.
How do we recognize the WASD keys? You are right! Just like any other event.
The WASD and arrow keys can be used to control the shapes we draw. How do we recognize these arrow keys? By recognizing and handling the events.
To run the code, press the space key, a circle is drawn where the mouse has been positioned. Move the mouse and press the space key and do the same.