How to camera scroll Python Pygame












0















I have this camera class im working on and attempting to make the camera follow the player and scroll the background. ive watched dozens of youtube videos and read similar questions on SO but im stuck
heres the source code: (main.py)



import pygame
import player
import enemy
import environment
import collision as collision
import camera

pygame.init()

size = WIDTH, HEIGHT = (640, 480)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Game 2")

FPS = 60
fpsclock = pygame.time.Clock()

player.player1 = player.player(300, 410, 32, 32)
player.player1.initialize()

enemyVector =
enemy.enemy1 = enemy.enemy(160, 170, 32, 32)
enemy.enemy1.initialize()
enemyVector.append(enemy.enemy1)

environmentVector =
#environment.environment1.load_map(environmentVector)




counter1 = 0
counter2 = 0
string = ''
with open('map.txt', 'r') as f:
for line in f:
for character in line:
if character == '0':
pass
elif character == '1':
environment.environment1 = environment.environment(160, 100, 32, 32)
environmentVector.append(environment.environment1)
environmentVector[-1].x = counter1 * 32
environmentVector[-1].y = counter2 * 32
counter1 += 1
counter1 = 0
counter2 += 1

print(environmentVector[1].x)

run = True
while run:
pygame.time.delay(50)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()

if player.player1.active:
player.player1.frame += 1
if player.player1.frame >= 2:
player.player1.frame = 0

for i in enemyVector:
if i.ready_to_move():
i.move()
if i.ready_to_animate():
i.animate()

player.player1.playerImage = player.player1.cells[player.player1.frame + player.player1.animation]



for i in enemyVector:
i.enemyImage = i.cells[i.frame + i.animation]

if keys[pygame.K_LEFT] and player.player1.canMoveLeft and player.player1.x > player.player1.vel:
player.player1.x -= player.player1.vel
player.player1.animation = 3
player.player1.active = True
elif keys[pygame.K_RIGHT] and player.player1.canMoveRight and player.player1.x < 640 - player.player1.width - player.player1.vel:
player.player1.x += player.player1.vel
player.player1.animation = 6
player.player1.active = True
elif keys[pygame.K_UP] and player.player1.canMoveUp and player.player1.y > player.player1.vel:
player.player1.y -= player.player1.vel
player.player1.animation = 9
player.player1.active = True
elif keys[pygame.K_DOWN] and player.player1.canMoveDown and player.player1.y < 480 - player.player1.height - player.player1.vel:
player.player1.y += player.player1.vel
player.player1.animation = 0
player.player1.active = True
elif keys[pygame.K_s]:
player.player1.attack = True
player.player1.animation = 12
else:
player.player1.active = False


collision.check_collision_enemy(enemyVector, environmentVector)
collision.check_collision_player(player.player1, environmentVector)



player.player1.update_xy()

enemy.enemy1.update_xy()

for i in environmentVector:
i.update_xy()


screen.fill((0, 0, 0))

for i in enemyVector:
screen.blit(i.enemyImage, (i.x, i.y))

screen.blit(player.player1.playerImage, (player.player1.x, player.player1.y))


for i in environmentVector:
pygame.draw.rect(screen, (255,255,255), (i.x, i.y, 32, 32))

pygame.display.flip()
fpsclock.tick(FPS)

pygame.quit()


(camera.py):



class Camera:
def __init__(self, players):
self.left_viewbox = display_width/2 - display_width/8
self.right_viewbox = display_width/2 + display_width/10

def follow(self, shift_x):
virtualwindow.x += shift_x
print virtualwindow.x
for i in players:
i.rect.x += shift_x

def viewbox(self):
if player.x <= self.left_viewbox:
view_difference = self.left_viewbox - player.x
player.x = self.left_viewbox
self.follow(view_difference)

if player.x >= self.right_viewbox:
view_difference = self.right_viewbox - player.x
player.x = self.right_viewbox
self.follow(view_difference)


how could this camera class be amended and implemented so that the camera follows the player and scrolls a list of recs in the background










