Distancing.

Today had a slight tail of productivity after most of the hours being spent avoiding CISCO revision and ‘researching’ Killer 7. Damn freaky game.

I did a tiny bit of work to see how easy it would be do get Pygame to calculate the distance between two objects, and send this information to Pure Data.

Turns out: Very Easy.

What will follow is a simple (and ugly) python script which creates a window with two rectangles, one of which is controlled by the mouse, and the other by both the arrow keys and the WASD keys. When this is linked to my Pure Data module (not included), the smaller the distance then the more the original sine wave signal gets modulated, both with frequency modulation and amplitude modulation.

I aim to use this sort of effect within the game as an immersive sign of danger for the player.

Here’s the script:

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

#------GLOBALS-----
IP = "127.0.0.1"
PORT = 65665
#------------------

#---EXTERNAL PROGRAM HANDLING----
if os.name == "nt":
    print "You're on Windows! Please tell me you illegally downloaded this."
    def sendOSC(msg, address='/default'):
        """
        Hello There! This is a docstring!
        """
        import simpleOSC
        simpleOSC.initOSCClient(IP,PORT)
        message = simpleOSC.sendOSCMsg(address, msg)

    def espeak(speech, voice='english'):
        args = ['C:/Program Files (x86)/eSpeak/command_line/espeak.exe', '-v', voice, speech]
        p = subprocess.Popen(args)

    def pd(patch='seq1'):
        args = ['C:/pd/bin/pd.exe', '-nogui', patch]
        p = subprocess.Popen(args)
elif os.name == "posix":
    print "You're on Linux! You hacker, you!"
    def sendOSC(msg, address='/default'):
        import liblo
        target = liblo.Address(IP,PORT)
        libloMsg = liblo.Message(address)
        for a in msg:
            libloMsg.add(a)
        liblo.send(target, libloMsg)

    def pd(patch='seq1'):
        args = ['jackd', '-d' 'alsa']
        args2 = ['pasuspender', '--', '/usr/bin/pdextended', '-jack', patch]
        p = subprocess.Popen(args)
        p2 = subprocess.Popen(args2)

    def espeak(speech, voice='english'):
        args = ['"usr/bin/espeak"', '-v', voice, speech]
        p = subprocess.Popen(args)
else:
    print "OS has not been recognised. This is either a bug, or you're using a Macintosh computing system. Either way you need help."
    pygame.quit()
    sys.exit()
#------------------------------

def getDist((x1,y1), (x2,y2)):
	return math.sqrt((x1-x2)**2 + (y1-y2)**2)

def main():
	"""
	The Game Loop.

	Fear It.
	"""
	pygame.init()
	screen = pygame.display.set_mode((640,480))
	aX, aY = 100,100
	bX, bY = 300,300

	movebX = 0
	movebY = 0

	while True:

		for event in pygame.event.get():
			if event.type == QUIT:
				pygame.quit()
				sys.exit()
			if event.type == KEYDOWN:
				if event.key == K_UP or event.key == K_w:
					movebY = -1
				if event.key == K_DOWN or event.key == K_s:
					movebY = 1
				if event.key == K_LEFT or event.key == K_a:
					movebX = -1
				if event.key == K_RIGHT or event.key == K_d:
					movebX = 1
				if event.key == K_ESCAPE:
					pygame.quit()
					sys.exit()
			elif event.type == KEYUP:
				if event.key == K_UP or event.key == K_w:
					movebY = 0
				if event.key == K_DOWN or event.key == K_s:
					movebY = 0
				if event.key == K_LEFT or event.key == K_a:
					movebX = 0
				if event.key == K_RIGHT or event.key == K_d:
					movebX = 0

		aX, aY = pygame.mouse.get_pos()

		bX += movebX
		bY += movebY

		screen.lock()
		screen.fill((0,0,0))
		shapeA = pygame.draw.rect(screen, (255,255,255), (aX,aY,100,100), 1)
		shapeB = pygame.draw.rect(screen, (255,255,0), (bX,bY,100,100), 1)
		screen.unlock()

		sendOSC([int(getDist((aX,aY),(bX,bY)))])

		pygame.display.update()

if __name__ == '__main__': main()

P.S. (geddit?): My housemate helped me come up with an awesome idea the other day: The Metatrap. It’s the idea of an in-game obstacle which doesn’t necessarily impede/annoy/hurt the charactor on screen, but one that impedes/annoys/hurts the player controlling him. Still, while it does have potential, I would have to make sure it doesn’t just make someone mad at me. They might hit me then :(

This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>