Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Learning Pygame(simple 2d), part 1


  • Please log in to reply
No replies to this topic

#1 spyder


    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 24 July 2010 - 01:24 PM

To start out with, Pygame is a simple 2d game library for Python.
Pygame uses the C/C++ game library SDL.
Pygame does not come with the CPython Interpreter from python.org.
You will have to install it if you have not already.
Go to pygame.org and download the binary related to your version of Python and your operating system.

Now to begin.

Okay to start you have to import pygame and any other imports you might want.
We will start with a simple Tic-Tac-Toe game for our example.

So first we will import:
import pygame
import sys
import time
from pygame import *

and before you can use ANY pygame functions, you must initialize pygame.
So now we will initialize pygame and set up the screen.

ttt = pygame.display.set_mode((300,325))

pygame.display.set_mode takes a tuple(a group of numbers, variables, and/or other Python objects) pertaining to the size of the rectangular box that your game is in.

Now leave enough space for the functions at the top and we will begin the main loop.

# Loop control variable
running = True
while (running == True):
	for event in pygame.event.get():
		if event.type is pygame.QUIT:
			running = False

We initialize a variable to control the loop although break would work fine.
The for loop gets all the events that happen and our if statement makes sure if we click that little red X in the top-right that the loop stops and the screen shuts down.
Now try the code that you have and see that it works!(even if it is a bit useless)
Now that we have the basics of a pygame program down, let's go to part 2!

Edited by spyder, 25 July 2010 - 09:12 AM.

  • 0
I C!(and Python, and C++, and ...)

Also tagged with one or more of these keywords: tic-tac-toe

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download