![]() We will need to change our draw() function and our update() function to respond to the gameStatus variable. In this game we will have three different states to the game stored in our variable gameStatus : We may as well test for collisions using the method colliderect() in our updateTrack() function. ![]() We need to make sure that our car doesn’t touch the track Actors. The only problem is that we can move the car over the track barriers and we want to keep the car inside them with some collision detection. If we run our code at the moment, we should see a track snaking down towards the car. Please refer to the zip file below named "RaceGameDemo". MakeTrack() # create a new track at the top of the screen If randint(0,4) = 1: trackDirection = not trackDirection If trackDirection = True: trackPosition -= 16 If trackDirection = False: trackPosition += 16 Global trackCount, trackPosition, trackDirection, trackWidth,SPEED trackDriction = Falseįrom random import randint # import the randint class from random module The track pieces are created by random numbers so each play is different. Let’s write a new function called updateTrack(), This function updates where the track blocks will appear. The next thing that we need to do is to move the sections of track down the screen towards the car. ![]() TrackRight.append(Actor("bare", pos = (trackPosition + trackWidth, 0))) TrackLeft.append(Actor("bare", pos = (trackPosition-trackWidth, 0))) Global trackCount, trackLeft, trackRight, trackPosition, trackWidth TrackWidth = 150 # width between left and right barriesĭef makeTrack(): # function to make one barrie at the left and right TrackCount = 0 # count the number of barries TrackRight = # list to store right barries Then let's make a new function called makeTrack(), The function will add one track Actor on the left and one the right, both using the image bare.png in our images folder. We will also need to set up a few more variables for the track. We will need to make some lists to keep track of the Actors we create. We are going to build our track out of Actors, one row at a time. Now that we have a car that we can steer, we need a track for it to drive on. Test your program to make sure this is working. The full program will be like this : #!/usr/bin/python3 You can set its position on the screen by typing : car.pos = 350, 560Īfter that in our draw() function we can type car.draw() # draw our race car at its defined position It could be a GIF or a JPG file, but it is recommended that your images are PNG files as that file type provides good-quality images with transparencies. So our Actor would be looking for an image file in the images folder called racecar.png. In Pygame Zero our images need to be stored in a directory called images, next to our program file. We can load an Actor by typing in the top of the program: car = Actor("racecar") Now that we have our stage set, we can create our Actors, they are dynamic objects in Pygame Zero. Then open a Terminal window, go to your file location and type. ("Hello World!", (270, 320), # draw "Hello World!"įirst of all you need to save your program file and give it a name. Screen.fill((128, 128, 128)) # fill the screen with RGB colour Pygame Zero follows the same formatting rules as Python, so you will need to take care to indent your code correctly. We can write this function into our program the same as we would normally define a function in Python. Pygame zero provides predefined functions to handle the game loop normally performs : The draw() Function The size of your window,there are two predinied variables you can set, if you include WIDTH = 700 HIGH = 800. By default, the Pygame Zero window opens at the size of 800 pixels wide by 600 pixels high.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |