Edge Tiles
Part of the Worlds section of Coddy's Pygame journey. Lesson 8 of 35.
A platform looks finished when its ends are rounded. The pack has grass_left.png and grass_right.png for the two ends of a grassy surface.
Now each tile looks at three neighbours: above, left and right. Some of them are outside the map, at the edges. A helper function answers for any position, inside or not:
def solid(row, col):
if row < 0 or row >= len(LEVEL) or col < 0 or col >= len(LEVEL[0]):
return False
return LEVEL[row][col] == "#"Outside the map counts as empty. With the helper, the choice reads like the rule:
if solid(row - 1, col):
image = dirt_img
elif not solid(row, col - 1) and solid(row, col + 1):
image = grass_left_img
elif solid(row, col - 1) and not solid(row, col + 1):
image = grass_right_img
else:
image = grass_img
tiles.append((image, pygame.Rect(x, y, TILE, TILE)))A tile with ground on both sides, or on neither side, keeps the plain grass. At the map's left edge, column 0, the tile to the left is outside, so it counts as empty and the ground there starts with grass_left_img.
Challenge
MediumThe starter loads grass_left.png into grass_left_img and grass_right.png into grass_right_img.
- Write a function
solid(row, col)that returnsFalseoutside the map andLEVEL[row][col] == "#"inside it. - For each
#:dirt_imgwhen the tile above is solid; elsegrass_left_imgwhen only the right neighbour is solid; elsegrass_right_imgwhen only the left neighbour is solid; elsegrass_img.
Try it yourself
import pygame
TILE = 36
LEVEL = [
"###.............",
"###.............",
"..........C.....",
".........####...",
"..C.............",
".####......C....",
"..........###...",
"#....C.........#",
"###..##...###.##",
"######..########",
]
pygame.init()
screen = pygame.display.set_mode((576, 360))
pygame.display.set_caption("Tiles")
clock = pygame.time.Clock()
grass_img = pygame.image.load("assets/images/grass.png").convert_alpha()
grass_left_img = pygame.image.load("assets/images/grass_left.png").convert_alpha()
grass_right_img = pygame.image.load("assets/images/grass_right.png").convert_alpha()
dirt_img = pygame.image.load("assets/images/dirt.png").convert_alpha()
coin_img = pygame.image.load("assets/images/coin.png").convert_alpha()
tiles = []
for row, line in enumerate(LEVEL):
for col, char in enumerate(line):
x, y = col * TILE, row * TILE
if char == "#":
if row == 0 or LEVEL[row - 1][col] != "#":
image = grass_img
else:
image = dirt_img
tiles.append((image, pygame.Rect(x, y, TILE, TILE)))
elif char == "C":
tiles.append((coin_img, pygame.Rect(x, y, TILE, TILE)))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((140, 200, 240))
for image, rect in tiles:
screen.blit(image, rect)
pygame.display.flip()
clock.tick(60)
pygame.quit()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Worlds
1Maps from Text
A Level in TextRows and ColumnsLetters With MeaningsWhere the Player StartsOne Screen at a Time3Gravity and Jumping
Falling With GravityJump From the GroundShort HopsA Top Falling SpeedJump BufferPractice on your own: Pygame playground