Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- WHITE = [255, 255, 255]
- BLACK = [0, 0, 0]
- SIZE = 600
- HALF = 300
- pygame.init()
- DS = pygame.display.set_mode((SIZE, SIZE))
- blockSize = 30 # size of the block
- blockX = int(HALF - blockSize / 2)
- blockY = SIZE - blockSize # starting position of the block
- jumpCounter = 0 # keep track of block's jump height
- velocityY = 0 # The velocity of the block -1 (jump up), 0 (still), 1 (falling)
- while True:
- e = pygame.event.get()
- k = pygame.key.get_pressed()
- if k[pygame.K_ESCAPE]: break # press escape to exit. Window controls don't work
- DS.fill(BLACK)
- pygame.draw.rect(DS, WHITE, (blockX, blockY, blockSize, blockSize))
- pygame.display.update()
- pygame.time.Clock().tick(60) # 60 FPS
- # block can only jump if the space bar is pressed and the jumpCounter is zero and the velocity is zero
- if k[pygame.K_SPACE] and not jumpCounter and not velocityY:
- jumpCounter = 100 # the height block will jump
- velocityY = -1 # jump up
- blockY += velocityY # add the velocity to the block's y position
- if jumpCounter: # if jumpCounter is greater than 0
- jumpCounter -= 1 # decrement the counter by 1
- if not jumpCounter and velocityY == -1: # if the jump counter is zero and the block is jumping up
- velocityY = 1 # change the velocity of the block so that it's falling
- jumpCounter = 100 # change the value of the jump counter back to 100 so the block falls the same distance as it did jumping up
- elif not jumpCounter and velocityY == 1: # if the jump counter is zero and the block is falling
- velocityY = 0 # set the velocity of the block to zero so that it's motionless. The block should now be able to jump again!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement