[PyGame] My program is very laggy - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] My program is very laggy (/thread-4391.html) |
My program is very laggy - GamePlanet - Aug-13-2017 I am making a program. But is verry laggy. Here's my code: 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) RE: My program is very laggy - Larz60+ - Aug-13-2017 You need to fix the indentation on your code and post between code tags. See BBCODE RE: My program is very laggy - GamePlanet - Aug-13-2017 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) RE: My program is very laggy - Larz60+ - Aug-13-2017 Thank you for the new code. Attempted to run the code, and cannot as the images Background.png and Door.png were not provided. I'd like to run through the profiler. This is also something that you can do yourself, see: https://docs.python.org/3/library/profile.html If you provide the images, I'll take a look at what you have to see if I can spot bottlenecks RE: My program is very laggy - nilamo - Aug-13-2017 I made up two random images (I don't think it would matter what they look like, unless they're massive), and didn't notice any lag at all. Unrelated, but is there a reason you're using pygame.display.update() instead of pygame.display.flip()? Scaling the images could be a major time sink. I'd try either a) lower resolution images, and b) a different format such as jpg. I ran it through the profiler, and got the following output: RE: My program is very laggy - GamePlanet - Aug-15-2017 http://imgur.com/a/zy6ey This are my images :) RE: My program is very laggy - nilamo - Aug-15-2017 Images copied for reference. These don't seem that large, in fact the ones (both the door and the room) that I made for testing were almost twice as big. According to the docs, you'll frequently want to call .convert() on the image immediately after loading it, so it's optimized for drawing to the screen (ref: http://pygame.org/docs/ref/image.html#pygame.image.load). I don't see that in your code... have you tried it?
|