Python Game Development - Pygame vs. Pyglet: Part 1

Here, you’ll find basic comparisons between Pygame and Pyglet, showing you how to accomplish the same tasks in each. As I learn, more posts on the subject will be made.

 Get a blank window to come up

 Pygame

import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))

if __name__ == "__main__":
while True:
    pygame.display.flip()

Source

 Pyglet

import pyglet
screen = pyglet.window.Window(800,600)

if __name__ == "__main__":
    pyglet.app.run()

Source

 Clear Screen

 Pygame

import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))

if __name__ == "__main__":
    while True:
        screen.fill((0,0,0))
        pygame.display.flip()

Source

 Pyglet

import pyglet
screen = pyglet.window.Window(800,600)

@screen.event
def on_draw():
screen.clear()

if __name__ == "__main__":
    pyglet.app.run()

Source

 Control Frames Per Second (FPS)

 Pygame

import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()

def update():
  #update objects here

if __name__ == "__main__":
    while True:
        update()
        pygame.display.flip()
        clock.tick(30)

Source

 Pyglet

import pyglet
screen = pyglet.window.Window(800,600)

def update(dt):
  #update objects here

if __name__ == "__main__":
    pyglet.clock.schedule_interval(update, 1/120.0)
    pyglet.app.run()

Source

 Handle events and closing application with Key_ESCAPE

 Pygame

import pygame, sys
from pygame.locals import *

running = True
pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()

def handleEvents():
  global running
    for event in pygame.event.get():
        if (event.type == QUIT) or (event.type == KEYDOWN and event.key == K_ESCAPE):
            running = False

if __name__ == "__main__":
    while running:
        handleEvents()
        pygame.display.flip()
        clock.tick(30)
    pygame.quit()
    sys.exit()

Source

 Pyglet

Note: Pyglet handles the Escape key event by default. This example shows you how to handle when the “a” key is pressed.

import pyglet
from pyglet.window import key

screen = pyglet.window.Window(800,600)

@screen.event
def on_key_press(symbol, modifiers):
  if symbol == key.A:
        print 'You pressed A'

def update(dt):
    #update objects here

if __name__ == "__main__":
    pyglet.clock.schedule_interval(update, 1/120.0)
    pyglet.app.run()

Source

Check out Part 2

 
13
Kudos
 
13
Kudos

Now read this

Splunk: Automatically update GeoIP database across environment

Information for this post was inspired by this post. Edit: Also see George Starcher’s implementation. On every Splunk upgrade, they also push out a GeoIP database found here. Instead of waiting, I wanted to automate the pull on search... Continue →