You have several problems in your code:
-
The last three lines are badly indented, they must be inside the mainloop. This is possibly an error when copying the code, if it is not, you should correct it.
-
blit
is a method of pygame.Surface
, therefore it is not pygame.blit
is screen.blit
. Also, if you want your character to move you must use the variables x
e y
, that is% screen.blit(imag_1, (x, y))
.
-
For against screen.display.flip()
must be pygame.display.flip()
.
-
This is not an error in itself, but you should not chain several if
in the event check. If a condition is fulfilled it does not make sense to check if the event is of another type, this is inefficient. Use if - elif
instead.
-
So that when your character changes position do not draw the previous positions (trace) you must fill the screen with the background again every time this happens. That is, you must do a screen.fill
within the mainloop. In certain cases for efficiency it is advisable to update only the part of the window where the sprite was before.
-
In PyGame the origin of the coordinates of the screen is located in the upper left corner of it. Therefore you should increase y
when you press K_DOWN
and reduce it when you press K_UP
, unless you want to reverse the displacement.
With this your character is already shown and you can move it:
import sys
import pygame
from pygame.locals import K_LEFT, K_UP, K_RIGHT, K_DOWN
VERDE = (90, 133, 52)
ANCHO = 700
ALTO = 500
imag_1 = pygame.image.load('prueba_Pers.png')
x = 0
y = 0
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((ANCHO, ALTO))
pygame.display.set_caption("Prueba")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x -= 10
elif event.key == pygame.K_RIGHT:
x += 10
elif event.key == pygame.K_UP:
y -= 10
elif event.key == pygame.K_DOWN:
y += 10
screen.fill(VERDE)
screen.blit(imag_1, (x, y))
pygame.display.flip()
clock.tick(60)
Note: The image scrolls 10 pixels each time you press one of the keys. To allow the image to scroll continuously while the key is pressed, it must be enabled using pygame.key.set_repeat
, which allows that while a key is pressed, KEYDOWN
events are generated continuously.
Although the above works in principle, it is normal to use a sprite for this:
import sys
import pygame
from pygame.locals import K_LEFT, K_UP, K_RIGHT, K_DOWN
# Constantes
VERDE = (90, 133, 52)
ANCHO = 700
ALTO = 500
# Clase que representa nuestro Sprite
class Personaje(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('prueba_Pers.png')
self.rect = self.image.get_rect()
self.vel = 10
def update(self):
keys = pygame.key.get_pressed()
if keys[K_DOWN]:
self.rect.y += self.vel
if self.rect.bottom > ALTO:
self.rect.bottom = ALTO
if keys[K_UP]:
self.rect.y -= self.vel
if self.rect.top < 0:
self.rect.top = 0
if keys[K_RIGHT]:
self.rect.x += self.vel
if self.rect.right > ANCHO:
self.rect.right = ANCHO
if keys[K_LEFT]:
self.rect.x -= self.vel
if self.rect.left < 0:
self.rect.left = 0
# Inicia pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((ANCHO, ALTO))
pygame.display.set_caption("Prueba")
pygame.key.set_repeat(1, 25)
# Sprite
personaje_sprite = Personaje()
# Grupo para el sprite
personaje = pygame.sprite.GroupSingle()
personaje.add(personaje_sprite)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(VERDE)
personaje.update()
personaje.draw(screen)
pygame.display.flip()
clock.tick(60)
As you can see in the method update
of the sprite is checked every time it moves if its position exceeds the limits of the screen, in case of not doing it the sprite can leave it no longer visible.