Giter Site home page Giter Site logo

Comments (2)

 avatar commented on July 23, 2024

I'm not sure, but the juddering may have something to do with repeated keyboard input not coming in that quickly at lower frame rates.

I also translated my example to PySFML. As you can see, it pretty much does the same thing as Pygame, the syntax and some concepts are just a little bit different. Scrolling is also not 100% accurate there, some artifact is running down the screen from time to time.
Maybe the background should also be redrawn in frames, when it hasn't been scrolled, I don't know.

#!/usr/bin/python
# coding: utf-8

from sfml import sf

class Background():

    def __init__(self, screen):
        self.screen = screen
        self.colour = sf.Color(0, 0, 0)

    def setTiles(self, tiles):

        if type(tiles) is str:
            self.tiles = [[self.loadImage(tiles)]]
        elif type(tiles[0]) is str:
            self.tiles = []
            t = []
            for i in tiles:
                t.append(self.loadImage(i))
            self.tiles.append(t)
        else:
            self.tiles = []
            for i in tiles:
                t = []
                for row in i:
                    t.append(self.loadImage(row))
                self.tiles.append(t)
        self.stagePosX = 0
        self.stagePosY = 0
        self.tileWidth  = int(self.tiles[0][0].global_bounds.width)
        self.tileHeight = int(self.tiles[0][0].global_bounds.height)
        self.screen.draw(self.tiles[0][0])

    def loadImage(self, fileName):
        texture = sf.Texture.from_file(fileName)
        sprite  = sf.Sprite(texture)
        sprite.position = (0, 0)
        return sprite

    def scroll(self, x, y):
        self.stagePosX -= x
        self.stagePosY -= y
        col = (self.stagePosX % (self.tileWidth * len(self.tiles[0]))) // self.tileWidth
        xOff = (0 - self.stagePosX % self.tileWidth)
        row = (self.stagePosY % (self.tileHeight * len(self.tiles))) // self.tileHeight
        yOff = (0 - self.stagePosY % self.tileHeight)

        col2 = ((self.stagePosX + self.tileWidth) % (self.tileWidth * len(self.tiles[0]))) // self.tileWidth
        row2 = ((self.stagePosY + self.tileHeight) % (self.tileHeight * len(self.tiles))) // self.tileHeight

        self.tiles[row][col].position = (xOff, yOff)
        self.screen.draw(self.tiles[row][col])

        self.tiles[row][col2].position = (xOff + self.tileWidth, yOff)
        self.screen.draw(self.tiles[row][col2])

        self.tiles[row2][col].position = (xOff, yOff + self.tileHeight)
        self.screen.draw(self.tiles[row2][col])

        self.tiles[row2][col2].position = (xOff + self.tileWidth, yOff + self.tileHeight)
        self.screen.draw(self.tiles[row2][col2])

screen = sf.RenderWindow(sf.VideoMode(600, 600), unicode("Scrolling Background"))
screen.position = (300, 30)
screen.framerate_limit = 60

background = Background(screen)
screen.clear(background.colour)
background.setTiles([["images/dungeonFloor1.png", "images/dungeonFloor2.png"],
                     ["images/dungeonFloor3.png", "images/dungeonFloor4.png"]])
scroll_by = 5

while screen.is_open:

    if sf.Keyboard.is_key_pressed(sf.Keyboard.ESCAPE):
        screen.close()
    if sf.Keyboard.is_key_pressed(sf.Keyboard.RIGHT):
        background.scroll(-scroll_by, 0)
    if sf.Keyboard.is_key_pressed(sf.Keyboard.DOWN):
        background.scroll(0, -scroll_by)
    if sf.Keyboard.is_key_pressed(sf.Keyboard.LEFT):
        background.scroll(scroll_by, 0)
    if sf.Keyboard.is_key_pressed(sf.Keyboard.UP):
        background.scroll(0, scroll_by)

    screen.display()

from pygame_functions.

StevePaget avatar StevePaget commented on July 23, 2024

Thanks for this. I have not used pySFML, but it sounds interesting.
I'll close this as it's not really related to pygame_functions, more pygame in general.

from pygame_functions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.