Python API Basics

I have set of imports that I like to have at the top of every API client I write, some are built in, but some are not. I’ll just quickly go over them before you get started on the fun stuff. This post is just to list off the modules you might need. If you’re on 64 bit OSes, you might need to take a look here for compatible packages, I know I did. For then entirety of the tutorials, I’ll be using Windows 7×64, with Python 2.7, so maybe you’ll have a different experience with Python 3, and on a different OS.*

First off, requests module is essential to everything we’ll be doing within the following tutorials. It handles all the hard-to-understand stuff, like POST, PUT, and GET requests, as well as cookies, so we don’t have to worry about it much at all; and ideal Python module.

Secondly, pprint is a nice thing to have, since we’ll be dealing a lot with dictionaries, and the regular print statement doesn’t print it off in the most readable of formats. Instead of printing everything in a single line, it linewraps, as well as uses separate lines where doing so would help with legibility. For example: a dictionary would be printed like this normally:

dictionary = {'a': 1, 'b': 2, 'c': 3}

but with pprint it’d look something more like this:

dictionary = {'a' : 1,
              'b' : 2,
              'c' : 3' }

which is amazing when you have huge dictionaries that you need to visually parse.

Thirdly, the json module is another essential one, as the APIs will often feed us JSON data. JSONs are essentially dictionaries writtien in Javascript. We’ll take the request data from the requests module, and feed it into the json.loads function and it’ll return a native python dict that we can manipulate like it’s not even a big deal. It’s pretty great.

Here’s an example of all three modules working together:

#imports
from pprint import pprint
import requests
import json

r = requests.get(r'http://www.reddit.com/user/tankorsmash/about/.json')
#print r.text  #raw text response as a string

j = json.loads(r.text)  #turn the json response into a python dict
#print j  #now it's a python dict

pprint(j)  #here's the final respone, printed out nice an readable format

There you have it,  the basic python modules I’ll be using throughout the next few tutorials.

*thanks to EuphoriaForAll for the Python Version inclusion here

Wrote a Tile system!

Welcome back! I’m very happy to be working on the game again, after the brief hiatus. I made a grid system, as shown below. Here’s the latest thing little app I made, the colors and text is all just there for making the tiles visible, otherwise it’d be a blank screen :

Hit the jump for code examples, and what my old broken version of the code was.

Continue reading Wrote a Tile system!

Windows7x64 Wallpaper Changer Script

I’ve been meaning to make this ever since I discovered win32 stuff. It uses the .2 version of SendKey, found here, that uses ctypes, instead of the win32 stuff I think, as well as working with 64 bit Windows. Make a shortcut to it on your desktop and just run it as a .pyw file, in order to hide the terminal window that pops up. Simple stuff

Anyways, all it does is is simulate a right-click and then an ‘n’ keystroke. It’s just a quick little app,  just a dozen lines long:

import win32api, win32con
import SendKeys
def click(x=-1,y=-1):
    oldX, oldY = win32api.GetCursorPos()
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,x,y,0,0)
    win32api.SetCursorPos((oldX, oldY))

click()
SendKeys.SendKeys('n')

Rotation is debugged!…mostly

Here’s just a quick update, to say that rotation is fixed, as far as mouse clicking to change the players direction. It used to have trouble with angles more than 90 degrees, but I made the rotation run until the angle between the mouse and the direction the player is facing zero rather than just trying to rotate twice, once clockwise, and again counterclockwise.

That’s really all I changed, beside tidying up some left over code from the earlier incarnation of project shooter. I’m just really happy I can use the mouse to control the dude again, rather than confined to the cardinal directions of the arrow keys.

A blog about gamedev and vim