Advertisement
Guest User

Untitled

a guest
Sep 5th, 2016
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. import pygame, time, random, math, logging
  2.  
  3. class Player(object):
  4.  
  5. # surface = The surface that the Player will draw itself to
  6. # x = Initial x coordinate of the center of the sprite
  7. # y = Initial y coordinate of the center of the sprite
  8. def __init__(self, surface, x, y):
  9.  
  10. # width and height of player sprite
  11. self.width = 50
  12. self.height = 50
  13.  
  14. # Movement speed in pixels per frame
  15. self.moveSpeed = 8
  16. # Prevent speed-up when moving diagonally
  17. self.diagMove = int(self.moveSpeed * math.sin(math.pi / 4)) + 1
  18. if self.moveSpeed < 4:
  19. self.diagMove - 1 # Adjust for low moveSpeed
  20.  
  21. # self.topLeft[0] = x coordinate of top left corner
  22. # self.topLeft[1] = y coordinate of top left corner
  23. self.topLeft = [x - self.width // 2, y - self.height // 2]
  24.  
  25. # The surface that the player sprite will be drawn to
  26. self.displaySurface = surface
  27. self.displaySurfaceWidth = surface.get_width()
  28. self.displaySurfaceHeight = surface.get_height()
  29.  
  30.  
  31. def getLeftX(self):
  32. return self.topLeft[0]
  33.  
  34. def getRightX(self):
  35. return self.topLeft[0] + self.width
  36.  
  37. def getTopY(self):
  38. return self.topLeft[1]
  39.  
  40. def getBottomY(self):
  41. return self.topLeft[1] + self.height
  42.  
  43. def getWidth(self):
  44. return self.width
  45.  
  46. def getHeight(self):
  47. return self.height
  48.  
  49. # Returns player movement speed
  50. def getMoveSpeed(self):
  51. return moveSpeed
  52.  
  53. # Set player movement speed
  54. def setMoveSpeed(self, speed):
  55. self.moveSpeed = speed
  56.  
  57. # Draw the player sprite to the surface passed into __init__
  58. def draw(self):
  59. self.playerSurface = pygame.Surface((self.width, self.height))
  60. self.playerSurface.fill((255, 0, 0))
  61. self.displaySurface.blit(self.playerSurface, self.getRect())
  62.  
  63. # Returns a rectangle at the position and size of the player
  64. def getRect(self):
  65. self.playerRect = pygame.Rect(self.topLeft[0], self.topLeft[1], self.width, self.height)
  66. return self.playerRect
  67.  
  68. # Move player sprite based on boolean inputs
  69. def move(self, up: bool, left: bool, down: bool, right: bool):
  70.  
  71. # Diagonal motion
  72. if up and right and not (down or left):
  73. self.topLeft[0] += self.diagMove
  74. self.topLeft[1] -= self.diagMove
  75. elif up and left and not (down or right):
  76. self.topLeft[0] -= self.diagMove
  77. self.topLeft[1] -= self.diagMove
  78. elif down and right and not (up or left):
  79. self.topLeft[0] += self.diagMove
  80. self.topLeft[1] += self.diagMove
  81. elif down and left and not (up or right):
  82. self.topLeft[0] -= self.diagMove
  83. self.topLeft[1] += self.diagMove
  84.  
  85. else: # Non-diagonal motion (stationary if opposite directions are pressed)
  86. if up and not down:
  87. self.topLeft[1] -= self.moveSpeed
  88. elif left and not right:
  89. self.topLeft[0] -= self.moveSpeed
  90. elif down and not up:
  91. self.topLeft[1] += self.moveSpeed
  92. elif right and not left:
  93. self.topLeft[0] += self.moveSpeed
  94.  
  95. # Prevent movement beyond window bounds
  96. if self.getLeftX() <= 0:
  97. self.topLeft[0] = 0
  98. if self.getRightX() >= self.displaySurfaceWidth:
  99. self.topLeft[0] = self.displaySurfaceWidth - self.width
  100. if self.getTopY() <= 0:
  101. self.topLeft[1] = 0
  102. if self.getBottomY() >= self.displaySurfaceHeight:
  103. self.topLeft[1] = self.displaySurfaceHeight - self.height
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement