I am making a game in pygame, and I am trying to make infinite generation through “areas”.
Basically, these areas are meant to be their own chunks of the map with gaps to move to other areas, and I am using a dictionary that uses the position values of these areas as keys to save the data in areas, but for some reason, whenever I try to go to [0, 0], I go to [0, 1] or [0, -1]. Here are some important segments of code I am using:
gapsList = generate_gaps()
areas = {}
areas[str([0, 0])] = Area(gapsList, [])
currentAreaTuple = [0, 0]
def update_area_tuple(current_area, direction):
operations = {
'left': lambda x: [x[0] - 1, x[0]],
'right': lambda x: [x[0] + 1, x[0]],
'top': lambda x: [x[0], x[0] + 1],
'bottom': lambda x: [x[0], x[0] - 1]
}
return operations.get(direction, lambda x: x)(current_area)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update game objects
plr1.update()
for gap in gapsx:
if plr1.rect.colliderect(gap) and (e_down and not area_move_activated):
print("gapMove")
gap_side = None
gap_pos = None
gap_width = None
if 0 <= plr1.rect.x <= 10:
plr1.rect.x = WINDOW_WIDTH - plr1.rect.width
gap_side = 'left'
elif 0 <= plr1.rect.y <= 10:
plr1.rect.y = WINDOW_HEIGHT - plr1.rect.height
gap_side = 'top'
elif WINDOW_WIDTH - plr1.rect.width - 10 <= plr1.rect.x <= WINDOW_WIDTH - plr1.rect.width:
plr1.rect.x = 0
gap_side = 'right'
elif WINDOW_HEIGHT - plr1.rect.height-10 <= plr1.rect.y <= WINDOW_HEIGHT - plr1.rect.height:
plr1.rect.y = 0
gap_side = 'bottom'
updated_area = update_area_tuple(currentAreaTuple, gap_side)
print(updated_area)
if str(updated_area) in areas:
gapsList = areas[str(updated_area)].gaps
currentAreaTuple = updated_area
print('a')
area_move_activated = True
break
if gap_side:
gap_pos = gapsList[0 if gap_side == 'top' else 2 if gap_side == 'bottom' else 1 if gap_side == 'left' else 3]['pos']
gap_width = gapsList[0 if gap_side == 'top' else 2 if gap_side == 'bottom' else 1 if gap_side == 'left' else 3]['width']
gapsList = generate_gaps(gap_side, gap_pos, gap_width)
areas[str(updated_area)] = Area(gapsList, [])
area_move_activated = True
currentAreaTuple = updated_area
# Clear screen
window.fill(WHITE)
# Draw borders with gaps
try:
draw_borders(window, gapsList)
except Exception as e:
print(e)
print(gapsList)
running = False
# Draw game objects
plr1.draw(window)
npc1.draw(window)
npc2.draw(window)
# Update display
pygame.display.flip()
# Cap the frame rate at 60 FPS
clock.tick(60)