games
pages
posts

iconJavaAppletTest

2000-01-01

{{{ from java import awt, applet, lang, net import synchronize, time
class Player: def __init__(self, pos): self.pos = pos
class FPSCounter: fps = 0 last_second = None counter = 0
last_t = 0.0 min_frame = 1000.0 max_frame = 0.0
def __init__(self, d = 1): self.d = d
def tick(self, t): if self.last_second == None: self.last_second = t self.last_five = t else: d = t - self.last_t self.min_frame = min(self.min_frame, d) self.max_frame = max(self.max_frame, d) self.last_t = t
self.counter += 1 if t - self.last_second >= self.d: self.fps = self.counter / self.d self.counter = 0 self.last_second = t self.min_frame = 1000.0 self.max_frame = 0.0
class Squirrel(applet.Applet, lang.Runnable, awt.event.KeyListener): def init(self): applet.Applet.init(self)
self.FPS = 60 self.ticks = 0
self.buffer = None self.thread = None
self.player = Player((0, 0))
self.fps_counter = FPSCounter(1) self.fps_counter5 = FPSCounter(5)
self.keys = { "left" : 0, "right" : 0, "up" : 0, "down" : 0 }
self.addKeyListener(self)
self.start()
def update(self, g): self.paint(g) # since this can be called from different threads, mark it synchronized update = synchronize.make_synchronized(update)
def create_images(self): self.buffer = self.createImage(self.size.width, self.size.height) self.g = self.buffer.getGraphics()
url = self.getDocumentBase() print "Loading images from", url self.grass = self.getImage(url, "grass.gif") self.nut = self.getImage(url, "nut.gif") self.squirrel = self.getImage(url, "squirrel.gif")
def paint(self, g): if not self.buffer: self.create_images()
for x in range(8): for y in range(8): self.g.drawImage(self.grass, x * 32, y * 32, None) x, y = self.player.pos self.g.drawImage(self.squirrel, x, y, None)
c = self.fps_counter self.g.drawString("%d fps (%f..%f)" % ( c.fps, c.min_frame, c.max_frame), 0, self.size.height)
fh = self.getFont().size c = self.fps_counter5 self.g.drawString("%d fps5 (%f..%f)" % ( c.fps, c.min_frame, c.max_frame), 0, self.size.height - fh)
g.drawImage(self.buffer, 0, 0, None) # since this can be called from different threads, mark it synchronized paint = synchronize.make_synchronized(paint)
def start(self): print "start" if not self.thread: self.thread = lang.Thread(self) self.thread.start()
def stop(self): if self.thread: self.thread = None
def keyPressed(self, event): if event.getKeyCode() == event.VK_LEFT: self.keysleft = 1 elif event.getKeyCode() == event.VK_RIGHT: self.keysright = 1 elif event.getKeyCode() == event.VK_UP: self.keysup = 1 elif event.getKeyCode() == event.VK_DOWN: self.keysdown = 1
def keyReleased(self, event): if event.getKeyCode() == event.VK_LEFT: self.keysleft = 0 elif event.getKeyCode() == event.VK_RIGHT: self.keysright = 0 elif event.getKeyCode() == event.VK_UP: self.keysup = 0 elif event.getKeyCode() == event.VK_DOWN: self.keysdown = 0
def keyTyped(self, event): pass
def logic(self): x, y = self.player.pos if self.keysleft: x -= 1 if self.keysright: x += 1 if self.keysup: y -= 1 if self.keysdown: y += 1 self.player.pos = x, y self.repaint()
def run(self): print "run" self.tick_time = time.time() while self.thread: self.logic() self.ticks += 1 self.tick_time += 1.0 / self.FPS
t = time.time() self.fps_counter.tick(t) self.fps_counter5.tick(t) # if too much time, sleep while t < self.tick_time: try: lang.Thread.sleep(int(1000 * (self.tick_time - t))) except lang.InterruptedException: print "uh oh" t = time.time()
if __name__ == '__main__': import pawt pawt.test(Squirrel(), (256, 256)) }}}