Skip to main content
Code Review

Return to Question

Post Reopened by Toby Speight, Sᴀᴍ Onᴇᴌᴀ
Fixed code (it now works as intended) and made code about general, not specific improvements.
Added to review
Source Link
sbottingota
  • 1.2k
  • 4
  • 18

I am creating a tile-map in python using pygame. ThThe code I have (see below) works OK, but I want it to be easy to scroll, and add both rows and columns. Right now, that requires a lot of complex shufflingwas wandering if there were any improvements.

This is my code creates a tilemap and scrolls it horizontally:

from enum import Enum
import pygame
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 500
TILE_SIZE = 50
class TileTypes(Enum):
 AIR = 0
 GRASS = 1
 ROCK = 2
 LAVA = 3
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen.fill((255,) * 3)
clock = pygame.time.Clock()
MAP_WIDTH = SCREEN_WIDTH // TILE_SIZE
MAP_HEIGHT = SCREEN_HEIGHT // TILE_SIZE
map_layout = [[TileTypes.AIR for j in range(MAP_WIDTH)]
 for i in range(MAP_HEIGHT)]
def draw_map():
 y = 0
 for row in map_layout:
 x = 0
 for tile in row:
 match tile:
 case TileTypes.AIR:
 pass
 case TileTypes.GRASS:
 pygame.draw.rect(screen, (0, 255, 0), (x, y, TILE_SIZE, TILE_SIZE))
 case TileTypes.ROCK:
 pygame.draw.rect(screen, (150, 150, 150), (x, y, TILE_SIZE, TILE_SIZE))
 x += TILE_SIZE
 y += TILE_SIZE
def scroll_map():
 for row in map_layout:
 row.append(map_layoutrow.pop(0))
map_layout[-2] = map_layout[-1] = [TileTypes.ROCK for i in range(MAP_WIDTH)]
map_layout[-3] = [TileTypes.GRASS for i in range(MAP_WIDTH)]
map_layout[-4][2:4] = map_layout[-4][6:8] = [TileTypes.GRASS for i in range(2)]
done = False
while not done:
 if any(event.type == pygame.QUIT for event in pygame.event.get()):
 done = True
 scroll_map()
 screen.fill((255,) * 3)
 draw_map()
 pygame.display.flip()
 clock.tick(20)
pygame.quit()

This code creates a tilemap and scrolls it: tilemap

However, scroll_map() only scrolls it vertically, not horizontally, and it's easy to add entire rows, but hard to add columns (I want both to be added easily).tilemap

I would be grateful for any help on how to do those two things, as well as any other improvements.

I am creating a tile-map in python using pygame. Th code I have (see below) works OK, but I want it to be easy to scroll, and add both rows and columns. Right now, that requires a lot of complex shuffling.

This is my code:

from enum import Enum
import pygame
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 500
TILE_SIZE = 50
class TileTypes(Enum):
 AIR = 0
 GRASS = 1
 ROCK = 2
 LAVA = 3
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen.fill((255,) * 3)
clock = pygame.time.Clock()
MAP_WIDTH = SCREEN_WIDTH // TILE_SIZE
MAP_HEIGHT = SCREEN_HEIGHT // TILE_SIZE
map_layout = [[TileTypes.AIR for j in range(MAP_WIDTH)]
 for i in range(MAP_HEIGHT)]
def draw_map():
 y = 0
 for row in map_layout:
 x = 0
 for tile in row:
 match tile:
 case TileTypes.AIR:
 pass
 case TileTypes.GRASS:
 pygame.draw.rect(screen, (0, 255, 0), (x, y, TILE_SIZE, TILE_SIZE))
 case TileTypes.ROCK:
 pygame.draw.rect(screen, (150, 150, 150), (x, y, TILE_SIZE, TILE_SIZE))
 x += TILE_SIZE
 y += TILE_SIZE
def scroll_map():
 map_layout.append(map_layout.pop(0))
map_layout[-2] = map_layout[-1] = [TileTypes.ROCK for i in range(MAP_WIDTH)]
map_layout[-3] = [TileTypes.GRASS for i in range(MAP_WIDTH)]
map_layout[-4][2:4] = map_layout[-4][6:8] = [TileTypes.GRASS for i in range(2)]
done = False
while not done:
 if any(event.type == pygame.QUIT for event in pygame.event.get()):
 done = True
 scroll_map()
 screen.fill((255,) * 3)
 draw_map()
 pygame.display.flip()
 clock.tick(20)
pygame.quit()

This code creates a tilemap and scrolls it: tilemap