share|improve this question





























    0















    I have this camera class im working on and attempting to make the camera follow the player and scroll the background. ive watched dozens of youtube videos and read similar questions on SO but im stuck
    heres the source code: (main.py)



    import pygame
    import player
    import enemy
    import environment
    import collision as collision
    import camera

    pygame.init()

    size = WIDTH, HEIGHT = (640, 480)
    screen = pygame.display.set_mode(size)

    pygame.display.set_caption("Game 2")

    FPS = 60
    fpsclock = pygame.time.Clock()

    player.player1 = player.player(300, 410, 32, 32)
    player.player1.initialize()

    enemyVector =
    enemy.enemy1 = enemy.enemy(160, 170, 32, 32)
    enemy.enemy1.initialize()
    enemyVector.append(enemy.enemy1)

    environmentVector =
    #environment.environment1.load_map(environmentVector)




    counter1 = 0
    counter2 = 0
    string = ''
    with open('map.txt', 'r') as f:
    for line in f:
    for character in line:
    if character == '0':
    pass
    elif character == '1':
    environment.environment1 = environment.environment(160, 100, 32, 32)
    environmentVector.append(environment.environment1)
    environmentVector[-1].x = counter1 * 32
    environmentVector[-1].y = counter2 * 32
    counter1 += 1
    counter1 = 0
    counter2 += 1

    print(environmentVector[1].x)

    run = True
    while run:
    pygame.time.delay(50)

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    run = False

    keys = pygame.key.get_pressed()

    if player.player1.active:
    player.player1.frame += 1
    if player.player1.frame >= 2:
    player.player1.frame = 0

    for i in enemyVector:
    if i.ready_to_move():
    i.move()
    if i.ready_to_animate():
    i.animate()

    player.player1.playerImage = player.player1.cells[player.player1.frame + player.player1.animation]



    for i in enemyVector:
    i.enemyImage = i.cells[i.frame + i.animation]

    if keys[pygame.K_LEFT] and player.player1.canMoveLeft and player.player1.x > player.player1.vel:
    player.player1.x -= player.player1.vel
    player.player1.animation = 3
    player.player1.active = True
    elif keys[pygame.K_RIGHT] and player.player1.canMoveRight and player.player1.x < 640 - player.player1.width - player.player1.vel:
    player.player1.x += player.player1.vel
    player.player1.animation = 6
    player.player1.active = True
    elif keys[pygame.K_UP] and player.player1.canMoveUp and player.player1.y > player.player1.vel:
    player.player1.y -= player.player1.vel
    player.player1.animation = 9
    player.player1.active = True
    elif keys[pygame.K_DOWN] and player.player1.canMoveDown and player.player1.y < 480 - player.player1.height - player.player1.vel:
    player.player1.y += player.player1.vel
    player.player1.animation = 0
    player.player1.active = True
    elif keys[pygame.K_s]:
    player.player1.attack = True
    player.player1.animation = 12
    else:
    player.player1.active = False


    collision.check_collision_enemy(enemyVector, environmentVector)
    collision.check_collision_player(player.player1, environmentVector)



    player.player1.update_xy()

    enemy.enemy1.update_xy()

    for i in environmentVector:
    i.update_xy()


    screen.fill((0, 0, 0))

    for i in enemyVector:
    screen.blit(i.enemyImage, (i.x, i.y))

    screen.blit(player.player1.playerImage, (player.player1.x, player.player1.y))


    for i in environmentVector:
    pygame.draw.rect(screen, (255,255,255), (i.x, i.y, 32, 32))

    pygame.display.flip()
    fpsclock.tick(FPS)

    pygame.quit()


    (camera.py):



    class Camera:
    def __init__(self, players):
    self.left_viewbox = display_width/2 - display_width/8
    self.right_viewbox = display_width/2 + display_width/10

    def follow(self, shift_x):
    virtualwindow.x += shift_x
    print virtualwindow.x
    for i in players:
    i.rect.x += shift_x

    def viewbox(self):
    if player.x <= self.left_viewbox:
    view_difference = self.left_viewbox - player.x
    player.x = self.left_viewbox
    self.follow(view_difference)

    if player.x >= self.right_viewbox:
    view_difference = self.right_viewbox - player.x
    player.x = self.right_viewbox
    self.follow(view_difference)


    how could this camera class be amended and implemented so that the camera follows the player and scrolls a list of recs in the background










    share|improve this question



























      0












      0








      0








      I have this camera class im working on and attempting to make the camera follow the player and scroll the background. ive watched dozens of youtube videos and read similar questions on SO but im stuck
      heres the source code: (main.py)



      import pygame
      import player
      import enemy
      import environment
      import collision as collision
      import camera

      pygame.init()

      size = WIDTH, HEIGHT = (640, 480)
      screen = pygame.display.set_mode(size)

      pygame.display.set_caption("Game 2")

      FPS = 60
      fpsclock = pygame.time.Clock()

      player.player1 = player.player(300, 410, 32, 32)
      player.player1.initialize()

      enemyVector =
      enemy.enemy1 = enemy.enemy(160, 170, 32, 32)
      enemy.enemy1.initialize()
      enemyVector.append(enemy.enemy1)

      environmentVector =
      #environment.environment1.load_map(environmentVector)




      counter1 = 0
      counter2 = 0
      string = ''
      with open('map.txt', 'r') as f:
      for line in f:
      for character in line:
      if character == '0':
      pass
      elif character == '1':
      environment.environment1 = environment.environment(160, 100, 32, 32)
      environmentVector.append(environment.environment1)
      environmentVector[-1].x = counter1 * 32
      environmentVector[-1].y = counter2 * 32
      counter1 += 1
      counter1 = 0
      counter2 += 1

      print(environmentVector[1].x)

      run = True
      while run:
      pygame.time.delay(50)

      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      run = False

      keys = pygame.key.get_pressed()

      if player.player1.active:
      player.player1.frame += 1
      if player.player1.frame >= 2:
      player.player1.frame = 0

      for i in enemyVector:
      if i.ready_to_move():
      i.move()
      if i.ready_to_animate():
      i.animate()

      player.player1.playerImage = player.player1.cells[player.player1.frame + player.player1.animation]



      for i in enemyVector:
      i.enemyImage = i.cells[i.frame + i.animation]

      if keys[pygame.K_LEFT] and player.player1.canMoveLeft and player.player1.x > player.player1.vel:
      player.player1.x -= player.player1.vel
      player.player1.animation = 3
      player.player1.active = True
      elif keys[pygame.K_RIGHT] and player.player1.canMoveRight and player.player1.x < 640 - player.player1.width - player.player1.vel:
      player.player1.x += player.player1.vel
      player.player1.animation = 6
      player.player1.active = True
      elif keys[pygame.K_UP] and player.player1.canMoveUp and player.player1.y > player.player1.vel:
      player.player1.y -= player.player1.vel
      player.player1.animation = 9
      player.player1.active = True
      elif keys[pygame.K_DOWN] and player.player1.canMoveDown and player.player1.y < 480 - player.player1.height - player.player1.vel:
      player.player1.y += player.player1.vel
      player.player1.animation = 0
      player.player1.active = True
      elif keys[pygame.K_s]:
      player.player1.attack = True
      player.player1.animation = 12
      else:
      player.player1.active = False


      collision.check_collision_enemy(enemyVector, environmentVector)
      collision.check_collision_player(player.player1, environmentVector)



      player.player1.update_xy()

      enemy.enemy1.update_xy()

      for i in environmentVector:
      i.update_xy()


      screen.fill((0, 0, 0))

      for i in enemyVector:
      screen.blit(i.enemyImage, (i.x, i.y))

      screen.blit(player.player1.playerImage, (player.player1.x, player.player1.y))


      for i in environmentVector:
      pygame.draw.rect(screen, (255,255,255), (i.x, i.y, 32, 32))

      pygame.display.flip()
      fpsclock.tick(FPS)

      pygame.quit()


      (camera.py):



      class Camera:
      def __init__(self, players):
      self.left_viewbox = display_width/2 - display_width/8
      self.right_viewbox = display_width/2 + display_width/10

      def follow(self, shift_x):
      virtualwindow.x += shift_x
      print virtualwindow.x
      for i in players:
      i.rect.x += shift_x

      def viewbox(self):
      if player.x <= self.left_viewbox:
      view_difference = self.left_viewbox - player.x
      player.x = self.left_viewbox
      self.follow(view_difference)

      if player.x >= self.right_viewbox:
      view_difference = self.right_viewbox - player.x
      player.x = self.right_viewbox
      self.follow(view_difference)


      how could this camera class be amended and implemented so that the camera follows the player and scrolls a list of recs in the background










      share|improve this question
















      I have this camera class im working on and attempting to make the camera follow the player and scroll the background. ive watched dozens of youtube videos and read similar questions on SO but im stuck
      heres the source code: (main.py)



      import pygame
      import player
      import enemy
      import environment
      import collision as collision
      import camera

      pygame.init()

      size = WIDTH, HEIGHT = (640, 480)
      screen = pygame.display.set_mode(size)

      pygame.display.set_caption("Game 2")

      FPS = 60
      fpsclock = pygame.time.Clock()

      player.player1 = player.player(300, 410, 32, 32)
      player.player1.initialize()

      enemyVector =
      enemy.enemy1 = enemy.enemy(160, 170, 32, 32)
      enemy.enemy1.initialize()
      enemyVector.append(enemy.enemy1)

      environmentVector =
      #environment.environment1.load_map(environmentVector)




      counter1 = 0
      counter2 = 0
      string = ''
      with open('map.txt', 'r') as f:
      for line in f:
      for character in line:
      if character == '0':
      pass
      elif character == '1':
      environment.environment1 = environment.environment(160, 100, 32, 32)
      environmentVector.append(environment.environment1)
      environmentVector[-1].x = counter1 * 32
      environmentVector[-1].y = counter2 * 32
      counter1 += 1
      counter1 = 0
      counter2 += 1

      print(environmentVector[1].x)

      run = True
      while run:
      pygame.time.delay(50)

      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      run = False

      keys = pygame.key.get_pressed()

      if player.player1.active:
      player.player1.frame += 1
      if player.player1.frame >= 2:
      player.player1.frame = 0

      for i in enemyVector:
      if i.ready_to_move():
      i.move()
      if i.ready_to_animate():
      i.animate()

      player.player1.playerImage = player.player1.cells[player.player1.frame + player.player1.animation]



      for i in enemyVector:
      i.enemyImage = i.cells[i.frame + i.animation]

      if keys[pygame.K_LEFT] and player.player1.canMoveLeft and player.player1.x > player.player1.vel:
      player.player1.x -= player.player1.vel
      player.player1.animation = 3
      player.player1.active = True
      elif keys[pygame.K_RIGHT] and player.player1.canMoveRight and player.player1.x < 640 - player.player1.width - player.player1.vel:
      player.player1.x += player.player1.vel
      player.player1.animation = 6
      player.player1.active = True
      elif keys[pygame.K_UP] and player.player1.canMoveUp and player.player1.y > player.player1.vel:
      player.player1.y -= player.player1.vel
      player.player1.animation = 9
      player.player1.active = True
      elif keys[pygame.K_DOWN] and player.player1.canMoveDown and player.player1.y < 480 - player.player1.height - player.player1.vel:
      player.player1.y += player.player1.vel
      player.player1.animation = 0
      player.player1.active = True
      elif keys[pygame.K_s]:
      player.player1.attack = True
      player.player1.animation = 12
      else:
      player.player1.active = False


      collision.check_collision_enemy(enemyVector, environmentVector)
      collision.check_collision_player(player.player1, environmentVector)



      player.player1.update_xy()

      enemy.enemy1.update_xy()

      for i in environmentVector:
      i.update_xy()


      screen.fill((0, 0, 0))

      for i in enemyVector:
      screen.blit(i.enemyImage, (i.x, i.y))

      screen.blit(player.player1.playerImage, (player.player1.x, player.player1.y))


      for i in environmentVector:
      pygame.draw.rect(screen, (255,255,255), (i.x, i.y, 32, 32))

      pygame.display.flip()
      fpsclock.tick(FPS)

      pygame.quit()


      (camera.py):



      class Camera:
      def __init__(self, players):
      self.left_viewbox = display_width/2 - display_width/8
      self.right_viewbox = display_width/2 + display_width/10

      def follow(self, shift_x):
      virtualwindow.x += shift_x
      print virtualwindow.x
      for i in players:
      i.rect.x += shift_x

      def viewbox(self):
      if player.x <= self.left_viewbox:
      view_difference = self.left_viewbox - player.x
      player.x = self.left_viewbox
      self.follow(view_difference)

      if player.x >= self.right_viewbox:
      view_difference = self.right_viewbox - player.x
      player.x = self.right_viewbox
      self.follow(view_difference)


      how could this camera class be amended and implemented so that the camera follows the player and scrolls a list of recs in the background







      python scroll camera pygame






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 23:34









      Vivek Pabani

      419311




      419311










      asked Nov 27 '18 at 20:48









      travisp4travisp4

      145




      145
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53507912%2fhow-to-camera-scroll-python-pygame%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53507912%2fhow-to-camera-scroll-python-pygame%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)