Python Forum
[PyGame] My program is very laggy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] My program is very laggy
#3
import pygame
import os

x = 0
y = 0

os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x, y)

pygame.init()

infoObject = pygame.display.Info()
display_width = infoObject.current_w
display_height = infoObject.current_h

gameDisplay = pygame.display.set_mode((display_width, display_height), pygame.NOFRAME)
pygame.display.set_caption("Shop Game")
clock = pygame.time.Clock()

white = (255, 255, 255)

Paused = False

BackgroundImg = pygame.image.load("Images\Background.png")
DoorImg = pygame.image.load("Images\Door.png")

def Game_Loop(infoObject, display_width, display_height):

    zoom = 1.1
    
    Scroll_Direction = "nothing"
    scroll_speed = 20

    #Default Position 
    object_x = -(((infoObject.current_w * zoom) - display_width) / 2)
    object_y = -(((infoObject.current_h * zoom) - display_height) / 2)

    def Background(New_BackgroundImg, Background_position):
        gameDisplay.blit(New_BackgroundImg, Background_position)

    def Display_Objects(object_img, object_x, object_y, cursor_x, cursor_y, zoom, object_scale_x, object_scale_y, infoObject, Scroll_Direction):         

        object_scale = (int(object_scale_x * zoom), int(object_scale_y * zoom))
        object_position = (object_x, object_y)
        new_object = pygame.transform.scale(object_img, object_scale)

        gameDisplay.blit(new_object, object_position)

        
    
    while not Paused:

        cursor_x, cursor_y = x, y = pygame.mouse.get_pos()

        for event in pygame.event.get():

            if event.type == pygame.KEYDOWN:

                 #ESC Button to close the program
                 if event.key == pygame.K_ESCAPE:
                     pygame.quit()
                     quit()

                 #Move keys
                 elif event.key == pygame.K_w:
                     Scroll_Direction = "up"

                 elif event.key == pygame.K_a:
                     Scroll_Direction = "left"

                 elif event.key == pygame.K_s:
                     Scroll_Direction = "down"

                 elif event.key == pygame.K_d:
                     Scroll_Direction = "right"



            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 4:
                    if zoom >= 1.1 and zoom <= 2:
                        zoom = zoom + 0.1
        
                        object_x += -((zoom / (zoom - 0.1)) * (cursor_x - object_x) + object_x - cursor_x)
                        object_y += -((zoom / (zoom - 0.1)) * (cursor_y - object_y) + object_y - cursor_y)
                        

                elif event.button == 5:
                    if zoom >= 1.2 and zoom <= 2.1:
                        zoom -= 0.1

                        object_x += -((zoom / (zoom + 0.1)) * (cursor_x - object_x) + object_x - cursor_x)
                        object_y += -((zoom / (zoom + 0.1)) * (cursor_y - object_y) + object_y - cursor_y)
                        

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_w or event.key:
                    pygame.K_a or event.key == pygame.K_s or event.key == pygame.K_d
                    Scroll_Direction = "nothing"

        
        if Scroll_Direction == "up":
            object_y += scroll_speed

        elif Scroll_Direction == "down":
            object_y -= scroll_speed

        elif Scroll_Direction == "right":
            object_x -= scroll_speed

        elif Scroll_Direction == "left":
            object_x += scroll_speed


        gameDisplay.fill(white)
        Display_Objects(BackgroundImg, object_x, object_y, cursor_x, cursor_y, zoom, infoObject.current_w, infoObject.current_h, infoObject, Scroll_Direction)
        Display_Objects(DoorImg, object_x + (infoObject.current_w * zoom / 2), object_y + (infoObject.current_h * zoom / 2), cursor_x, cursor_y, zoom, 100, 100, infoObject, Scroll_Direction)

        clock.tick(60)
        pygame.display.update()

Game_Loop(infoObject, display_width, display_height)
Reply


Messages In This Thread
My program is very laggy - by GamePlanet - Aug-13-2017, 09:32 AM
RE: My program is very laggy - by Larz60+ - Aug-13-2017, 10:25 AM
RE: My program is very laggy - by GamePlanet - Aug-13-2017, 11:46 AM
RE: My program is very laggy - by Larz60+ - Aug-13-2017, 03:45 PM
RE: My program is very laggy - by nilamo - Aug-13-2017, 05:01 PM
RE: My program is very laggy - by GamePlanet - Aug-15-2017, 02:16 PM
RE: My program is very laggy - by nilamo - Aug-15-2017, 03:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Laggy Game Movement game_slayer_99 12 4,651 Oct-05-2022, 11:34 AM
Last Post: metulburr
  [PyGame] sound effect delay and program laggy xBlackHeartx 26 13,289 Oct-09-2019, 11:36 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020