title: Can I get the rect.x from one class to use in another class?
body: <p>So basically I want to my Road class to always know where my Car (Player1) class is so road knows whether to slow down as Car has left road (or be able to speed back up when Car gets back within road area.</p>
  
  <p>As Car only moves left / right and it is the road that speeds up coming down screen, I figured a way to mimic the car using a "self.where_player" at same start position and then add / subtract x position and follow it with key's pressed (a, d), but after awhile it can lose Car placement as Car can either have x values added or subtracted while slipping left or right on ice or have it keys (a, d) temporarily disabled if spinning on oil, while the road does not know these things...</p>
  
  <p>If I could figure out how the Road class could "always" know where the Car class rect.x was, well that would be fantastic!</p>
  
  <pre><code>import pygame
  
  W = 1000
  H = 800
  pygame.init()
  pygame.display.set_mode((W, H))
  
  class Road1(pygame.sprite.Sprite):
      def __init__(self, top, player):
          pygame.sprite.Sprite.__init__(self)
          bkgrnd = pygame.image.load("Images/Road.png").convert_alpha()
          self.image = pygame.transform.scale(bkgrnd, (300, 100))
          self.rect = self.image.get_rect(topleft = (120, top))
          self.top = top
          self.speedy = 0
          self.player = player
          self.out_of_bounds = False
  
      def update(self):
          self.top += self.speedy
          if self.top &gt;= 800:
              self.top = self.top - 900
          self.rect.top = self.top
          keys = pygame.key.get_pressed()
          if keys[pygame.K_w]:
              self.speedy = min(20, self.speedy + 0.25)
          if keys[pygame.K_s]:
              self.speedy = max(0, self.speedy - 0.25)
  
          if keys[pygame.K_a]:
              self.player -= 5
          if self.player &lt;= 120:
              self.player = 120
              self.out_of_bounds = True
              if self.out_of_bounds and self.speedy &gt; 5:
                  self.speedy -= 0.35
              else:
                  self.out_of_bounds = False
  
          if keys[pygame.K_d]:
              self.player += 5
          if self.player &gt;= 420:
              self.player = 420
              self.out_of_bounds = True
              if self.out_of_bounds and self.speedy &gt; 5:
                  self.speedy -= 0.35
              else:
                  self.out_of_bounds = False
  </code></pre>
  
  <p>The following is a portion from the Main page enough to make Road work:</p>
  
  <pre><code>import pygame
  import random
  import sys
  # from Ice_Patch import Ice1, Ice2
  # from Oil_Slick import Oil1, Oil2
  from Player1 import Player1, car1_image
  # from Player2 import Player2, car2_image
  from Roads import Road1, # Road2
  
  W, H = 1000, 800
  HW, HH = W / 2, H / 2
  AREA = W * H
  FPS = 30
  GREEN = (0, 200, 0)
  pygame.init()
  CLOCK = pygame.time.Clock()
  DS = pygame.display.set_mode((W, H))
  pygame.display.set_caption("Racing Fury")
  
  
  def events():
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              pygame.quit()
              sys.exit()
  
  player = Player1()
  
  all_sprites = pygame.sprite.Group()
  road = pygame.sprite.Group()
  for i in range(9):
      rd = Road1((i - 1) * 100, player)
      all_sprites.add(rd)
      road.add(rd)
  
  while True:
      CLOCK.tick(FPS)
      events()
      all_sprites.update()
      DS.fill(GREEN)
      all_sprites.draw(DS)
      pygame.display.flip()
  </code></pre>
  
  <p>Player1 Class:</p>
  
  <pre><code>import pygame
  W = 1000
  H = 800
  pygame.init()
  pygame.display.set_mode((W, H))
  car1_image = pygame.image.load("Images/GalardB.png").convert_alpha()
  car1_image = pygame.transform.scale(car1_image, (40, 70))
  GREY = (211, 211, 211)
  ANIM_DELAY = 50
  POWERUP_TIME = 5000
  SPIN_TIME = 1950
  
  class Player1(pygame.sprite.Sprite):
      def __init__(self, car1_image, x, y):
          pygame.sprite.Sprite.__init__(self)
          self.image = car1_image
          self.image.set_colorkey(GREY)
          self.rect = self.image.get_rect()
          self.rect.center = (x, y)
          self.radius = 25
          self.speedx = 0
          self.rotations = [self.image]
          self.spinning_since = 0
          self.rotate()
          self.damage = 100
          self.shoot_delay = 250
          self.last_shot = pygame.time.get_ticks()
          self.lives = 3
          self.hidden = False
          self.hide_timer = pygame.time.get_ticks()
          self.score = 0
          self.crashx = 0
          self.crashy = 0
          self.power = 0
          self.power_time = pygame.time.get_ticks()
          self.not_spinning = True
  
      def update(self):
          if self.power &gt;= 1 and pygame.time.get_ticks() - self.power_time &gt; POWERUP_TIME:
              self.power -= 1
              self.power_time = pygame.time.get_ticks()
          if self.hidden and pygame.time.get_ticks() - self.hide_timer &gt; 1500:
              self.hidden = False
              self.rect.center = (300, 700)
          self.speedx = 0
          keystate1 = pygame.key.get_pressed()
          if keystate1[pygame.K_SPACE]:
              self.shoot()
          if self.not_spinning:
              if keystate1[pygame.K_a]:
                  self.speedx = -5
              if keystate1[pygame.K_d]:
                  self.speedx = 5
          self.rect.x += self.speedx
          if self.rect.right &gt; 420:
              self.rect.right = 420
          if self.rect.left &lt; 120:
              self.rect.left = 120
          if self.spinning_since &gt; 0:
              time_spinning = pygame.time.get_ticks() - self.spinning_since
              if time_spinning &gt;= SPIN_TIME:
                  self.spinning_since = 0
                  index = 0
                  self.not_spinning = True
              else:
                  index = int(time_spinning / ANIM_DELAY)
                  index %= len(self.rotations)
                  self.not_spinning = False
              cx, cy = self.rect.center
              self.image = self.rotations[index]
              self.rect = self.image.get_rect()
              self.rect.centerx = cx
              self.rect.centery = cy
  
      def powerup(self):
          self.power += 1
          self.power_time = pygame.time.get_ticks()
  
      def shoot(self):
          now = pygame.time.get_ticks()
          if now - self.last_shot &gt; self.shoot_delay:
              self.last_shot = now
              if self.power == 0:
                  pass
              if self.power &gt;= 1:
                  bullet1 = Bullet(self.rect.centerx, self.rect.top)
                  all_sprites.add(bullet1)
                  bullets1.add(bullet1)
  
      def rotate(self):
          ROT_FRAMES = 36
          for i in range(ROT_FRAMES):
              angle = i * (720 / ROT_FRAMES)
              rotated_image = pygame.transform.rotozoom(self.image, angle, 1)
              self.rotations.append(rotated_image)
  
      def spin(self):
          if self.spinning_since == 0:
              self.spinning_since = pygame.time.get_ticks()
          else:
              pass
  
      def slide(self):
          keystate1 = pygame.key.get_pressed()
          if keystate1[pygame.K_a]:
              self.rect.x -= 2
              self.rect.x -= 1.5
              self.rect.x -= 1
              self.rect.x -= 0.5
              self.rect.x -= 0.25
          if keystate1[pygame.K_d]:
              self.rect.x += 2
              self.rect.x += 1.5
              self.rect.x += 1
              self.rect.x += 0.5
              self.rect.x += 0.25
  
      def hide(self):
          self.crashx = self.rect.x
          self.crashy = self.rect.y
          self.hidden = True
          self.hide_timer = pygame.time.get_ticks()
          self.rect.center = (W * 2, H * 2)
  </code></pre>