Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | None | 0 0
  1. import os
  2. import random
  3. import pygame
  4.  
  5. acceleration = .3
  6. gravity = .4
  7. speedlimit = 3
  8. friction = .4
  9.  
  10. # Class for the player
  11. class Player(object):
  12.    
  13.     def __init__(self):
  14.         self.rect = pygame.Rect(32, 32, 16, 16)
  15.         self.falling = True
  16.         self.doublejump = False
  17.         self.xspeed = 0
  18.         self.yspeed = 0
  19.         self.moving = False
  20.  
  21.     def move(self, dx, dy):
  22.        
  23.         # Move each axis separately. Collision checked both times.
  24.         if dx != 0:
  25.             self.move_single_axis(dx, 0)
  26.         if dy != 0:
  27.             self.move_single_axis(0, dy)
  28.  
  29.     def jump(self, height):
  30.         if not self.falling:
  31.             self.yspeed = -height
  32.             self.falling = True
  33.         elif not self.doublejump and self.yspeed < 3:
  34.             self.yspeed = -height
  35.             self.doublejump = True
  36.    
  37.     def move_single_axis(self, dx, dy):
  38.        
  39.         # Move the rect
  40.         self.rect.x += dx
  41.         self.rect.y += dy
  42.  
  43.         # If you collide with a wall, move out based on velocity
  44.         for wall in walls:
  45.             if self.rect.colliderect(wall.rect):
  46.                 if dx > 0: # Moving right; Hit the left side of the wall
  47.                     self.rect.right = wall.rect.left
  48.                     if self.moving:
  49.                         self.xspeed = 0
  50.                 if dx < 0: # Moving left; Hit the right side of the wall
  51.                     self.rect.left = wall.rect.right
  52.                     if self.moving:
  53.                         self.xspeed = 0
  54.                 if dy > 0: # Moving down; Hit the top side of the wall
  55.                     self.rect.bottom = wall.rect.top
  56.                     if self.moving:
  57.                         self.yspeed = 0
  58.                         self.xspeed + (self.xspeed<0)*friction - (self.xspeed>0)*friction
  59.                     self.falling = False
  60.                     self.doublejump = False
  61.                 if dy < 0: # Moving up; Hit the bottom side of the wall
  62.                     self.rect.top = wall.rect.bottom
  63.                     if self.moving:
  64.                         self.yspeed = 0
  65.  
  66. # Nice class to hold a wall rect
  67. class Wall(object):
  68.    
  69.     def __init__(self, pos):
  70.         walls.append(self)
  71.         self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
  72.  
  73. # Initialise pygame
  74. os.environ["SDL_VIDEO_CENTERED"] = "1"
  75. pygame.init()
  76.  
  77. # Set up the display
  78. pygame.display.set_caption("TwipplyCraft!!1!!eleven!")
  79. screen = pygame.display.set_mode((320, 240))
  80.  
  81. clock = pygame.time.Clock() # SMEE, STOP THE TICKING!!
  82. walls = [] # List to hold the walls
  83. player = Player() # Create the player
  84.  
  85. # Holds the level layout in a list of strings.
  86. level = [
  87. "####################",
  88. "#                  #",
  89. "#                  #",
  90. "#                  #",
  91. "#                  #",
  92. "#                  #",
  93. "#                  #",
  94. "#                  #",
  95. "#                  #",
  96. "#                  #",
  97. "#                  #",
  98. "#                  #",
  99. "#                  #",
  100. "####################",
  101. ]
  102.  
  103. # Parse the level string above. W = wall
  104. x = y = 0
  105. for row in level:
  106.     for col in row:
  107.         if col == "#":
  108.             Wall((x, y))
  109.         x += 16
  110.     y += 16
  111.     x = 0
  112.  
  113. running = True
  114. while running:
  115.    
  116.     clock.tick(60)
  117.    
  118.     for e in pygame.event.get():
  119.         if e.type == pygame.QUIT:
  120.             running = False
  121.         if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
  122.             running = False
  123.    
  124.     # Move the player if an arrow key is pressed
  125.     key = pygame.key.get_pressed()
  126.     if key[pygame.K_LEFT] or key[pygame.K_a]:
  127.         if player.yspeed > speedlimit:
  128.             player.xspeed -= acceleration
  129.     if key[pygame.K_RIGHT] or key[pygame.K_d]:
  130.         if player.xspeed < speedlimit:
  131.             player.xspeed += acceleration
  132.     # Jump with space
  133.     if key[pygame.K_SPACE]:
  134.         player.jump(6)
  135.        
  136.     # Falling
  137.     if player.falling:
  138.         player.yspeed+=gravity
  139.  
  140.     #Update Position
  141.     player.move(player.xspeed, 0)
  142.     player.move(0, player.yspeed)
  143.     if not (player.xspeed == 0 or player.yspeed == 0): player.moving = True
  144.     else: player.moving = False
  145.    
  146.     # Draw the scene
  147.     screen.fill((0, 0, 0))
  148.     if len(walls) > 0:
  149.         for wall in walls:
  150.             pygame.draw.rect(screen, (255, 255, 255), wall.rect)
  151.     if player.rect: pygame.draw.rect(screen, (255, 200, 0), player.rect)
  152.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement