python pygame (scroll camera with player)

Viewed 85

I am using pygame framework to implement my game in Python. Below is my code.

I do not know how to properly ask questions at StackOverflow, so I have included the whole program so that you can see all the details.

I would like to have a camera that would follow my player, but I do not know how to do that.

Could you, please, help me?

import pygame
pygame.init()

class Oyun():
    def __init__(self):
        self.x = 20
        pygame.display.set_caption("Cabbar_Oyunda_Vol01")
        self.uzunluk = 1280
        self.genişlik = 800
        self.pencere = pygame.display.set_mode((self.uzunluk, self.genişlik))
        self.area = self.uzunluk * self.genişlik
        self.clock = pygame.time.Clock()
        self.adam = pygame.image.load("R8E.png")
        self.bg = pygame.image.load("mountains.png").convert()
        self.bgWidth, self.bgHeight = self.bg.get_rect().size
        self.stageWidth = self.bgWidth * 2
        self.stagePosX=0
        self.startScrollingPosX=self.uzunluk/2
        self.circleRadius=25
        self.circlePosX=self.circleRadius
        self.playerPosX = self.circleRadius
        self.playerPosY = 685
        self.playerVelocityX = 0
        self.right = False
        self.left = False
        self.walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load("L3.png"), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
        self.walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'),pygame.image.load('R9.png')]
        self.walkCount=0
        self.canavar_left=[pygame.image.load("L1E.png"),pygame.image.load("L2E.png"),pygame.image.load("L3E.png"),pygame.image.load("L4E.png"),pygame.image.load("L5E.png"),pygame.image.load("L6E.png"),pygame.image.load("L7E.png"),pygame.image.load("L8E.png"),pygame.image.load("L9E.png")]
        self.canavar_right = [pygame.image.load("R1E.png"),  pygame.image.load("R2E.png"), pygame.image.load("R3E.png"),pygame.image.load("R4E.png"), pygame.image.load("R5E.png"), pygame.image.load("R6E.png"),pygame.image.load("R7E.png"), pygame.image.load("R8E.png"), pygame.image.load("R9E.png")]
        self.c_walkount=0
        self.isJump = False
        self.jumpCount = 10
        self.sayac=0
        self.yorgunluk_x=70
        self.font=pygame.font.SysFont("Arial",17)
        self.mermi=pygame.image.load("bullet.png")
        self.mermi_x=0
        self.mermi_y=0
        self.mermi_stage="ready"
        self.deneme=pygame.image.load("bg.jpg")
        self.a = self.stagePosX + 1300
        self.canavar=False
        pygame.mixer.music.load("background.wav")
        pygame.mixer.music.play(-1)
        self.yon=1
        self.scroll=[0,0]

    def move(self):
        self.clock.tick(40)
        if self.walkCount + 1 >= 27:
            self.walkCount = 0
        if self.right:
            self.pencere.blit(self.walkRight[self.walkCount // 3], (self.circlePosX, self.playerPosY))
            self.walkCount += 1

        elif self.left:
            self.pencere.blit(self.walkLeft[self.walkCount // 3], (self.circlePosX, self.playerPosY))
            self.walkCount += 1
        else:
            self.pencere.blit(self.walkRight[0], (self.circlePosX, self.playerPosY))
        if self.canavar:
            if self.a>1400 or self.a<1200:
                self.yon*=-1
            self.a += 2 * self.yon

    def fire_bullet(self,circlePosX,playerPosY):
        self.mermi_stage="fire"
        self.pencere.blit(self.mermi,(circlePosX+40,playerPosY+20))

    def kontrol(self):
        keys=pygame.key.get_pressed()
        if keys[pygame.K_SPACE]:
            if self.mermi_stage is "ready":
                self.mermi_x=self.circlePosX
                self.mermi_y=self.playerPosY
                self.fire_bullet(self.mermi_x,self.mermi_y)
                self.mermi_ses = pygame.mixer.Sound("laser.wav")
                self.mermi_ses.play()

        if self.mermi_stage is "fire":
            self.fire_bullet(self.mermi_x,self.mermi_y)
            self.mermi_x+=18
        if self.mermi_x>=1280:
            self.mermi_x=self.circlePosX
            self.mermi_stage="ready"

        self.pencere.blit(self.adam, (self.a, 685))

    def Game(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return "son"
        self.pencere.blit(self.bg, (0, 0))
        keys = pygame.key.get_pressed()
        if keys[pygame.K_RIGHT]:
            self.left = False
            self.right = True
            self.circlePosX += 5

        elif keys[pygame.K_LEFT]:
            self.left = True
            self.right = False
            self.circlePosX=-5


        if not (self.isJump):
            if keys[pygame.K_UP]:
                self.isJump = True
                self.left = False
                self.right = True
                self.walkCount = 0
                self.sayac += 1
        else:
            if self.jumpCount >= -10:
                self.playerPosY -= (self.jumpCount * abs(self.jumpCount)) * 0.5
                self.jumpCount -= 1
            else:
                self.jumpCount = 10
                self.isJump = False
        self.canavar=True
        self.move()
        self.kontrol()
        pygame.display.update()

Game = Oyun()
while True:
    Durum = Game.Game()
    if Durum == "son":
        break
1 Answers

A simple(but not most efficient) way to do this would be to move the background and all other assets, while keeping the player stationary.

# insert code in each object in background/assets
if right:
    OBJECT.right()  # code to change location
if left:
    OBJECT.left()  #code to change location

Also, off-topic, but it is easier to have a single direction variable with -1 for left, 0 for stationary, and 1 for right. You can also easily add speed to this, too, so keep this in mind and implement it if you can.

Related