Introduction
The Pygame library has support for sprites. A sprite is a two-layered picture essential for the bigger graphical scene. Regularly a sprite will be some sort of item in the scene that will be communicated with like a vehicle, frog, or minimal handyman fellow. The essential method for making a Rect object in Pygame is to pass two tuples into the Pygame. Rect() Class. The first tuple (left, top) addresses the place of the rect on the window, while the second tuple (width, level) addresses the components of the Rect.
Creating and controlling Sprites in pygame
Sprites are objects with various properties like level, width, variety, and so forth, and techniques like moving right, left, all over, hop, and so on. In this article, we are hoping to make an item in which clients have some control over that item and push it ahead, in reverse, up, and down utilizing bolt keys.
Creating Sprite
Let's first gander at our top notch, i.e., the class where our sprite is characterized; we will call that class Sprite. This Sprite class depicts its positions(x and y facilitates), aspect of an item, variety, and so forth. To start with, we will call our __init__() technique. It is known as a constructor for a class.
Code Creating Sprite class
import pygame
# GLOBAL VARIABLES
COLOR = (255, 100, 98)
SURFACE_COLOR = (167, 255, 100)
WIDTH = 500
HEIGHT = 500
# Object class
class Sprite(pygame.sprite.Sprite):
def __init__(self, color, height, width):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(SURFACE_COLOR)
self.image.set_colorkey(COLOR)
pygame.draw.rect(self.image,
color,
pygame.Rect(0, 0, width, height))
self.rect = self.image.get_rect()Presently, that the class has been made, we can create objects from the course. It empowers us to make however many articles we want to utilize a similar style. Presently we will make an item using our Class Sprite.
Syntax:
object = Sprite(RED,WIDTH,HEIGHT)Of course, the article will be on position (0,0), i.e., upper left of the screen. We can change the x and y properties of the article.
Syntax:
object.rect.x = valueobject.rect.y = valueWe can characterize n of sprites that we need to make; however, to comprehend, how about we rearrange. Here we have made a square shape sprite of specific aspects, on which we can perform various tasks on spirits like push ahead, in reverse, bounce, slow, speed up, and so on.
import pygame
import random
# GLOBAL VARIABLES
COLOR = (255, 100, 98)
SURFACE_COLOR = (167, 255, 100)
WIDTH = 500
HEIGHT = 500
# Object class
class Sprite(pygame.sprite.Sprite):
def __init__(self, color, height, width):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(SURFACE_COLOR)
self.image.set_colorkey(COLOR)
pygame.draw.rect(self.image,color,pygame.Rect(0, 0, width, height))
self.rect = self.image.get_rect()
pygame.init()
RED = (255, 0, 0)
size = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Creating Sprite")
all_sprites_list = pygame.sprite.Group()
object_ = Sprite(RED, 20, 30)
object_.rect.x = 200
object_.rect.y = 300
all_sprites_list.add(object_)
exit = True
clock = pygame.time.Clock()
while exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = False
all_sprites_list.update()
screen.fill(SURFACE_COLOR)
all_sprites_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()Output:

Controlling Sprite
Now we will see instructions to control the sprite, such as pushing ahead, in reverse, slow, or speed up, and a portion of the properties that the sprite ought to have. We will add occasion overseers to our program to answer keystroke occasions; when the player utilizes the bolt keys on the console, we will call our unadulterated techniques to move the article on the screen.
Functions Used
- move right(): This strategy takes contention pixels, which implies the number of pixels an article that ought to be carried in the correct course. It is essentially adding pixels to the ongoing x-direction of the article.
- Movement (): This strategy takes contention pixels, which implies the number of pixels an essay needs to be moved in the left course. It is fundamentally deducting pixels to the ongoing x-direction of the article.
- Move forward(): This strategy takes a contention speed, which implies the rate that will increment by the number of elements. It is essentially speeding up with the component of n in the y-heading of the article.
- Move backward(): This strategy takes a contention speed, and that implies by the number of elements the momentum that will diminish. It is essentially a diminishing rate with the aspect of n in the y-heading of the article.
- Let us first take a gander at the execution of a Sprite class, which assists us with making an item on our PyGame surface, and, alongside added four techniques that will help us push ahead, in reverse, both ways.
Code for sprite class
import pygame
# GLOBAL VARIABLES
COLOR = (255, 100, 98)
SURFACE_COLOR = (167, 255, 100)
WIDTH = 500
HEIGHT = 500
# Object class
class Sprite(pygame.sprite.Sprite):
def __init__(self, color, height, width):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(SURFACE_COLOR)
self.image.set_colorkey(COLOR)
pygame.draw.rect(self.image,
color,
pygame.Rect(0, 0, width, height))
self.rect = self.image.get_rect()
def moveRight(self, pixels):
self.rect.x += pixels
def moveLeft(self, pixels):
self.rect.x -= pixels
def moveForward(self, speed):
self.rect.y += speed * speed/10
def moveBack(self, speed):
self.rect.y -= speed * speed/10We will perceive how we control our fundamental program circle to deal with the sprites. The initial segment of the process will answer the occasions, for example, collaborations when the client utilizes the mouse or the console. Afterward, on above techniques for experience dealing with our article will be dealt with. On every occasion controller will call the relevant strategy from the Sprite class.
In this piece of code, we have control of our article, i.e., our article is an item according to our given bearings; assuming we press the right bolt key, it will move that way and the equivalent with all the bolt keys. Here, we use pygame.KEYDOWN technique to instate the strategy to involve the bolt keys for controlling the items; later on, we need to control the particular system to set off the unique key to play out the specific activity.
For example, if we have the correct bolt key, we need to call pygame.K_RIGHT strategy to move straight toward the item, and comparative for pygame.K_DOWN technique is utilized for the move up toward the object.





