Python Tutorial 01: A Basic window using Python and Pygame.

The best way to get the most updated version of Python is from the Official Python Web-site, and Pygame found at the Pygame Official Web-site.  The most popular question when starting with Python, “Should I start with 2.x or 3.x?”.  The answer I would suggest, if you’re not supporting legacy code (versions like 2.5 or older) start with the newest version of Python you can run on your system, and handle your coding requirements.  As an example, for my situation, I have multiple versions installed ( 2.4 for Nvidia’s Fx Composer 2.5, 2.6 for Blender 2.49 and Panda 1.7, and 3.1 for coding and Blender 2.5/2.6 ), it’s what works for me.  Others might decide to have a particular version of Python on their hard drive, and other versions on memory sticks.  Just a reminder, the Python install is typically small ( less than 30 MB, so many applications written in Python include the Python code for simplicity – Blender and FX Composer both run this way), so if you have Open Source software on your system, or are running a non-Microsoft OS your system probably already has some version of Python.

The included code is in Python (it works with both 2.x and 3.x) and uses the Pygame version 1.9 library.

#!/usr/bin/python
# This example runs in Python 2.6 and 3.1 using Pygame 1.9

import pygame
from pygame.locals import *

def main():
# Initialize pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
pygame.display.set_caption('Basic Window')

# Fill background
background = pygame.Surface(window.get_size())
background = background.convert()
background.fill((250, 250, 250))

# Display some text-using the included font from pygame
font = pygame.font.Font(None, 36)
text = font.render('Not very exciting.', 1, (10, 10, 10))
text2 = font.render('Yet...', 1, (10, 10, 10))
txtposition = text.get_rect()
txtposition.centerx = background.get_rect().centerx
background.blit(text, txtposition)
txtposition2 = text2.get_rect()
txtposition2.centerx = background.get_rect().centerx
txtposition2.centery = background.get_rect().centery
background.blit(text2, txtposition2)

# Blit everything to the window
window.blit(background, (0, 0))
pygame.display.flip()

# Event loop - in future examples this will be the game loop.
while 1:
for event in pygame.event.get():
if event.type == QUIT:
return

window.blit(background, (0, 0))
pygame.display.flip()


if __name__ == '__main__':
main()

After you get the code into and editor(text or IDE) and execute in Python what should happen is…

basic_window

Advertisements

, ,

  1. Note about tutorials « Savalia Studios

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: