Sebastian Villate
Sebastian Villate

Reputation: 55

Getting my character to move fluidly in Pygame

I am trying to create a pygame for my com sci final, but my teacher has not done an excellent job at explaining how pygame works. My trouble here is I need make my character, a custom sprite I created, move fluidly when any of the 4 arrow keys are held. So far, all I've been able to do is make it move with every individual key press, and I have no idea how to fix this.

I'm not sure if my code is the most efficient, but essentially depending on what key you press, not only do the X and Y coordinates change, but the png image changes to face the appropriate direction. This is all draw using blits.

import pygame

pygame.init()

def redraw_game_window(texture):
    screen = pygame.display.set_mode(size)
    background_image = pygame.image.load("lava.jpg").convert()
    background_image = pygame.transform.scale(background_image, (size))
    screen.blit(background_image, [0, 0])
    texture= pygame.transform.scale(texture, (65,64))
    texture=texture.convert_alpha()
    screen.blit(texture,(EarlX,EarlY))

size=1200,600
EarlWidth=65
EarlLength=64
texture = pygame.image.load("UpFacingEarl.png")
EarlX=500
EarlY=150
speed=50

inPlay= True
while inPlay:
    redraw_game_window(texture)
    pygame.time.delay(20)

    keys = pygame.key.get_pressed()
    if keys[pygame.K_ESCAPE]:
        inPlay = False
    if keys[pygame.K_LEFT]:
        texture = pygame.image.load("LeftFacingEarl.png")
        EarlX-=speed
    if keys[pygame.K_RIGHT]:
       texture = pygame.image.load("RightFacingEarl.png")
       EarlX+=speed
    if keys[pygame.K_UP]:
        texture = pygame.image.load("UpFacingEarl.png")
        EarlY-=speed
    if keys[pygame.K_DOWN]:
       texture = pygame.image.load("DownFacingEarl.png")
       EarlY+=speed

    pygame.display.flip()

Any help on how to improve my code would be greatly appreciated, I know im not including the png's but theres not much I can do there.

Upvotes: 0

Views: 120

Answers (1)

Ethan
Ethan

Reputation: 132

First, you don't need to double space your code. Now that we've gotten that out of the way:

screen = pygame.display.set_mode(size)

should only called at the beginning of the program. Not every time! This is the reason your program isn't working. Here are other less important things to fix:

this should also only be called at the beginning:

background_image = pygame.image.load("lava.jpg").convert()
background_image = pygame.transform.scale(background_image, (size))

all in all, you redraw_game_window function should look like:

def redraw_game_window(texture, EarlX, EarlY, screen): #you should pass all the variables you are using to the function. Python doesn't work well with global variables. if you really want to use global variables like you did here, use the global keyword
    screen.fill((0,0,0)) #clear the screen

    screen.blit(background_image, [0, 0]) #draw new background

    texture = pygame.transform.scale(texture, (65,64))
    texture = texture.convert_alpha()
    screen.blit(texture,(EarlX,EarlY)) #draw earl

This event loop is fine:

for event in pygame.event.get():
    if event.type == pygame.QUIT: #you need this or else pressing the red "x" on the window won't close it
        quit()

keys = pygame.key.get_pressed()

if keys[pygame.K_ESCAPE]: #press esc to exit program
    quit()

if keys[pygame.K_UP]:
    EarlY -= 1

if keys[pygame.K_DOWN]:
    EarlY -= -1

if keys[pygame.K_LEFT]:
    EarlX += -1

if keys[pygame.K_RIGHT]:
    EarlX += 1

Upvotes: 3

Related Questions