0

My pygame project is revolved around an adventure game, ive got the basic character down, but cant seem to make an npc character.

My idea is to give the npc character an idle form, where it's head bobs left to right, but it just doesnt works, while also lags the game

heres the code ->

import sys,time,pygame,os
from pygame import mixer
pygame.init()
pygame.mixer.init()

#main displays
width,height=(900,600)
border= pygame.Rect(0,0,6,height)
WIN=pygame.display.set_mode((width,height))
pygame.display.set_caption("rpg game")
bg= pygame.image.load(os.path.join('','pixel_bg.jpg'))
bg=pygame.transform.scale(bg,(width,height))
junkyard_bg= pygame.image.load(os.path.join('','junkyard_bg.jpg'))
bar_bg=pygame.image.load(os.path.join('','bar_bg.jpg'))
cafe_bg=pygame.image.load(os.path.join('','cafe_bg.jpg'))
city_bg=pygame.image.load(os.path.join('','city_bg.jpg'))
city_bg_clear=pygame.image.load(os.path.join('','city_bg_clear.png'))

#characters
c_scale_width=80
c_scale_height= 90
c_scale_stand_width=90
c_scale_stand_height= 105
c_s_y= 290
cx = 0
cy = 480
move_left = False
move_right = False

standing_1 = pygame.image.load(os.path.join('','P1(stand).png'))
standing=pygame.transform.scale(standing_1,(c_scale_stand_width,c_scale_stand_height))

P2_right_11=pygame.image.load(os.path.join('','P1(1).png'))
P2_right_1=pygame.transform.scale(P2_right_11,(c_scale_width,c_scale_height))
P2_right_12=pygame.image.load(os.path.join('','P1(2).png'))
P2_right_2=pygame.transform.scale(P2_right_12,(c_scale_width,c_scale_height))
P2_right_13=pygame.image.load(os.path.join('','P1(3).png'))
P2_right_3=pygame.transform.scale(P2_right_13,(c_scale_width,c_scale_height))
P2_right_14=pygame.image.load(os.path.join('','P1(4).png'))
P2_right_4=pygame.transform.scale(P2_right_14,(c_scale_width,c_scale_height))

P2_right= [P2_right_1,
           P2_right_2,
           P2_right_3,
           P2_right_4]

P2_left_1= pygame.transform.flip(P2_right_1,True,False)
P2_left_2= pygame.transform.flip(P2_right_2,True,False)
P2_left_3= pygame.transform.flip(P2_right_3,True,False)
P2_left_4= pygame.transform.flip(P2_right_4,True,False)

P2_left= [P2_left_1,
          P2_left_2,
          P2_left_3,
          P2_left_4]
step_index=0

#npc 1
npc_scale_width=80
npc_scale_height= 90
npc_x=400
npc_y=400

npc1_1_pre=pygame.image.load(os.path.join('','npc_animation_1.png'))
npc1_2_pre=pygame.image.load(os.path.join('','npc_animation_2.png'))
npc1_3_pre=pygame.image.load(os.path.join('','npc_animation_3.png'))

npc1_1=pygame.transform.scale(npc1_1_pre,(npc_scale_width,npc_scale_height))
npc1_2=pygame.transform.scale(npc1_2_pre,(npc_scale_width,npc_scale_height))
npc1_3=pygame.transform.scale(npc1_3_pre,(npc_scale_width,npc_scale_height))

npc_arr=[npc1_1,npc1_2,npc1_3]

#speed
Velocity= 4
global jump_vel_y
jump_vel_y = 15
jump_height = 3
FPS = 60
global jump
jump= False

#music
mixer.music.load("adventurebeat.mp3")
mixer.music.play(-1)


def c_move(keys_pressed):

    global move_left
    global move_right
    global jump
    global cx
    global cy
    global jump_vel_y
    global  step_index


    current_image = standing

    if step_index >= 4:
        step_index = 0
    if keys_pressed[pygame.K_a]:  # left
        cx -= Velocity
        move_left = True

    elif keys_pressed[pygame.K_d]:  # right
        cx += Velocity
        move_right = True
    else:
        move_left = False
        move_right = False
        step_index = 0

    if jump is False and keys_pressed[pygame.K_SPACE]:
        jump = True

    if jump:
        cy -= jump_vel_y
        jump_vel_y -= 1
        if jump_vel_y < -15:
            jump = False
            jump_vel_y = 15
        step_index=0

    if move_right:
        current_image = P2_right[step_index // 2]
        step_index += 1

    elif move_left:
        current_image = P2_left[step_index // 2]
        step_index += 1
    WIN.blit(current_image, (cx, cy))
    pygame.display.update()

def npc1():
    for x in range(len(npc_arr)):
        WIN.blit(npc_arr[x],(npc_x,npc_y))
        time.sleep(0.03)

def draw():
    WIN.blit(city_bg_clear, (0, 0))

def main():

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick_busy_loop(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                sys.exit()


        keys_pressed = pygame.key.get_pressed()
        c_move(keys_pressed)
        draw()
        npc1()

main()

enter image description here

heres an image for reference

The character used to move fast, but after adding the npc's code, the character goes into slow-mo

im pretty sure this is because of the time.sleep(0.03), but i use that to make the npc's animations not get blited instantly. (but i dont really need to add that since its not even blitting without the time.sleep)

Does anyone have any suggestions on how to solve this? (or any ways to make the npc have idle movements)

thanks in advance

  • 1
    Right now, you explicitly pause, which of course slows the entire game. The basic theory is to update state independently from drawing. Imagine you instead had a timer, and every ~3 seconds or whatever it changes the variable `current_npc_image` or something. Every iteration through the game loop you just blit the `current_npc_image` to the screen. – Starbuck5 Aug 03 '21 at 09:17
  • 1
    Some other things: it's strange `pygame.display.update()` is in a move method. The os.path.joins do nothing if the first parameter is `"."`. And rather than having `image_pre` you can just overwrite the same variable with the output from pygame.transform.scale. For example, you can load into the variable `npc1_1` and then scale it into `npc1_1`, which would reduce your memory usage. – Starbuck5 Aug 03 '21 at 09:20
  • 1
    Also, if you call `convert()` or `convert_alpha()` on your images after you load them, it will increase your game performance. – Starbuck5 Aug 03 '21 at 09:21
  • thanks so much for the info, i'm quite new to programming. So i'll learn from the mistakes – captainchungus Aug 03 '21 at 14:19

0 Answers0