Breakout paddle game
How to Play: Bounce and destroy! Launch the ball towards the bricks to destroy them, some bricks are destroyed after being hit once, and others are tougher. When you destroy bricks, there is a chance to get power ups to help you clear the level, and some negative effects that will make it harder to do so.
Beware of the bombs, they will destroy your platform and cost you a life, avoid them at all cost. The game can be played on PC and mobile. Use your mouse or touch your screen to aim and release the ball, bounce it back with the platform. Once you load the game press the Play button and select a level to begin playing.
Clear all the levels and get all the stars. Pass in the color of the paddle, its width and height. Set the background color and set it to be transparent. Surface [ width , height ]. Draw the paddle a rectangle! Fetch the rectangle object that has the dimensions of the image.
Let's import the Paddle Class from paddle import Paddle. Let's import the Paddle Class. Import the pygame library and initialise the game engine import pygame Let's import the Paddle Class from paddle import Paddle pygame. For now we only have 2 sprites! Import the pygame library and initialise the game engine. Define some colors. Open a new window. This will be a list that will contain all the sprites we intend to use in our game. Create the Paddle. Add the paddle to the list of sprites.
When the ball hits a brick it will disapear The ball will have 3 lives. Note: the trajectory of the ball will change based on where it hits the brick, the paddle or the sides of the screen. The Ball class extends an Ellipse class from the Macalester Kilt Graphics to create a ball object with variables for radius, center x and y coordinates, and initial speed.
Within Ball class there are several methods that handle ball movement, including individual methods for handling collision with the walls, bricks, or paddle. So the method for hitting bricks calls on a method from a different class, Brick Manager, to remove them.
Since Java recognizes Brick and Paddle as separate types, it makes handling these collisions easier. We use a separate Paddle class which extends Rectangle— another from the Macalester Kilt Graphics library— to create a paddle object. There are only two methods in Paddle: one moves the paddle horizontally and the other returns a boolean based on whether a graphical object at any given point on the canvas is a paddle object.
The third of our simple classes is Brick, which creates a single brick object. Similar to the paddle, a brick is a rectangle. There are two constructors in the Brick class. The first sets the width, height, and x and y coordinates on a brick object. The second constructor also takes in x and y coordinates, but instead of width and height, it takes in a canvas object.
The same pattern follows with the right cursor and the rightPressed variable. We've now set up the variables for storing the info about the pressed keys, event listeners, and relevant functions. Next we'll get into the code to use all of the things we just set up and to move the paddle on the screen. Inside the draw function, we will check if the left or right cursor keys are pressed when each frame is rendered.
Our code might look like this:. If the left cursor is pressed, the paddle will move seven pixels to the left, and if the right cursor is pressed, the paddle will move seven pixels to the right. This currently works, but the paddle disappears off the edge of the canvas if we hold either key for too long. We could improve that and move the paddle only within the boundaries of the canvas by changing the code like this:.
The paddleX position we're using will move between 0 on the left side of the canvas and canvas. Add the above code block into the draw function at the bottom, just above the closing curly brace. The only thing left to do now is call the drawPaddle function from within the draw function, to actually print it on the screen.
Add the following line inside your draw function, just below the line that calls drawBall :.
0コメント