55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
import pygame
|
|
|
|
|
|
class Player:
|
|
"""
|
|
Creates new instance of player or NPC.
|
|
|
|
:param surface: Surface to draw player onto
|
|
:param x: Horizontal position of player
|
|
:return: Returns nothing
|
|
"""
|
|
|
|
def __init__(self, surface: pygame.surface, x):
|
|
self._surface = surface
|
|
self._image = pygame.image.load('PaddleA.png').convert_alpha()
|
|
self._rect = self._image.get_rect()
|
|
self.x = x
|
|
self.y = round(((self._surface.get_height() / 2) - self._rect.height / 2) / 10, 0) * 10
|
|
self.vec = 5
|
|
self.player = None
|
|
self.dist_bot = 0
|
|
self.dist_top = 0
|
|
|
|
def use_alternate_sprite(self):
|
|
""" Changes out default sprite for alternate colorized version. """
|
|
self._image = pygame.image.load('PaddleB.png').convert_alpha()
|
|
|
|
def reverse_player(self):
|
|
""" Rotates player sprite 180°. """
|
|
|
|
self._image = pygame.transform.rotate(self._image, 180)
|
|
|
|
def draw(self):
|
|
""" Draw player to screen and update positions relative to screen area. """
|
|
|
|
# self.player = pygame.draw.circle(self._surface,
|
|
# Colors.White,
|
|
# (self.x, self.y),
|
|
# 25)
|
|
|
|
self.player = self._surface.blit(self._image, (self.x, self.y))
|
|
# Calculate player position relative to screen area
|
|
self.dist_bot = (self._surface.get_height() - self._rect.y - self.y) - self._rect.height
|
|
self.dist_top = 0 + self._rect.y + self.y
|
|
|
|
def control(self):
|
|
""" Handover physical controls to player. """
|
|
|
|
keys = pygame.key.get_pressed()
|
|
# print(f"{self.dist_top} {self.dist_bot}")
|
|
if keys[pygame.K_w] and self.dist_top > 0:
|
|
self.y -= self.vec
|
|
if keys[pygame.K_s] and self.dist_bot > 0:
|
|
self.y += self.vec
|