However, scroll_map() only scrolls it vertically, not horizontally, and it's easy to add entire rows, but hard to add columns (I want both to be added easily).

I would be grateful for any help on how to do those two things, as well as any other improvements.

I am creating a tile-map in python using pygame. The code I have (see below) works OK, but I was wandering if there were any improvements.

This code creates a tilemap and scrolls it horizontally:

from enum import Enum
import pygame
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 500
TILE_SIZE = 50
class TileTypes(Enum):
 AIR = 0
 GRASS = 1
 ROCK = 2
 LAVA = 3
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen.fill((255,) * 3)
clock = pygame.time.Clock()
MAP_WIDTH = SCREEN_WIDTH // TILE_SIZE
MAP_HEIGHT = SCREEN_HEIGHT // TILE_SIZE
map_layout = [[TileTypes.AIR for j in range(MAP_WIDTH)]
 for i in range(MAP_HEIGHT)]
def draw_map():
 y = 0
 for row in map_layout:
 x = 0
 for tile in row:
 match tile:
 case TileTypes.AIR:
 pass
 case TileTypes.GRASS:
 pygame.draw.rect(screen, (0, 255, 0), (x, y, TILE_SIZE, TILE_SIZE))
 case TileTypes.ROCK:
 pygame.draw.rect(screen, (150, 150, 150), (x, y, TILE_SIZE, TILE_SIZE))
 x += TILE_SIZE
 y += TILE_SIZE
def scroll_map():
 for row in map_layout:
 row.append(row.pop(0))
map_layout[-2] = map_layout[-1] = [TileTypes.ROCK for i in range(MAP_WIDTH)]
map_layout[-3] = [TileTypes.GRASS for i in range(MAP_WIDTH)]
map_layout[-4][2:4] = map_layout[-4][6:8] = [TileTypes.GRASS for i in range(2)]
done = False
while not done:
 if any(event.type == pygame.QUIT for event in pygame.event.get()):
 done = True
 scroll_map()
 screen.fill((255,) * 3)
 draw_map()
 pygame.display.flip()
 clock.tick(20)
pygame.quit()

tilemap

I would be grateful for any improvements.

Post Closed as "Not suitable for this site" by Sᴀᴍ Onᴇᴌᴀ
Source Link
sbottingota
  • 1.2k
  • 4
  • 18

Drawing a tilemap using python and pygame

I am creating a tile-map in python using pygame. Th code I have (see below) works OK, but I want it to be easy to scroll, and add both rows and columns. Right now, that requires a lot of complex shuffling.

This is my code:

from enum import Enum
import pygame
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 500
TILE_SIZE = 50
class TileTypes(Enum):
 AIR = 0
 GRASS = 1
 ROCK = 2
 LAVA = 3
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen.fill((255,) * 3)
clock = pygame.time.Clock()
MAP_WIDTH = SCREEN_WIDTH // TILE_SIZE
MAP_HEIGHT = SCREEN_HEIGHT // TILE_SIZE
map_layout = [[TileTypes.AIR for j in range(MAP_WIDTH)]
 for i in range(MAP_HEIGHT)]
def draw_map():
 y = 0
 for row in map_layout:
 x = 0
 for tile in row:
 match tile:
 case TileTypes.AIR:
 pass
 case TileTypes.GRASS:
 pygame.draw.rect(screen, (0, 255, 0), (x, y, TILE_SIZE, TILE_SIZE))
 case TileTypes.ROCK:
 pygame.draw.rect(screen, (150, 150, 150), (x, y, TILE_SIZE, TILE_SIZE))
 x += TILE_SIZE
 y += TILE_SIZE
def scroll_map():
 map_layout.append(map_layout.pop(0))
map_layout[-2] = map_layout[-1] = [TileTypes.ROCK for i in range(MAP_WIDTH)]
map_layout[-3] = [TileTypes.GRASS for i in range(MAP_WIDTH)]
map_layout[-4][2:4] = map_layout[-4][6:8] = [TileTypes.GRASS for i in range(2)]
done = False
while not done:
 if any(event.type == pygame.QUIT for event in pygame.event.get()):
 done = True
 scroll_map()
 screen.fill((255,) * 3)
 draw_map()
 pygame.display.flip()
 clock.tick(20)
pygame.quit()

This code creates a tilemap and scrolls it: tilemap

However, scroll_map() only scrolls it vertically, not horizontally, and it's easy to add entire rows, but hard to add columns (I want both to be added easily).

I would be grateful for any help on how to do those two things, as well as any other improvements.

lang-py

AltStyle によって変換されたページ (->オリジナル) /