Python

From Mario Fan Games Galaxy Wiki
Revision as of 17:31, 1 October 2008 by Hohoo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Python is a high-level programming and scripting language. Its syntax is well designed and it's meant to be powerful yet readable. There are some people using Python at MFGG.

There are some game libraries for Python, for example Pygame (SDL based) and Pyglet (OpenGL based).

Examples

Hello World

print "Hello world!"

Hello World with Pygame

This example requires Pygame.

#! /usr/bin/env python
# -*- coding: utf-8 -*-

# ^--- First row = Unix stuff (path to interpreter); 2nd row = text encoding

import sys                  # imports sys module

import pygame               # imports Pygame
from pygame.locals import * # imports Pygame's constants and other stuff
pygame.init ()
screen = pygame.display.set_mode ((320, 240)) # Creates a screen with resolution
                                              # 320x240. Try changing it!

font = pygame.font.Font (None, 25) # Loads a font. Since None was specified,
                                   # it loads Pygame's builtin font.

text1 = font.render ("Hello, World of Pygame!", 1, (12, 34, 56, 255))
text2 = font.render ("Press Enter to Exit!", 1, (65, 43, 21, 255))
# <fontname>.render arguments: (text, (red, green, blue, alpha))
# Try changing them!

while 1:
    screen.fill ((255, 255, 255))
    screen.blit (text1, (10, 10))
    screen.blit (text2, (40, 40)) # <displayname>.blit arguments:
                                  # (<textname>, (x, y))
    pygame.display.flip ()
    event = pygame.event.poll () # Gets the last event
    if event.type == KEYDOWN:    # Checks if any key is pressed down
        if event.key == K_RETURN:# If enter is pressed...
            pygame.quit ()       # ...quit Pygame...
            sys.exit ()          # ...and finally exit.