Skip to content

Instantly share code, notes, and snippets.

@gavinmyers
Created September 16, 2012 01:15
Show Gist options
  • Save gavinmyers/3730667 to your computer and use it in GitHub Desktop.
Save gavinmyers/3730667 to your computer and use it in GitHub Desktop.
pyglet, simple window
#!/usr/bin/env python
from math import pi, sin, cos
from pyglet.gl import *
from pyglet.window import *
import pyglet
tile = 24
class ItemBank:
def __init__(self):
self.item_map = {}
self.item_list = []
def add(self,item):
self.item_map[item.id] = item
self.item_list.append(item)
def draw(self):
window.clear()
for item in self.item_list:
item.draw()
class Item:
def __init__(self,id,icon,x,y):
global itemBank
self.icon = pyglet.sprite.Sprite(icon)
self.id = id
self.x = x
self.y = y
itemBank.add(self)
def move(self,d):
if d == 5:
#do nothing
print "nothing"
elif d == 1:
self.x -= tile
self.y -= tile
elif d == 2:
self.y -= tile
elif d == 3:
self.x += tile
self.y -= tile
elif d == 4:
self.x -= tile
elif d == 6:
self.x += tile
elif d == 7:
self.x -= tile
self.y += tile
elif d == 8:
self.y += tile
elif d == 9:
self.x += tile
self.y += tile
def draw(self):
self.icon.set_position(self.x,self.y)
self.icon.draw()
window = pyglet.window.Window()
itemBank = ItemBank()
pyglet.gl.glClearColor(0, 255, 239, 1)
sprite = pyglet.image.load('sprite.png')
heroIcon = sprite.get_region(0,1129,24,24)
badguyIcon = sprite.get_region(24,1129,24,24)
wallIcon = sprite.get_region(0,244,24,24)
floorIcon = sprite.get_region(72,168,24,24)
for x in range(0,27):
for y in range(0,20):
f = Item('f_'+str(x)+'_'+str(y),floorIcon,24 * x, 24 * y)
hero = Item('h',heroIcon,0,0)
badguy = Item('b',badguyIcon,96,96)
wall = Item('w',wallIcon,144,144)
#@window.event
#def on_draw():
# print "drawing"
@window.event
def on_show():
itemBank.draw()
@window.event
def on_key_press(symbol, modifiers):
if symbol == key.NUM_9:
hero.move(9)
elif symbol == key.NUM_8:
hero.move(8)
elif symbol == key.NUM_7:
hero.move(7)
elif symbol == key.NUM_6:
hero.move(6)
elif symbol == key.NUM_4:
hero.move(4)
elif symbol == key.NUM_3:
hero.move(3)
elif symbol == key.NUM_2:
hero.move(2)
elif symbol == key.NUM_1:
hero.move(1)
itemBank.draw()
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment