简体   繁体   English

我做的相机不好用,播放器移动速度比相机快,为什么?

[英]The Camera I Made Does Not Work Well Player Moves Faster Than Camera why?

I made a game but when I want to add a camera to move the player it does not work: the player moves faster than the camera and leaves the screen.我做了一个游戏,但是当我想添加一个相机来移动玩家时它不起作用:玩家移动得比相机快并离开屏幕。

I tried removing the size of the player from the terrain but nothing works - the player still passes out of the screen.我尝试从地形中删除玩家的大小,但没有任何效果 - 玩家仍然从屏幕中消失。

Here is my code:这是我的代码:

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()

I actually don't understand what is the problem.我其实不明白是什么问题。 Thank you for reading.感谢您的阅读。

It actually works.它确实有效。 The "camera", however, defines the view of the scene.然而,“相机”定义了场景的视图。 All objects must be drawn relative to the camera.所有对象都必须相对于相机进行绘制。 This also applies to the player.这也适用于播放器。 The player is also a part of the scene:玩家也是场景的一部分:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM