![]() The variable frame keeps track of how many times the main game loops round. The intfunction is used to round down the scaled coordinates so that there are no decimal places: it converts the coordinates into integer values. Lines 59 and 60 scale dxand dydown from between 0-99 to being between 0-4. This is so that we can keep track of the speed and position of the alien with more precision so that it appears to move more smoothly. The micro:bit only has an LED screen that’s 5×5 pixels in size but we’re using dimensions for dx and dy (the doodle jump alien’s coordinates) between 0 and 99 rather than 0 and 4. # scale the doodle coordinates down to fit on the micro:bit screen We use the number 3 (where 9 is full brightness and 0 and completely off) to represent a platform that the doodle jump alien can jump from. This image contains numbers which represent the brightness of each LED. Line 56 displays the image background to the LED screen on the micro:bit. Once you’ve defined a function, you can call it as many times as you like, so it’s a great way of making your code more efficient because you can avoid repeating the same lines of code all over your program. The function definition (that’s what def stands for on line 10) tells python what the function should do, but it doesn’t actually do it until the function is called. Here, we call the function check_boundsthat we defined on lines 10-15: # force a value to be between a minimum and maximumĪ function is a section of code that we can use more than once that calculate something that we can use or store somewhere else in the program. ![]() We’ve already talked about lines 51 -53 on the previous page. # make sure doodle x and y coordinates are between 0 and 99 Can you find a value that works better for the game than 5? Try changing the amount that dxis increased or decreased by on lines 45 and 49. Try changing the values and see what happens ![]() If so, the x coordinate of the alien ( dx ) is decreased or increased respectively. Lines 44 and 48 check to see if button A or button B are currently being pressed. When this happens on line 86, the y coordinate of the alien decreases, which moves the alien up towards the top of the screen, before gravity (or lines 39 and 40) bring it back down again. This means that all the code has to do in order to make the alien jump is set speed_y to a negative number. Line 41 increases the y coordinate of the alien by the value stored in speed_y . Lines 39-40 make the alien accelerate downwards up to a maximum speed of 5 going down. The variable speed_ystores the speed that the doodle jump alien is falling down. The while True loop on line 36 makes all of the lines that are indented beneath it (lines 38 – 96) repeat until line 93 breaks out of the loop with break when the game is over: # model gravity: increase speed downwards until it reaches terminal velocity If background.get_pixel(dx_scaled, dy_scaled) = 3 and speed_y > 2:ĭt_pixel(dx_scaled, dy_scaled, 9) Platform_end = random.randint(platform_start + 1, 4)įor x in range(platform_start, platform_end): # model gravity: increase speed downwards until it reaches terminal velocity This page breaks down that main loop and explains what each part does: # keep looping Most games have a main loop that keeps repeating whilst the game is being played. ![]() How does it work: Part 2: The main game loop ![]()
0 Comments
Leave a Reply. |