## Better way of repeating background movement?

### Better way of repeating background movement?

Hello everyone!

I am new to the forums so help me out if I posted this incorrectly. I am new to python as well and I am learning kind of as I go.

So I have began creating a small game that is a driving / avoiding obstacle kind of game. You simply don't want to crash. Now I have my background called road. The road is simply just a still image of the asphalt. So I am trying to make the stripes on the road move down and the grass outside of the road move down to simulate that the car is actually moving forward.

Now the code that I have works, and worked better than I expected. Except I really am sure there is a better way of doing it. I wanted to see if you guys can help me figure it out. My assumption is that I can use a for or while loop to create two of the grass/stripe objects to make it always continuously move down. Another note about the two images that are moving down is that they are both 800 x 600 (the size of the display) and they are transparent, just sitting on top of the road pretty much.

So here is the code that is related to the background images:

The functions being defined:
Code: Select all
`def road(x,y):   gameDisplay.blit(roadImg,(x,y))def grass(x,y):   gameDisplay.blit(grassImg,(x,y))   def grass2(x,y):   gameDisplay.blit(grassImg,(x,y))   def stripe(x,y):   gameDisplay.blit(stripeImg,(x,y))`

This next part of code is inside of the game loop itself:
Code: Select all
`#these variables are defined before the while loop begins inside of the game loop function.   grass_speed = 10   grass_y = 0   grass2_y = -600   stripe_y = 0   stripe2_y = -600      #these are within the while loop after the game loop function, after the event handling but before the logic handling.      road(0,0)      stripe(0,stripe_y)      stripe2(0,stripe2_y)      stripe_y += grass_speed      stripe2_y += grass_speed         grass(0,grass_y)      grass2(0,grass2_y)      grass_y += grass_speed      grass2_y += grass_speed      #This is what resets the pictures to their original location to give the appearance that the grass and the stripes are never ending.      if stripe_y > display_height:         stripe_y = 0         stripe2_y = -600            if grass_y > display_height:         grass_y = 0         grass2_y = -600      `
Last edited by micseydel on Thu Jun 09, 2016 4:15 pm, edited 1 time in total.
Reason: Initial post lock. Moved to game dev.
blpeter

Posts: 2
Joined: Thu Jun 09, 2016 2:56 pm

### Re: Better way of repeating background movement?

So I have gotten multiple vehicles to appear and contact can be made with the vehicles.. Except it seems like my car will randomly crash every now and then into thin air. I have tested to see which arguments are causing the crash and it is the one that says you crashed into x. Can anyone help me figure this out? Here is the full code:

