text: When I started to play with the player movement physics, I went back to the original prototype that grew into the game idea. I re-wrote my code to better optimize it but in both the original prototype and the re-written one I have movement issues. This video of the prototype is fairly accurate with very little video capture lag. Pay attention to the gaps between the buildings, as they get within the right most 3rd of the display area, the gap will suddenly shrink as if the building to the left of the gap is suddenly shifting right a few pixels. The smaller the gap, the more noticeable it is. Here's a very short video that clearly shows this phenomenon: ... e=youtu.be
  At about 1 second in you'll notice a very narrow gap between buildings in the rear layer. At :04 seconds in the gap is even with the blue player object, the left rectangle shifts and the gap vanishes. There's a second, larger gap to the right of that one that does the same thing but since the gap is larger it doesn't completely vanish. I've looked over the code numerous times but can't see what could be causing this anomaly. I'm hoping someone can tell me if it's something I did or a limitation I'm encountering.
  here's the code I created for this:
  - Code: Select all
  import pygame
  import sys
  import random
  from pygame.locals import *
  # set up pygame
  pygame.init()
  mainClock = pygame.time.Clock()
  # set up window
  WINDOWWIDTH = 400
  WINDOWHEIGHT = 400
  windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
  pygame.display.set_caption('DEMO #1: City Skyline')
  # set up the colors
  BLACK = (0, 0, 0)
  WHITE = (255, 255, 255)
  DARK = (38, 16, 30)
  MEDIUM = (74, 34, 58)
  BRIGHT = (112, 52, 88)
  SKY = (70, 10, 88)
  BLUE = (0, 0, 255)
  class Scroller():
  def __init__(self, speed, color, heightMax):
  # Speed of the layer scroll, the color of the layer and the maximum height for buildings
  # set up the building parameters
  self.buildingHeightMax = heightMax
  self.buildingHeightMin = 100
  self.buildingWidthMax = 125
  self.buildingWidthMin = 75
  self.buildings = []
  self.layerspeed = speed
  self.buildTime = True
  self.buildCountdown = 10
  self.color = color
  def update(self):
  # Check if it's time to build. If not, decrement counter
  if self.buildTime == False:
  self.buildCountdown -= 1
  # If time is 0, time to build, reset counter to a new random time
  if self.buildCountdown <= 0:
  self.buildTime = True
  self.buildCountdown = random.randint(3, self.layerspeed)
  # create front layer building if it's time
  if self.buildTime:
  # generate random width and height of building
  buildingHeight = random.randint(self.buildingHeightMin, self.buildingHeightMax)
  buildingWidth = random.randint(self.buildingWidthMin, self.buildingWidthMax)
  buildingTop = WINDOWHEIGHT - buildingHeight
  # This generates the building object from the above parameters
  building = pygame.Rect(WINDOWWIDTH, buildingTop, buildingWidth, WINDOWHEIGHT)
  self.buildTime = False
  self.buildCountdown = random.randint(3, self.layerspeed * 5)
  # add building to buildings list
  self.buildings.append(building)
  # move all buildings on layer at set speed
  for building in self.buildings:
  # if the building is off the screen, trash it. If not, move it to the
  # right at the objects speed.
  if building.right < 0:
  self.buildings.remove(building)
  else:
  building.left -= self.layerspeed
  # draw the Front buildings
  for i in range(len(self.buildings)):
  pygame.draw.rect(windowSurface, self.color, self.buildings[i])
  def main():
  # set up the player
  player = pygame.Rect(50, 50, 20, 10)
  moveUp = False
  moveDown = False
  moveRight = False
  moveLeft = False
  player_vert_speed = 4
  player_forward_speed = 3
  player_reverse_speed = 7
  # Set up scrolling background layers
  # Works best when front row is darker than middle
  # and back row is lightest
  city_back_row = Scroller(3, BRIGHT, 350)
  city_middle_row = Scroller(5, MEDIUM, 300)
  city_front_row = Scroller(7, DARK, 250)
  # run the main loop
  while True:
  for event in pygame.event.get():
  if event.type == QUIT:
  pygame.quit()
  sys.exit()
  if event.type == KEYDOWN:
  if event.key == K_UP or event.key == ord('w'):
  moveUp = True
  moveDown = False
  if event.key == K_DOWN or event.key == ord('s'):
  moveUp = False
  moveDown = True
  if event.key == K_RIGHT or event.key == ord('d'):
  moveRight = True
  moveLeft = False
  if event.key == K_LEFT or event.key == ord('a'):
  moveLeft = True
  moveRight = False
  if event.type == KEYUP:
  if event.key == K_ESCAPE:
  pygame.quit()
  sys.exit()
  if event.key == K_UP or event.key == ord('w'):
  moveUp = False
  if event.key == K_DOWN or event.key == ord('s'):
  moveDown = False
  if event.key == K_RIGHT or event.key == ord ('d'):
  moveRight = False
  if event.key == K_LEFT or event.key == ord ('a'):
  moveLeft = False
  # Move the player
  if moveDown and player.bottom < (WINDOWHEIGHT - 10):
  player.bottom += player_vert_speed
  if moveUp and player.top > 10:
  player.top -= player_vert_speed
  if moveRight and player.right < (WINDOWWIDTH - 10):
  player.right += player_forward_speed
  if moveLeft and player.left > 10:
  player.left -= player_reverse_speed
  # draw the sky color background onto the surface
  windowSurface.fill(SKY)
  # Create 3 parallax scrollers for background
  city_back_row.update()
  city_middle_row.update()
  city_front_row.update()
  # draw the player
  pygame.draw.rect(windowSurface, BLUE, player)
  # draw the scene
  pygame.display.update()
  mainClock.tick(30)
  main()