I am writing a little game with pygame. I want to add four different camera perspectives to be able to look at the screen from four sides. The first camera perspective is working perfectly fine (player in center and dungeon moving along). However, in the second perspective, the player does not stay on the same spot. I did add the other perspectives, but did not work on them just yet, so I will only give the relevant code to the first and second perspective.
<code>WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
FONT = pygame.font.SysFont('comicsans',20)
PLAYER = pygame.transform.scale(pygame.image.load("assets/blob.png"),(core_diameter,core_diameter))
GROUND_PARCEL = pygame.transform.scale(pygame.image.load("assets/stone.png"),(parcel_size,parcel_size))
#creating the statics of the dungeon
DUNGEON_WIDTH, DUNGEON_HEIGHT = 20, 20 #Number of tetures, essentially m²
def draw_window(core, xpos, ypos, cam_perspective):
#Here the floor display begins...
#cam_perspective 1 working totally fine!
x_tot = int(WIDTH // parcel_size + 2) #window
y_tot = int(HEIGHT // parcel_size + 2)
x = xpos - WIDTH/(2*parcel_size) #bitmap position at window edge (top left)
y = ypos - HEIGHT/(2*parcel_size)
x_start = int(int(x) * parcel_size - x * parcel_size) #window position of generation start
y_start = int(int(y) * parcel_size - y * parcel_size)
#Some Code to display the floor always correctly, else it bugs.
grid_y = int(i + y) #current bitmap y pos
if grid_y == 0 and y != int(y) and y < 0:
grid_x = int(j + x) #current bitmap x pos
if grid_x == 0 and x != int(x) and x < 0:
if 0 <= grid_y < DUNGEON_HEIGHT and 0 <= grid_x < DUNGEON_WIDTH: #if in dungeon
if DUNGEON_FLOOR[grid_y][grid_x] == 1: #bitmap
WINDOW.blit(GROUND_PARCEL, (x_start + j * parcel_size, y_start + i * parcel_size)) #window
x_tot = int(WIDTH//parcel_size+2)
y_tot = int(HEIGHT//parcel_size+2)
x_edge_coords = xpos + WIDTH/(2*parcel_size) #Hier was ändern
y_edge_coords = ypos - HEIGHT/(2*parcel_size) #Hier was ändern
x_start_square = math.ceil(x_edge_coords)
x_start_display_pos = -(x_start_square-x_edge_coords)*parcel_size
y_start_square = int(y_edge_coords)
y_start_display_pos = -(y_edge_coords-y_start_square)*parcel_size
if 0 <= x_start_square-j < DUNGEON_WIDTH and 0 <= y_start_square+i < DUNGEON_HEIGHT: #if in dungeon
if DUNGEON_FLOOR[x_start_square-j][y_start_square+i] == 1: #bitmap
WINDOW.blit(GROUND_PARCEL, (x_start_display_pos + j * parcel_size, y_start_display_pos + i * parcel_size)) #window
WINDOW.blit(PLAYER, (core.x, core.y))
coords = FONT.render("X:" + str(int(xpos)) + " | Y:" + str(int(ypos)), 1, (255, 0, 0))
cam = FONT.render("Cam Perspective: " + str(cam_perspective), 1, (255, 0, 0))
WINDOW.blit(coords, (0, 0))
WINDOW.blit(cam, (0, 24))
<code>WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
#Debug-Screen
pygame.font.init()
FONT = pygame.font.SysFont('comicsans',20)
core_diameter = 32
parcel_size = 64
PLAYER = pygame.transform.scale(pygame.image.load("assets/blob.png"),(core_diameter,core_diameter))
GROUND_PARCEL = pygame.transform.scale(pygame.image.load("assets/stone.png"),(parcel_size,parcel_size))
#creating the statics of the dungeon
DUNGEON_WIDTH, DUNGEON_HEIGHT = 20, 20 #Number of tetures, essentially m²
def draw_window(core, xpos, ypos, cam_perspective):
WINDOW.fill((0,0,0))
#Here the floor display begins...
#cam_perspective 1 working totally fine!
if cam_perspective == 1:
x_tot = int(WIDTH // parcel_size + 2) #window
y_tot = int(HEIGHT // parcel_size + 2)
x = xpos - WIDTH/(2*parcel_size) #bitmap position at window edge (top left)
y = ypos - HEIGHT/(2*parcel_size)
x_start = int(int(x) * parcel_size - x * parcel_size) #window position of generation start
y_start = int(int(y) * parcel_size - y * parcel_size)
#Some Code to display the floor always correctly, else it bugs.
hinder_gen_bug_y = False
for i in range(y_tot):
if not hinder_gen_bug_y:
grid_y = int(i + y) #current bitmap y pos
elif grid_y != -1:
grid_y = int(i + y + 1)
if grid_y == 0 and y != int(y) and y < 0:
hinder_gen_bug_y = True
hinder_gen_bug_x = False
for j in range(x_tot):
if not hinder_gen_bug_x:
grid_x = int(j + x) #current bitmap x pos
elif grid_x != -1:
grid_x = int(j + x + 1)
if grid_x == 0 and x != int(x) and x < 0:
hinder_gen_bug_x = True
#...and the display
if 0 <= grid_y < DUNGEON_HEIGHT and 0 <= grid_x < DUNGEON_WIDTH: #if in dungeon
if DUNGEON_FLOOR[grid_y][grid_x] == 1: #bitmap
WINDOW.blit(GROUND_PARCEL, (x_start + j * parcel_size, y_start + i * parcel_size)) #window
if cam_perspective == 2:
x_tot = int(WIDTH//parcel_size+2)
y_tot = int(HEIGHT//parcel_size+2)
x_edge_coords = xpos + WIDTH/(2*parcel_size) #Hier was ändern
y_edge_coords = ypos - HEIGHT/(2*parcel_size) #Hier was ändern
x_start_square = math.ceil(x_edge_coords)
x_start_display_pos = -(x_start_square-x_edge_coords)*parcel_size
y_start_square = int(y_edge_coords)
y_start_display_pos = -(y_edge_coords-y_start_square)*parcel_size
for i in range(y_tot):
for j in range(x_tot):
if 0 <= x_start_square-j < DUNGEON_WIDTH and 0 <= y_start_square+i < DUNGEON_HEIGHT: #if in dungeon
if DUNGEON_FLOOR[x_start_square-j][y_start_square+i] == 1: #bitmap
WINDOW.blit(GROUND_PARCEL, (x_start_display_pos + j * parcel_size, y_start_display_pos + i * parcel_size)) #window
WINDOW.blit(PLAYER, (core.x, core.y))
coords = FONT.render("X:" + str(int(xpos)) + " | Y:" + str(int(ypos)), 1, (255, 0, 0))
cam = FONT.render("Cam Perspective: " + str(cam_perspective), 1, (255, 0, 0))
WINDOW.blit(coords, (0, 0))
WINDOW.blit(cam, (0, 24))
pygame.display.update()
</code>
WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
#Debug-Screen
pygame.font.init()
FONT = pygame.font.SysFont('comicsans',20)
core_diameter = 32
parcel_size = 64
PLAYER = pygame.transform.scale(pygame.image.load("assets/blob.png"),(core_diameter,core_diameter))
GROUND_PARCEL = pygame.transform.scale(pygame.image.load("assets/stone.png"),(parcel_size,parcel_size))
#creating the statics of the dungeon
DUNGEON_WIDTH, DUNGEON_HEIGHT = 20, 20 #Number of tetures, essentially m²
def draw_window(core, xpos, ypos, cam_perspective):
WINDOW.fill((0,0,0))
#Here the floor display begins...
#cam_perspective 1 working totally fine!
if cam_perspective == 1:
x_tot = int(WIDTH // parcel_size + 2) #window
y_tot = int(HEIGHT // parcel_size + 2)
x = xpos - WIDTH/(2*parcel_size) #bitmap position at window edge (top left)
y = ypos - HEIGHT/(2*parcel_size)
x_start = int(int(x) * parcel_size - x * parcel_size) #window position of generation start
y_start = int(int(y) * parcel_size - y * parcel_size)
#Some Code to display the floor always correctly, else it bugs.
hinder_gen_bug_y = False
for i in range(y_tot):
if not hinder_gen_bug_y:
grid_y = int(i + y) #current bitmap y pos
elif grid_y != -1:
grid_y = int(i + y + 1)
if grid_y == 0 and y != int(y) and y < 0:
hinder_gen_bug_y = True
hinder_gen_bug_x = False
for j in range(x_tot):
if not hinder_gen_bug_x:
grid_x = int(j + x) #current bitmap x pos
elif grid_x != -1:
grid_x = int(j + x + 1)
if grid_x == 0 and x != int(x) and x < 0:
hinder_gen_bug_x = True
#...and the display
if 0 <= grid_y < DUNGEON_HEIGHT and 0 <= grid_x < DUNGEON_WIDTH: #if in dungeon
if DUNGEON_FLOOR[grid_y][grid_x] == 1: #bitmap
WINDOW.blit(GROUND_PARCEL, (x_start + j * parcel_size, y_start + i * parcel_size)) #window
if cam_perspective == 2:
x_tot = int(WIDTH//parcel_size+2)
y_tot = int(HEIGHT//parcel_size+2)
x_edge_coords = xpos + WIDTH/(2*parcel_size) #Hier was ändern
y_edge_coords = ypos - HEIGHT/(2*parcel_size) #Hier was ändern
x_start_square = math.ceil(x_edge_coords)
x_start_display_pos = -(x_start_square-x_edge_coords)*parcel_size
y_start_square = int(y_edge_coords)
y_start_display_pos = -(y_edge_coords-y_start_square)*parcel_size
for i in range(y_tot):
for j in range(x_tot):
if 0 <= x_start_square-j < DUNGEON_WIDTH and 0 <= y_start_square+i < DUNGEON_HEIGHT: #if in dungeon
if DUNGEON_FLOOR[x_start_square-j][y_start_square+i] == 1: #bitmap
WINDOW.blit(GROUND_PARCEL, (x_start_display_pos + j * parcel_size, y_start_display_pos + i * parcel_size)) #window
WINDOW.blit(PLAYER, (core.x, core.y))
coords = FONT.render("X:" + str(int(xpos)) + " | Y:" + str(int(ypos)), 1, (255, 0, 0))
cam = FONT.render("Cam Perspective: " + str(cam_perspective), 1, (255, 0, 0))
WINDOW.blit(coords, (0, 0))
WINDOW.blit(cam, (0, 24))
pygame.display.update()
The variables given to the function are just the coordinates of the player as well as the cam perspective.
I tried to solve the problem by just copying the code for cam perspective 1 and adjusting it a little bit, but that did not work. I also tried adjusting the x_tot/y_tot as well as x_edge_coords/y_edge_coords, but that didnt solve the issue, it just made zooming in and out near impossible.