Thanks, I'm starting to get the hang of using pyglet. One thing that bugs the heck out of me, though, is that x and y coordinates in pyglet start from the bottom left. That's the way it works in math, of course, but doesn't click with me. I'm used to thinking of data structures as being in rows and columns starting from the top left. So either I have to structure my map data "backwards" or program my display function to read the data backwards.
This admittedly ugly code works to produce a simple @ moving around a hex map (to run it, you need to steal a sprite set from dwarf fortress and put it in a 'resources' folder). The nice thing about pyglet is that it has no dependencies, so you don't need to install SDL or any other libraries.
import pyglet
from pyglet.window import key
pyglet.resource.path = ['resources','resources/tiles']
window = pyglet.window.Window()
# IMPORT GRAPHICS
sprites = pyglet.resource.image("curses_800x600.png")
spriteset = pyglet.image.ImageGrid(sprites,16,16)
# re-organize the list because pyglet reads it in a bizarre way
spriteset = [spriteset[x:x+16] for x in range(0,len(spriteset),16)] #chunk into "rows"
spriteset = spriteset[::-1] # reverse the order of columns
#access by [row][column]
# figure out map dimensions and make a hex map of the background sprite(s)
w,h = sprites.width//16, sprites.height//16 # sprite dimensions
ww, wh = window.width, window.height # window dimensions
ncol, nrow = ww//w, wh//h # maximum columns/rows we could squeeze onto the screen
mapbatch = pyglet.graphics.Batch()
maptiles = []
for r in range(nrow):
if r % 2: # odd rows are shorter
for c in range(ncol-1):
maptiles.append( pyglet.sprite.Sprite( spriteset[15][10], x=(w*c)+(w//2), y=h*r, batch=mapbatch ) )
else: # even rows are longer
for c in range(ncol):
maptiles.append( pyglet.sprite.Sprite( spriteset[15][10], x=w*c, y=h*r, batch=mapbatch ) )
atsign = pyglet.sprite.Sprite(spriteset[4][0],x=window.width//2, y=window.height//2)
fps_display = pyglet.clock.ClockDisplay()
@window.event
def on_draw():
window.clear()
mapbatch.draw()
atsign.draw()
fps_display.draw()
@window.event
def on_key_press(symbol, modifiers):
moves = { key.LEFT:(-w,0), # num-lock off
key.RIGHT:(w,0),
key.HOME:(-w//2,h),
key.PAGEUP:(w//2,h),
key.END:(-w//2,-h),
key.PAGEDOWN:(w//2,-h),
key.NUM_4:(-w,0), # num-lock on
key.NUM_6:(w,0),
key.NUM_7:(-w//2,h),
key.NUM_9:(w//2,h),
key.NUM_1:(-w//2,-h),
key.NUM_3:(w//2,-h)
} # symbol : (x move,y move)
# these next lines break if the symbol isn't in the dictionary just created
atsign.x += moves[symbol][0] # horizontal movement
atsign.y += moves[symbol][1] # vertical movement
print("key "+str(symbol)+": move "+str(moves[symbol]))
pyglet.app.run()