Python programming 2
Learning The Basics Of Pygame
Python is a very powerful programming langage that is used by many famous sites and buisnesses. If you want to learn the basics, click [ilink url=”https://thebraithwaites.co.uk/?p=2359″]this link[/ilink]. There is an add-on for python called Pygame. It is very useful for creating games. You could use the built-in library called Tkinter (use: from tkinter import *) but it is not as powerful and does not work with some versions of Linux. The only thing about pygame is that you have to download it at: [ilink url=”http://www.pygame.org/download.shtml” style=”download”]http://www.pygame.org/download.shtml[/ilink]. A pygame program works just like an ordinary program, but pygame needs to be imported at the start. The following code creates a pygame window:
#a hash mark (#) is a comment; it is not needed to make a program work. But it is a good idea to add one at
# the start of the program
import pygame, sys #<—————|
from pygame.locals import * #<—-|import the modules needed
pygame.init() # set up pygame
pygame.display.set_caption(‘pygame window’) #change the window title
screen = pygame.display.set_mode((widowwidth,windowheight)) #create the window
pygame.display.update() # update the screen
Replace windowwidth with how wide you want the window to be and windowheight with the height of the window. This code will make a Pygame window appear, change it’s title to ‘pygame window’, and then quickly vanish.
Making Pygame Windows Last Longer
To make a pygame window last forever, you need to add a while True loop, as in the example below (all lines after a line ending with ‘:’ are auto-indented. This means they are four more spaces away from the edge. To delete an auto-indent, press backspace once. To make auto-indents visible I will use ~ marks):
import pygame, sys #<—————|
from pygame.locals import * #<—-|import the modules needed
pygame.init() # set up pygame
pygame.display.set_caption(‘pygame window’) #change the window title
screen = pygame.display.set_mode((widowwidth,windowheight)) #create the window
pygame.display.update() # update the screen
while True:
~~~~pygame.display.update()
This will create an everlasting window. But you will find that it will not close, even when you click the close button, so add the following lines:
import pygame, sys #<—————|
from pygame.locals import * #<—-|import the modules needed
pygame.init() # set up pygame
pygame.display.set_caption(‘pygame window’) #change the window title
screen = pygame.display.set_mode((widowwidth,windowheight)) #create the window
pygame.display.update() # update the screen
while True:
~~~~pygame.display.update()
~~~~for event in pygame.event.get():
~~~~~~~~if event.type == pygame.QUIT: #if you click close…
~~~~~~~~~~~~pygame.quit()
~~~~~~~~~~~~sys.exit()
~~~~~~~~elif event.type == KEYDOWN:
~~~~~~~~~~~~print(event.key)
This program will print the keys pressed. With some experimenting you can make games. The possibilities are almost endless…
No Comment