我制作的相机效果不佳播放器的移动速度比相机快?为什么? [英] The Camera I Made Does Not Work Well Player Moves Faster Than Camera why?

查看:57
本文介绍了我制作的相机效果不佳播放器的移动速度比相机快?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个游戏,但是当我想添加一个摄像头来移动播放器时,它不起作用:播放器的移动速度比摄像头快并离开了屏幕.

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.

这是我的代码:

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)

这篇关于我制作的相机效果不佳播放器的移动速度比相机快?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