Monday, June 30, 2008

Handle Chinese in Python while using Pydev and Pygame

I never really try to program anything that has to deal with the Chinese before, even though I'm a 100% Chinese. However, I recently came accross a problem of renaming some of my files, which have Chinese in them. I decided to write a little script to do the task in order to practice my Python skill. Very soon I encountered the unicode problem. After some searching and hacking, I was able to solve the problem. To avoid forgetting the solution, I put down this sample program as my note for future reference.

To display the string correctly in the eclipse console, set the encoding to UTF-8 under "Run Dialog => Common". Also in the project properties, set the "Text file encoding" to UTF-8.

# -*- coding: UTF-8 -*-

import sys
import pygame
from pygame.locals import QUIT

pygame
.init()

# This string contains both simplified Chinese and traditional Chinese.
unicode_string
= u'Hello Unicode String!简体!繁體!'
# Test the string in Eclipse console.
print unicode_string

# Must use a font that is capable of displaying Chinese.
string_font
= pygame.font.SysFont('simsun&nsimsun', 16, True, False);
string_surface
= string_font.render(unicode_string, True, (0, 255, 0))

# Display the string to the pygame screen.
screen
= pygame.display.set_mode((300, 40), 0, 32)

while True:
# Missing this event handle loop will cause the pygame window freezes somehow.
for event in pygame.event.get():
if event.type == QUIT:
exit()

screen
.fill((0, 0, 0))
screen
.blit(string_surface, (10, 10))
pygame
.display.update()


Sunday, June 29, 2008

Ready . . . Get Set . . . Go!

It's been a while after my last post. I was trying to keep up the posting as the summer starts, but didn't expect the blogger was blocked by the GFW of China. -_- Fortunately it's finally unlocked now, and I can access my blog again.

It seems like the Blogger has upgraded the system and added a bunch of cool new features. Therefore I figured I might as well give a new look to my blog, and I even changed the title and URL of the blog.

Since I don't really start looking for jobs until August, I have quite a long summer to spend. I think it will be helpful for me to learn something and probably do a project during this time. Currently I'm learning the Flex 3 and python, even thought they don't really go together. Also, an old article about making a game in a week came across me accidentally, and it really inspired me. I think I will try to do something similar in this summer.

After all, 1/3 of the summer has been gone, and I think I am getting ready. It's time to run.