Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, time, random, math, logging
- class Player(object):
- # surface = The surface that the Player will draw itself to
- # x = Initial x coordinate of the center of the sprite
- # y = Initial y coordinate of the center of the sprite
- def __init__(self, surface, x, y):
- # width and height of player sprite
- self.width = 50
- self.height = 50
- # Movement speed in pixels per frame
- self.moveSpeed = 8
- # Prevent speed-up when moving diagonally
- self.diagMove = int(self.moveSpeed * math.sin(math.pi / 4)) + 1
- if self.moveSpeed < 4:
- self.diagMove - 1 # Adjust for low moveSpeed
- # self.topLeft[0] = x coordinate of top left corner
- # self.topLeft[1] = y coordinate of top left corner
- self.topLeft = [x - self.width // 2, y - self.height // 2]
- # The surface that the player sprite will be drawn to
- self.displaySurface = surface
- self.displaySurfaceWidth = surface.get_width()
- self.displaySurfaceHeight = surface.get_height()
- def getLeftX(self):
- return self.topLeft[0]
- def getRightX(self):
- return self.topLeft[0] + self.width
- def getTopY(self):
- return self.topLeft[1]
- def getBottomY(self):
- return self.topLeft[1] + self.height
- def getWidth(self):
- return self.width
- def getHeight(self):
- return self.height
- # Returns player movement speed
- def getMoveSpeed(self):
- return moveSpeed
- # Set player movement speed
- def setMoveSpeed(self, speed):
- self.moveSpeed = speed
- # Draw the player sprite to the surface passed into __init__
- def draw(self):
- self.playerSurface = pygame.Surface((self.width, self.height))
- self.playerSurface.fill((255, 0, 0))
- self.displaySurface.blit(self.playerSurface, self.getRect())
- # Returns a rectangle at the position and size of the player
- def getRect(self):
- self.playerRect = pygame.Rect(self.topLeft[0], self.topLeft[1], self.width, self.height)
- return self.playerRect
- # Move player sprite based on boolean inputs
- def move(self, up: bool, left: bool, down: bool, right: bool):
- # Diagonal motion
- if up and right and not (down or left):
- self.topLeft[0] += self.diagMove
- self.topLeft[1] -= self.diagMove
- elif up and left and not (down or right):
- self.topLeft[0] -= self.diagMove
- self.topLeft[1] -= self.diagMove
- elif down and right and not (up or left):
- self.topLeft[0] += self.diagMove
- self.topLeft[1] += self.diagMove
- elif down and left and not (up or right):
- self.topLeft[0] -= self.diagMove
- self.topLeft[1] += self.diagMove
- else: # Non-diagonal motion (stationary if opposite directions are pressed)
- if up and not down:
- self.topLeft[1] -= self.moveSpeed
- elif left and not right:
- self.topLeft[0] -= self.moveSpeed
- elif down and not up:
- self.topLeft[1] += self.moveSpeed
- elif right and not left:
- self.topLeft[0] += self.moveSpeed
- # Prevent movement beyond window bounds
- if self.getLeftX() <= 0:
- self.topLeft[0] = 0
- if self.getRightX() >= self.displaySurfaceWidth:
- self.topLeft[0] = self.displaySurfaceWidth - self.width
- if self.getTopY() <= 0:
- self.topLeft[1] = 0
- if self.getBottomY() >= self.displaySurfaceHeight:
- self.topLeft[1] = self.displaySurfaceHeight - self.height
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement