Advertisement
cookertron

Victor Platform Demo - Python Pygame

Jan 22nd, 2022
1,226
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. from pygame import Rect
  3.  
  4. GRAVITY = 0.1
  5.  
  6. PLAYER_RADIUS = 20
  7.  
  8. WHITE = (255, 255, 255)
  9. BLACK = (0, 0, 0)
  10.  
  11. pygame.init()
  12. PDR = Rect(0, 0, 1280, 720)
  13. PDS = pygame.display.set_mode(PDR.size)
  14.  
  15. class platform:
  16.     def __init__(s, position, width):
  17.         s.rect = Rect(position, (width, 20))
  18.    
  19.     def draw(s):
  20.         global PDS, WHITE
  21.  
  22.         pygame.draw.rect(PDS, WHITE, s.rect)
  23.  
  24. class player:
  25.     def __init__(s, position):
  26.         s.pos = position
  27.         s.velocity = 0
  28.         s.jumping = True
  29.  
  30.     def draw(s):
  31.         global PDS, PLAYER_RADIUS
  32.  
  33.         pygame.draw.circle(PDS, WHITE, s.pos, PLAYER_RADIUS)
  34.  
  35.     def update(s):
  36.         global PLATFORMS, GRAVITY, PLAYER_RADIUS
  37.  
  38.         s.previous_pos = (s.pos[0], s.pos[1])
  39.         s.pos[1] += s.velocity
  40.         s.velocity += GRAVITY
  41.  
  42.         k = pygame.key.get_pressed()
  43.         if k[pygame.K_SPACE] and not s.jumping:
  44.             s.velocity = -7
  45.             s.jumping = True
  46.         if k[pygame.K_LEFT]:
  47.             s.pos[0] -= 3
  48.         if k[pygame.K_RIGHT]:
  49.             s.pos[0] += 3
  50.  
  51.         if s.velocity >= 0:
  52.             for p in PLATFORMS:
  53.                 if s.pos[0] >= p.rect.left and s.pos[0] <= p.rect.right:
  54.                     if s.previous_pos[1] + PLAYER_RADIUS <= p.rect.top and s.pos[1] + PLAYER_RADIUS > p.rect.top:
  55.                         s.pos = [s.pos[0], p.rect.top - PLAYER_RADIUS]
  56.                         s.velocity = 0
  57.                         s.jumping = False
  58.  
  59. PLATFORMS = [platform((0, PDR.h - 20), PDR.w),
  60.     platform((PDR.centerx - 75, PDR.bottom - 200), 150),
  61.     platform((PDR.centerx - 75, PDR.bottom - 400), 150),
  62.     ]
  63. MARIO = player([PDR.centerx, PLAYER_RADIUS])
  64.  
  65. exit_demo = False
  66.  
  67. while not exit_demo:
  68.     events = pygame.event.get()
  69.     for event in events:
  70.         if event.type == pygame.KEYUP:
  71.             if event.key == pygame.K_ESCAPE:
  72.                 exit_demo = True
  73.  
  74.     PDS.fill(BLACK)
  75.  
  76.     for p in PLATFORMS:
  77.         p.draw()
  78.     MARIO.draw()
  79.     MARIO.update()
  80.  
  81.     pygame.display.update()
  82.     pygame.time.Clock().tick(120)
  83.  
  84. pygame.quit()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement