我制作的相机效果不佳 播放器移动速度比相机快 为什么?
The Camera I Made Does Not Work Well Player Moves Faster Than Camera why?
我做了一个游戏,但是当我想添加一个摄像机来移动玩家时,它不起作用:玩家移动得比摄像机快,然后离开了屏幕。
我尝试从地形中移除玩家的大小,但没有任何效果 - 玩家仍然从屏幕中消失。
这是我的代码:
pygame.init()
scsizeX = 600
scsizeY = 400
screen = pygame.display.set_mode ((600, 400))
clock = pygame.time.Clock ()
plrX = 300
plrY = 200
speed = 3
plrOri = "Right"
cameraX = 0
cameraY = 0
genSize = 1
gen = []
background_colour = (255,255,255)
clo = (255,255,0)
pygame.mouse.set_visible(False)
playerimg = pygame.image.load('assets/player.png')
mouseimg = pygame.image.load('assets/mouse.png')
grassimg = pygame.image.load('assets/grass.png')
stoneimg = pygame.image.load('assets/stone.png')
grassimg = pygame.transform.scale(grassimg, (64, 64))
stoneimg = pygame.transform.scale(stoneimg, (64, 64))
mouseimg = pygame.transform.scale(mouseimg, (48, 48))
playerimg = pygame.transform.scale(playerimg, (72, 72))
screen.fill(background_colour)
player = screen.blit(playerimg, (300, 200))
mouse = screen.blit(mouseimg, (300, 200))
for x in range(genSize):
for y in range(genSize):
g = random.randint(1,2)
gen.append(g)
def block(tp, posX, posY):
if tp == "grass":
print(posX, cameraX, plrX)
return screen.blit(grassimg, (posX - cameraX - 32, posY - cameraY - 32))
elif tp == "stone":
return screen.blit(stoneimg, (posX - cameraX - 32, posY - cameraY - 32))
game = True
while game:
screen.fill(background_colour)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouseimg = pygame.transform.scale(mouseimg, (44, 44))
if event.type == pygame.MOUSEBUTTONUP:
mouseimg = pygame.image.load('assets/mouse.png')
mouseimg = pygame.transform.scale(mouseimg, (48, 48))
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
plrY -= speed
if keys[pygame.K_a]:
plrX -= speed
plrOri = "Left"
if keys[pygame.K_s]:
plrY += speed
if keys[pygame.K_d]:
plrX += speed
plrOri = "Right"
cameraX = plrX - (scsizeX / 2)
cameraY = plrY - (scsizeY / 2)
#player = screen.blit(playerimg, (plrX, plrY))
for x in range(genSize):
for y in range(genSize):
g = gen[x + y]
if g == 1:
block("grass", x * 64, y * 64)
elif g == 2:
block("stone", x * 64, y * 64)
if plrOri == "Right":
player = screen.blit(playerimg, (plrX, plrY))
elif plrOri == "Left":
player = screen.blit(pygame.transform.flip(playerimg, True, False), (plrX, plrY))
Mx, My = pygame.mouse.get_pos()
msrct = mouseimg.get_rect()
msrct = msrct.move((Mx, My))
mouse = screen.blit(mouseimg, msrct)
pygame.display.flip()
clock.tick(60)
pygame.quit()
我其实不明白这是什么问题。
感谢阅读。
它确实有效。然而,“相机”定义了场景的视图。所有对象都必须相对于相机绘制。这也适用于播放器。玩家也是场景的一部分:
game = True
while game:
# [...]
p_pos = plrX - cameraX - 32, plrY - cameraY - 32,
if plrOri == "Right":
player = screen.blit(playerimg, p_pos)
elif plrOri == "Left":
player = screen.blit(pygame.transform.flip(playerimg, True, False), p_pos)
我做了一个游戏,但是当我想添加一个摄像机来移动玩家时,它不起作用:玩家移动得比摄像机快,然后离开了屏幕。
我尝试从地形中移除玩家的大小,但没有任何效果 - 玩家仍然从屏幕中消失。
这是我的代码:
pygame.init()
scsizeX = 600
scsizeY = 400
screen = pygame.display.set_mode ((600, 400))
clock = pygame.time.Clock ()
plrX = 300
plrY = 200
speed = 3
plrOri = "Right"
cameraX = 0
cameraY = 0
genSize = 1
gen = []
background_colour = (255,255,255)
clo = (255,255,0)
pygame.mouse.set_visible(False)
playerimg = pygame.image.load('assets/player.png')
mouseimg = pygame.image.load('assets/mouse.png')
grassimg = pygame.image.load('assets/grass.png')
stoneimg = pygame.image.load('assets/stone.png')
grassimg = pygame.transform.scale(grassimg, (64, 64))
stoneimg = pygame.transform.scale(stoneimg, (64, 64))
mouseimg = pygame.transform.scale(mouseimg, (48, 48))
playerimg = pygame.transform.scale(playerimg, (72, 72))
screen.fill(background_colour)
player = screen.blit(playerimg, (300, 200))
mouse = screen.blit(mouseimg, (300, 200))
for x in range(genSize):
for y in range(genSize):
g = random.randint(1,2)
gen.append(g)
def block(tp, posX, posY):
if tp == "grass":
print(posX, cameraX, plrX)
return screen.blit(grassimg, (posX - cameraX - 32, posY - cameraY - 32))
elif tp == "stone":
return screen.blit(stoneimg, (posX - cameraX - 32, posY - cameraY - 32))
game = True
while game:
screen.fill(background_colour)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouseimg = pygame.transform.scale(mouseimg, (44, 44))
if event.type == pygame.MOUSEBUTTONUP:
mouseimg = pygame.image.load('assets/mouse.png')
mouseimg = pygame.transform.scale(mouseimg, (48, 48))
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
plrY -= speed
if keys[pygame.K_a]:
plrX -= speed
plrOri = "Left"
if keys[pygame.K_s]:
plrY += speed
if keys[pygame.K_d]:
plrX += speed
plrOri = "Right"
cameraX = plrX - (scsizeX / 2)
cameraY = plrY - (scsizeY / 2)
#player = screen.blit(playerimg, (plrX, plrY))
for x in range(genSize):
for y in range(genSize):
g = gen[x + y]
if g == 1:
block("grass", x * 64, y * 64)
elif g == 2:
block("stone", x * 64, y * 64)
if plrOri == "Right":
player = screen.blit(playerimg, (plrX, plrY))
elif plrOri == "Left":
player = screen.blit(pygame.transform.flip(playerimg, True, False), (plrX, plrY))
Mx, My = pygame.mouse.get_pos()
msrct = mouseimg.get_rect()
msrct = msrct.move((Mx, My))
mouse = screen.blit(mouseimg, msrct)
pygame.display.flip()
clock.tick(60)
pygame.quit()
我其实不明白这是什么问题。 感谢阅读。
它确实有效。然而,“相机”定义了场景的视图。所有对象都必须相对于相机绘制。这也适用于播放器。玩家也是场景的一部分:
game = True
while game:
# [...]
p_pos = plrX - cameraX - 32, plrY - cameraY - 32,
if plrOri == "Right":
player = screen.blit(playerimg, p_pos)
elif plrOri == "Left":
player = screen.blit(pygame.transform.flip(playerimg, True, False), p_pos)