Monday, 29 March 2021

Pygame Screen Shake Slows Down Game How To Fix?

so I'm trying to make a screenshake for the player when ever he jumps

video when ever I am blitting my render_offset which will allow my screen to shake when ever my player jumps, when I load into my game the game will just run slow and I'm not sure why? I'm trying to implment a screenshake method for my game but this will always make my game start slow and I have no idea why because I'm just blitting my render_offset to my window asoon as I add this part to make my screenshake work and I load in my game everything just runs slow and the screenshake works but if I remove it wont work and the game will run smooth I need a way to make this work without lagging my screen Thank you!

        window.blit(window,(render_offset))

heres how the screenshake works if we click space then our shake will equal 20 below it says if the shake is not > 0: then keep - the shake by 1 then we have a render varibale with a list then below it we said if the shake then randomize both of the lists

         # screenshakey
        if keys[pygame.K_SPACE]:
            shake = 20

        if shake > 0:
            shake -= 1

        render_offset = [0,0]
        if shake :
            render_offset[0] = random.randint(0, 8) - 4
            render_offset[1] = random.randint(0, 8) - 4

my full game code:Script

I'm not sure why it works on my Test Version even though I did the same thing for my games why is it laggying on my game but not lagging here any helpful fix is appreciated! I'm trying to make my screenshake when ever my player jumps with no lag thank you!

import pygame,time,random
pygame.init()

WINDOW_SIZE = (500,500)
window = pygame.display.set_mode((WINDOW_SIZE), pygame.NOFRAME)
display = pygame.Surface((500,500)) # used as the surface for rendering, which is scaled

class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(display,self.color,self.rect)

white = (205,105,205)
player1 = player(250,250,50,50,white)
player2 = player(290,250,150,50,white)
player3 = player(350,250,50,150,white)

def redraw():
    display.fill((0,0,0))
    player1.draw()
    player2.draw()
    player3.draw()


run = True
fps = 60
clock = pygame.time.Clock()

screenshake = 0
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
# 
     # screenshakey
    if keys[pygame.K_SPACE]:
        screenshake = 30

    if keys[pygame.K_RIGHT]:
        player1.x += 4
    if keys[pygame.K_LEFT]:
        player1.x -= 4
        
        
    if screenshake > 0:
        screenshake -= 1

    render_offset = [0,0]
    if screenshake:
        render_offset[0] = random.randint(0, 8) - 4
        render_offset[1] = random.randint(0, 8) - 4

    redraw()
    window.blit(pygame.transform.scale(display,WINDOW_SIZE),render_offset)

    pygame.display.update()

pygame.quit()



from Pygame Screen Shake Slows Down Game How To Fix?

No comments:

Post a Comment