Aug-13-2017, 09:32 AM
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)
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)