load ( 'standing.png' ) x = 50 y = 400 width = 40 height = 60 vel = 5 clock = pygame. set_caption ( "First Game" ) walkRight = walkLeft = bg = pygame. The full code should look like the following. You may notice when running the program that the background does not cover the entire screen, to fix this simply change there screen height to 480. To do this we are going to create a clock object using clock = () outside of our main loop and write clock.tick(27) inside our main loop. The last thing we need to do is change our framerate. blit ( char, ( x, y )) # If the character is standing still pygame. blit ( walkRight, ( x, y )) walkCount += 1 else : win. blit ( walkLeft, ( x, y )) # We integer divide walkCounr by 3 to ensure each walkCount += 1 # image is shown 3 times every animation elif right : win. blit ( bg, ( 0, 0 )) if walkCount + 1 >= 27 : walkCount = 0 if left : # If we are facing left win. NOTE: This is very complex, please refer to the video for more in depth explanations.ĭef redrawGameWindow (): # We have 9 images for our walking animation, I want to show the same image for 3 frames # so I use the number 27 as an upper bound for walkCount because 27 / 3 = 9. quit ()Īnd finally we can start working on drawing our character to the screen. get_pressed () if keys and x > vel : x -= vel left = True right = False elif keys and x = - 10 : y -= ( jumpCount * abs ( jumpCount )) * 0.5 jumpCount -= 1 else : jumpCount = 10 isJump = False redrawGameWindow () pygame. blit ( bg, ( 0, 0 )) # This will draw our background image at (0,0) pygame. load ( 'standing.png' ) x = 50 y = 50 width = 40 height = 60 vel = 5 isJump = False jumpCount = 10 left = False right = False walkCount = 0 def redrawGameWindow (): global walkCount win. Now we are going to set our left and right variables appropriately from inside the main loop. get_pressed () if keys and x > vel : x -= vel if keys and x = - 10 : y -= ( jumpCount * abs ( jumpCount )) * 0.5 jumpCount -= 1 else : jumpCount = 10 isJump = False redrawGameWindow () pygame. This means we are going to move our old drawing code that is inside the main loop into a function and make a few modifications. It is often good practice to do all of our drawing from within a function. # This goes outside the while loop, near the top of the program walkRight = walkLeft = bg = pygame.
0 Comments
Leave a Reply. |