[Solved] Make image to use it like character in pygame [closed]


Your question is vague: Do you want to MAKE a character or make an image BEHAVE like a character?

If you wanted to make a character
If you wanted to make a character, I suggest going to PixelArtor and choosing the file size you want. I recommend 64 by 64 or 128 by 128, because if you choose 32 by 32 or below, it might be too small. If you are on a Unix system, there might already be an installation package for GIMP on there, so go to downloads page and read the instructions on how you install it. If there isn’t you can download it on the downloads page. (I’m running Windows, so I wouldn’t know for sure)

You can then display the image on your PyGame screen like so:

import pygame
from pygame.locals import *
display = pygame.display.set_mode((1024,1024)) # window size is determined here
pygame.init()
character = pygame.image.load("your/path/to/character.png")
display.blit(character,(0,0)) # These are the X and Y coordinates
pygame.display.update()

But of course, that creates a still image that will not move no matter what keys you press.

If you want a loaded image to behave like a character

import pygame
from pygame.locals import *
display = pygame.display.set_mode((1024,1024)) # window size is determined here
pygame.init()
character = pygame.image.load("your/path/to/character.png")
background = pygame.image.load("your/path/to/background.png")
characterx = 0
charactery = 0
while True:
    display.blit(background,(0,0))
    display.blit(character,(characterx,charactery))
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_a:
                characterx -= 2
            if event.key == K_d:
                characterx += 2
            if event.key == K_w:
                charactery -= 2
            if event.key == K_s:
                charactery += 2
        if event.type == QUIT:
            pygame.quit()
            exit()
    pygame.display.update()

Okay, so in this code, the objects in play are:
characterx and charactery store the X and Y positions of the loaded image character. These change when keys are pressed, changing where the character is displayed, simulating movement.
The event loop is the line that starts
for event in pygame.event.get():
This loop handles events like mouse movement, clicking, keyboard detection, and quitting.
The background is so that the extra trails left by the character don’t show up.
Try removing the line
display.blit(background,(0,0))
and you will see that the character “trail” is left behind.

solved Make image to use it like character in pygame [closed]