Rotate the mana object

In this chapter, we will continue to develop the mana object. We will rotate the mana object on each update. If you still remember that we have created an update method under the mana class then in this chapter we will update the rotation angle position of that mana object accordingly on each update under this method. We will also create the get height and get width method so that mana object will return both the width and the height of that mana object that will be used by the mana sprite class to calculate the new position of the mana object on the game scene before drawing it. We also used the clock object from the pygame.time module to control the speed of rotation.

from pygame.locals import *
from GameSprite import GameSprite
import pygame

class Mana(object):

    def __init__(self, original_position_list):

        self.count = 0

        #original location for mana object

        self.x = original_position_list[0]
        self.y = original_position_list[1]

        self.alive = True

        # the width and height of mana
        self.w = 64
        self.h = 64

        self.clock = pygame.time.Clock()
        self.rotation = 0. # the rotation angle of the mana
        self.rotation_speed = 360.  # 360 degrees per second
        self.rotation_direction = 1.0

        self.mana_image_sprite_right = 'Asset/right.png'


    def create_surface(self, mana_image_sprite):

        self.mana_rect = Rect(7 * 64, 0, 64, 64)
        self.sprite = GameSprite(mana_image_sprite, self.mana_rect)
        self.sprite_surface = self.sprite.getImage()
        self.rotated_surface = self.sprite_surface # the mana rotate surface which will be returned by the mana object

    def update(self):

        self.rotated_surface = pygame.transform.rotate(self.sprite_surface, self.rotation ) # Return the rotated mana surface object
        self.w, self.h = self.rotated_surface.get_size()

        self.time_passed_seconds = (self.clock.tick())/ 2600.0

        self.rotation += self.rotation_direction * self.rotation_speed * self.time_passed_seconds

    def get_mana_surface(self):

        return self.rotated_surface

    def get_width(self):

        return self.w

    def get_height(self):

        return self.h

Finally, we will edit the mana sprite file accordingly to draw the rotating mana object on the game scene.

from pygame import math as mt
from Mana import Mana
from ManaObject import ManaObject

class ManaSprite(object):

    def __init__(self, level, scene):

        self.row = 10
        self.column = 10
        self.scene = scene # game scene instance = 64 # image width and height

        self.level = level # current game level

        self.total_mana_list = []

        self.mana_object = ManaObject()


    def create_mana(self, level):

        self.mana_original_position = self.mana_object.get_original_position(level)

        i = 0
        if(level == 1):
            while(i < 4):
                i += 1

    def update(self):

        for mana in self.total_mana_list:

    def draw(self):
        self.counter = 0
        for row in range(self.row):

            for column in range(self.column):

                if(self.mana_list[row][column] == 1):

                    self.mana = self.total_mana_list[self.counter]
                    if(self.mana.alive == True):
                        self.draw_pos = mt.Vector2(self.mana.x - self.mana.get_width()/7, self.mana.y - self.mana.get_height()/7)  # the position of the image on game scene
                        self.scene.blit(self.mana.get_mana_surface(), self.draw_pos)  # draw mana object
                    self.counter += 1

    def get_level(self, level):
        self.mana_list = self.mana_object.get_object_list(level)

    def get_mana_list(self):
        return self.total_mana_list

Here is the product of the above program.

Leave a Reply

Your email address will not be published. Required fields are marked *