mixster

ManFlardin

Apr 5th, 2010
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. #!user/bin/env python
  2.  
  3.  
  4. import sys, pygame
  5. from pygame.locals import *
  6.  
  7.  
  8. #--- Our Variables
  9. black = (0,0,0)
  10. size = (320, 240)
  11. speed = [0,5]
  12.  
  13. #--- Init pygame
  14. pygame.init
  15.  
  16. #--- Display
  17. pygame.display.set_caption("Rectangle Man: Story of a Square")
  18. screen = pygame.display.set_mode(size)
  19.  
  20. class Player(object):
  21.  
  22. def __init__(self):
  23. self.rect = pygame.Rect(0, 0, 16, 16)
  24.  
  25. def move(self, dx, dy):
  26. if dx != 0:
  27. self.move_single_axis(dx, 0)
  28. if dy != 0:
  29. self.move_single_axis(0, dy)
  30.  
  31. def move_single_axis(self, dx, dy):
  32. self.rect.x += dx
  33. self.rect.y += dy
  34.  
  35. for h in range(0, len(walls)):
  36. for m in range(0, len(walls[h])):
  37. if walls[h][m] == 1:
  38. wam = pygame.Rect(m * 16, h * 16, 16, 16)
  39. if self.rect.colliderect(wam):
  40. if dx > 0:
  41. self.rect.right = wam.left
  42. if dx < 0:
  43. self.rect.left = wam.right
  44. if dy > 0:
  45. self.rect.bottom = wam.top
  46. global jump
  47. jump = 0
  48. speed[1] = 0
  49. if dy < 0:
  50. self.rect.top = wam.bottom
  51. speed[1] = 0
  52.  
  53. class Wall(object):
  54.  
  55. def __init__(self, pos, tile):
  56. tNum = tile
  57.  
  58. #--- Variables
  59. clock = pygame.time.Clock()
  60. #ball = pygame.image.load("ball.jpg").convert()
  61. #buff = pygame.image.load("buff.jpg").convert()
  62. rects = [pygame.Rect(0,0, 1, size[1]), pygame.Rect(0, size[1] - 20, size[0], size[1]), pygame.Rect(size[0] - 1, 0, size[0], size[1])]
  63. jumping = False
  64. jump = 0
  65. jumpNum = 2
  66. inc = 1
  67. jHeight = 10
  68.  
  69. level = ["""WWWWWWWWWWWWWWWWWWWW.WS W.W WWWWWW W.W WWWW W W.W W WWWW W.W WWW WWWW W.W W W W W.W W W WWW WW.W WWW WWW W W W.W W W W W W.WWW W WWWWW W W.W W WW W.W W WWWW WWW W.W W W W W.WWWWWWWWWWWWWWWWWWWW."""
  70. ]
  71.  
  72. walls = []
  73. walls.append([])
  74. player = Player()
  75.  
  76. def getEvents():
  77. for event in pygame.event.get():
  78. if event.type == KEYDOWN:
  79. if event.key == K_UP:
  80. global jumping, jump
  81. if jumping == False:
  82. jumping = True
  83. jump += 1
  84. speed[1] = -jHeight
  85. global inc
  86. inc = 1
  87. if event.key == K_DOWN:
  88. player.rect = pygame.Rect(player.rect.x, player.rect.y + 8,16,8)
  89. pygame.draw.rect(screen, (0,0,0), ballbuff)
  90. if event.key == K_LEFT:
  91. speed[0] = -4
  92. if event.key == K_RIGHT:
  93. speed[0] = 4
  94. if event.type == KEYUP:
  95. if event.key == K_UP:
  96. inc = 2
  97. if event.key == K_DOWN and speed[1] > 0:
  98. player.rect = pygame.Rect(player.rect.x, player.rect.y - 8,16,16)
  99. if event.key == K_LEFT and speed[0] < 0:
  100. speed[0] = 0
  101. if event.key == K_RIGHT and speed[0] > 0:
  102. speed[0] = 0
  103. if event.type == pygame.QUIT:
  104. running = False
  105. sys.exit()
  106.  
  107. def loadMap(num):
  108. y = 0
  109. for row in level[num]:
  110. if row == ".":
  111. walls.append([])
  112. y += 1
  113. if row == "W":
  114. walls[y].append(1)
  115. if row == " ":
  116. walls[y].append(0)
  117. if row == "S":
  118. walls[y].append(2)
  119. player.rect.x = (len(walls) - 1) * 16
  120. player.rect.y = y * 16
  121.  
  122. loadMap(0)
  123. running = True
  124. while running:
  125. ballbuff = pygame.Rect(player.rect.x, player.rect.y, 16, player.rect.bottom - player.rect.top)
  126. getEvents()
  127.  
  128. player.move(speed[0], speed[1])
  129. if speed[1] < jHeight:
  130. if speed[1] < 0:
  131. speed[1] += inc
  132. else:
  133. speed[1] += 1
  134. if jumping:
  135. if (speed[1] >= 0) and (jump <= jumpNum - 1):
  136. jumping = False
  137. for i in range(0, len(rects)):
  138. pygame.draw.rect(screen, (0, 255, 0), rects[i])
  139. for i in range(0, len(walls)):
  140. for e in range(0, len(walls[i])):
  141. if walls[i][e] == 1:
  142. pygame.draw.rect(screen, (0, 0, 255), [e * 16, i * 16, 16, 16])
  143. if walls[i][e] == 0:
  144. pygame.draw.rect(screen, (0, 0, 0), [e * 16, i * 16, 16, 16])
  145. pygame.draw.rect(screen, (0,0,0), ballbuff) #used to be screen.blit...so change back for images
  146. pygame.draw.rect(screen, (255, 200, 0), player)
  147. clock.tick(30)
  148. pygame.display.flip()
Add Comment
Please, Sign In to add comment