Search code examples
pythonpygamecamerapygame-surface

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.


Solution

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