Create a Player object in Pygame

Hello, sorry for not posting any article yesterday because I am very busy with my offline business but today I have a whole day to write more articles and I am going to create three articles for today. First of all, do you people still remember I have mentioned that I will create one single Game Manager to manage all the game objects on the game scene in my previous article? Yes we are going to do that soon, but before we doing that we will need to first create a new class for each game objects on the game scene. In this article we will start to turn our first game object into a class by it’s own, we will create a class for the Player object.

This player object will receive the scene object from the main pygame file which it can then use to blit the player graphic on the scene, besides that it can also receive the key down event and the key up event so it can move the player object to a new location on the game scene with it’s update position method.

Below is the entire Player class, more methods will be added in the future but for now here is it.

from pygame.locals import *
from GameSprite import GameSprite
from pygame import math as mt
import pygame

class Player(object):

    def __init__(self, scene):
        self.image = 'Asset/player.png'
        self.scene = scene
        self.width = 40
        self.height = 40
        self.direction_x = 0
        self.direction_y = 0
        self.rect = Rect(0, 0, self.width, self.height)
        self.sprite = GameSprite(self.image, self.rect)
        self.sprite_surface = self.sprite.getImage()  # get the player sprite surface
        self.bwidth, self.bheight = 660, 660
        self.pos = mt.Vector2(self.bwidth / 2, self.bheight / 2)  # initialize the position of the player sprite
        self.draw_pos = mt.Vector2(self.pos.x, self.pos.y)

        pygame.display.set_icon(self.sprite_surface) # use the same player surface object as the icon for the game window

    def setX(self, _x):

        # set new x position and detect the boundary on the game scene
        self.direction_x = _x

    def setY(self, _y):

        # set new y position and detect the boundary on the game scene
        self.direction_y = _y

    def update(self):

        if(self.direction_x == -0.1):
            if(self.pos.x > 0):
                self.pos.x += self.direction_x
        elif(self.direction_x == 0.1):
            if(self.pos.x + self.width <= self.bwidth):
                self.pos.x += self.direction_x
        if(self.direction_y == -0.1):
            if (self.pos.y > 0):
                self.pos.y += self.direction_y
        elif (self.direction_y == 0.1):
            if (self.pos.y + self.height <= self.bheight):
                self.pos.y += self.direction_y

        self.draw_pos = mt.Vector2(self.pos.x, self.pos.y)

    def get(self):
        return (self.pos.x, self.pos.y)


    def draw(self):
        self.scene.blit(self.sprite_surface,  self.draw_pos)

As you can see, it also has an extra get method which will return the new x and y position of the player object to be used by the missile manager to position the new missile. That is it, we will now move on to create the background object class in the next article.