captainchungus
captainchungus

Reputation: 94

Im trying to solve this un-smooth and choppy animations and movements on pygame, i've been trying everything but nothing works

heres the video of the animation https://www.youtube.com/watch?v=uhPdN3v8vg0

other pygame codes dont act like this, only this specific one, so i'm pretty sure its not hardware problem

import sys
import time
import pygame
import os
from pygame import mixer
 pygame.init()
pygame.mixer.init()
width,height=(900,500)

border= pygame.Rect(0,0,6,height)
WIN=pygame.display.set_mode((width,height))
bg= pygame.image.load(os.path.join('','pixel_bg.jpg'))
bg=pygame.transform.scale(bg,(width,height))
person1=pygame.image.load(os.path.join('','mario.png'))
p1_width, p1_height = (50,60)
person1=pygame.transform.scale(person1,(p1_width,p1_height))
black=(0,0,0)
p1_rect = pygame.Rect(50,340,p1_width,p1_height)
pygame.display.set_caption("rpg game")
Velocity= 4
jump_height = 50
FPS = 60
mixer.music.load("adventurebeat.mp3")
mixer.music.play(-1)

def draw():
    WIN.blit(bg, (0, 0))
    WIN.blit(person1, (p1_rect.x,p1_rect.y))
    pygame.display.update()
def p1_movement(keys_pressed, p1_rect):
    if keys_pressed[pygame.K_a] and p1_rect.x - Velocity > border.x + border.width:  # left
        p1_rect.x -= Velocity
    if keys_pressed[pygame.K_d] and p1_rect.x + Velocity + p1_rect.width < width:  # right
        p1_rect.x += Velocity
    if keys_pressed[pygame.K_w] and p1_rect.y - Velocity > 0:  # up
        p1_rect.y -= Velocity
    if keys_pressed[pygame.K_SPACE] and p1_rect.y - Velocity > 0:  # up
        p1_rect.y -= jump_height
    if keys_pressed[pygame.K_s] and p1_rect.y + Velocity + p1_rect.height < height:  # down
        p1_rect.y += Velocity

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()
            draw()
        keys_pressed = pygame.key.get_pressed()
        p1_movement(keys_pressed,p1_rect)
main()

I've tried changing the clock.tick_busy_loop() to clock.tick() but it still doesnt work :/

Upvotes: 1

Views: 192

Answers (1)

Rabbid76
Rabbid76

Reputation: 210909

You need to draw the object in the application loop instead of the event loop:

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()
            
            # draw()   <--- DELETE
   
        keys_pressed = pygame.key.get_pressed()
        p1_movement(keys_pressed,p1_rect)

        draw()       # <--- INSERT

The application loop is executed in every frame, but the event loop only is executed when an event occurs.

Upvotes: 1

Related Questions