Game programming: Your first pygame code!

Install pygame to Python 3. We don’t want to re-invent the wheels. Follow this tutorial to get pygame installed on your system:

http://danielj.se/2012/06/16/how-to-install-pygame-to-python-3-on-ubuntu/

Once you got it all set up, type the following game in your editor, save it as hellogame.py, and run it with python3 hellogame.py

import pygame, sys
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello Pygame World!')
while True: # main game loop
  for event in pygame.event.get():
    if event.type == QUIT:
      pygame.quit()
      sys.exit()

This will give you a basic skeleton of a windowed game, showing a black screen. The good thing is the X logo on the top right corner can actualy close the window.

Reference:

(Visited 273 times, 1 visits today)