I'm currently working on a very simple RPG game but when I try to load all the tile images for the background, the game lags to the point it freezes (framerate drops to 0 or will just crash). The tiles are 40x40 .png images and are contained in the tiles folders.
The game is divided in 3 files:
Structure of the project:
main.py:
import pygame
import sys
import time
from textures import *
from globalsvar import *
pygame.init()
cSecs = 0
cFrame = 0
fps = 0
black = (0, 0, 0)
map_data = []
for x in range(0, 32):
for y in range(0, 24):
map_data.append([x, y, "2"])
clock = pygame.time.Clock()
tile_size = 25
info_font = pygame.font.SysFont("Verdana", 20)
class Mage:
def __init__(self, location):
self.x, self.y = location
self.name = "Character"
self.image = pygame.image.load("graphics\\sprites\\ninja_left.png")
self.x_update = 0
self.y_update = 0
self.mage_width_dif = win_width - self.image.get_width()
self.mage_height_dif = win_height - self.image.get_height()
def draw_self(self):
window.blit(self.image, (self.x, self.y))
def update(self):
self.x += self.x_update
self.y += self.y_update
def create_window():
global window, win_height, win_width, win_title
win_width, win_height = 800, 600
win_title = "A Little RPG Game"
pygame.display.set_caption(win_title)
window = pygame.display.set_mode((win_width, win_height), pygame.HWSURFACE|pygame.DOUBLEBUF)
def draw_grid_terrain():
for x in range(-900, 1800, tile_size):
for y in range(-900, 1800, tile_size):
for i in map_data:
tile = (i[0]*Tiles.File_size, i[1]*Tiles.File_size)
if (x, y) == tile:
window.blit(Tiles.Texture_Tags[i[2]], (x + Globals.camera_X, y + Globals.camera_Y))
def show_fps():
fps_overlay = info_font.render(str(fps), True, black)
window.blit(fps_overlay, (10, 10))
def fps_manager():
global cFrame, fps, cSecs, deltaTime
if cSecs == time.strftime("%S"):
cFrame += 1
else:
fps = cFrame
cFrame = 0
cSecs = time.strftime("%S")
gameRunning = True
create_window()
character = Mage((win_width/2, win_height/2))
while(gameRunning):
if character.x + character.x_update >= character.mage_width_dif or character.x + character.x_update <= 0:
character.x -= character.x_update
if character.y + character.y_update >= character.mage_height_dif or character.y + character.y_update <= 0:
character.y -= character.y_update
if character.y > ((75 * win_height)/100):
Globals.camera_MOVE = 1
elif character.y < ((5 * win_height)/100):
Globals.camera_MOVE = 2
elif character.x > ((75 * win_width)/100):
Globals.camera_MOVE = 3
elif character.x < ((5 * win_width)/100):
Globals.camera_MOVE = 4
else:
Globals.camera_MOVE = 0
if Globals.camera_MOVE == 1:
Globals.camera_Y -= 3.5
elif Globals.camera_MOVE == 2:
Globals.camera_Y += 3.5
elif Globals.camera_MOVE == 3:
Globals.camera_X -= 3.5
elif Globals.camera_MOVE == 4:
Globals.camera_X += 3.5
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameRunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
character.x_update = -1
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
character.x_update = 1
elif event.key == pygame.K_UP or event.key == pygame.K_w:
character.y_update = -1
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
character.y_update = 1
if event.type == pygame.KEYUP:
if (event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT
or event.key == pygame.K_a or event.key == pygame.K_d):
character.x_update = 0
elif (event.key == pygame.K_DOWN or event.key == pygame.K_UP
or event.key == pygame.K_w or event.key == pygame.K_s):
character.y_update = 0
clock.tick(60)
draw_grid_terrain()
character.update()
character.draw_self()
fps_manager()
show_fps()
pygame.display.update()
pygame.quit()
sys.exit()
textures.py:
import pygame
pygame.init()
class Tiles:
File_size = 25
def load_textures(file, size):
bitmap = pygame.image.load(file)
bitmap = pygame.transform.scale(bitmap, (size, size))
surface = pygame.Surface((size, size), pygame.HWSURFACE|pygame.SRCALPHA)
surface.blit(bitmap, (0, 0))
return surface
Grass = load_textures("graphics\\tiles\\grass.png", File_size)
Ground = load_textures("graphics\\tiles\\ground.png", File_size)
Water = load_textures("graphics\\tiles\\water.png", File_size)
Floor = load_textures("graphics\\tiles\\floor.png", File_size)
Texture_Tags = {"1": Grass, "2": Ground, "3": Water, "4": Floor}
globalsvar.py:
class Globals:
camera_X = 0
camera_Y = 0
camera_MOVE = 0
camera_pos_X = 0
camera_pos_Y = 0
Game screenshot:
I read somewhere that I should try loading all the tiles on a surface once and then display said surface, but I don't know how.
Why is my game lagging so much, and how could I improve the framerate?