The last line,, defines a starting position for the Rect. We will be using this in Collision Detection later on. This function is able to automatically create a rectangle of the same size as the image. This is instead done using the get_rect() function. Note, this does not define the borders for our Player Sprite. Next is the image.load() function to which we pass the file path of our image. You can look it up if you’re interested, else just include it the way we’ve shown above. super()._init_() is a whole different concept related to Classes in Python. Passing super().init() then calls the init() function of the Sprite class. Passing into the parameters,makes the Player Class it’s child class. Now, this doesn’t really apply to the Player Class, since most games will only have one player but it does apply to the Enemy Class as most games will have multiple enemies. The benefit of using classes here is that we can spawn multiple entities from the same block of code. Self.image = ("Player.png")Ībove you can see the Code for the Player Class. More about information about Pygame Rects can be found here.ĭISPLAYSURF = _mode((400,600)) The Width (Length) of the Rectangle in pixels.The Y co-ordinate of the upper left corner of the rectangle, also known as the Y co-ordinate from where the Rectangle begins.The X co-ordinate of the upper left corner of the rectangle, also known as the X co-ordinate from where the Rectangle begins.(The order is very important and must not be mixed up). ![]() ![]() The purpose of each value is shown below in order. Rather than taking a set of co-ordinates like a Circle or Line function does, it takes a tuple containing 4 values. This function takes a rather unique parameter, so we’ll discuss it here. As you can see, this function is used to draw rectangles in Pygame. One thing we didn’t explain earlier was the () function. It’s just there to you know what one looks like for when we use it later. First of all, in a program with no possible events or that can occur and no updating is required, we don’t need a game loop. The Game Loop that we included in this example above is purely for show. When run the above code produces the below window. # Setup a 300x300 pixel display with captionĭISPLAYSURF = _mode((300,300)) Likewise, center_point is the origin point of a circle. The line begins at one set of co-ordinates and ends at another. start_point and end_point represent a set of co-ordinates.width is an optional parameter that determines the size of the outline of the shape.For instance, for a rectangle you will pass a tuple with 4 co-ordinate pairs inside it. The pointlist parameter is a tuple containing co-ordinates or “points”.color parameter is the designated color of the assigned shape.surface parameter is the surface object on which pygame will draw the shape.You’ll get to see some of them being used later on in this tutorial. But we’ll give you a general overview of what these parameters are. We can’t go over each and every function in detail here, you can look up each of them up individually later on if you wish. Due to their similarities, they often share some parameters which they use to create the required shape. Sign up to +=1 for access to these, video downloads, and no ads.Drawing functions are used to create objects in Pygame. There exists 1 quiz/question(s) for this tutorial. That's really all there is to moving the car around! Remember that "x" is used to position our car image in the car function. Then, we use x_change to change our x variable: x += x_change Finally, if the key change is actually a KEYUP, meaning if we have released a key, then x_change is 0. If the KEYDOWN is a RIGHT arrow key, then x_change is 5. ![]() ![]() So, is there a key being pressed? If so, is that key a LEFT arrow key? If it is, then our x_change is -5. Later, we can combine all of this into 1 statement, but here I'd like to keep things as simple as possible. First, we're asking if the event is a keydown event, which means if there is any key being pressed. The main bit of logic here is: if event.type = pygame.KEYDOWN:Įasy enough if we break this down. So again, the changed code is around the # signs. If event.key = pygame.K_LEFT or event.key = pygame.K_RIGHT: _caption('A bit Racey')ĬarImg = ('racecar.png') GameDisplay = _mode((display_width,display_height)) Luckily for us, PyGame handles a lot of the event handling in the background, simply feeding us the events that have happened, allowing us to then do what we please. In this PyGame with Python 3 tutorial, we cover how to move our epic race car image about using key inputs from the user.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |