The Player sprite is always moving based on the updates of a function within the Player class. We have to add this function after the control function. We must tell Python to redraw the sprite in its new location to move it to Pygame.
Pygame.time.clock function makes a clock object. It is used to keep track of time. It has three methods.
- tick(): This method should be executed once per frame. For example, the program will only run 20 frames per second at most if we give 20 as a parameter.
- get_time(): This method finds the time difference between two ticks().
- get_fps(): It returns information about the clock frame rate.
The pygame.Surface() function is used to make the Surface object. It takes as a parameter with two values. The first defines its width, and the second represents its length. The sprites have width, height, colour properties and moving right, left, up, and down jumping methods. Users can control that object by moving it forward, backwards, up, and down using arrow keys. The class in which our sprite is defined is Our first class, and we will call that class Sprite. This Sprite class represents its positions(x and y coordinates), the dimension of an object, its colour, etc. First, we must call our __init__() method. The fill method will apply the red colour to the object. If the left key is pressed, the rectangular object is moved to the left place. The same process will continue on all sides. The pygame sprite group is a beneficial class. It is used to manage multiple Sprite objects.
Here is an example of how to move a Sprite on the screen in PyGame:
Preview of the output that you will get on running this code from your IDE.
In this solution, we used pygame library.
import pygame from pygame.locals import * clock = pygame.time.Clock() fps = 60 size = 1000, 700 screen = pygame.display.set_mode(size) pygame.display.set_caption('game') sky = pygame.Surface(size) def draw_sky(): screen.blit(sky, (0,0)) class Ekkdie(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((100,100)) self.image.fill('red') self.rect = self.image.get_rect() self.rect.center = [x,y] def update(self): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.rect.x -= 7 if keys[pygame.K_RIGHT]: self.rect.x += 7 if keys[pygame.K_UP]: self.rect.y -= 7 if keys[pygame.K_DOWN]: self.rect.y += 7 ekkdie = Ekkdie(100, 100) ekkdie_group = pygame.sprite.Group() ekkdie_group.add(ekkdie) run = True while run: clock.tick(fps) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False draw_sky() ekkdie_group.draw(screen) ekkdie.update() pygame.display.update() pygame.quit()
Follow the steps carefully to get the output easily.
- Download and Install the PyCharm Community Edition on your computer.
- Open the terminal and install the required libraries with the following commands.
- Install pygame - pip install pygame.
- Create a new python file on your IDE.
- Copy the snippet using the 'copy' button and paste it into your python file.
- Run the current file to generate the output.
I hope you found this useful. I have added the link to dependent libraries, and version information in the following sections.
I found this code snippet by searching for ' Pygame sprite won't move ' in kandi. You can try any such use case!
I tested this solution in the following versions. Be mindful of changes when working with other versions.
- The solution is created in PyCharm 2022.3.
- The solution is tested on Python 3.11.1
- pygame version- 2.3.0
Using this solution, we are able to move a sprite on the screen in PyGame with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to move a sprite on the screen in PyGame.
Python 841 Version:Current License: Permissive (MIT)