Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sf
- import math
- class Background ():
- def __init__ (self, texture_id, game):
- self.texture_id = texture_id
- self.game = game
- texture = self.game.resourcemanager.get(self.texture_id)
- self.sprite = sf.Sprite(texture)
- self.sprite.set_sub_rect((0, 0, texture.width, texture.height))
- # whether to tile the background
- self.tile = True
- # whether the background is relative to the camera
- self.fixed = True
- # how much to scroll relative to the camera
- # for example, it will scroll at 10% of the camera speed
- self.scroll = 0.1
- # x and y "speed" for auto scrolling
- self.autoscroll = (1, 1)
- def render (self, t, dt):
- topx = (self.game.view.center[0] - self.game.view.width/2) * self.scroll
- topy = (self.game.view.center[1] - self.game.view.height/2) * self.scroll
- if self.tile:
- self.sprite.x = topx
- self.sprite.y = topy
- xrepeat = int(math.ceil(self.sprite.width/self.game.view.width) / self.scroll)
- yrepeat = int(math.ceil(self.sprite.height/self.game.view.height) / self.scroll)
- xautoscroll = (t * self.autoscroll[0]) % self.sprite.width
- yautoscroll = (t * self.autoscroll[1]) % self.sprite.height
- for y in range(-1, yrepeat):
- for x in range(-1, xrepeat):
- # % so the sprite position "tiles" too
- self.sprite.x = int(topx + xautoscroll +
- (x * self.sprite.width))
- self.sprite.y = int(topy + yautoscroll +
- (y * self.sprite.height))
- self.game.window.draw(self.sprite)
- else:
- self.sprite.x = topx
- self.sprite.y = topy
- self.game.window.draw(self.sprite)
Advertisement
Add Comment
Please, Sign In to add comment