1

In pygame, I am trying to make a scrolling background that has a border in which a player(the circle) can move about in. I am trying to make it so when a player reaches the middle of the screen, the player stays in the middle, of either x or y, and then the background moves. When the player reaches the border (the edge of the image), the background will stop moving and the player will move instead. I have kind of done this, however the player cannot move left/right without first either pressing up/down and the character does not stay in the middle when moving down. Here is my code:

import pygame

#setting variables
width, height = 650, 650
half_width, half_height = width/2, height/2

#defining colours
white = (255, 255, 255)

#general setup
pygame.init()
clock = pygame.time.Clock()

#setting up screen
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Treasure Hunt Game")
background = pygame.image.load("galaxy.jpeg").convert()
bg = pygame.transform.scale(background, (1950, 1950))
bg_width, bg_height = bg.get_rect().size

#stage variables
stage_width, stage_height = bg_width, bg_height
stage_x, stage_y = 0, 0

#when to start scrolling (middle of screen)
start_scrolling_x = half_width
start_scrolling_y = half_height

#cirlce properties
circle_radius = 25
circle_x = circle_radius
circle_y = circle_radius

#player properties
player_x = circle_radius
player_y = circle_radius
player_velx = 0
player_vely = 0

#setting up events - if screen shut down
def events():
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      running = False


running = True

#------------------- main loop -------------------
while running:
  events()

  #key functions
  key = pygame.key.get_pressed()
  dist = 5
  if key[pygame.K_RIGHT]:
    player_velx = dist
  elif key[pygame.K_LEFT]:
    player_velx = -dist
  if key[pygame.K_UP]:
    player_vely = -dist
  elif key[pygame.K_DOWN]:
    player_vely = dist
  else:
    player_velx = 0
    player_vely = 0

  player_x += player_velx
  player_y += player_vely

  #controlling the x-axis
  if player_x > stage_width - circle_radius:
    player_x = stage_width - circle_radius
  if player_x < circle_radius:
    player_x = circle_radius
  if player_x < start_scrolling_x:
    circle_x = player_x
  elif player_x > stage_width - start_scrolling_x:
    circle_x = player_x - stage_width + width
  else:
    circle_x = start_scrolling_x
    stage_x += -player_velx

  #controlling the y axis
  if player_y > stage_height - circle_radius:
    player_y = stage_height - circle_radius
  if player_y < circle_radius:
    player_y = circle_radius
  if player_y < start_scrolling_y:
    circle_y = player_y
  elif player_y > stage_height - start_scrolling_y:
    circle_y = player_y - stage_height + height
  else:
    circle_y = start_scrolling_y
    stage_y += -player_vely

  # drawing the circle and displaying the screen
 
  screen.blit(bg, (stage_x - width, 0))
  if stage_x < width:
    screen.blit(bg, (stage_x, 0))
    
  pygame.draw.circle(screen, white, (circle_x, player_y - circle_radius), circle_radius, 0)

  pygame.display.update()
  clock.tick(30)



pygame.quit()
milljones
  • 21
  • 2
  • 1
    it called `"camera"` and few days aga was similar question [Basic camera system for my pygame platformer](https://stackoverflow.com/questions/70297648/basic-camera-system-for-my-pygame-platformer/70298178#70298178) – furas Dec 15 '21 at 02:33

0 Answers0