Hello, I am relatively new to pyglet, and I am trying to make a fairly
straightforward game. A roguelike game, turn based, with sprites
instead of text. It's really just prototype code at this point.

I create a map as a 2d array with a batch to hold sprites. I redraw
the batch at new positions when the player gets close to the edge of
the screen. I create a 16x16cell square room in my map. It looks like
this after the initial draw():
http://i.imgur.com/BAVP2.png

after I move around a bit, and cause the sprites to redraw by
approaching the edge of the screen, the problem occurs:
http://i.imgur.com/Z5noF.png

those extra room cells on the bottom aren't by design. I've been
trying to fix this problem for days now, but I don't really have the
first clue about what's causing it. I'm sure most of what i've tried
is silly. I think the problem is caused by the way i'm drawing
sprites. All sprites are created with position attribute outside the
bounds of the app window, and repositioned inside by updateViewport.
This was the only way I could figure out how to have a scrolling map
window without constantly switching sprites into and out of batches
(which the docs call an expensive operation)

My theory is that i'm doing something fundamentally wrong and I would
be very grateful if someone can give me some pointers in the right
direction.

Code follows:

import pyglet, sys
from pyglet.window.key import *
from pyglet.sprite import Sprite
from pyglet.gl import *

(WINDOW_W, WINDOW_H) = (1024, 768)
OFFSCREEN = 2000

classfile = 'dg_classm32.png'
dungeonfile = 'dg_dungeon32.gif'
groundfile = 'dg_grounds32.gif'

classimg = pyglet.resource.image(classfile)
dungeonimg = pyglet.resource.image(dungeonfile)
groundimg = pyglet.resource.image(groundfile)

classgrid = pyglet.image.ImageGrid(classimg, 11, 8)
dungeongrid = pyglet.image.ImageGrid(dungeonimg, 10, 9)
groundgrid = pyglet.image.ImageGrid(groundimg, 19, 9)

classgrid = pyglet.image.TextureGrid(classgrid)
dungeongrid = pyglet.image.TextureGrid(dungeongrid)
groundgrid = pyglet.image.TextureGrid(groundgrid)

class Map:
    def __init__(self, width=300, height=300):
        self.map = []
        self.batch = pyglet.graphics.Batch()
        self.playerX = 0
        self.playerY = 0
        self.player = MapCell(classgrid[79], x=OFFSCREEN, y=OFFSCREEN)
        for x in range(width):
            list = []
            for y in range(height):
                list.append(MapCell(dungeongrid[81], x=OFFSCREEN,
y=OFFSCREEN, batch=self.batch))
            self.map.append(list)
        self.makeRectRoom(50,50,16,16)
        self.positionPlayer(58,58)

    def positionPlayer(self, x, y):
        (self.playerX, self.playerY) = (x, y)

    def movePlayer(self, x, y):
        (newXpx, newYpx) = (self.player.x + x * 33, self.player.y + y
* 33)
        (newX, newY) = (self.playerX + x, self.playerY + y)
        if self.map[newX][newY].blocked == True:
            return
        else:
            print "moving player to %i,%i" % (newX,newY)
            (self.playerX, self.playerY) = (newX, newY)
            self.player.set_position(newXpx, newYpx)

    def updateViewport(self, width, height):
        startX = self.playerX - width / 2
        startY = self.playerY - height / 2
        endX = startX + width
        endY = startY + height
        xPixels = 0

        if endX > (len(self.map) - 1):
            endX = len(self.map) - 1
        if endY > (len(self.map[endX])):
            endY = len(self.map[endX]) - 1
        if startX < 0:
            startX = 0
        if startY < 0:
            startY = 0

        for x in range(startX, endX + 1):
            yPixels = 0
            for y in range(startY, endY + 1):
                if x == self.playerX and y == self.playerY:
                    self.player.set_position(xPixels, yPixels)
                self.map[x][y].set_position(xPixels, yPixels)
                yPixels += 33
            xPixels += 33

    def makeRectRoom(self, x, y, w, h):
        for r in range(x, x+w+1):
            for n in range(y, y+h+1):
                self.map[r][n] = MapCell(groundgrid[170],
blocked=False, x=OFFSCREEN, y=OFFSCREEN, batch=self.batch)

class MapCell(Sprite):
    def __init__(self,
             img, blocked=True, x=0, y=0,
             blend_src=GL_SRC_ALPHA,
             blend_dest=GL_ONE_MINUS_SRC_ALPHA,
             batch=None,
             group=None,
             usage='dynamic'):
        self.blocked = blocked
        Sprite.__init__(self, img, x, y, blend_src, blend_dest, batch,
group, usage)

def isNearEdge(x,y):
    lowPass = 100
    highPassX = WINDOW_W - 100
    highPassY = WINDOW_H - 100
    if lowPass <= x <= highPassX:
        if lowPass <= y <= highPassY:
            return False
    return True

map = Map(width=200, height=200)
map.updateViewport(20,20)

window = pyglet.window.Window(width=WINDOW_W, height=WINDOW_H)

@window.event
def on_text_motion(motion):
    if motion == MOTION_UP:
        map.movePlayer(0,1)
    elif motion == MOTION_RIGHT:
        map.movePlayer(1,0)
    elif motion == MOTION_DOWN:
        map.movePlayer(0,-1)
    elif motion == MOTION_LEFT:
        map.movePlayer(-1,0)
    else:
        pass

@window.event
def on_draw():
    window.clear()
    if isNearEdge(map.player.x, map.player.y):
        map.updateViewport(20,20)
    map.batch.draw()
    map.player.draw()

try:
    pyglet.app.run()
except KeyboardInterrupt:
    sys.exit()

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en.

Reply via email to