Comments (2)
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.
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)
- show TextBox not working, not able to use pygame_functions HOT 4
- Add support for cmaps HOT 7
- video system not initializing HOT 12
- Graphics Window Issues HOT 7
- Regarding setting background color HOT 1
- Spritesheet animation not working, please help! HOT 5
- Please add shapes movement HOT 1
- update to pygame 2 HOT 7
- Making a sprite causes and error HOT 1
- rotation problem
- Error in make label HOT 5
- Not accepting new font HOT 4
- [Enhancements] - Is Exit or Quit - Exec Quit HOT 1
- can't load image HOT 2
- hello HOT 1
- Error with setBackgroundImage(img), (Scrolling Background) HOT 2
- Long time waiting before the file runs HOT 1
- exit() error
- REQUEST - make pygame_Functions compatible with pygame zero
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pygame_functions.