Code: Select all
`#Importsimport pygame, time, random#initialize pygamepygame.init()#resolutiondisplay_width = 800display_height = 600#define our colorsblack = (0,0,0)white = (255,255,255)red = (255,0,0)block_color = (53, 115, 255)#image demensionsmoto_width = 45 #needs to be adjusted.veh_width = 42#load the vehiclesmotoImg = pygame.image.load('moto.png')roadImg = pygame.image.load('road.png')grassImg = pygame.image.load('grass.png')stripeImg = pygame.image.load('roadstripes.png')ambImg = pygame.image.load('Ambulance.png')audiImg = pygame.image.load('Audi.png')copImg = pygame.image.load('Police.png')viperImg = pygame.image.load('Black_viper.png')#set up the actual windowscreenDisplay = pygame.display.set_mode((display_width,display_height))pygame.display.set_caption('Dont Hit It!')clock = pygame.time.Clock()#classesclass Vehicle(object):   def __init__(self, vehW, vehH, vehImg, surface, vehSpd):      self.vehicle_w = vehW      self.vehicle_h = vehH      self.vehicle_speed = vehSpd      self.vehicle_img = vehImg      self.vehicle_y = random.randrange(0 - 600 + self.vehicle_h, 0 - self.vehicle_h)      self.vehicle_x = random.randrange(139,660 - self.vehicle_w)      self.screenDisplay = surface         def veh_move(self):      self.vehicle_y += self.vehicle_speed      self.screenDisplay.blit(self.vehicle_img, (self.vehicle_x, self.vehicle_y))         def update(self):      self.screenDisplay.blit(self.vehicle_img, (self.vehicle_x, self.vehicle_y))#functionsdef things_dodged(count):   font = pygame.font.SysFont(None, 25)   text = font.render("Dodged: "+str(count), True, black)   screenDisplay.blit(text, (0,0))def things(thingx, thingy, thingw, thingh, color):   pygame.draw.rect(screenDisplay, color, [thingx, thingy, thingw, thingh])def car(x,y):   screenDisplay.blit(motoImg,(x,y))   def road(x,y):   screenDisplay.blit(roadImg,(x,y))def grass(x,y):   screenDisplay.blit(grassImg,(x,y))   def grass2(x,y):   screenDisplay.blit(grassImg,(x,y))   def stripe(x,y):   screenDisplay.blit(stripeImg,(x,y))   def stripe2(x,y):   screenDisplay.blit(stripeImg,(x,y))   def text_objects(text, font):   textSurface = font.render(text, True, black)   return textSurface, textSurface.get_rect()   def message_display(text):   largeText = pygame.font.Font('freesansbold.ttf',115) #freesansbold.ttf   TextSurf, TextRect = text_objects(text, largeText)   TextRect.center = ((display_width/2),(display_height/2))   screenDisplay.blit(TextSurf, TextRect)      pygame.display.update()      time.sleep(2)      game_loop()'''   def game_intro():      intro = True      while intro:      for event in pygame.event.get():         if event.type == pygame.QUIT:            pygame.quit()            quit()               screenDisplay.fill(white)      largeText = pygame.font.Font('freesansbold.ttf',115)      TextSurf, TextRect = text_objects("You Can't Touch This!", largeText)      TextRect.center = ((display_width/2),(display_height/2))      screenDisplay.blit(TextSurf, TextRect)      pygame.display.update()      clock.tick(15)'''      def crash():   message_display('You crashed!')def game_loop():      x = (display_width * 0.45)   y = (display_height * 0.8)   #position change variable   x_change = 0   #vehicle spawn points.   vehicleSpeed = random.randrange(2,8)   vehicleW = 42   vehicleH = 109   vehicleX = random.randrange(139,660 - vehicleW)   vehicleY = random.randrange(0 - 600 + vehicleH, 0 - vehicleH)   vehicleCount = 2      stripe_speed = 10   grass_speed = 10   grass_y = 0   grass2_y = -600   stripe_y = 0   stripe2_y = -600   #grass_count = 1   #amount of objects dodged.   dodged = 0   # This is to set that we haven't crashed yet.   gameExit = False      vehList = [ambImg, audiImg, copImg, viperImg]   vehicleList = [Vehicle(vehicleW, vehicleH, random.choice(vehList), screenDisplay, random.randrange(2,8)) for x in range(4)]      # Game loop!   while not gameExit:            #event handling      for event in pygame.event.get():         if event.type == pygame.QUIT:            pygame.quit()            quit()                  if event.type == pygame.KEYDOWN:            if event.key == pygame.K_LEFT:               x_change = -5            elif event.key == pygame.K_RIGHT:               x_change = 5                     if event.type == pygame.KEYUP:            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:               x_change = 0            #vehicle movement      x += x_change            #fill the background      screenDisplay.fill(white)      road(0,0)      stripe(0,stripe_y)      stripe2(0,stripe2_y)      stripe_y += stripe_speed      stripe2_y += stripe_speed         #grass1 = Grass(10,0,0,grassImg)      #grass2 = Grass(10,0,-600,grassImg)      grass(0,grass_y)      grass2(0,grass2_y)      grass_y += grass_speed      grass2_y += grass_speed            for vehicle in vehicleList:         if vehicle.vehicle_y < display_height:            vehicle.veh_move()         if vehicle.vehicle_y > display_height:            vehicle.vehicle_y = random.randrange(0 - 600 + vehicle.vehicle_h, 0 - vehicle.vehicle_h)            vehicle.vehicle_x = random.randrange(139,660 - vehicle.vehicle_w)            vehicle.veh_move()            dodged += 1                     if x > vehicle.vehicle_x and x < vehicle.vehicle_x + vehicle.vehicle_w and y < vehicle.vehicle_y + vehicle.vehicle_h or x + moto_width > vehicle.vehicle_x and x + moto_width < vehicle.vehicle_x + vehicle.vehicle_w and y < vehicle.vehicle_y + vehicle.vehicle_h:            print('vehicle x crossover')            print("you hit the other vicle X")            crash()               #draw the       car(x,y)            #draw scores      things_dodged(dodged)            #logic handling      if x > display_width - 139 - moto_width or x < 139:         print("You hit the wall")         crash()                  if stripe_y > display_height:         stripe_y = 0         stripe2_y = -600            if grass_y > display_height:         grass_y = 0         grass2_y = -600                  for vehicle in vehicleList:         vehicle.update()            # This updates the display.      pygame.display.update()            # This sets the frames per second.      clock.tick(60)#run the game loop & intro#game_intro()game_loop()            #You must exit pygames before quitting python.   pygame.quit()quit()`
blpeter

Posts: 2
Joined: Thu Jun 09, 2016 2:56 pm