我正在用 pygame 编写一个小游戏。我想添加四个不同的相机视角,以便能够从四个侧面查看屏幕。第一个相机视角运行良好(请...
我正在用 pygame 编写一个小游戏。我想添加四个不同的摄像机视角,以便能够从四个侧面查看屏幕。第一个摄像机视角工作得很好(玩家在中心,地牢沿着移动)。然而,在第二个视角中,玩家不会停留在同一个位置。我确实添加了其他视角,但还没有对它们进行处理,所以我只会给出与第一个和第二个视角相关的代码。
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()
赋予该函数的变量只是玩家的坐标以及摄像机视角。
我尝试通过复制 cam perspective 1 的代码并进行一些调整来解决问题,但这种方法没有奏效。我还尝试调整 x_tot/y_tot 以及 x_edge_coords/y_edge_coords,但这并没有解决问题,只是让放大和缩小变得几乎不可能。