Reputation: 1
My computer isn't ancient, so not having enough processing power can't be the cause. I think that means it must be being caused by something in my code that takes a long time to execute. I have pored over my code but I do not see what possibly could be causing this. I would very much appreciate if someone could point out the cause of this to me. My code is in 4 modules:
dungeon_crawl.py
import pygame, sys
import game_function as gf
from tilemap import Tilemap
from player import Player
def run_game():
# Start the game and create a screen object.
pygame.init()
screen_info = pygame.display.Info()
screen = pygame.display.set_mode((screen_info.current_w, screen_info.current_h))
pygame.display.set_caption('Dungeon Crawl')
clock = pygame.time.Clock()
# Create objects for classes.
tilemap = Tilemap(screen)
player = Player(screen)
# Set the background color.
bg_color = (255,255,255)
# Game loop
while True:
gf.check_events(player)
player.update()
gf.update_screen(bg_color, screen, player)
clock.tick(30)
run_game()_
game_function.py
import pygame, sys
from tilemap import Tilemap
def check_events(player):
"""Respond to keypresses and mouse events."""
# Quit if the player closes out of the window.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
# Move the player to the right.
player.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d:
player.moving_right = False
def update_screen(bg_color, screen, player):
"""Update images on the screen and flip to the new screen."""
# Create objects for classes.
tilemap = Tilemap(screen)
# Redraw the screen during each pass through the loop.
screen.fill(bg_color)
tilemap.draw_self(screen, player.x, player.y)
player.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
player.py
import pygame
class Player():
def __init__(self, screen):
# Load the hero image and the screen and get their rects.
self.image = pygame.transform.scale2x(pygame.image.load('hero.bmp'))
self.screen = screen
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Set position on screen.
self.rect.centerx = 330
self.rect.centery = 330
# Set initial coordinates on the tilemap.
self.x = 1000
self.y = 1000
# Movement flag
self.moving_right = False
def update(self):
"""Update the player's position based on the movement flags."""
if self.moving_right:
self.rect.centerx += 1
def blitme(self):
"""Draw the player."""
self.screen.blit(self.image, self.rect)
tilemap.py
import pygame
from pygame import sprite
class Tilemap():
def __init__(self, screen):
# Set constants.
BLACK = (0,0,0)
LIGHTGREY = (200,200,200)
DARKGREY = (100,100,100)
NONE = 0
FLOOR = 1
WALL = 2
# Link each structure to its color.
self.colors = {
NONE: BLACK,
FLOOR: LIGHTGREY,
WALL: DARKGREY
}
# Decide which structures block movements.
self.block_movement = {
NONE: 'n',
FLOOR: 'n',
WALL: 'y',
}
# Create tilemap.
tiles = []
for x in range(0, 2000):
tiles.append([])
for y in range(0, 2000):
tiles[x].append(NONE)
for i in range(0, 10):
tiles[1001][1000 + i] = WALL
self.tiles = tiles
# Set size of tiles and map height/width in tiles.
self.TILESIZE = 60
self.MAPHEIGHT = 11
self.MAPWIDTH = 11
def draw_self(self, screen, playerx, playery):
"""Draw the map."""
for row in range(self.MAPHEIGHT):
for column in range(self.MAPWIDTH):
pygame.draw.rect(screen, self.colors[self.tiles[playerx + column][playery + row]], (column * self.TILESIZE, row * self.TILESIZE, self.TILESIZE, self.TILESIZE))
Upvotes: 0
Views: 81
Reputation: 9756
Every time you update the screen, you create a new Tilemap
. In the initializer you have the following code:
for x in range(0, 2000):
tiles.append([])
for y in range(0, 2000):
tiles[x].append(NONE)
Meaning that you unnecessarily recreating the Tilemap
using a nested for loop that iterates 2,000 * 2,000 = 4,000,000
times (each frame!), just to create a 2D-list. This list doesn't need to be created every frame; it just needs to be created before each scene in the game. I believe it's what's causing the performance issues.
My guess is that you want to pass the tilemap
variable in dungeon_crawl.py into the function gf.update_screen
, rather than recreating it at the beginning of the function.
Upvotes: 2