Python Game Development - Pygame vs. Pyglet: Part 2

Here in part 2, you’ll find intermediate level snippet comparisons between Pygame and Pyglet

If you missed it, check out Part 1.

 Display an image and text on the screen

 Pygame

import pygame, sys, os

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

#calculate current path + location of player image. Image must be on same level as this file
mypath = os.path.dirname( os.path.realpath( __file__) )
p_path = os.path.join(mypath, 'player.png')

#create player image, move to 400,300
player_image = pygame.sprite.Sprite()
player_image.image = pygame.image.load(p_path).convert()
player_image.rect = player_image.image.get_rect().move(400,300)

#create text label
font = pygame.font.Font(None, 24)
font_color = (255,255,255)
font_background = (0,0,0)
t = font.render("Hello World", True, font_color, font_background)
t_rect = t.get_rect()
t_rect.centerx, t_rect.centery = 100, 100

def update():
    #draw player to screen
    screen.blit(player_image.image, player_image.rect)
    #draw text to screen
    screen.blit(t, t_rect)

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

Source

 Pyglet

import pyglet

#index all resources. Resources folder must be on same level as this file.
pyglet.resource.path = ['resources']
pyglet.resource.reindex()

#create window object
screen = pyglet.window.Window(800, 600)

#create player object. Player image must be in resource folder.
player_image = pyglet.resource.image("player.png")
player_sprite = pyglet.sprite.Sprite(img=player_image,x=400,y=300)

#create text label
score_label = pyglet.text.Label(text="Hello World", x=100, y=500)

@screen.event
def on_draw():
    screen.clear()
    score_label.draw()
    player_sprite.draw()

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

Source

 Display a group of items to the screen with one draw command

 Pygame

import pygame, sys, os

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

#calculate current path + location of player image
mypath = os.path.dirname( os.path.realpath( __file__) )
p_path = os.path.join(mypath, 'player.png')

#create player image, move to 400,300
player_image = pygame.sprite.Sprite()
player_image.image = pygame.image.load(p_path).convert()
player_image.rect = player_image.image.get_rect().move(400,300)

player_group = pygame.sprite.Group()
player_group.add(player_image)

def update():
    #draw player group to screen
    player_group.draw(screen)

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

Source

 Pyglet

import pyglet

#index all resources
pyglet.resource.path = ['resources']
pyglet.resource.reindex()

#create window object
screen = pyglet.window.Window(800, 600)

#create batch group to put all objects in
main_batch = pyglet.graphics.Batch()

#create player object
player_image = pyglet.resource.image("player.png")
player_sprite = pyglet.sprite.Sprite(img=player_image,x=400,y=300, batch=main_batch)

#create text label
score_label = pyglet.text.Label(text="Hello World", x=100, y=500, batch=main_batch)

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

    #draw batch group (player and text)
    main_batch.draw()

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

Source

 Execute code when mouse buttons are pressed

 Pygame

import pygame, sys

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

def handleEvents():
  for event in pygame.event.get():
        #print current mouse position
        if event.type == pygame.MOUSEMOTION:
            print pygame.mouse.get_pos()

        #when a user presses a mouse button, print which one and where the mouse is
        if event.type == pygame.MOUSEBUTTONDOWN:
            print "{0} pressed at: {1},{2}.".format(pygame.mouse.get_pressed(), pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])

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

Source

 Pyglet

import pyglet

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

@screen.event
def on_mouse_motion(x, y, dx, dy):
    #print current mouse position
    print x,y

@screen.event
def on_mouse_press(x, y, button, modifiers):
    #when a user presses a mouse button, print which one and where the mouse is
    print "{0} pressed at: {1},{2}.".format(button, x, y)

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

Source

 Play a sound file when a key is pressed

 Pygame

import pygame, sys, os
from pygame.locals import *

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

#Create path to sound file
mypath = os.path.dirname( os.path.realpath( __file__) )
laser = os.path.join(mypath, 'resources/laser.wav')

#Create sound object for laser
pygame.mixer.init()
sound = pygame.mixer.Sound(laser)

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 event.type == KEYDOWN:
                if event.key == K_SPACE:
                    sound.play()

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

Source

 Pyglet

import pyglet
from pyglet.window import key

#Put sound files in a folder called "resources"
pyglet.resource.path = ['resources']
pyglet.resource.reindex()

#Create sound object for laser
screen = pyglet.window.Window(800,600)
laser = pyglet.resource.media('laser.wav')

@screen.event
def on_key_press(symbol, modifiers):
    if symbol == key.SPACE:
        laser.play()

def update(dt):
    #update objects here
    pass

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

Source

 
18
Kudos
 
18
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 →